Пример #1
0
def temci__completion__bash():
    subcommands = "\n\t".join(sorted(command_docs.keys()))

    def process_options(options: CmdOptionList) -> str:
        typecheck(options, CmdOptionList)
        strs = []
        for option in sorted(options.options):
            strs.append("--" + option.option_name)
            if option.short is not None:
                strs.append("-" + option.short)
            if option.is_flag:
                strs.append("--no-" + option.option_name)
        return "\n\t".join(strs)

    def process_misc_commands():
        ret_str = ""
        for misc_cmd in misc_commands:
            if "sub_commands" not in misc_commands[misc_cmd]:
                continue
            ret_str += """
                case ${{COMP_WORDS[1]}} in
                {misc_cmd})
                    case ${{COMP_WORDS[2]}} in
            """.format(misc_cmd=misc_cmd)
            for sub_cmd in misc_commands[misc_cmd]["sub_commands"].keys():
                ret_str += """
                        {sub_cmd})
                            args=(
                                ${{common_opts[@]}}
                                {common_opts}
                                {cmd_ops}
                            )
                            # printf '   _%s ' "${{args[@]}}" >> /tmp/out
                            # printf '   __%s ' "${{args[*]}}" >> /tmp/out
                            COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                        ;;
                """.format(
                    sub_cmd=sub_cmd,
                    cmd_ops=process_options(
                        misc_commands[misc_cmd]["sub_commands"][sub_cmd]),
                    common_opts=process_options(
                        misc_commands[misc_cmd]["common"]))
            ret_str += """
                        *)
                            local args=( )
                            COMPREPLY=( $(compgen -W "" -- $cur) ) && return 0
                    esac
                    ;;
                *)
                ;;
              esac
            """
        return ret_str

    def process_misc_commands_case():
        ret_str = ""
        for misc_cmd in misc_commands:
            args = []
            if "sub_commands" in misc_commands[misc_cmd]:
                args = " ".join(
                    sorted(misc_commands[misc_cmd]["sub_commands"].keys()))
            else:
                typecheck(misc_commands[misc_cmd], CmdOptionList)
                args = process_options(
                    misc_commands[misc_cmd].append(common_options))
            ret_str += """
            {misc_cmd})
                args=({sub_cmds})
                ;;
            """.format(misc_cmd=misc_cmd, sub_cmds=args)
        return ret_str

    run_cmd_file_code = ""
    for driver in run_driver.RunDriverRegistry.registry:
        run_cmd_file_code += """
            {driver})
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $run_common_opts
                        {driver_opts}
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
        """.format(driver=driver,
                   driver_opts=process_options(
                       run_options["run_driver_specific"][driver]))

    file_structure = """
    # Auto generated tab completion for the temci ({version}) benchmarking tool.


    _temci(){{
        local cur=${{COMP_WORDS[COMP_CWORD]}}
        local prev=${{COMP_WORDS[COMP_CWORD-1]}}

        local common_opts=(
            {common_opts}
        )
        local args=(
            {common_opts}
        )
        local run_common_opts=(
            {run_common_opts}
        )
        local report_common_opts=(
            {report_common_opts}
        )
        local build_common_opts=(
            {build_common_opts}
        )

        {misc_commands_code}

        case ${{COMP_WORDS[1]}} in
            format)
                args=(
                    $common_opts
                    $format_common_opts
                )
                COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
            report)
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $report_common_opts
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
            build)
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $build_common_opts
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 01
                ;;
                esac
                ;;
            {run_cmd_file_code}
            *)
            ;;
        esac

        case ${{COMP_WORDS[1]}} in
            (report|build|{run_drivers})
                local IFS=$'\n'
                local LASTCHAR=' '
                COMPREPLY=($(compgen -o plusdirs -o nospace -f -X '!*.yaml' -- "${{COMP_WORDS[COMP_CWORD]}}"))

                if [ ${{#COMPREPLY[@]}} = 1 ]; then
                    [ -d "$COMPREPLY" ] && LASTCHAR=/
                    COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
                else
                    for ((i=0; i < ${{#COMPREPLY[@]}}; i++)); do
                        [ -d "${{COMPREPLY[$i]}}" ] && COMPREPLY[$i]=${{COMPREPLY[$i]}}/
                    done
                fi
                return 0
                ;;
            {misc_commands_case_code}
            *)
                args=({commands})
        esac
        COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) )
    }}
    shopt -s extglob
    complete -F _temci temci
    """.format(common_opts=process_options(common_options),
               run_common_opts=process_options(run_options["common"]),
               report_common_opts=process_options(report_options),
               format_common_opts=process_options(format_options),
               commands=" ".join(sorted(command_docs.keys())),
               run_drivers="|".join(run_options["run_driver_specific"].keys()),
               misc_commands_case_code=process_misc_commands_case(),
               misc_commands_code=process_misc_commands(),
               build_common_opts=process_options(build_options),
               run_cmd_file_code=run_cmd_file_code,
               version=temci.scripts.version.version)
    create_completion_dir()
    file_name = completion_file_name("bash")
    with open(file_name, "w") as f:
        f.write(file_structure)
        logging.debug("\n".join(
            "{:>3}: {}".format(i, s)
            for (i, s) in enumerate(file_structure.split("\n"))))
        f.flush()
    os.chmod(file_name, 0o777)
    print(file_name)
Пример #2
0
def temci__completion__bash():
    subcommands = "\n\t".join(sorted(command_docs.keys()))

    def process_options(options: CmdOptionList) -> str:
        typecheck(options, CmdOptionList)
        strs = []
        for option in sorted(options.options):
            strs.append("--" + option.option_name)
            if option.short is not None:
                strs.append("-" + option.short)
            if option.is_flag:
                strs.append("--no-" + option.option_name)
        return "\n\t".join(strs)

    def process_misc_commands():
        ret_str = ""
        for misc_cmd in misc_commands:
            if "sub_commands" not in misc_commands[misc_cmd]:
                continue
            ret_str += """
                case ${{COMP_WORDS[1]}} in
                {misc_cmd})
                    case ${{COMP_WORDS[2]}} in
            """.format(
                misc_cmd=misc_cmd
            )
            for sub_cmd in misc_commands[misc_cmd]["sub_commands"].keys():
                ret_str += """
                        {sub_cmd})
                            args=(
                                ${{common_opts[@]}}
                                {common_opts}
                                {cmd_ops}
                            )
                            # printf '   _%s ' "${{args[@]}}" >> /tmp/out
                            # printf '   __%s ' "${{args[*]}}" >> /tmp/out
                            COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                        ;;
                """.format(
                    sub_cmd=sub_cmd,
                    cmd_ops=process_options(misc_commands[misc_cmd]["sub_commands"][sub_cmd]),
                    common_opts=process_options(misc_commands[misc_cmd]["common"]),
                )
            ret_str += """
                        *)
                            local args=( )
                            COMPREPLY=( $(compgen -W "" -- $cur) ) && return 0
                    esac
                    ;;
                *)
                ;;
              esac
            """
        return ret_str

    def process_misc_commands_case():
        ret_str = ""
        for misc_cmd in misc_commands:
            args = []
            if "sub_commands" in misc_commands[misc_cmd]:
                args = " ".join(sorted(misc_commands[misc_cmd]["sub_commands"].keys()))
            else:
                typecheck(misc_commands[misc_cmd], CmdOptionList)
                args = process_options(misc_commands[misc_cmd].append(common_options))
            ret_str += """
            {misc_cmd})
                args=({sub_cmds})
                ;;
            """.format(
                misc_cmd=misc_cmd, sub_cmds=args
            )
        return ret_str

    run_cmd_file_code = ""
    for driver in run_driver.RunDriverRegistry.registry:
        run_cmd_file_code += """
            {driver})
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $run_common_opts
                        {driver_opts}
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
        """.format(
            driver=driver, driver_opts=process_options(run_options["run_driver_specific"][driver])
        )

    file_structure = """
    # Auto generated tab completion for the temci ({version}) benchmarking tool.


    _temci(){{
        local cur=${{COMP_WORDS[COMP_CWORD]}}
        local prev=${{COMP_WORDS[COMP_CWORD-1]}}

        local common_opts=(
            {common_opts}
        )
        local args=(
            {common_opts}
        )
        local run_common_opts=(
            {run_common_opts}
        )
        local report_common_opts=(
            {report_common_opts}
        )
        local build_common_opts=(
            {build_common_opts}
        )

        {misc_commands_code}

        case ${{COMP_WORDS[1]}} in
            report)
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $report_common_opts
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
            build)
                case ${{COMP_WORDS[2]}} in
                *.yaml)
                    args=(
                        $common_opts
                        $build_common_opts
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
            run_package|exec_package)
                case ${{COMP_WORDS[2]}} in
                *.temci)
                    args=(
                        $common_opts
                        {package_opts}
                    )
                    COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) ) && return 0
                ;;
                esac
                ;;
            {run_cmd_file_code}
            *)
            ;;
        esac

        case ${{COMP_WORDS[1]}} in
            (report|build|{run_drivers})
                local IFS=$'\n'
                local LASTCHAR=' '
                COMPREPLY=($(compgen -o plusdirs -o nospace -f -X '!*.yaml' -- "${{COMP_WORDS[COMP_CWORD]}}"))

                if [ ${{#COMPREPLY[@]}} = 1 ]; then
                    [ -d "$COMPREPLY" ] && LASTCHAR=/
                    COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
                else
                    for ((i=0; i < ${{#COMPREPLY[@]}}; i++)); do
                        [ -d "${{COMPREPLY[$i]}}" ] && COMPREPLY[$i]=${{COMPREPLY[$i]}}/
                    done
                fi
                return 0
                ;;
            (run_package|exec_package)
                local IFS=$'\n'
                local LASTCHAR=' '
                COMPREPLY=($(compgen -o plusdirs -o nospace -f -X '!*.temci' -- "${{COMP_WORDS[COMP_CWORD]}}"))

                if [ ${{#COMPREPLY[@]}} = 1 ]; then
                    [ -d "$COMPREPLY" ] && LASTCHAR=/
                    COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
                else
                    for ((i=0; i < ${{#COMPREPLY[@]}}; i++)); do
                        [ -d "${{COMPREPLY[$i]}}" ] && COMPREPLY[$i]=${{COMPREPLY[$i]}}/
                    done
                fi
                return 0
                ;;
            {misc_commands_case_code}
            *)
                args=({commands})
        esac
        COMPREPLY=( $(compgen -W "${{args[*]}}" -- $cur) )
    }}
    shopt -s extglob
    complete -F _temci temci
    """.format(
        common_opts=process_options(common_options),
        run_common_opts=process_options(run_options["common"]),
        report_common_opts=process_options(report_options),
        commands=" ".join(sorted(command_docs.keys())),
        run_drivers="|".join(run_options["run_driver_specific"].keys()),
        misc_commands_case_code=process_misc_commands_case(),
        misc_commands_code=process_misc_commands(),
        build_common_opts=process_options(build_options),
        run_cmd_file_code=run_cmd_file_code,
        version=temci.scripts.version.version,
        package_opts=process_options(package_options),
    )
    create_completion_dir()
    file_name = completion_file_name("bash")
    with open(file_name, "w") as f:
        f.write(file_structure)
        logging.debug("\n".join("{:>3}: {}".format(i, s) for (i, s) in enumerate(file_structure.split("\n"))))
        f.flush()
    os.chmod(file_name, 0o777)
    print(file_name)
Пример #3
0
def temci__completion__zsh():
    subcommands = "\n\t".join([
        '"{}:{}"'.format(cmd, command_docs[cmd])
        for cmd in sorted(command_docs.keys())
    ])

    def process_options(options: CmdOptionList, one_line=False):
        typecheck(options, CmdOptionList)
        strs = []
        for option in sorted(options):
            multiple = isinstance(option.type_scheme, List) or isinstance(
                option.type_scheme, ListOrTuple)
            rounds = 10 if multiple else 1  # hack to allow multiple applications of an option
            assert isinstance(option, CmdOption)
            descr = "{}".format(
                option.description
            ) if option.description is not None else "Undoc"
            option_str = "--{}".format(option.option_name)
            if option.has_short:
                option_str = "{{-{},--{}}}".format(option.short,
                                                   option.option_name)
            if option.is_flag:
                option_str = "{{--{o},--no-{o}}}".format(o=option.option_name)
            new_completion = ""
            if option.has_completion_hints and "zsh" in option.completion_hints:
                new_completion = '{option_str}\"[{descr}]: :{hint}"'.format(
                    option_str=option_str,
                    descr=descr,
                    hint=option.completion_hints["zsh"])
            else:
                format_str = '{option_str}\"[{descr}]"' if option.is_flag else '{option_str}\"[{descr}]: :()"'
                new_completion = format_str.format(option_str=option_str,
                                                   descr=descr)
            for i in range(rounds):
                strs.append(new_completion)

        if one_line:
            return " ".join(strs)
        return "\n\t".join(strs)

    misc_cmds_wo_subcmds = list(
        filter(lambda x: isinstance(misc_commands[x], CmdOptionList),
               misc_commands.keys()))
    misc_cmds_w_subcmds = list(
        filter(lambda x: isinstance(misc_commands[x], dict),
               misc_commands.keys()))

    ret_str = """
# Auto generated tab completion for the temci ({version}) benchmarking tool.


#compdef temci
_temci(){{
    # printf '%s ' "${{words[@]}}" > /tmp/out
    local ret=11 state

    local -a common_opts
    common_opts=(
        {common_opts}
    )

    typeset -A opt_args
    _arguments   -C  ':subcommand:->subcommand' '2: :->second_level' '*::options:->options' && ret=0
    #echo $state > tmp_file

    local sub_cmd=""
    case $words[1] in
        temci)
            sub_cmd=$words[2]
            ;;
        *)
            sub_cmd=$words[1]
    esac

    #echo $words[@] >> tmp_file

    case $words[2] in
        ({misc_cmds_wo_subs})
            state="options"
            ;;
    esac


    case $state in
    subcommand)
        local -a subcommands
        subcommands=(
            {subcommands}
        )

        _describe -t subcommands 'temci subcommand' subcommands && ret=0
    ;;
    """.format(common_opts=process_options(common_options),
               subcommands=" ".join("\"{}:{}\"".format(cmd, command_docs[cmd])
                                    for cmd in command_docs),
               misc_cmds_wo_subs="|".join(misc_cmds_wo_subcmds),
               version=temci.scripts.version.version)
    ret_str += """
    second_level)

        #echo $words[@] > tmp_file
        case $words[2] in
    """
    for misc_cmd in misc_cmds_w_subcmds:
        ret_str += """
            ({misc_cmd})
                #echo "here" > tmp_file
                local -a subcommands
                subcommands=(
                    {sub_cmds}
                )
                _describe -t subcommands 'temci subcommand' subcommands && ret=0 && return 0
                ;;
        """.format(
            misc_cmd=misc_cmd,
            sub_cmds="\n\t".join(
                "\"{}:{}\"".format(x, misc_commands_description[misc_cmd][x])
                for x in misc_commands_description[misc_cmd]))
    cmds = {
        "report": {
            "pattern": r"*\.yaml",
            "type": "files",
            "options": report_options,
        },
        "build": {
            "pattern": r"*\.yaml",
            "type": "files",
            "options": build_options
        },
        "format": {
            "pattern": "*",
            "type": "any",
            "options": format_options
        }
    }
    for cmd, conf in cmds.items():
        ret_str += r"""
                ({cmd})
                               args=(
                            $common_opts
                            {options}
                            )
                    _arguments "2: :{completion} " $args \
                    ;;
    
            """.format(drivers="|".join(
            sorted(run_driver.RunDriverRegistry.registry.keys())),
                       options=process_options(conf["options"]),
                       completion={
                           "files": "_files -g '{}'".format(conf["pattern"]),
                           "any": conf["pattern"]
                       }[conf["type"]],
                       cmd=cmd)
    for name in run_driver.RunDriverRegistry.registry.keys():
        ret_str += r"""
                       ({driver})
                       args=(
                            $common_opts
                            {options}
                            )
                           _arguments "2: :_files -g '*\.yaml' " $args \
                       ;;
        """.format(driver=name,
                   options=process_options(
                       run_options["run_driver_specific"][name]))
    ret_str += """
                    esac
            ;;
    (options)
        local -a args
        args=(
        $common_opts
        )
        #echo "options" $words[@] > tmp_file


        case $words[1] in

        """

    for driver in run_driver.RunDriverRegistry.registry.keys():
        ret_str += """
        {driver})
            case $words[2] in
                *.yaml)
                    args=(
                    $common_opts
                    {opts}
                    )
                    _arguments "1:: :echo 3" $args && ret=0
                ;;
                *)
                    _arguments "1:: :echo 3" && ret=0
            esac
        ;;
        """.format(driver=driver,
                   opts=process_options(
                       run_options["run_driver_specific"][driver]))

    for name in cmds:
        ret_str += """
            ({name})
                #echo "({name})" $words[2]
                #case $words[2] in
                 #   {pattern})
                        args=(
                        $common_opts
                        {options}
                        )
                        _arguments "1:: :echo 3" $args && ret=0
                 #   ;;
                 #   *)
                 #       _arguments "1:: :echo 3" && ret=0
                #esac
            ;;
        """.format(name=name,
                   pattern=cmds[name]["pattern"],
                   options=process_options(cmds[name]["options"]))

    for misc_cmd in misc_cmds_w_subcmds:
        ret_str += """
        ({misc_cmd})
            case $words[2] in
            """.format(misc_cmd=misc_cmd)
        for sub_cmd in misc_commands[misc_cmd]["sub_commands"]:
            ret_str += """
                {sub_cmd})
                    #echo "{sub_cmd}" $words[@] > tmp_file
                    args+=(
                        {common_opts}
                        {opts}
                    )

                    #echo "sdf" $args[@] > tmp_file
                    _arguments "1:: :echo 3" $args && ret=0
                ;;
            """.format(sub_cmd=sub_cmd,
                       opts=process_options(
                           misc_commands[misc_cmd]["sub_commands"][sub_cmd]),
                       common_opts=process_options(
                           misc_commands[misc_cmd]["common"]))
        ret_str += """
            esac
            ;;
        """

    ret_str += """
    esac



        case $sub_cmd in
    """

    for misc_cmd in misc_cmds_wo_subcmds:
        ret_str += """
        {misc_cmd})
            # echo "{misc_cmd}" $words[@] >> tmp_file
            args+=(
                {opts}
            )
            case $words[2] in
                $sub_cmd)
                    _arguments "1:: :echo 3" $args && ret=0
                    ;;
                *)
                    # echo "Hi" >> tmp_file
                    _arguments $args && ret=0
                    ;;
            esac
        ;;
    """.format(misc_cmd=misc_cmd,
               opts=process_options(misc_commands[misc_cmd]))

    ret_str += """
        esac

        #_arguments $common_opts && ret=0 && return 0
    ;;
    esac
    }

    compdef _temci temci=temci
    """
    create_completion_dir()
    file_name = completion_file_name("zsh")
    if not os.path.exists(os.path.dirname(file_name)):
        os.mkdir(os.path.dirname(file_name))
    with open(file_name, "w") as f:
        f.write(ret_str)
        logging.debug("\n".join("{:>3}: {}".format(i, s)
                                for (i, s) in enumerate(ret_str.split("\n"))))
        f.flush()
    os.chmod(file_name, 0o777)
    print(file_name)
Пример #4
0
def temci__completion__zsh():
    subcommands = "\n\t".join(['"{}:{}"'.format(cmd, command_docs[cmd]) for cmd in sorted(command_docs.keys())])

    def process_options(options: CmdOptionList, one_line=False):
        typecheck(options, CmdOptionList)
        strs = []
        for option in sorted(options):
            multiple = isinstance(option.type_scheme, List) or isinstance(option.type_scheme, ListOrTuple)
            rounds = 10 if multiple else 1  # hack to allow multiple applications of an option
            assert isinstance(option, CmdOption)
            descr = "{}".format(option.description) if option.description is not None else "Undoc"
            option_str = "--{}".format(option.option_name)
            if option.has_short:
                option_str = "{{-{},--{}}}".format(option.short, option.option_name)
            if option.is_flag:
                option_str = "{{--{o},--no-{o}}}".format(o=option.option_name)
            new_completion = ""
            if option.has_completion_hints and "zsh" in option.completion_hints:
                new_completion = '{option_str}"[{descr}]: :{hint}"'.format(
                    option_str=option_str, descr=descr, hint=option.completion_hints["zsh"]
                )
            else:
                format_str = '{option_str}"[{descr}]"' if option.is_flag else '{option_str}"[{descr}]: :()"'
                new_completion = format_str.format(option_str=option_str, descr=descr)
            for i in range(rounds):
                strs.append(new_completion)

        if one_line:
            return " ".join(strs)
        return "\n\t".join(strs)

    misc_cmds_wo_subcmds = list(filter(lambda x: isinstance(misc_commands[x], CmdOptionList), misc_commands.keys()))
    misc_cmds_w_subcmds = list(filter(lambda x: isinstance(misc_commands[x], dict), misc_commands.keys()))

    ret_str = """
# Auto generated tab completion for the temci ({version}) benchmarking tool.


#compdef temci
_temci(){{
    # printf '%s ' "${{words[@]}}" > /tmp/out
    local ret=11 state

    local -a common_opts
    common_opts=(
        {common_opts}
    )

    typeset -A opt_args
    _arguments   -C  ':subcommand:->subcommand' '2: :->second_level' '*::options:->options' && ret=0
    #echo $state > tmp_file

    local sub_cmd=""
    case $words[1] in
        temci)
            sub_cmd=$words[2]
            ;;
        *)
            sub_cmd=$words[1]
    esac

    #echo $words[@] >> tmp_file

    case $words[2] in
        ({misc_cmds_wo_subs})
            state="options"
            ;;
    esac


    case $state in
    subcommand)
        local -a subcommands
        subcommands=(
            {subcommands}
        )

        _describe -t subcommands 'temci subcommand' subcommands && ret=0
    ;;
    """.format(
        common_opts=process_options(common_options),
        subcommands=" ".join('"{}:{}"'.format(cmd, command_docs[cmd]) for cmd in command_docs),
        misc_cmds_wo_subs="|".join(misc_cmds_wo_subcmds),
        version=temci.scripts.version.version,
    )
    ret_str += """
    second_level)

        #echo $words[@] > tmp_file
        case $words[2] in
    """
    for misc_cmd in misc_cmds_w_subcmds:
        ret_str += """
            ({misc_cmd})
                #echo "here" > tmp_file
                local -a subcommands
                subcommands=(
                    {sub_cmds}
                )
                _describe -t subcommands 'temci subcommand' subcommands && ret=0 && return 0
                ;;
        """.format(
            misc_cmd=misc_cmd,
            sub_cmds="\n\t".join(
                '"{}:{}"'.format(x, misc_commands_description[misc_cmd][x]) for x in misc_commands_description[misc_cmd]
            ),
        )
    ret_str += """
            (build|report|{drivers})
                _arguments "2: :_files -g '*\.yaml' "\
            ;;
            (exec_package|run_package)
                _arguments "2: :_files -g '*\.temci' "\
            ;;
        esac
        ;;
        """.format(
        drivers="|".join(sorted(run_driver.RunDriverRegistry.registry.keys()))
    )
    ret_str += """
    (options)
        local -a args
        args=(
        $common_opts
        )
        #echo "options" $words[@] > tmp_file


        case $words[1] in

        """

    for driver in run_driver.RunDriverRegistry.registry.keys():
        ret_str += """
        {driver})
            case $words[2] in
                *.yaml)
                    args=(
                    $common_opts
                    {opts}
                    )
                    _arguments "1:: :echo 3" $args && ret=0
                ;;
                *)
                    _arguments "1:: :echo 3" && ret=0
            esac
        ;;
        """.format(
            driver=driver, opts=process_options(run_options["run_driver_specific"][driver])
        )

    cmds = {
        "report": {"pattern": "*.yaml", "options": report_options},
        "build": {"pattern": "*.yaml", "options": build_options},
        "exec_package|run_package": {"pattern": "*.temci", "options": package_options},
    }

    for name in cmds:
        ret_str += """
            ({name})
                #echo "({name})" $words[2]
                case $words[2] in
                    {pattern})
                        args=(
                        $common_opts
                        {options}
                        )
                        _arguments "1:: :echo 3" $args && ret=0
                    ;;
                    *)
                        _arguments "1:: :echo 3" && ret=0
                esac
            ;;
        """.format(
            name=name, pattern=cmds[name]["pattern"], options=process_options(cmds[name]["options"])
        )

    for misc_cmd in misc_cmds_w_subcmds:
        ret_str += """
        ({misc_cmd})
            case $words[2] in
            """.format(
            misc_cmd=misc_cmd
        )
        for sub_cmd in misc_commands[misc_cmd]["sub_commands"]:
            ret_str += """
                {sub_cmd})
                    #echo "{sub_cmd}" $words[@] > tmp_file
                    args+=(
                        {common_opts}
                        {opts}
                    )

                    #echo "sdf" $args[@] > tmp_file
                    _arguments "1:: :echo 3" $args && ret=0
                ;;
            """.format(
                sub_cmd=sub_cmd,
                opts=process_options(misc_commands[misc_cmd]["sub_commands"][sub_cmd]),
                common_opts=process_options(misc_commands[misc_cmd]["common"]),
            )
        ret_str += """
            esac
            ;;
        """

    ret_str += """
    esac



        case $sub_cmd in
    """

    for misc_cmd in misc_cmds_wo_subcmds:
        ret_str += """
        {misc_cmd})
            # echo "{misc_cmd}" $words[@] >> tmp_file
            args+=(
                {opts}
            )
            case $words[2] in
                $sub_cmd)
                    _arguments "1:: :echo 3" $args && ret=0
                    ;;
                *)
                    # echo "Hi" >> tmp_file
                    _arguments $args && ret=0
                    ;;
            esac
        ;;
    """.format(
            misc_cmd=misc_cmd, opts=process_options(misc_commands[misc_cmd])
        )

    ret_str += """
        esac

        #_arguments $common_opts && ret=0 && return 0
    ;;
    esac
    }

    compdef _temci temci=temci
    """
    create_completion_dir()
    file_name = completion_file_name("zsh")
    if not os.path.exists(os.path.dirname(file_name)):
        os.mkdir(os.path.dirname(file_name))
    with open(file_name, "w") as f:
        f.write(ret_str)
        logging.debug("\n".join("{:>3}: {}".format(i, s) for (i, s) in enumerate(ret_str.split("\n"))))
        f.flush()
    os.chmod(file_name, 0o777)
    print(file_name)