def execute(context): # choose which module(s) to look for command if context.args.built_in: modules = [BUILTIN_MODULE] elif context.args.module: modules = [context.args.module] else: modules = list_modules() matched_commands = [] for module_name in modules: commands = load_commands_from_module(module_name) found_command = commands.get(context.args.command, None) if found_command: matched_commands.append(found_command) if len(matched_commands) == 0: log.error("Command %s is not found" % context.args.command) return False elif len(matched_commands) == 1: matched_commands[0].show_help() else: log.title("Found multiple commands") log.write("Use suggested argument to show help for specified command only.") for command in matched_commands: if command.module_name == BUILTIN_MODULE: narrow_help = "run with --built-in" else: narrow_help = "run with --module=%s" % command.module_name log.write(" " * 4 + "%-25s%s" % (str(command), narrow_help)) return True
def execute(context): aliases = load_aliases() log.title("Found %d alias(es)" % len(aliases)) for alias, alias_to in sorted(aliases.items()): log.write(" " * 4 + ("%-20s " + log.GRAY + "%s" + log.END) % (alias, alias_to)) return True
def execute(context): modules = module.list_modules() modules.remove(BUILTIN_MODULE) log.title("Found %d module(s)" % len(modules)) for module_name in sorted(modules): log.write(" " * 4 + module_name) return True
def execute( command, output=False, show_command=True, command_title=None, output_on_error=True, error_text=None, checked=True ): """ executes the command """ if show_command: if command_title: title = "~ " + command_title elif output: title = command else: title = command if len(command) < 70 else command[0:20] + " ... " + command[-20:] log.title("execute " + title) if not checked: return subprocess.call( command, shell=True, stdout=(None if output else DEVNULL), stderr=(None if output else DEVNULL) ) if output: try: return subprocess.check_call( command, shell=True, stdout=(None if output else DEVNULL), stderr=(None if output else DEVNULL) ) except subprocess.CalledProcessError as e: if error_text: log.error(error_text) raise e try: subprocess.check_output(command, shell=True) return 0 except subprocess.CalledProcessError as e: if output_on_error: print_failed_command_output(e) if error_text: log.error("%s: error code %d. Run with --verbose flag to see more output" % (error_text, e.returncode)) raise e
def find_and_execute(parent_context, module, command, args=()): module_commands = load_commands_from_module(module) module_command = module_commands.get(command, None) if module_command: cmd_args = list(args) # add twisted path and project path cmd_args.append('--use-twisted-path') cmd_args.append(parent_context.twisted_path) cmd_args.append('--use-project-path') cmd_args.append(parent_context.project_path) # add verbose parameter if need verbose = getattr(parent_context.args, 'verbose', False) if verbose: cmd_args.append("--verbose") context = build_command_context(module_command, cmd_args) log.title("command %s %s" % (module_command, " ".join(args))) return module_command.execute(context, show_exec_time=False) else: log.error("Command %s %s is not found" % (module, command)) return False
def print_module_with_commands(module_name, module_commands): log.title(module_name) for command_name, command in sorted(module_commands.items()): log.write(" " * 4 + ("%-25s " + log.GRAY + "%s" + log.END) % (str(command), command.get_short_description()))