Esempio n. 1
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)
Esempio n. 2
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