Esempio n. 1
0
def run_command(command, prefix, *arguments, **kwargs):
    use_exception_handler = kwargs.get('use_exception_handler', False)
    arguments = list(arguments)
    p, sub_parsers = generate_parser()
    parser_config[command](sub_parsers)

    if command is Commands.CONFIG:
        arguments.append("--file {0}".format(join(prefix, 'condarc')))
    if command in (Commands.LIST, Commands.CREATE, Commands.INSTALL,
                   Commands.REMOVE, Commands.UPDATE):
        arguments.append("-p {0}".format(prefix))
    if command in (Commands.CREATE, Commands.INSTALL, Commands.REMOVE, Commands.UPDATE):
        arguments.extend(["-y", "-q"])

    arguments = list(map(escape_for_winpath, arguments))
    command_line = "{0} {1}".format(command, " ".join(arguments))

    args = p.parse_args(split(command_line))
    context._set_argparse_args(args)
    print("\n\nEXECUTING COMMAND >>> $ conda %s\n\n" % command_line, file=sys.stderr)
    with stderr_log_level(TEST_LOG_LEVEL, 'conda'), stderr_log_level(TEST_LOG_LEVEL, 'requests'):
        with captured() as c, replace_log_streams():
            if use_exception_handler:
                conda_exception_handler(args.func, args, p)
            else:
                args.func(args, p)
    print(c.stderr, file=sys.stderr)
    print(c.stdout, file=sys.stderr)
    if command is Commands.CONFIG:
        reload_config(prefix)
    return c.stdout, c.stderr
Esempio n. 2
0
def run_command(command, prefix, *arguments, **kwargs):
    use_exception_handler = kwargs.get('use_exception_handler', False)
    arguments = list(arguments)
    p, sub_parsers = generate_parser()
    parser_config[command](sub_parsers)

    if command is Commands.CONFIG:
        arguments.append("--file {0}".format(join(prefix, 'condarc')))
    if command in (Commands.LIST, Commands.CREATE, Commands.INSTALL,
                   Commands.REMOVE, Commands.UPDATE):
        arguments.append("-p {0}". format(prefix))
    if command in (Commands.CREATE, Commands.INSTALL, Commands.REMOVE, Commands.UPDATE):
        arguments.extend(["-y", "-q"])

    arguments = list(map(escape_for_winpath, arguments))
    command_line = "{0} {1}".format(command, " ".join(arguments))

    args = p.parse_args(split(command_line))
    context._set_argparse_args(args)
    print("\n\nEXECUTING COMMAND >>> $ conda %s\n\n" % command_line, file=sys.stderr)
    with stderr_log_level(TEST_LOG_LEVEL, 'conda'), stderr_log_level(TEST_LOG_LEVEL, 'requests'):
        with captured() as c, replace_log_streams():
            if use_exception_handler:
                conda_exception_handler(args.func, args, p)
            else:
                args.func(args, p)
    print(c.stderr, file=sys.stderr)
    print(c.stdout, file=sys.stderr)
    if command is Commands.CONFIG:
        reload_config(prefix)
    return c.stdout, c.stderr
Esempio n. 3
0
def make_temp_env(*packages, **kwargs):
    prefix = kwargs.pop('prefix', None) or make_temp_prefix()
    with stderr_log_level(DEBUG, 'conda'), stderr_log_level(DEBUG, 'requests'):
        with disable_logger('fetch'), disable_logger('dotupdate'):
            try:
                # try to clear any config that's been set by other tests
                reset_context([join(prefix, 'condarc')])
                run_command(Commands.CREATE, prefix, *packages)
                yield prefix
            finally:
                rmtree(prefix, ignore_errors=True)
Esempio n. 4
0
def make_temp_env(*packages, **kwargs):
    prefix = kwargs.pop('prefix', None) or make_temp_prefix()
    assert isdir(prefix), prefix
    with stderr_log_level(DEBUG, 'conda'), stderr_log_level(DEBUG, 'requests'):
        with disable_logger('fetch'), disable_logger('dotupdate'):
            try:
                # try to clear any config that's been set by other tests
                reset_context([os.path.join(prefix+os.sep, 'condarc')])
                run_command(Commands.CREATE, prefix, *packages)
                yield prefix
            finally:
                rmtree(prefix, ignore_errors=True)
Esempio n. 5
0
def run_command(command, prefix, *arguments, **kwargs):

    assert isinstance(arguments,
                      tuple), "run_command() arguments must be tuples"
    arguments = massage_arguments(arguments)

    use_exception_handler = kwargs.get("use_exception_handler", False)
    # These commands require 'dev' mode to be enabled during testing because
    # they end up calling run_script() in link.py and that uses wrapper scripts for e.g. activate.
    # Setting `dev` means that, in these scripts, conda is executed via:
    #   `sys.prefix/bin/python -m conda` (or the Windows equivalent).
    # .. and the source code for `conda` is put on `sys.path` via `PYTHONPATH` (a bit gross but
    # less so than always requiring `cwd` to be the root of the conda source tree in every case).
    # If you do not want this to happen for some test you must pass dev=False as a kwarg, though
    # for nearly all tests, you want to make sure you are running *this* conda and not some old
    # conda (it was random which you'd get depending on the initial values of PATH and PYTHONPATH
    # - and likely more variables - before `dev` came along). Setting CONDA_EXE is not enough
    # either because in the 4.5 days that would just run whatever Python was found first on PATH.
    command_defaults_to_dev = command in (
        Commands.CREATE,
        Commands.INSTALL,
        Commands.REMOVE,
        Commands.RUN,
    )
    dev = kwargs.get("dev", True if command_defaults_to_dev else False)
    debug = kwargs.get("debug_wrapper_scripts", False)

    p = generate_parser()

    if command is Commands.CONFIG:
        arguments.append("--file")
        arguments.append(join(prefix, "condarc"))
    if command in (
            Commands.LIST,
            Commands.COMPARE,
            Commands.CREATE,
            Commands.INSTALL,
            Commands.REMOVE,
            Commands.UPDATE,
            Commands.RUN,
    ):
        arguments.insert(0, "-p")
        arguments.insert(1, prefix)
    if command in (Commands.CREATE, Commands.INSTALL, Commands.REMOVE,
                   Commands.UPDATE):
        arguments.extend(["-y", "-q"])

    arguments.insert(0, command)
    if dev:
        arguments.insert(1, "--dev")
    if debug:
        arguments.insert(1, "--debug-wrapper-scripts")

    # It would be nice at this point to re-use:
    # from conda.cli.python_api import run_command as python_api_run_command
    # python_api_run_command
    # .. but that does not support no_capture and probably more stuff.

    args = p.parse_args(arguments)
    context._set_argparse_args(args)
    init_loggers(context)
    cap_args = tuple() if not kwargs.get("no_capture") else (None, None)
    # list2cmdline is not exact, but it is only informational.
    print("\n\nEXECUTING COMMAND >>> $ conda %s\n\n" % " ".join(arguments),
          file=sys.stderr)
    with stderr_log_level(TEST_LOG_LEVEL,
                          "conda"), stderr_log_level(TEST_LOG_LEVEL,
                                                     "requests"):
        arguments = encode_arguments(arguments)
        is_run = arguments[0] == "run"
        if is_run:
            cap_args = (None, None)
        with argv(["python_api"] + arguments), captured(*cap_args) as c:
            if use_exception_handler:
                result = conda_exception_handler(do_call, args, p)
            else:
                result = do_call(args, p)
        if is_run:
            stdout = result.stdout
            stderr = result.stderr
            result = result.rc
        else:
            stdout = c.stdout
            stderr = c.stderr
        print(stdout, file=sys.stdout)
        print(stderr, file=sys.stderr)

    # Unfortunately there are other ways to change context, such as Commands.CREATE --offline.
    # You will probably end up playing whack-a-bug here adding more and more the tuple here.
    if command in (Commands.CONFIG, ):
        reset_context([os.path.join(prefix + os.sep, "condarc")], args)
    return stdout, stderr, result