Ejemplo n.º 1
0
 def populate_torun(cmd_path):
     if "." in cmd_path:
         parent_path = ".".join(cmd_path.split(".")[:-1])
         deps = reversed(get_flow_commands_to_run(parent_path))
     else:
         deps = []
     if cmd_path.startswith(("[")):
         return
     # force the loading of cmd_path to fill flowdeps
     get_command(cmd_path)
     deps = list(reversed(flowdeps[cmd_path])) + list(deps)
     for dep in deps:
         torun.insert(0, dep)
         populate_torun(dep)
Ejemplo n.º 2
0
def bash(name, open, force, description, body, from_alias, flowdeps,
         source_bash_helpers):
    """Create a bash custom command"""
    if name.endswith(".sh"):
        LOGGER.warning("Removing the extra .sh so that clk won't confuse it"
                       " with a command name.")
        name = name[:len(".sh")]
    script_path = Path(config.customcommands.profile.location) / "bin" / name
    makedirs(script_path.parent)
    if script_path.exists() and not force:
        raise click.UsageError(f"Won't overwrite {script_path} unless"
                               " explicitly asked so with --force")
    options = []
    arguments = []
    flags = []
    remaining = ""
    args = ""

    if from_alias:
        if body:
            body = body + "\n"
        body = body + "\n".join(
            config.main_command.path + " " + " ".join(map(quote, command))
            for command in config.settings["alias"][from_alias]["commands"])
        flowdeps = list(flowdeps) + get_flow_commands_to_run(from_alias)
        alias_cmd = get_command(from_alias)
        if description:
            description = description + "\n"
        description = description + f"Converted from the alias {from_alias}"

        def guess_type(param):
            if type(param.type) == click.Choice:
                return json.dumps(list(param.type.choices))
            elif param.type == int:
                return "int"
            elif param.type == float:
                return "float"
            else:
                return "str"

        for param in alias_cmd.params:
            if type(param) == Option:
                if param.is_flag:
                    flags.append(
                        f"F:{','.join(param.opts)}:{param.help}:{param.default}"
                    )
                    args += f"""
if [ "${{{config.main_command.path.upper()}___{param.name.upper()}}}" == "True" ]
then
    args+=({param.opts[-1]})
fi"""
                else:
                    options.append(
                        f"O:{','.join(param.opts)}:{guess_type(param)}:{param.help}"
                    )
                    args += f"""
if [ -n "${{{config.main_command.path.upper()}___{param.name.upper()}}}" ]
then
    args+=({param.opts[-1]} "${{{config.main_command.path.upper()}___{param.name.upper()}}}")
fi"""
            elif type(param) == Argument:
                if param.nargs == -1:
                    remaining = param.help
                else:
                    arguments.append(
                        f"A:{','.join(param.opts)}:{guess_type(param)}:{param.help}"
                    )
                    args += f"""
args+=("${{{config.main_command.path.upper()}___{param.name.upper()}}}")
"""
        if args:
            args = """# Build the arguments of the last command of the alias
args=()""" + args
            body += ' "${args[@]}"'
        if remaining:
            body += ' "${@}"'

    if flowdeps:
        flowdeps_str = "flowdepends: " + ", ".join(flowdeps) + "\n"
    else:
        flowdeps_str = ""
    if options:
        options_str = "\n".join(options) + "\n"
    else:
        options_str = ""
    if arguments:
        arguments_str = "\n".join(arguments) + "\n"
    else:
        arguments_str = ""
    if flags:
        flags_str = "\n".join(flags) + "\n"
    else:
        flags_str = ""
    if remaining:
        remaining_str = f"N:{remaining}\n"
    else:
        remaining_str = ""

    script_path.write_text(f"""#!/bin/bash -eu

source "${{CLK_INSTALL_LOCATION}}/commands/customcommand/_clk.sh"

clk_usage () {{
    cat<<EOF
$0

{description}
--
{flowdeps_str}{options_str}{flags_str}{arguments_str}{remaining_str}
EOF
}}

clk_help_handler "$@"

{args}
{body}
""")
    chmod(script_path, 0o755)
    if open:
        click.edit(filename=str(script_path))
Ejemplo n.º 3
0
 def _get_command(self, path, parent):
     cmd_name = path.split(".")[-1]
     parent = parent.original_command
     original_path = "{}.{}".format(parent.path, cmd_name)
     return get_command(original_path)
Ejemplo n.º 4
0
def resolve(path):
    """Resolve a command to help understanding where a command comes from"""
    cmd, resolver = get_command(path, True)
    click.echo(
        f"The command {path} is resolved by the resolver {resolver.name}"
    )