def get_choices(ctx, args_, incomplete): args = config.commandline_profile.get_settings("parameters")[ctx.command.path][:] while args and args[0].startswith("-"): a = args.pop(0) if args and (a == "-r" or a == "--recipe"): args.pop(0) if not args: choices = compute_choices(ctx, args_, incomplete) else: path = args.pop(0).split(".") args = path + args ctx = get_ctx(path) choices = compute_choices(ctx, args, incomplete) for item, help in choices: yield (item, help)
def execute_flow(args): if args: from click_project.overloads import get_ctx c = get_ctx(args) # get rid of the app part app_path_len = len(config.main_command.path) subpath = c.command_path[app_path_len + 1:].replace(" ", ".") cmd = subpath.split(".")[-1] args = args[args.index(cmd)+1:] else: subpath = 'build' args = [] LOGGER.debug("Will execute the flow of {} with args {}".format(subpath, args)) execute_flow_dependencies(subpath) execute_flow_step(subpath.split("."), args)
def get_choices(ctx, args_, incomplete): args = config.commandline_profile.get_settings("parameters")[ctx.command.path][:] while args and args[0].startswith("-"): a = args.pop(0) if args and (a == "-r" or a == "--recipe"): args.pop(0) if not args: choices = compute_choices(ctx, args_, incomplete) else: args.pop(0) while ',' in args: args = args[args.index(',')+1:] ctx = get_ctx(args, side_effects=True) choices = compute_choices(ctx, args, incomplete) for item, help in choices: yield (item, help)
def alias_command(ctx, *args, **kwargs): if "config" in kwargs: del kwargs["config"] commands = list(commands_to_run) for command_ in commands[:-1]: LOGGER.debug("Running command: {}".format(" ".join( quote(c) for c in command_))) run(command_) arguments = ctx.command.complete_arguments[:] arguments = clean_flow_arguments(arguments) whole_command = commands[-1] + arguments original_command_ctx = get_ctx(whole_command, side_effects=True) cur_ctx = original_command_ctx ctxs = [] # if the resolution of the context brought too many commands, we # must not call the call back of the children of the original_command while cur_ctx and ctx.command.original_command != cur_ctx.command: cur_ctx = cur_ctx.parent while cur_ctx: ctxs.insert(0, cur_ctx) cur_ctx = cur_ctx.parent LOGGER.develop("Running command: {}".format(" ".join( quote(c) for c in commands[-1]))) def run_callback(_ctx): LOGGER.develop( "Running callback of {} with args {}, params {}".format( _ctx.command.path, config.commandline_profile.get_settings("parameters")[ _ctx.command.path], _ctx.params, )) with _ctx: old_resilient_parsing = _ctx.resilient_parsing _ctx.resilient_parsing = ctx.resilient_parsing _ctx.command.callback(**_ctx.params) _ctx.resilient_parsing = old_resilient_parsing for cur_ctx in ctxs: run_callback(cur_ctx)
def _get_command(self, path, parent=None): name = path.split(".")[-1] commands_to_run = get_settings('alias')[path]["commands"] cmdhelp = get_settings('alias')[path]["documentation"] cmdhelp = cmdhelp or "Alias for: {}".format(' , '.join(' '.join(quote(arg) for arg in cmd) for cmd in commands_to_run)) short_help = cmdhelp.splitlines()[0] if len(cmdhelp) > 55: short_help = cmdhelp[:52] + '...' deps = [] for cmd in commands_to_run: cmdctx = get_ctx(cmd) # capture the flow of the aliased command only if it is not called # with an explicit flow if ( not cmdctx.params.get("flow") and not cmdctx.params.get("flow_from") and not cmdctx.params.get("flow_after") ): deps += get_flow_commands_to_run(cmdctx.command.path) c = get_ctx(commands_to_run[-1]) kind = None def create_cls(cls): return cls( name=name, help=cmdhelp, short_help=short_help, ignore_unknown_options=c is not None and c.ignore_unknown_options) if c is not None: if isinstance(c.command, Group): cls = create_cls(group) kind = "group" elif isinstance(c.command, Command): cls = create_cls(command) kind = "command" elif isinstance(c.command, config.main_command.__class__): cls = click.group(cls=config.main_command.__class__, name=name, help=cmdhelp, short_help=short_help) kind = config.main_command.path else: raise NotImplementedError() elif commands_to_run[-1][0] == config.main_command.path: cls = click.group(cls=config.main_command.__class__, name=name, help=cmdhelp, short_help=short_help) del commands_to_run[-1][0] c = get_ctx(commands_to_run[-1]) kind = config.main_command.path else: cls = create_cls(command) def alias_command(ctx, *args, **kwargs): if "config" in kwargs: del kwargs["config"] commands = list(commands_to_run) for command_ in commands[:-1]: LOGGER.debug("Running command: {}".format(" ".join(quote(c) for c in command_))) run(command_) arguments = ctx.command.complete_arguments[:] arguments = clean_flow_arguments(arguments) whole_command = commands[-1] + arguments original_command_ctx = get_ctx(whole_command, side_effects=True) cur_ctx = original_command_ctx ctxs = [] # if the resolution of the context brought too many commands, we # must not call the call back of the children of the original_command while cur_ctx and ctx.command.original_command != cur_ctx.command: cur_ctx = cur_ctx.parent while cur_ctx: ctxs.insert(0, cur_ctx) cur_ctx = cur_ctx.parent LOGGER.develop("Running command: {}".format(" ".join(quote(c) for c in commands[-1]))) def run_callback(_ctx): LOGGER.develop("Running callback of {} with args {}, params {}".format( _ctx.command.path, config.command_line_settings["parameters"][_ctx.command.path], _ctx.params, )) with _ctx: old_resilient_parsing = _ctx.resilient_parsing _ctx.resilient_parsing = ctx.resilient_parsing _ctx.command.callback( **_ctx.params ) _ctx.resilient_parsing = old_resilient_parsing for cur_ctx in ctxs: run_callback(cur_ctx) alias_command = pass_context(alias_command) alias_command = cls(alias_command) if deps: alias_command.clickproject_flowdepends = deps alias_command.commands_to_run = commands_to_run if c is not None: alias_command.original_command = c.command if kind == "group": if c.command.default_cmd_name is not None: alias_command.set_default_command(c.command.default_cmd_name) elif kind == "command": alias_command.handle_dry_run = c.command.handle_dry_run alias_param_names = list(map(lambda c: c.name, alias_command.params)) def was_given(param): return not ( # catched the default value only because it was not # given to the command line param.name in c.click_project_default_catch or # not given for sure c.params.get(param.name) is None ) alias_command.params = [ param for param in c.command.params if param.name not in alias_param_names and param.name not in ("flow", "flow_from", "flow_after") and ( # options may be given several times isinstance(param, click.Option) or ( # it is an argument then! not was_given(param) or # may be given, but may be given again param.multiple or # may be given, but may be given again param.nargs == -1 ) ) ] + alias_command.params # any option required with nargs=-1 that was already given should be # no more required for param in alias_command.params: if param.nargs == -1 and param.required and was_given(param): param.required = False return alias_command