示例#1
0
文件: help.py 项目: retoter/twister
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
示例#2
0
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
示例#3
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)
示例#4
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
示例#5
0
def execute(context):
    commands = {}
    if context.args.built_in:
        commands.update({BUILTIN_MODULE: module.load_builtin()})
    if context.args.module:
        module_name = context.args.module
        commands.update({module_name: module.load_commands_from_module(module_name)})
    if not context.args.built_in and not context.args.module:
        modules = module.list_modules()
        for module_name in modules:
            commands[module_name] = module.load_commands_from_module(module_name)

    builtin_commands = commands.get(BUILTIN_MODULE, [])
    if builtin_commands:
        print_module_with_commands("built-in", builtin_commands)

    for module_name, module_commands in sorted(commands.items()):
        if module_name == BUILTIN_MODULE: continue

        print_module_with_commands(module_name, module_commands)

    return True
示例#6
0
def get_twister_args(args=sys.argv[1:]):
    module_name = None
    command_name = None
    command_args = []

    # read module and command
    if len(args) == 1:
        command_name = args[0]
        command_args = []
    elif len(args) >= 2:
        module_name = args[0]
        command_name = args[1]
        command_args = args[2:]

    # -- handle the case when we have only parameters or command + parameter
    if module_name and module_name.startswith("--"):
        module_name = None
    if command_name and command_name.startswith("--"):
        command_name = module_name
        module_name = None
        command_args = args[1:]

    # -- if module name is incorrect, this might be a command..
    if module_name and module_name not in list_modules():
        command_name = module_name
        command_args = args[1:]
        module_name = None

    # -- handle the aliases
    if not module_name:
        aliases = load_aliases()
        if command_name in aliases.keys():
            log.info("Using alias '%s' => '%s'" % (command_name, aliases[command_name]))
            alias_parts = re.split("\s+", aliases[command_name])
            alias_args, command_name, module_name = get_twister_args(alias_parts)
            # sum up alias args and command args
            command_args = alias_args + command_args

    return command_args, command_name, module_name