def runCmd(cls, cmd): """run command and show if success or failed Args: cmd: string Returns: bool: if this command run successfully """ cit.echo(cmd, "command") result = os.system(cmd) cls.checkResult(result)
def run_cmd(cmd: str) -> bool: """Run command and show if success or failed Args: cmd: string Returns: bool: Does this command run successfully """ SUCCESS_CODE = 0 cit.echo(cmd, "command") is_success = (os.system(cmd) == SUCCESS_CODE) if not is_success: cit.warn("Command Failed")
def read_cmd(cmd: str) -> str: """Run command and return command's output Args: cmd: string Returns: str: What the command's output to stdout """ import shlex import subprocess cit.echo(cmd, "command") args = shlex.split(cmd) proc = subprocess.Popen(args, stdout=subprocess.PIPE) (proc_stdout, proc_stderr) = proc.communicate(input=None) # proc_stdin return proc_stdout.decode() # Decode stdout and stderr bytes to str
def show_menu(): """Show commands menu Globals: COMMANDS: Dict of menu commands. Key: description of func Val: func """ # 'Django system check': functools.partial(run_by_py3, ), # } menu = sorted(COMMANDS.keys()) if len(sys.argv) > 1: arg = sys.argv.pop() selection = arg if arg in COMMANDS else menu[int(arg) - 1] else: cit.echo('Select one of these:') selection = cit.get_choice(menu) return COMMANDS.get(selection)
def show_menu(): """Show commands menu returns: a callable function name """ commands = collections.OrderedDict({ 'Install Requirements Modules': requirements_install, 'Make & migrate database': migrate_db, 'Create superuser account': create_superuser, 'Runserver (localhost:8000)': runserver_dev, 'Runserver (LAN ip:8000)': runserver_lan, 'Shell: Interactive': interactive_shell, 'Shell: Database': db_shell, 'Django system check': system_check, 'Exit': cit.bye, }) cit.echo('Select one of these:') selection = cit.get_choice(sorted(commands.keys())) return commands.get(selection)
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 test_echo_pre(self): with patch("sys.stdout", new=StringIO()) as fake_out: cit.echo("ABC", pre="prefix") self.assertEqual(fake_out.getvalue(), "| (Prefix) ABC\n")
def test_echo(self): with patch("sys.stdout", new=StringIO()) as fake_out: cit.echo("ABC") self.assertEqual(fake_out.getvalue(), "| ABC\n")
def checkResult(cls, result: bool): if 0 == result: cit.echo("Done", "result") else: cit.echo("Failed", "result")
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'], phrases=phrases) cit.end() cit.ask("Open Output Folder?") if cit.get_choice(('Yes', 'No')) == 'Yes': show_file(GENERATED_DIR)
def test_echo_pre(self): cit.echo("ABC", pre="prefix") self.assertEqual(self.fakeout.readline(ansi=False), "| (Prefix) ABC\n")
def test_echo(self): cit.echo("ABC") self.assertEqual(self.fakeout.readline(ansi=False), "| ABC\n")