Example #1
0
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
Example #2
0
    def not_empty(self, arg_name):
        arg_value = getattr(self.context.args, arg_name, None)
        if arg_value is None:
            return

        if len(arg_value) == 0:
            self.valid = False
            log.error("No value specified for [%s]" % arg_name)
Example #3
0
    def arg_required(self, arg_name):
        """ Check that required argument is specified """
        if not self.valid: return

        attr = getattr(self.context.args, arg_name, None)
        if not attr:
            self.valid = False
            self.show_usage = True
            log.error("Argument %s is not specified, but required\n" % arg_name)
Example #4
0
def load_py_module(module_name, path):
    import imp

    try:
        return imp.load_source(module_name, path)
    except IOError:
        # file is not found, log and return None
        log.error("module %s is not found" % path)
        return None
Example #5
0
    def any_arg_required(self, *args):
        if not self.valid: return

        num = 0
        for arg in args:
            attr = getattr(self.context.args, arg, None)
            if attr:
                num += 1

        if num == 0:
            self.valid = False
            self.show_usage = True
            log.error("At least one of [%s] arguments is required.\n" % ", ".join(args))
Example #6
0
    def project_path_required(self):
        """ Check if project path is specified and it exists """

        if not self.valid: return

        self.valid = False
        if not self.context.project_path:
            log.error("project path is not specified")
            return
        if not os.path.exists(self.context.project_path):
            log.error("project path %s is not found" % self.context.project_path)
            return

        self.valid = True
Example #7
0
    def no_args_conflicts(self, *args):
        """ Check there is not conflicts between arguments """
        if not self.valid: return

        num = 0
        found_args = []
        for arg in args:
            attr = getattr(self.context.args, arg, None)
            if attr:
                num += 1
                found_args.append(arg)

        if num > 1:
            self.valid = False
            self.show_usage = True
            log.error("Found conflict between arguments [%s], which can't be used together.\n" % ", ".join(found_args))
Example #8
0
def main():
    if len(sys.argv) < 2:
        print_help()
        sys.exit(-1)

    args, command_name, module_name = get_twister_args()

    builtin_commands = load_builtin()

    if module_name is None:
        if not command_name:
            log.error("Missing command name. See usage below:")
            print_help()
            sys.exit(-1)
        if command_name not in builtin_commands.keys():
            log.error("Unknown command: %s" % command_name)
            log.help(("Run " + log.command("find-command %s") + " to find command.") % command_name)
            sys.exit(-1)
    elif module_name in builtin_commands.keys():
        command_name = module_name
        module_name = None
        args = sys.argv[2:]

    command = None
    if module_name is None:
        command = builtin_commands[command_name]
    else:
        if module_name not in list_modules():
            log.error("Unknown module %s." % module_name)
            log.help("Run " + log.command("list-modules") + " to get list of available modules")
            sys.exit(-1)
        commands = load_commands_from_module(module_name)
        if command_name not in commands.keys():
            if command_name in builtin_commands.keys():
                # interesting hook for built-in commands to allow them use within specified module
                command = builtin_commands[command_name]
                args.append("--module")
                args.append(module_name)
            else:
                # command not found in module
                log.error("Unknown command %s in module %s." % (command_name, module_name))
                log.help(("Run " + log.command("%(module)s list-commands") +
                          " to get list of available commands in module %(module)s.") % dict(module=module_name))
                log.help(("Run " + log.command("find-command %s") + " to find command.") % command_name)
                sys.exit(-1)

        # in case if command already found in built-ins
        command = command or commands[command_name]

    if not command.validate():
        sys.exit(-1)

    context = build_command_context(command, args)

    if not command.execute(context):
        sys.exit(-1)
Example #9
0
def execute(context):
    command_name = context.args.command
    module_name = context.args.module

    best_matched_commands = []
    matched_module_commands = []
    matched_description_commands = []

    modules = module.list_modules()
    if module_name:
        if module_name not in modules:
            log.error("Unknown module %s" % module_name)
            return False
        # module name is specified, find commands in this module only
        modules = [module_name]


    for module_name in modules:
        module_commands = module.load_commands_from_module(module_name)
        for module_command_name, command in module_commands.items():
            if command_name in module_command_name:
                best_matched_commands.append(command)
            elif module_name == command_name:
                # include all module_commands if module name is
                # same as searched command
                matched_module_commands.append(command)
            elif len(command_name) >= 3 and (command_name in command.get_short_description() or command_name in command.get_help()):
                # include command if searched command is mentioned
                # in the description
                matched_description_commands.append(command)

    matched_commands = best_matched_commands + matched_module_commands + matched_description_commands
    if not matched_commands:
        print "No commands found"
        return False

    print ("[ Found %d commands ]" % len(matched_commands)).ljust(80, "-")
    for command in sorted(best_matched_commands):
        show_command_info(command)
    for command in sorted(matched_module_commands):
        show_command_info(command)
    for command in sorted(matched_description_commands):
        show_command_info(command)

    return True
Example #10
0
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
Example #11
0
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