def update_file(filename: str, url: str) -> bool: """Check and update file compares with remote_url Args: filename: str. Local filename, normally it's `__file__` url: str or urllib.request.Request object. Remote url of raw file content. Use urllib.request.Request object for headers. Returns: bool: file updated or not """ def compare(s1, s2): return s1 == s2, len(s2) - len(s1) if not url or not filename: return False try: raw_codes = read_url(url) with open(filename, "rb") as f: current_codes = f.read().replace(b"\r", b"") is_same, diff = compare(current_codes, raw_codes) if is_same: cit.info("{} is already up-to-date.".format(filename)) return False else: cit.ask("A new version is available. Update? (Diff: {})".format(diff)) if cit.get_choice(["Yes", "No"]) == "Yes": with open(filename, "wb") as f: f.write(raw_codes) cit.info("Update Success.") return True else: cit.warn("Update Canceled") return False except Exception as e: cit.err("{f} update failed: {e}".format(f=filename, e=e)) return False
def show_running_status(pid_file): """check if this uwsgi is already running""" if os.path.exists(pid_file): cit.warn("uwsgi is running @ " + pid_file) return True else: cit.info("No uwsgi running") return False
def generate_filepath(filename): home_dir = os.path.expanduser('~') cit.info("Home Dir\t: {}".format(home_dir)) if not os.path.isdir(home_dir): cit.warn('Home Dir does not exist, creating it') os.makedirs(home_dir) new_file = os.path.join(home_dir, filename) cit.info("Target File\t: {}".format(new_file)) return new_file
def readFile(cls, filepath): """Try different encoding to open a file in readonly mode""" for mode in ("utf-8", 'gbk', 'cp1252', 'windows-1252', 'latin-1'): try: with open(filepath, mode='r', encoding=mode) as f: content = f.read() cit.info('以 {} 格式打开文件'.format(mode)) return content except UnicodeDecodeError: cit.warn('打开文件:尝试 {} 格式失败'.format(mode)) return None
def get_operation(): """start a new uwsgi, stop a running uwsgi, or reload the config and codes""" operations = ["*** update uwsgiTool ***", "start", "stop", "reload"] if len(sys.argv) != 2: return cit.get_choice(operations) elif sys.argv[1] in operations: selected = sys.argv[1] cit.info("Selected: {}".format(selected)) return selected else: cit.err("Wrong Params: " + sys.argv[1]) cit.bye()
def get_config_file(xml_file='./uwsgi.xml'): """check if uswgi config file exists""" dir_path = os.path.dirname(os.path.abspath(__file__)) default_xml_file = "{}/uwsgi.xml".format(dir_path) if xml_file and os.path.isfile(xml_file): cit.info("uwsgi config file: " + xml_file) return xml_file elif os.path.isfile(default_xml_file): return get_config_file(default_xml_file) else: cit.err("uwsgi config file not found: " + xml_file) return None
def menu(): confs = ['.vimrc', '.tcshrc', '.bashrc', '.tcsh_aliases', '.tcsh_prompt', '.bash_aliases', '.bash_prompt'] cit.ask('Which config file to update:') choice = cit.get_choice(['ALL'] + confs + ['exit']) if choice == 'ALL': for conf in confs: apply_config(conf) cit.info('Done') cit.bye() elif choice == 'exit': cit.bye() else: return choice
def load_all_phrases(files: tuple) -> list: def load_phrases_from_json(filename: str) -> list: cit.info("Parsing {}".format(filename)) jup = JsonPhraser() jup.from_file(filename) return jup.phrases phrases = [ phrase for filename in files for phrase in load_phrases_from_json(filename) ] cit.info('Loaded {} phrases'.format(len(phrases))) return phrases
def read_file(filepath, *args, **kwargs) -> str: """Try different encoding to open a file in readonly mode.""" for mode in ("utf-8", "gbk", "cp1252", "windows-1252", "latin-1", "ascii"): try: with open(filepath, *args, encoding=mode, **kwargs) as f: content = f.read() cit.info("File is read in {} mode.".format(mode)) return content except UnicodeDecodeError: cit.warn("File cannot be opened in {} mode".format(mode)) with open(filepath, *args, encoding='ascii', errors='surrogateescape', **kwargs) as f: content = f.read() cit.info("File is read in ASCII surrogate escape mode.") return content
def generate_filepath(filename): home_dir = os.path.expanduser('~') cit.info("Home Dir\t: {}".format(home_dir)) if not os.path.isdir(home_dir): cit.warn('Home Dir does not exist, creating it', lvl=1) os.makedirs(home_dir) new_file = os.path.join(home_dir, filename) cit.info("Target File\t: {}".format(new_file)) if os.path.exists(new_file): backup = '{}.old'.format(new_file) os.rename(new_file, backup) cit.warn("Target file is already exist.", lvl=1) cit.warn('Old target file renamed as {}'.format(backup), lvl=1) return new_file
def menu(): confs = ['.vimrc', '.tcshrc', '.aliases', '.promptrc'] cit.ask('Which config file to update:') choice = cit.get_choice(['ALL'] + confs + ['exit']) if choice == 'ALL': for conf in confs: config(conf) cit.info('Done') cit.pause('Press Enter to exit ...') cit.bye() elif choice == 'exit': cit.bye() else: return choice
def main(): # precheck # ktk.needPlatform("linux") cit.info('Uwsgi Tool: version {}'.format(__version__)) cit.br() # defines uwsgi_xml = get_config_file("./uwsgi.xml") # uwsgi config file pid_file = get_pid_file() # exist when running log_file = get_log_file() # run if pid_file and uwsgi_xml: show_running_status(pid_file) operation = get_operation() run_operation(operation, uwsgi_xml, pid_file, log_file) else: cit.bye()
def generate_UDP_file(Phraser: object, output: str, phrases: list): if not Phraser: raise Exception("Phraser must provided!") if not output: raise Exception("No output filename provided!") phraser = Phraser(phrases) filepath = os.path.join(GENERATED_DIR, output) if os.path.exists(filepath): cit.ask("'{}' is already exists. Overwrite it?".format(filepath)) if cit.get_choice(['Yes', 'No']) == 'Yes': os.remove(filepath) else: cit.warn("Output is not overwrited. No file generated.") return phraser.to_file(filepath) cit.info("'{o}' is generated, {length} phrases.".format( o=output, length=len(phraser.phrases)))
def run_operation(oprtn, config_file, pid_file, log_file): if "start" == oprtn: if os.path.exists(pid_file): cit.ask('uwsgi is already running, start a new one? (Y/n)\n(Doing this will overwrite pid_file)') if cit.get_input().lower() != 'y': cit.info('User canceled start operation') return False ktk.runCmd("sudo uwsgi -x '{c}' --pidfile '{p}' --daemonize '{d}'".format(c=config_file, p=pid_file, d=log_file)) elif "stop" == oprtn: ktk.runCmd("sudo uwsgi --stop " + pid_file) ktk.runCmd("sudo rm " + pid_file) elif "reload" == oprtn: ktk.runCmd("sudo uwsgi --reload " + pid_file) elif "*** update uwsgiTool ***" == oprtn: update_uwsgitool() else: cit.err("Wrong operation: " + oprtn) cit.bye()
def updateFile(cls, file_, url): """Check and update file compares with remote_url Args: file_: str. Local filename. Normally it's __file__ url: str. Remote url of raw file content. Normally it's https://raw.github.com/... Returns: bool: file updated or not """ def compare(s1, s2): return s1 == s2, len(s2) - len(s1) if not url or not file_: return False try: req = urllib.request.urlopen(url) raw_codes = req.read() with open(file_, 'rb') as f: current_codes = f.read().replace(b'\r', b'') is_same, diff = compare(current_codes, raw_codes) if is_same: cit.info("{} is already up-to-date.".format(file_)) return False else: cit.ask("A new version is available. Update? (Diff: {})".format(diff)) if cit.get_choice(['Yes', 'No']) == 'Yes': with open(file_, 'wb') as f: f.write(raw_codes) cit.info("Update Success.") return True else: cit.warn("Update Canceled") return False except Exception as e: cit.err("{f} update failed: {e}".format(f=file_, e=e)) return False
def copy(from_path: str, to_path: str) -> bool: # deal from if not os.path.isabs(from_path): current_dir = os.path.dirname(os.path.realpath(__file__)) from_path = os.path.join(current_dir, from_path) cit.info('From\t: {}'.format(from_path)) if not os.path.isfile(from_path): cit.err("config file does not exists, copy cancelled") return False # deal to cit.info('To\t: {}'.format(to_path)) if os.path.isfile(to_path): cit.err('target file exists, copy cancelled') return False cit.info('Copying file ...') shutil.copyfile(from_path, to_path) cit.info('Changing file owner ...') current_user = getpass.getuser() shutil.chown(to_path, user=current_user) return True
def copy_my_file(config_name, to_): # deal from if not os.path.isabs(config_name): current_dir = os.path.dirname(os.path.realpath(__file__)) from_ = os.path.join(current_dir, config_name) else: from_ = config_name cit.info('From\t: {}'.format(from_)) if not os.path.isfile(from_): cit.err("config file does not exists, copy cancelled", lvl=1) cit.bye() # deal to cit.info('To\t: {}'.format(to_)) if os.path.isfile(to_): cit.err('target file exists, copy cancelled', lvl=1) cit.bye() cit.info('Copying file ...') shutil.copyfile(from_, to_) cit.info('Changing file owner ...') current_user = getpass.getuser() shutil.chown(to_, user=current_user)
run_by_py3('manage.py check') @register('i18n: Make Messages (.po)') @cit.as_session def make_messages(): """Django i18n Make .po Messaages File""" run_by_py3('manage.py makemessages') @register('i18n: Compile Messages (.mo)') @cit.as_session def compile_messages(): """Django i18n Compile .po files into .mo files""" run_by_py3('manage.py compilemessages') if __name__ == '__main__': cit.echo('Django Tool: version {}'.format(__version__)) cit.br() if not manage_file_exist(): cit.err('No manage.py detected. Please run this under projects folder') cit.bye() try: while True: to_run = show_menu() to_run() except KeyboardInterrupt: cit.info('Thanks for using. Bye bye!') cit.bye(0)
def create_superuser(): """Create superuser account for Django admin""" git_username = cit.get_input('Username:'******'Email:') cit.info('Password is specified, ask someone for it') run_by_py3('manage.py createsuperuser --username {username} --email {email}'.format(username=git_username, email=git_email))
# print clients_count = len(clients) if clients_count == 0: cit.err("0 client logged").bye() cit.title("Total: {} clients:".format(clients_count)) threshold = 5 cit.ask("See clients lower than {} ?".format(threshold)) see_all = cit.get_choice(['Yes', 'No']) if clients: for (k, v) in clients.items(): if v < threshold and see_all == 'No': continue if k == '173.230.148.199': k += ' (superfarmer.net)' if k == '106.186.20.163': k += ' (lnhote.me)' cit.info("{} : {}".format(k, v)) # clear cit.ask("Need clear the log?") need_clear = cit.get_choice(['Yes', 'No']) if "Yes" == need_clear: ktk.runCmd("sudo echo") if os.path.exists(backup_path): ktk.runCmd("sudo rm {}".format(backup_path)) ktk.runCmd("sudo mv {log} {bck}".format(log=log_path, bck=backup_path)) ktk.runCmd("sudo touch {}".format(log_path)) ktk.runCmd("sudo chown proxyuser {}".format(log_path)) cit.info("Finish clear the log, old log @ {}".format(backup_path))
ip_list = [ '80.239.173.156', '80.239.173.151', '184.50.87.33', '184.51.198.91', '184.51.198.73', '213.248.126.138', '213.248.126.137', '213.248.126.155', '111.108.54.16', '52.76.139.242', ] if not ip_list: cit.err('Ip List is empty').bye() vpn_route = '10.100.0.1' # duetime cit.info('VPN route = {}'.format(vpn_route)) # get mode, delete / set / print available_modes = ['set', 'delete', 'print'] cit.ask("Choose mode:") mode = cit.get_choice(available_modes) if mode not in available_modes: cit.err('Mode {} is not supported, available modes: {}'.format(mode, available_modes)).bye() cit.info('Mode = {}'.format(mode)) if mode == 'set': for ip in ip_list: cmd = 'route add {ip} mask 255.255.255.255 {vpn_route}'.format(vpn_route=vpn_route, ip=ip) ktk.runCmd(cmd) elif mode == 'delete': for ip in ip_list:
def test_info(self): with patch("sys.stdout", new=StringIO()) as fake_out: cit.info("ABC") self.assertEqual(fake_out.getvalue(), "| (Info) ABC\n")
def needUser(cls, expect_user: str): cit.info("Need: " + expect_user) cit.info("Current: " + cls.getUser()) if cls.getUser() != expect_user: cit.bye("User Check Failed")
def needPlatform(cls, expect_platform: str): cit.info("Need: " + expect_platform) cit.info("Current: " + sys.platform) if expect_platform not in sys.platform: cit.bye("Platform Check Failed")
import os import sys import consoleiotools as cit import KyanToolKit ktk = KyanToolKit.KyanToolKit() # -Pre-conditions Check------------------------------------------- ktk.needPlatform("linux") # -set params----------------------------------------------------- # config file uwsgi_xml = "./uwsgi.xml" if os.path.isfile(uwsgi_xml): cit.info("uwsgi config file: " + uwsgi_xml) else: cit.err("uwsgi config file not found: " + uwsgi_xml) # pid file dir_name = os.path.basename(os.path.dirname(os.path.abspath(__file__))) pid_file = "/var/run/uwsgi_{}.pid".format(dir_name) if os.path.exists(pid_file): cit.warn("uwsgi is running @ " + pid_file) else: cit.info("No uwsgi running") # choice operations = ["start", "stop", "reload"] oprtn = "" if len(sys.argv) != 2: oprtn = cit.get_choice(operations) elif sys.argv[1] in operations:
def test_info(self): cit.info("ABC") self.assertEqual(self.fakeout.readline(ansi=False), "| (Info) ABC\n")
phraser.to_file(filepath) cit.info("'{o}' is generated, {length} phrases.".format( o=output, length=len(phraser.phrases))) def show_file(path: str): if sys.platform.startswith("win"): os.startfile(path) elif platform.system() == "Darwin": subprocess.Popen(["open", path]) else: subprocess.Popen(["xdg-open", path]) if __name__ == "__main__": cit.info("Output Folder: {}".format(GENERATED_DIR)) cit.info("Phrases File location: {}".format(PHRASES_DIR)) deco = "\n| * " phrases_filenames = get_phrases_filenames() cit.info("Phrases JSON Files:") for filename in phrases_filenames: cit.echo(filename, pre="*") phrases_paths = [os.path.join(PHRASES_DIR, fn) for fn in phrases_filenames] phrases = load_all_phrases(phrases_paths) cit.ask("Which one you wanna convert?") phrsr_keys = cit.get_choices(list(AVAIL_PHRASER.keys()), allable=True) for key in phrsr_keys: cit.title("Generating {}".format(key)) phraselet = AVAIL_PHRASER[key] generate_UDP_file(Phraser=phraselet['phraser'], output=phraselet['output'],
def runserver_lan(): """Runserver in development environment, for Local Area Network debug use""" my_ip = socket.gethostbyname(socket.gethostname()) cit.info('Your LAN IP address: {}'.format(my_ip)) run_by_py3('manage.py runserver 0.0.0.0:8000')
def assume_unchanged(): """Show and add a file to 'No-tracking' in Git""" cit.info('Current assume unchanged files:') ktk.runCmd('git ls-files -v | grep -e "^[hsmrck]"') filename = cit.get_input("Enter a TRACKED file's filename: (Ctrl + C to stop)") ktk.runCmd('git update-index --assume-unchanged {}'.format(filename))
def load_phrases_from_json(filename: str) -> list: cit.info("Parsing {}".format(filename)) jup = JsonPhraser() jup.from_file(filename) return jup.phrases