def test_custom_venv_install_scheme_is_prefered(mocker): # The paths in this test are Fedora paths, but we set them for nt as well, so the test also works on Windows, # despite the actual values are nonsense there. # Values were simplified to be compatible with all the supported Python versions. default_scheme = { "stdlib": "{base}/lib/python{py_version_short}", "platstdlib": "{platbase}/lib/python{py_version_short}", "purelib": "{base}/local/lib/python{py_version_short}/site-packages", "platlib": "{platbase}/local/lib/python{py_version_short}/site-packages", "include": "{base}/include/python{py_version_short}", "platinclude": "{platbase}/include/python{py_version_short}", "scripts": "{base}/local/bin", "data": "{base}/local", } venv_scheme = {key: path.replace("local", "") for key, path in default_scheme.items()} sysconfig_install_schemes = { "posix_prefix": default_scheme, "nt": default_scheme, "pypy": default_scheme, "pypy_nt": default_scheme, "venv": venv_scheme, } if getattr(sysconfig, "get_preferred_scheme", None): sysconfig_install_schemes[sysconfig.get_preferred_scheme("prefix")] = default_scheme if sys.version_info[0] == 2: sysconfig_install_schemes = _stringify_schemes_dict(sysconfig_install_schemes) # On Python < 3.10, the distutils schemes are not derived from sysconfig schemes # So we mock them as well to assert the custom "venv" install scheme has priority distutils_scheme = { "purelib": "$base/local/lib/python$py_version_short/site-packages", "platlib": "$platbase/local/lib/python$py_version_short/site-packages", "headers": "$base/include/python$py_version_short/$dist_name", "scripts": "$base/local/bin", "data": "$base/local", } distutils_schemes = { "unix_prefix": distutils_scheme, "nt": distutils_scheme, } if sys.version_info[0] == 2: distutils_schemes = _stringify_schemes_dict(distutils_schemes) # We need to mock distutils first, so they don't see the mocked sysconfig, # if imported for the first time. # That can happen if the actual interpreter has the "venv" INSTALL_SCHEME # and hence this is the first time we are touching distutils in this process. # If distutils saw our mocked sysconfig INSTALL_SCHEMES, we would need # to define all install schemes. mocker.patch("distutils.command.install.INSTALL_SCHEMES", distutils_schemes) mocker.patch("sysconfig._INSTALL_SCHEMES", sysconfig_install_schemes) pyinfo = PythonInfo() pyver = f"{pyinfo.version_info.major}.{pyinfo.version_info.minor}" assert pyinfo.install_path("scripts") == "bin" assert pyinfo.install_path("purelib").replace(os.sep, "/") == f"lib/python{pyver}/site-packages"
import logging import os import random import sys from collections import OrderedDict from pathlib import Path from shlex import quote from string import ascii_lowercase, ascii_uppercase, digits from subprocess import Popen from virtualenv.app_data import AppDataDisabled from virtualenv.discovery.py_info import PythonInfo from virtualenv.util.subprocess import subprocess _CACHE = OrderedDict() _CACHE[Path(sys.executable)] = PythonInfo() def from_exe(cls, app_data, exe, env=None, raise_on_error=True, ignore_cache=False): env = os.environ if env is None else env result = _get_from_cache(cls, app_data, exe, env, ignore_cache=ignore_cache) if isinstance(result, Exception):
def test_py_info_setuptools(): from setuptools.dist import Distribution assert Distribution PythonInfo()