Example #1
0
def run_conda_command(command, prefix, *arguments):
    """
        Run conda command,
    Args:
        command: conda create, list, info
        prefix: The prefix or the name of environment
        *arguments: Extra arguments
    """
    p = generate_parser()

    prefix = escape_for_winpath(prefix)
    if arguments:
        arguments = list(map(escape_for_winpath, arguments))
    if command is Commands.INFO:  # INFO
        command_line = "{0} {1}".format(command, " ".join(arguments))
    elif command is Commands.LIST:  # LIST
        command_line = "{0} -n {1} {2}".format(command, prefix,
                                               " ".join(arguments))
    else:  # CREATE
        command_line = "{0} -y -q -n {1} {2}".format(command, prefix,
                                                     " ".join(arguments))

    from conda.auxlib.compat import shlex_split_unicode
    commands = shlex_split_unicode(command_line)
    args = p.parse_args(commands)
    context._set_argparse_args(args)
    with captured() as c:
        do_call(args, p)

    return c.stdout, c.stderr
Example #2
0
def subprocess_call(command,
                    env=None,
                    path=None,
                    stdin=None,
                    raise_on_error=True,
                    capture_output=True,
                    live_stream=False):
    """This utility function should be preferred for all conda subprocessing.
    It handles multiple tricky details.
    """
    env = encode_environment(env if env else os.environ)
    cwd = sys.prefix if path is None else abspath(path)
    if not isiterable(command):
        command = shlex_split_unicode(command)
    command_str = command if isinstance(command,
                                        string_types) else ' '.join(command)
    log.debug("executing>> %s", command_str)

    if capture_output:
        p = Popen(encode_arguments(command),
                  cwd=cwd,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=PIPE,
                  env=env)
        ACTIVE_SUBPROCESSES.add(p)
        stdin = ensure_binary(stdin) if isinstance(stdin,
                                                   string_types) else stdin

        if live_stream:
            stdout, stderr = _realtime_output_for_subprocess(p)
        else:
            stdout, stderr = p.communicate(input=stdin)

        if hasattr(stdout, "decode"):
            stdout = stdout.decode('utf-8', errors='replace')
        if hasattr(stderr, "decode"):
            stderr = stderr.decode('utf-8', errors='replace')
        rc = p.returncode
        ACTIVE_SUBPROCESSES.remove(p)
    elif stdin:
        raise ValueError("When passing stdin, output needs to be captured")
    else:
        p = Popen(encode_arguments(command), cwd=cwd, env=env)
        ACTIVE_SUBPROCESSES.add(p)
        p.communicate()
        rc = p.returncode
        ACTIVE_SUBPROCESSES.remove(p)
        stdout = None
        stderr = None

    if (raise_on_error and rc != 0) or log.isEnabledFor(TRACE):
        formatted_output = _format_output(command_str, cwd, rc, stdout, stderr)
    if raise_on_error and rc != 0:
        log.info(formatted_output)
        raise CalledProcessError(rc, command, output=formatted_output)
    if log.isEnabledFor(TRACE):
        log.trace(formatted_output)

    return Response(stdout, stderr, int(rc))
Example #3
0
def call(command, path=None, raise_on_error=True):
    path = sys.prefix if path is None else abspath(path)
    p = Popen(shlex_split_unicode(command), cwd=path, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()
    rc = p.returncode
    log.debug("{0} $  {1}\n"
              "  stdout: {2}\n"
              "  stderr: {3}\n"
              "  rc: {4}".format(path, command, stdout, stderr, rc))
    if raise_on_error and rc != 0:
        raise CalledProcessError(
            rc, command, "stdout: {0}\nstderr: {1}".format(stdout, stderr))
    return Response(stdout.decode('utf-8'), stderr.decode('utf-8'), int(rc))
Example #4
0
def run_inprocess_conda_command(command, disallow_stderr=True):
    # anything that uses this function is an integration test
    reset_context(())
    # May want to do this to command:
    with argv(encode_arguments(shlex_split_unicode(command))), captured(disallow_stderr) as c:
        initialize_logging()
        try:
            exit_code = cli.main()
        except SystemExit:
            pass
    print(c.stderr, file=sys.stderr)
    print(c.stdout)
    return c.stdout, c.stderr, exit_code