Exemplo 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)
Exemplo n.º 2
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