def setup_report(verbose, quiet):
    verbosity = max(verbose - quiet, 0)
    _clean_handlers(LOGGER)
    if verbosity > MAX_LEVEL:
        verbosity = MAX_LEVEL  # pragma: no cover
    level = LEVELS[verbosity]
    msg_format = "%(message)s"
    filelock_logger = logging.getLogger("filelock")
    if level <= logging.DEBUG:
        locate = "module"
        msg_format = "%(relativeCreated)d {} [%(levelname)s %({})s:%(lineno)d]".format(
            msg_format, locate)
        filelock_logger.setLevel(level)
    else:
        filelock_logger.setLevel(logging.WARN)

    formatter = logging.Formatter(ensure_str(msg_format))
    stream_handler = logging.StreamHandler(stream=sys.stdout)
    stream_handler.setLevel(level)
    LOGGER.setLevel(logging.NOTSET)
    stream_handler.setFormatter(formatter)
    LOGGER.addHandler(stream_handler)
    level_name = logging.getLevelName(level)
    logging.debug("setup logging to %s", level_name)
    return verbosity
Пример #2
0
def pip_wheel_env_run(search_dirs, app_data):
    for_py_version = "{}.{}".format(*sys.version_info[0:2])
    env = os.environ.copy()
    env.update(
        {
            ensure_str(k): str(
                v)  # python 2 requires these to be string only (non-unicode)
            for k, v in {
                "PIP_USE_WHEEL": "1",
                "PIP_USER": "******",
                "PIP_NO_INPUT": "1"
            }.items()
        }, )
    wheel = get_wheel(
        distribution="pip",
        version=None,
        for_py_version=for_py_version,
        search_dirs=search_dirs,
        download=False,
        app_data=app_data,
        do_periodic_update=False,
    )
    if wheel is None:
        raise RuntimeError("could not find the embedded pip")
    env[str("PYTHONPATH")] = str(wheel.path)
    return env
Пример #3
0
def pip_cert(tmp_path_factory):
    # workaround for https://github.com/pypa/pip/issues/8984 - if the certificate is explicitly set no error can happen
    key = ensure_str("PIP_CERT")
    if key in os.environ:
        return
    cert = tmp_path_factory.mktemp("folder") / "cert"
    import pkgutil

    cert_data = pkgutil.get_data("pip._vendor.certifi", "cacert.pem")
    cert.write_bytes(cert_data)
    with change_os_environ(key, str(cert)):
        yield
Пример #4
0
def test_python_path(monkeypatch, tmp_path, python_path_on):
    result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", ""])
    monkeypatch.chdir(tmp_path)
    case_sensitive = fs_is_case_sensitive()

    def _get_sys_path(flag=None):
        cmd = [str(result.creator.exe)]
        if flag:
            cmd.append(flag)
        cmd.extend(["-c", "import json; import sys; print(json.dumps(sys.path))"])
        return [i if case_sensitive else i.lower() for i in json.loads(subprocess.check_output(cmd))]

    monkeypatch.delenv(str("PYTHONPATH"), raising=False)
    base = _get_sys_path()

    # note the value result.creator.interpreter.system_stdlib cannot be set, as that would disable our custom site.py
    python_paths = [
        str(Path(result.creator.interpreter.prefix)),
        str(Path(result.creator.interpreter.system_stdlib) / "b"),
        str(result.creator.purelib / "a"),
        str(result.creator.purelib),
        str(result.creator.bin_dir),
        str(tmp_path / "base"),
        str(tmp_path / "base_sep") + os.sep,
        "name",
        "name{}".format(os.sep),
        str(tmp_path.parent / (ensure_text(tmp_path.name) + "_suffix")),
        ".",
        "..",
        "",
    ]
    python_path_env = os.pathsep.join(ensure_str(i) for i in python_paths)
    monkeypatch.setenv(str("PYTHONPATH"), python_path_env)

    extra_all = _get_sys_path(None if python_path_on else "-E")
    if python_path_on:
        assert extra_all[0] == ""  # the cwd is always injected at start as ''
        extra_all = extra_all[1:]
        assert base[0] == ""
        base = base[1:]

        assert not (set(base) - set(extra_all))  # all base paths are present
        abs_python_paths = list(OrderedDict((os.path.abspath(ensure_text(i)), None) for i in python_paths).keys())
        abs_python_paths = [i if case_sensitive else i.lower() for i in abs_python_paths]

        extra_as_python_path = extra_all[: len(abs_python_paths)]
        assert abs_python_paths == extra_as_python_path  # python paths are there at the start

        non_python_path = extra_all[len(abs_python_paths) :]
        assert non_python_path == [i for i in base if i not in extra_as_python_path]
    else:
        assert base == extra_all
Пример #5
0
def test_ini_can_be_overwritten_by_flag(tmp_path, monkeypatch):
    custom_ini = tmp_path / "conf.ini"
    custom_ini.write_text(
        dedent("""
        [virtualenv]
        copies = True
        """, ), )
    monkeypatch.setenv(ensure_str("VIRTUALENV_CONFIG_FILE"), str(custom_ini))

    result = session_via_cli(["venv", "--symlinks"])

    symlinks = result.creator.symlinks
    assert symlinks is True
Пример #6
0
def pip_wheel_env_run(version, app_data):
    env = os.environ.copy()
    env.update({
        ensure_str(k):
        str(v)  # python 2 requires these to be string only (non-unicode)
        for k, v in {
            "PIP_USE_WHEEL": "1",
            "PIP_USER": "******",
            "PIP_NO_INPUT": "1"
        }.items()
    })
    with ensure_file_on_disk(get_bundled_wheel("pip", version),
                             app_data) as pip_wheel_path:
        # put the bundled wheel onto the path, and use it to do the bootstrap operation
        env[str("PYTHONPATH")] = str(pip_wheel_path)
        yield env
Пример #7
0
def get_env_var(key, as_type):
    """Get the environment variable option.

    :param key: the config key requested
    :param as_type: the type we would like to convert it to
    :return:
    """
    environ_key = ensure_str("VIRTUALENV_{}".format(key.upper()))
    if os.environ.get(environ_key):
        value = os.environ[environ_key]
        # noinspection PyBroadException
        try:
            source = "env var {}".format(ensure_text(environ_key))
            as_type = convert(value, as_type, source)
            return as_type, source
        except Exception:  # note the converter already logs a warning when failures happen
            pass
Пример #8
0
def setup_report(verbosity, show_pid=False):
    _clean_handlers(LOGGER)
    if verbosity > MAX_LEVEL:
        verbosity = MAX_LEVEL  # pragma: no cover
    level = LEVELS[verbosity]
    msg_format = "%(message)s"
    if level <= logging.DEBUG:
        locate = "module"
        msg_format = "%(relativeCreated)d {} [%(levelname)s %({})s:%(lineno)d]".format(
            msg_format, locate)
    if show_pid:
        msg_format = "[%(process)d] " + msg_format
    formatter = logging.Formatter(ensure_str(msg_format))
    stream_handler = logging.StreamHandler(stream=sys.stdout)
    stream_handler.setLevel(level)
    LOGGER.setLevel(logging.NOTSET)
    stream_handler.setFormatter(formatter)
    LOGGER.addHandler(stream_handler)
    level_name = logging.getLevelName(level)
    logging.debug("setup logging to %s", level_name)
    logging.getLogger("distlib").setLevel(logging.ERROR)
    return verbosity
Пример #9
0
 def __repr__(self):
     return ensure_str(self.__unicode__())
Пример #10
0
 def __str__(self):
     return ensure_str(self._path)
Пример #11
0
 def __repr__(self):
     return ensure_str("Path({})".format(ensure_text(self._path)))
Пример #12
0
class IniConfig(object):
    VIRTUALENV_CONFIG_FILE_ENV_VAR = ensure_str("VIRTUALENV_CONFIG_FILE")
    STATE = {None: "failed to parse", True: "active", False: "missing"}

    section = "virtualenv"

    def __init__(self):
        config_file = os.environ.get(self.VIRTUALENV_CONFIG_FILE_ENV_VAR, None)
        self.is_env_var = config_file is not None
        config_file = (
            Path(config_file) if config_file is not None else
            Path(user_config_dir(appname="virtualenv", appauthor="pypa")) /
            "virtualenv.ini")
        self.config_file = config_file
        self._cache = {}

        exception = None
        self.has_config_file = None
        try:
            self.has_config_file = self.config_file.exists()
        except OSError as exc:
            exception = exc
        else:
            if self.has_config_file:
                self.config_file = self.config_file.resolve()
                self.config_parser = ConfigParser.ConfigParser()
                try:
                    self._load()
                    self.has_virtualenv_section = self.config_parser.has_section(
                        self.section)
                except Exception as exc:
                    exception = exc
        if exception is not None:
            logging.error("failed to read config file %s because %r",
                          config_file, exception)

    def _load(self):
        with self.config_file.open("rt") as file_handler:
            reader = getattr(self.config_parser,
                             "read_file" if PY3 else "readfp")
            reader(file_handler)

    def get(self, key, as_type):
        cache_key = key, as_type
        if cache_key in self._cache:
            return self._cache[cache_key]
        # noinspection PyBroadException
        try:
            source = "file"
            raw_value = self.config_parser.get(self.section, key.lower())
            value = convert(raw_value, as_type, source)
            result = value, source
        except Exception:
            result = None
        self._cache[cache_key] = result
        return result

    def __bool__(self):
        return bool(self.has_config_file) and bool(self.has_virtualenv_section)

    @property
    def epilog(self):
        msg = "{}config file {} {} (change{} via env var {})"
        return msg.format(
            os.linesep,
            self.config_file,
            self.STATE[self.has_config_file],
            "d" if self.is_env_var else "",
            self.VIRTUALENV_CONFIG_FILE_ENV_VAR,
        )
Пример #13
0
def ignore_global_config(tmp_path, monkeypatch):
    monkeypatch.setenv(ensure_str("VIRTUALENV_CONFIG_FILE"),
                       str(tmp_path / "this-should-never-exist"))
Пример #14
0
def ignore_global_config(tmp_path_factory):
    filename = str(tmp_path_factory.mktemp("folder") / "virtualenv-test-suite.ini")
    with change_os_environ(ensure_str("VIRTUALENV_CONFIG_FILE"), filename):
        yield