Example #1
0
 def test_shellcode_utility(self):
     with NamedTemporaryFile() as fh:
         sc = shellcode("prog", use_defaults=True, shell="bash", complete_arguments=None)
         fh.write(sc.encode())
         fh.flush()
         subprocess.check_call(['bash', '-n', fh.name])
     with NamedTemporaryFile() as fh:
         sc = shellcode("prog", use_defaults=False, shell="bash", complete_arguments=["-o", "nospace"])
         fh.write(sc.encode())
         fh.flush()
         subprocess.check_call(['bash', '-n', fh.name])
     sc = shellcode("prog", use_defaults=False, shell="tcsh", complete_arguments=["-o", "nospace"])
     sc = shellcode("prog", use_defaults=False, shell="woosh", complete_arguments=["-o", "nospace"])
Example #2
0
 def completion(shell: str) -> int:
     from sys import stdout
     from argcomplete import shellcode  # type: ignore
     stdout.write(shellcode(
         ['nichtparasoup'], shell=shell,
         use_defaults=True, complete_arguments=None,
     ))
     return 0
Example #3
0
def env_setup(default=False,
              interactive=False,
              missing_only=False,
              override=False,
              configure=False,
              auto_completion=False,
              verbose=False):
    """
    Prints out shell commands that can be used in terminal to setup the environment.

    This tool inspects the current environment, and/or adds default values, and/or interactively
    asks user for values and prints all or the missing variables that need to be set. It can also
    provide completion for this script. You can source the setup as follows:

    ```
    python/run.py envsetup -d -c > ./python/.setup.sh && source ./python/.setup.sh
    ```

    :param default: if default values should be set in this script or not
    :param interactive: if user should be prompted for values or not
    :param missing_only: if only missing variable should be printed
    :param override: if current environment variables should be overridden
    :param configure: if configuration should be printed or not
    :param auto_completion: if auto complete code should be printed
    :param verbose: if user should be guided or not
    """
    given_env, missing_env = _get_required_env(default=default,
                                               interactive=interactive,
                                               override=override,
                                               verbose=verbose)

    # print according to options
    output_env = {}
    output_env.update(missing_env)

    if not missing_only:
        output_env.update(given_env)

    if configure:
        required_env = dict(given_env)
        required_env.update(missing_env)
        configured_env = _get_configured_env(required_env)
        output_env.update(configured_env)

    env_str = "#!/bin/bash\n"
    env_str += _env2shellcode(output_env)

    if auto_completion:
        env_str += argcomplete.shellcode(sys.argv[0])

    print(env_str)
    return 0
Example #4
0
def env_setup(default=False, interactive=False, missing_only=False, override=False, configure=False,
             auto_completion=False, verbose=False):
    """
    Prints out shell commands that can be used in terminal to setup the environment.

    This tool inspects the current environment, and/or adds default values, and/or interactively
    asks user for values and prints all or the missing variables that need to be set. It can also
    provide completion for this script. You can source the setup as follows:

    ```
    python/run.py envsetup -d -c > ./python/.setup.sh && source ./python/.setup.sh
    ```

    :param default: if default values should be set in this script or not
    :param interactive: if user should be prompted for values or not
    :param missing_only: if only missing variable should be printed
    :param override: if current environment variables should be overridden
    :param configure: if configuration should be printed or not
    :param auto_completion: if auto complete code should be printed
    :param verbose: if user should be guided or not
    """
    given_env, missing_env = _get_required_env(
        default=default, interactive=interactive, override=override, verbose=verbose)

    # print according to options
    output_env = {}
    output_env.update(missing_env)

    if not missing_only:
        output_env.update(given_env)

    if configure:
        required_env = dict(given_env)
        required_env.update(missing_env)
        configured_env = _get_configured_env(required_env)
        output_env.update(configured_env)

    env_str = "#!/bin/bash\n"
    env_str += _env2shellcode(output_env)

    if auto_completion:
        env_str += argcomplete.shellcode(sys.argv[0])

    print(env_str)
    return 0
Example #5
0
def autocomplete(args: AutoArgs):
    import argcomplete

    try:
        if args.executables:
            exs = extract_scripts(args.executables, args.mark)
        else:
            exs = find_scripts(args.mark)
    except FileNotFoundError as e:
        sys.stderr.write(f"{e}\n")
        return

    # noinspection PyTypeChecker
    print(
        argcomplete.shellcode(exs, args.use_defaults, args.shell,
                              args.complete_arguments))

    print("added autocompletion to files (if you ran this with eval):",
          file=sys.stderr)
    for file in exs:
        print(f"- {file}", file=sys.stderr)
Example #6
0
def completion(shell: str) -> None:
    print(argcomplete.shellcode(sys.argv[0], shell=shell))
Example #7
0
 def output_completion(self):
     self._out(argcomplete.shellcode(
         ['vindaloo'],
         False,
         'bash',
     ))
Example #8
0
def cmd_setup_completion(args: argparse.Namespace):
    print(argcomplete.shellcode('randrctl', True, 'bash', None))
    return 0
Example #9
0
def completion_code(args):
    print(argcomplete.shellcode([args.program], args.shell))
Example #10
0
def print_completion(prog, **kwargs):
    shell = os.path.basename(os.getenv('SHELL'))
    if shell == 'bash' or shell == 'tcsh':
        sys.stdout.write(argcomplete.shellcode(prog, shell=shell))