first commit

This commit is contained in:
Yura 2024-09-15 15:12:16 +03:00
commit 417e54da96
5696 changed files with 900003 additions and 0 deletions

View file

@ -0,0 +1,226 @@
from __future__ import annotations
import json
import logging
import os
import sys
from abc import ABC, abstractmethod
from argparse import ArgumentTypeError
from ast import literal_eval
from collections import OrderedDict
from pathlib import Path
from virtualenv.discovery.cached_py_info import LogCmd
from virtualenv.util.path import safe_delete
from virtualenv.util.subprocess import run_cmd
from virtualenv.version import __version__
from .pyenv_cfg import PyEnvCfg
HERE = Path(os.path.abspath(__file__)).parent
DEBUG_SCRIPT = HERE / "debug.py"
class CreatorMeta:
def __init__(self) -> None:
self.error = None
class Creator(ABC):
"""A class that given a python Interpreter creates a virtual environment."""
def __init__(self, options, interpreter) -> None:
"""
Construct a new virtual environment creator.
:param options: the CLI option as parsed from :meth:`add_parser_arguments`
:param interpreter: the interpreter to create virtual environment from
"""
self.interpreter = interpreter
self._debug = None
self.dest = Path(options.dest)
self.clear = options.clear
self.no_vcs_ignore = options.no_vcs_ignore
self.pyenv_cfg = PyEnvCfg.from_folder(self.dest)
self.app_data = options.app_data
self.env = options.env
def __repr__(self) -> str:
return f"{self.__class__.__name__}({', '.join(f'{k}={v}' for k, v in self._args())})"
def _args(self):
return [
("dest", str(self.dest)),
("clear", self.clear),
("no_vcs_ignore", self.no_vcs_ignore),
]
@classmethod
def can_create(cls, interpreter): # noqa: ARG003
"""
Determine if we can create a virtual environment.
:param interpreter: the interpreter in question
:return: ``None`` if we can't create, any other object otherwise that will be forwarded to \
:meth:`add_parser_arguments`
"""
return True
@classmethod
def add_parser_arguments(cls, parser, interpreter, meta, app_data): # noqa: ARG003
"""
Add CLI arguments for the creator.
:param parser: the CLI parser
:param app_data: the application data folder
:param interpreter: the interpreter we're asked to create virtual environment for
:param meta: value as returned by :meth:`can_create`
"""
parser.add_argument(
"dest",
help="directory to create virtualenv at",
type=cls.validate_dest,
)
parser.add_argument(
"--clear",
dest="clear",
action="store_true",
help="remove the destination directory if exist before starting (will overwrite files otherwise)",
default=False,
)
parser.add_argument(
"--no-vcs-ignore",
dest="no_vcs_ignore",
action="store_true",
help="don't create VCS ignore directive in the destination directory",
default=False,
)
@abstractmethod
def create(self):
"""Perform the virtual environment creation."""
raise NotImplementedError
@classmethod
def validate_dest(cls, raw_value): # noqa: C901
"""No path separator in the path, valid chars and must be write-able."""
def non_write_able(dest, value):
common = Path(*os.path.commonprefix([value.parts, dest.parts]))
msg = f"the destination {dest.relative_to(common)} is not write-able at {common}"
raise ArgumentTypeError(msg)
# the file system must be able to encode
# note in newer CPython this is always utf-8 https://www.python.org/dev/peps/pep-0529/
encoding = sys.getfilesystemencoding()
refused = OrderedDict()
kwargs = {"errors": "ignore"} if encoding != "mbcs" else {}
for char in str(raw_value):
try:
trip = char.encode(encoding, **kwargs).decode(encoding)
if trip == char:
continue
raise ValueError(trip) # noqa: TRY301
except ValueError:
refused[char] = None
if refused:
bad = "".join(refused.keys())
msg = f"the file system codec ({encoding}) cannot handle characters {bad!r} within {raw_value!r}"
raise ArgumentTypeError(msg)
if os.pathsep in raw_value:
msg = (
f"destination {raw_value!r} must not contain the path separator ({os.pathsep})"
f" as this would break the activation scripts"
)
raise ArgumentTypeError(msg)
value = Path(raw_value)
if value.exists() and value.is_file():
msg = f"the destination {value} already exists and is a file"
raise ArgumentTypeError(msg)
dest = Path(os.path.abspath(str(value))).resolve() # on Windows absolute does not imply resolve so use both
value = dest
while dest:
if dest.exists():
if os.access(str(dest), os.W_OK):
break
non_write_able(dest, value)
base, _ = dest.parent, dest.name
if base == dest:
non_write_able(dest, value) # pragma: no cover
dest = base
return str(value)
def run(self):
if self.dest.exists() and self.clear:
logging.debug("delete %s", self.dest)
safe_delete(self.dest)
self.create()
self.set_pyenv_cfg()
if not self.no_vcs_ignore:
self.setup_ignore_vcs()
def set_pyenv_cfg(self):
self.pyenv_cfg.content = OrderedDict()
self.pyenv_cfg["home"] = os.path.dirname(os.path.abspath(self.interpreter.system_executable))
self.pyenv_cfg["implementation"] = self.interpreter.implementation
self.pyenv_cfg["version_info"] = ".".join(str(i) for i in self.interpreter.version_info)
self.pyenv_cfg["virtualenv"] = __version__
def setup_ignore_vcs(self):
"""Generate ignore instructions for version control systems."""
# mark this folder to be ignored by VCS, handle https://www.python.org/dev/peps/pep-0610/#registered-vcs
git_ignore = self.dest / ".gitignore"
if not git_ignore.exists():
git_ignore.write_text("# created by virtualenv automatically\n*\n", encoding="utf-8")
# Mercurial - does not support the .hgignore file inside a subdirectory directly, but only if included via the
# subinclude directive from root, at which point on might as well ignore the directory itself, see
# https://www.selenic.com/mercurial/hgignore.5.html for more details
# Bazaar - does not support ignore files in sub-directories, only at root level via .bzrignore
# Subversion - does not support ignore files, requires direct manipulation with the svn tool
@property
def debug(self):
""":return: debug information about the virtual environment (only valid after :meth:`create` has run)"""
if self._debug is None and self.exe is not None:
self._debug = get_env_debug_info(self.exe, self.debug_script(), self.app_data, self.env)
return self._debug
@staticmethod
def debug_script():
return DEBUG_SCRIPT
def get_env_debug_info(env_exe, debug_script, app_data, env):
env = env.copy()
env.pop("PYTHONPATH", None)
with app_data.ensure_extracted(debug_script) as debug_script_extracted:
cmd = [str(env_exe), str(debug_script_extracted)]
logging.debug("debug via %r", LogCmd(cmd))
code, out, err = run_cmd(cmd)
try:
if code != 0:
if out:
result = literal_eval(out)
else:
if code == 2 and "file" in err: # noqa: PLR2004
# Re-raise FileNotFoundError from `run_cmd()`
raise OSError(err) # noqa: TRY301
raise Exception(err) # noqa: TRY002, TRY301
else:
result = json.loads(out)
if err:
result["err"] = err
except Exception as exception: # noqa: BLE001
return {"out": out, "err": err, "returncode": code, "exception": repr(exception)}
if "sys" in result and "path" in result["sys"]:
del result["sys"]["path"][0]
return result
__all__ = [
"Creator",
"CreatorMeta",
]

View file

@ -0,0 +1,102 @@
"""Inspect a target Python interpreter virtual environment wise."""
from __future__ import annotations
import sys # built-in
def encode_path(value):
if value is None:
return None
if not isinstance(value, (str, bytes)):
value = repr(value) if isinstance(value, type) else repr(type(value))
if isinstance(value, bytes):
value = value.decode(sys.getfilesystemencoding())
return value
def encode_list_path(value):
return [encode_path(i) for i in value]
def run():
"""Print debug data about the virtual environment."""
try:
from collections import OrderedDict # noqa: PLC0415
except ImportError: # pragma: no cover
# this is possible if the standard library cannot be accessed
OrderedDict = dict # pragma: no cover # noqa: N806
result = OrderedDict([("sys", OrderedDict())])
path_keys = (
"executable",
"_base_executable",
"prefix",
"base_prefix",
"real_prefix",
"exec_prefix",
"base_exec_prefix",
"path",
"meta_path",
)
for key in path_keys:
value = getattr(sys, key, None)
value = encode_list_path(value) if isinstance(value, list) else encode_path(value)
result["sys"][key] = value
result["sys"]["fs_encoding"] = sys.getfilesystemencoding()
result["sys"]["io_encoding"] = getattr(sys.stdout, "encoding", None)
result["version"] = sys.version
try:
import sysconfig # noqa: PLC0415
# https://bugs.python.org/issue22199
makefile = getattr(sysconfig, "get_makefile_filename", getattr(sysconfig, "_get_makefile_filename", None))
result["makefile_filename"] = encode_path(makefile())
except ImportError:
pass
import os # landmark # noqa: PLC0415
result["os"] = repr(os)
try:
import site # site # noqa: PLC0415
result["site"] = repr(site)
except ImportError as exception: # pragma: no cover
result["site"] = repr(exception) # pragma: no cover
try:
import datetime # site # noqa: PLC0415
result["datetime"] = repr(datetime)
except ImportError as exception: # pragma: no cover
result["datetime"] = repr(exception) # pragma: no cover
try:
import math # site # noqa: PLC0415
result["math"] = repr(math)
except ImportError as exception: # pragma: no cover
result["math"] = repr(exception) # pragma: no cover
# try to print out, this will validate if other core modules are available (json in this case)
try:
import json # noqa: PLC0415
result["json"] = repr(json)
except ImportError as exception:
result["json"] = repr(exception)
else:
try:
content = json.dumps(result, indent=2)
sys.stdout.write(content)
except (ValueError, TypeError) as exception: # pragma: no cover
sys.stderr.write(repr(exception))
sys.stdout.write(repr(result)) # pragma: no cover
raise SystemExit(1) # noqa: B904 # pragma: no cover
if __name__ == "__main__":
run()

View file

@ -0,0 +1,110 @@
from __future__ import annotations
from abc import ABC
from collections import OrderedDict
from pathlib import Path
from virtualenv.info import IS_WIN
class Describe:
"""Given a host interpreter tell us information about what the created interpreter might look like."""
suffix = ".exe" if IS_WIN else ""
def __init__(self, dest, interpreter) -> None:
self.interpreter = interpreter
self.dest = dest
self._stdlib = None
self._stdlib_platform = None
self._system_stdlib = None
self._conf_vars = None
@property
def bin_dir(self):
return self.script_dir
@property
def script_dir(self):
return self.dest / self.interpreter.install_path("scripts")
@property
def purelib(self):
return self.dest / self.interpreter.install_path("purelib")
@property
def platlib(self):
return self.dest / self.interpreter.install_path("platlib")
@property
def libs(self):
return list(OrderedDict(((self.platlib, None), (self.purelib, None))).keys())
@property
def stdlib(self):
if self._stdlib is None:
self._stdlib = Path(self.interpreter.sysconfig_path("stdlib", config_var=self._config_vars))
return self._stdlib
@property
def stdlib_platform(self):
if self._stdlib_platform is None:
self._stdlib_platform = Path(self.interpreter.sysconfig_path("platstdlib", config_var=self._config_vars))
return self._stdlib_platform
@property
def _config_vars(self):
if self._conf_vars is None:
self._conf_vars = self._calc_config_vars(self.dest)
return self._conf_vars
def _calc_config_vars(self, to):
sys_vars = self.interpreter.sysconfig_vars
return {k: (to if v is not None and v.startswith(self.interpreter.prefix) else v) for k, v in sys_vars.items()}
@classmethod
def can_describe(cls, interpreter): # noqa: ARG003
"""Knows means it knows how the output will look."""
return True
@property
def env_name(self):
return self.dest.parts[-1]
@property
def exe(self):
return self.bin_dir / f"{self.exe_stem()}{self.suffix}"
@classmethod
def exe_stem(cls):
"""Executable name without suffix - there seems to be no standard way to get this without creating it."""
raise NotImplementedError
def script(self, name):
return self.script_dir / f"{name}{self.suffix}"
class Python3Supports(Describe, ABC):
@classmethod
def can_describe(cls, interpreter):
return interpreter.version_info.major == 3 and super().can_describe(interpreter) # noqa: PLR2004
class PosixSupports(Describe, ABC):
@classmethod
def can_describe(cls, interpreter):
return interpreter.os == "posix" and super().can_describe(interpreter)
class WindowsSupports(Describe, ABC):
@classmethod
def can_describe(cls, interpreter):
return interpreter.os == "nt" and super().can_describe(interpreter)
__all__ = [
"Describe",
"PosixSupports",
"Python3Supports",
"WindowsSupports",
]

View file

@ -0,0 +1,66 @@
from __future__ import annotations
import logging
import os
from collections import OrderedDict
class PyEnvCfg:
def __init__(self, content, path) -> None:
self.content = content
self.path = path
@classmethod
def from_folder(cls, folder):
return cls.from_file(folder / "pyvenv.cfg")
@classmethod
def from_file(cls, path):
content = cls._read_values(path) if path.exists() else OrderedDict()
return PyEnvCfg(content, path)
@staticmethod
def _read_values(path):
content = OrderedDict()
for line in path.read_text(encoding="utf-8").splitlines():
equals_at = line.index("=")
key = line[:equals_at].strip()
value = line[equals_at + 1 :].strip()
content[key] = value
return content
def write(self):
logging.debug("write %s", self.path)
text = ""
for key, value in self.content.items():
normalized_value = os.path.realpath(value) if value and os.path.exists(value) else value
line = f"{key} = {normalized_value}"
logging.debug("\t%s", line)
text += line
text += "\n"
self.path.write_text(text, encoding="utf-8")
def refresh(self):
self.content = self._read_values(self.path)
return self.content
def __setitem__(self, key, value) -> None:
self.content[key] = value
def __getitem__(self, key):
return self.content[key]
def __contains__(self, item) -> bool:
return item in self.content
def update(self, other):
self.content.update(other)
return self
def __repr__(self) -> str:
return f"{self.__class__.__name__}(path={self.path})"
__all__ = [
"PyEnvCfg",
]

View file

@ -0,0 +1,103 @@
"""Patches that are applied at runtime to the virtual environment."""
from __future__ import annotations
import os
import sys
VIRTUALENV_PATCH_FILE = os.path.join(__file__)
def patch_dist(dist):
"""
Distutils allows user to configure some arguments via a configuration file:
https://docs.python.org/3/install/index.html#distutils-configuration-files.
Some of this arguments though don't make sense in context of the virtual environment files, let's fix them up.
""" # noqa: D205
# we cannot allow some install config as that would get packages installed outside of the virtual environment
old_parse_config_files = dist.Distribution.parse_config_files
def parse_config_files(self, *args, **kwargs):
result = old_parse_config_files(self, *args, **kwargs)
install = self.get_option_dict("install")
if "prefix" in install: # the prefix governs where to install the libraries
install["prefix"] = VIRTUALENV_PATCH_FILE, os.path.abspath(sys.prefix)
for base in ("purelib", "platlib", "headers", "scripts", "data"):
key = f"install_{base}"
if key in install: # do not allow global configs to hijack venv paths
install.pop(key, None)
return result
dist.Distribution.parse_config_files = parse_config_files
# Import hook that patches some modules to ignore configuration values that break package installation in case
# of virtual environments.
_DISTUTILS_PATCH = "distutils.dist", "setuptools.dist"
# https://docs.python.org/3/library/importlib.html#setting-up-an-importer
class _Finder:
"""A meta path finder that allows patching the imported distutils modules."""
fullname = None
# lock[0] is threading.Lock(), but initialized lazily to avoid importing threading very early at startup,
# because there are gevent-based applications that need to be first to import threading by themselves.
# See https://github.com/pypa/virtualenv/issues/1895 for details.
lock = [] # noqa: RUF012
def find_spec(self, fullname, path, target=None): # noqa: ARG002
if fullname in _DISTUTILS_PATCH and self.fullname is None: # noqa: PLR1702
# initialize lock[0] lazily
if len(self.lock) == 0:
import threading # noqa: PLC0415
lock = threading.Lock()
# there is possibility that two threads T1 and T2 are simultaneously running into find_spec,
# observing .lock as empty, and further going into hereby initialization. However due to the GIL,
# list.append() operation is atomic and this way only one of the threads will "win" to put the lock
# - that every thread will use - into .lock[0].
# https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
self.lock.append(lock)
from functools import partial # noqa: PLC0415
from importlib.util import find_spec # noqa: PLC0415
with self.lock[0]:
self.fullname = fullname
try:
spec = find_spec(fullname, path)
if spec is not None:
# https://www.python.org/dev/peps/pep-0451/#how-loading-will-work
is_new_api = hasattr(spec.loader, "exec_module")
func_name = "exec_module" if is_new_api else "load_module"
old = getattr(spec.loader, func_name)
func = self.exec_module if is_new_api else self.load_module
if old is not func:
try: # noqa: SIM105
setattr(spec.loader, func_name, partial(func, old))
except AttributeError:
pass # C-Extension loaders are r/o such as zipimporter with <3.7
return spec
finally:
self.fullname = None
return None
@staticmethod
def exec_module(old, module):
old(module)
if module.__name__ in _DISTUTILS_PATCH:
patch_dist(module)
@staticmethod
def load_module(old, name):
module = old(name)
if module.__name__ in _DISTUTILS_PATCH:
patch_dist(module)
return module
sys.meta_path.insert(0, _Finder())

View file

@ -0,0 +1,114 @@
from __future__ import annotations
import logging
import os
from abc import ABC
from pathlib import Path
from virtualenv.create.creator import Creator, CreatorMeta
from virtualenv.info import fs_supports_symlink
class ViaGlobalRefMeta(CreatorMeta):
def __init__(self) -> None:
super().__init__()
self.copy_error = None
self.symlink_error = None
if not fs_supports_symlink():
self.symlink_error = "the filesystem does not supports symlink"
@property
def can_copy(self):
return not self.copy_error
@property
def can_symlink(self):
return not self.symlink_error
class ViaGlobalRefApi(Creator, ABC):
def __init__(self, options, interpreter) -> None:
super().__init__(options, interpreter)
self.symlinks = self._should_symlink(options)
self.enable_system_site_package = options.system_site
@staticmethod
def _should_symlink(options):
# Priority of where the option is set to follow the order: CLI, env var, file, hardcoded.
# If both set at same level prefers copy over symlink.
copies, symlinks = getattr(options, "copies", False), getattr(options, "symlinks", False)
copy_src, sym_src = options.get_source("copies"), options.get_source("symlinks")
for level in ["cli", "env var", "file", "default"]:
s_opt = symlinks if sym_src == level else None
c_opt = copies if copy_src == level else None
if s_opt is True and c_opt is True:
return False
if s_opt is True:
return True
if c_opt is True:
return False
return False # fallback to copy
@classmethod
def add_parser_arguments(cls, parser, interpreter, meta, app_data):
super().add_parser_arguments(parser, interpreter, meta, app_data)
parser.add_argument(
"--system-site-packages",
default=False,
action="store_true",
dest="system_site",
help="give the virtual environment access to the system site-packages dir",
)
if not meta.can_symlink and not meta.can_copy:
msg = "neither symlink or copy method supported"
raise RuntimeError(msg)
group = parser.add_mutually_exclusive_group()
if meta.can_symlink:
group.add_argument(
"--symlinks",
default=True,
action="store_true",
dest="symlinks",
help="try to use symlinks rather than copies, when symlinks are not the default for the platform",
)
if meta.can_copy:
group.add_argument(
"--copies",
"--always-copy",
default=not meta.can_symlink,
action="store_true",
dest="copies",
help="try to use copies rather than symlinks, even when symlinks are the default for the platform",
)
def create(self):
self.install_patch()
def install_patch(self):
text = self.env_patch_text()
if text:
pth = self.purelib / "_virtualenv.pth"
logging.debug("create virtualenv import hook file %s", pth)
pth.write_text("import _virtualenv", encoding="utf-8")
dest_path = self.purelib / "_virtualenv.py"
logging.debug("create %s", dest_path)
dest_path.write_text(text, encoding="utf-8")
def env_patch_text(self):
"""Patch the distutils package to not be derailed by its configuration files."""
with self.app_data.ensure_extracted(Path(__file__).parent / "_virtualenv.py") as resolved_path:
text = resolved_path.read_text(encoding="utf-8")
return text.replace('"__SCRIPT_DIR__"', repr(os.path.relpath(str(self.script_dir), str(self.purelib))))
def _args(self):
return [*super()._args(), ("global", self.enable_system_site_package)]
def set_pyenv_cfg(self):
super().set_pyenv_cfg()
self.pyenv_cfg["include-system-site-packages"] = "true" if self.enable_system_site_package else "false"
__all__ = [
"ViaGlobalRefApi",
"ViaGlobalRefMeta",
]

View file

@ -0,0 +1,19 @@
from __future__ import annotations
from abc import ABC
from virtualenv.create.creator import Creator
from virtualenv.create.describe import Describe
class VirtualenvBuiltin(Creator, Describe, ABC):
"""A creator that does operations itself without delegation, if we can create it we can also describe it."""
def __init__(self, options, interpreter) -> None:
Creator.__init__(self, options, interpreter)
Describe.__init__(self, self.dest, interpreter)
__all__ = [
"VirtualenvBuiltin",
]

View file

@ -0,0 +1,73 @@
from __future__ import annotations
import re
from abc import ABC
from collections import OrderedDict
from pathlib import Path
from virtualenv.create.describe import PosixSupports, WindowsSupports
from virtualenv.create.via_global_ref.builtin.ref import RefMust, RefWhen
from virtualenv.create.via_global_ref.builtin.via_global_self_do import ViaGlobalRefVirtualenvBuiltin
class CPython(ViaGlobalRefVirtualenvBuiltin, ABC):
@classmethod
def can_describe(cls, interpreter):
return interpreter.implementation == "CPython" and super().can_describe(interpreter)
@classmethod
def exe_stem(cls):
return "python"
class CPythonPosix(CPython, PosixSupports, ABC):
"""Create a CPython virtual environment on POSIX platforms."""
@classmethod
def _executables(cls, interpreter):
host_exe = Path(interpreter.system_executable)
major, minor = interpreter.version_info.major, interpreter.version_info.minor
targets = OrderedDict((i, None) for i in ["python", f"python{major}", f"python{major}.{minor}", host_exe.name])
yield host_exe, list(targets.keys()), RefMust.NA, RefWhen.ANY
class CPythonWindows(CPython, WindowsSupports, ABC):
@classmethod
def _executables(cls, interpreter):
# symlink of the python executables does not work reliably, copy always instead
# - https://bugs.python.org/issue42013
# - venv
host = cls.host_python(interpreter)
for path in (host.parent / n for n in {"python.exe", host.name}): # noqa: PLC0208
yield host, [path.name], RefMust.COPY, RefWhen.ANY
# for more info on pythonw.exe see https://stackoverflow.com/a/30313091
python_w = host.parent / "pythonw.exe"
yield python_w, [python_w.name], RefMust.COPY, RefWhen.ANY
@classmethod
def host_python(cls, interpreter):
return Path(interpreter.system_executable)
def is_mac_os_framework(interpreter):
if interpreter.platform == "darwin":
return interpreter.sysconfig_vars.get("PYTHONFRAMEWORK") == "Python3"
return False
def is_macos_brew(interpreter):
return interpreter.platform == "darwin" and _BREW.fullmatch(interpreter.system_prefix) is not None
_BREW = re.compile(
r"/(usr/local|opt/homebrew)/(opt/python@3\.\d{1,2}|Cellar/python@3\.\d{1,2}/3\.\d{1,2}\.\d{1,2})/Frameworks/"
r"Python\.framework/Versions/3\.\d{1,2}",
)
__all__ = [
"CPython",
"CPythonPosix",
"CPythonWindows",
"is_mac_os_framework",
"is_macos_brew",
]

View file

@ -0,0 +1,135 @@
from __future__ import annotations
import abc
import fnmatch
from itertools import chain
from operator import methodcaller as method
from pathlib import Path
from textwrap import dedent
from virtualenv.create.describe import Python3Supports
from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest
from virtualenv.create.via_global_ref.store import is_store_python
from .common import CPython, CPythonPosix, CPythonWindows, is_mac_os_framework, is_macos_brew
class CPython3(CPython, Python3Supports, abc.ABC):
"""CPython 3 or later."""
class CPython3Posix(CPythonPosix, CPython3):
@classmethod
def can_describe(cls, interpreter):
return (
is_mac_os_framework(interpreter) is False
and is_macos_brew(interpreter) is False
and super().can_describe(interpreter)
)
def env_patch_text(self):
text = super().env_patch_text()
if self.pyvenv_launch_patch_active(self.interpreter):
text += dedent(
"""
# for https://github.com/python/cpython/pull/9516, see https://github.com/pypa/virtualenv/issues/1704
import os
if "__PYVENV_LAUNCHER__" in os.environ:
del os.environ["__PYVENV_LAUNCHER__"]
""",
)
return text
@classmethod
def pyvenv_launch_patch_active(cls, interpreter):
ver = interpreter.version_info
return interpreter.platform == "darwin" and ((3, 7, 8) > ver >= (3, 7) or (3, 8, 3) > ver >= (3, 8))
class CPython3Windows(CPythonWindows, CPython3):
"""CPython 3 on Windows."""
@classmethod
def setup_meta(cls, interpreter):
if is_store_python(interpreter): # store python is not supported here
return None
return super().setup_meta(interpreter)
@classmethod
def sources(cls, interpreter):
if cls.has_shim(interpreter):
refs = cls.executables(interpreter)
else:
refs = chain(
cls.executables(interpreter),
cls.dll_and_pyd(interpreter),
cls.python_zip(interpreter),
)
yield from refs
@classmethod
def executables(cls, interpreter):
return super().sources(interpreter)
@classmethod
def has_shim(cls, interpreter):
return interpreter.version_info.minor >= 7 and cls.shim(interpreter) is not None # noqa: PLR2004
@classmethod
def shim(cls, interpreter):
shim = Path(interpreter.system_stdlib) / "venv" / "scripts" / "nt" / "python.exe"
if shim.exists():
return shim
return None
@classmethod
def host_python(cls, interpreter):
if cls.has_shim(interpreter):
# starting with CPython 3.7 Windows ships with a venvlauncher.exe that avoids the need for dll/pyd copies
# it also means the wrapper must be copied to avoid bugs such as https://bugs.python.org/issue42013
return cls.shim(interpreter)
return super().host_python(interpreter)
@classmethod
def dll_and_pyd(cls, interpreter):
folders = [Path(interpreter.system_executable).parent]
# May be missing on some Python hosts.
# See https://github.com/pypa/virtualenv/issues/2368
dll_folder = Path(interpreter.system_prefix) / "DLLs"
if dll_folder.is_dir():
folders.append(dll_folder)
for folder in folders:
for file in folder.iterdir():
if file.suffix in {".pyd", ".dll"}:
yield PathRefToDest(file, cls.to_bin)
@classmethod
def python_zip(cls, interpreter):
"""
"python{VERSION}.zip" contains compiled *.pyc std lib packages, where
"VERSION" is `py_version_nodot` var from the `sysconfig` module.
:see: https://docs.python.org/3/using/windows.html#the-embeddable-package
:see: `discovery.py_info.PythonInfo` class (interpreter).
:see: `python -m sysconfig` output.
:note: The embeddable Python distribution for Windows includes
"python{VERSION}.zip" and "python{VERSION}._pth" files. User can
move/rename *zip* file and edit `sys.path` by editing *_pth* file.
Here the `pattern` is used only for the default *zip* file name!
""" # noqa: D205
pattern = f"*python{interpreter.version_nodot}.zip"
matches = fnmatch.filter(interpreter.path, pattern)
matched_paths = map(Path, matches)
existing_paths = filter(method("exists"), matched_paths)
path = next(existing_paths, None)
if path is not None:
yield PathRefToDest(path, cls.to_bin)
__all__ = [
"CPython3",
"CPython3Posix",
"CPython3Windows",
]

View file

@ -0,0 +1,279 @@
"""The Apple Framework builds require their own customization."""
from __future__ import annotations
import logging
import os
import struct
import subprocess
from abc import ABC, abstractmethod
from pathlib import Path
from textwrap import dedent
from virtualenv.create.via_global_ref.builtin.ref import (
ExePathRefToDest,
PathRefToDest,
RefMust,
)
from virtualenv.create.via_global_ref.builtin.via_global_self_do import BuiltinViaGlobalRefMeta
from .common import CPython, CPythonPosix, is_mac_os_framework, is_macos_brew
from .cpython3 import CPython3
class CPythonmacOsFramework(CPython, ABC):
@classmethod
def can_describe(cls, interpreter):
return is_mac_os_framework(interpreter) and super().can_describe(interpreter)
def create(self):
super().create()
# change the install_name of the copied python executables
target = self.desired_mach_o_image_path()
current = self.current_mach_o_image_path()
for src in self._sources:
if isinstance(src, ExePathRefToDest) and (src.must == RefMust.COPY or not self.symlinks):
exes = [self.bin_dir / src.base]
if not self.symlinks:
exes.extend(self.bin_dir / a for a in src.aliases)
for exe in exes:
fix_mach_o(str(exe), current, target, self.interpreter.max_size)
@classmethod
def _executables(cls, interpreter):
for _, targets, must, when in super()._executables(interpreter):
# Make sure we use the embedded interpreter inside the framework, even if sys.executable points to the
# stub executable in ${sys.prefix}/bin.
# See http://groups.google.com/group/python-virtualenv/browse_thread/thread/17cab2f85da75951
fixed_host_exe = Path(interpreter.prefix) / "Resources" / "Python.app" / "Contents" / "MacOS" / "Python"
yield fixed_host_exe, targets, must, when
@abstractmethod
def current_mach_o_image_path(self):
raise NotImplementedError
@abstractmethod
def desired_mach_o_image_path(self):
raise NotImplementedError
class CPython3macOsFramework(CPythonmacOsFramework, CPython3, CPythonPosix):
def current_mach_o_image_path(self):
return "@executable_path/../../../../Python3"
def desired_mach_o_image_path(self):
return "@executable_path/../.Python"
@classmethod
def sources(cls, interpreter):
yield from super().sources(interpreter)
# add a symlink to the host python image
exe = Path(interpreter.prefix) / "Python3"
yield PathRefToDest(exe, dest=lambda self, _: self.dest / ".Python", must=RefMust.SYMLINK)
@property
def reload_code(self):
result = super().reload_code
return dedent(
f"""
# the bundled site.py always adds the global site package if we're on python framework build, escape this
import sys
before = sys._framework
try:
sys._framework = None
{result}
finally:
sys._framework = before
""",
)
def fix_mach_o(exe, current, new, max_size):
"""
https://en.wikipedia.org/wiki/Mach-O.
Mach-O, short for Mach object file format, is a file format for executables, object code, shared libraries,
dynamically-loaded code, and core dumps. A replacement for the a.out format, Mach-O offers more extensibility and
faster access to information in the symbol table.
Each Mach-O file is made up of one Mach-O header, followed by a series of load commands, followed by one or more
segments, each of which contains between 0 and 255 sections. Mach-O uses the REL relocation format to handle
references to symbols. When looking up symbols Mach-O uses a two-level namespace that encodes each symbol into an
'object/symbol name' pair that is then linearly searched for by first the object and then the symbol name.
The basic structurea list of variable-length "load commands" that reference pages of data elsewhere in the filewas
also used in the executable file format for Accent. The Accent file format was in turn, based on an idea from Spice
Lisp.
With the introduction of Mac OS X 10.6 platform the Mach-O file underwent a significant modification that causes
binaries compiled on a computer running 10.6 or later to be (by default) executable only on computers running Mac
OS X 10.6 or later. The difference stems from load commands that the dynamic linker, in previous Mac OS X versions,
does not understand. Another significant change to the Mach-O format is the change in how the Link Edit tables
(found in the __LINKEDIT section) function. In 10.6 these new Link Edit tables are compressed by removing unused and
unneeded bits of information, however Mac OS X 10.5 and earlier cannot read this new Link Edit table format.
"""
try:
logging.debug("change Mach-O for %s from %s to %s", exe, current, new)
_builtin_change_mach_o(max_size)(exe, current, new)
except Exception as e: # noqa: BLE001
logging.warning("Could not call _builtin_change_mac_o: %s. Trying to call install_name_tool instead.", e)
try:
cmd = ["install_name_tool", "-change", current, new, exe]
subprocess.check_call(cmd)
except Exception:
logging.fatal("Could not call install_name_tool -- you must have Apple's development tools installed")
raise
def _builtin_change_mach_o(maxint): # noqa: C901
MH_MAGIC = 0xFEEDFACE # noqa: N806
MH_CIGAM = 0xCEFAEDFE # noqa: N806
MH_MAGIC_64 = 0xFEEDFACF # noqa: N806
MH_CIGAM_64 = 0xCFFAEDFE # noqa: N806
FAT_MAGIC = 0xCAFEBABE # noqa: N806
BIG_ENDIAN = ">" # noqa: N806
LITTLE_ENDIAN = "<" # noqa: N806
LC_LOAD_DYLIB = 0xC # noqa: N806
class FileView:
"""A proxy for file-like objects that exposes a given view of a file. Modified from macholib."""
def __init__(self, file_obj, start=0, size=maxint) -> None:
if isinstance(file_obj, FileView):
self._file_obj = file_obj._file_obj # noqa: SLF001
else:
self._file_obj = file_obj
self._start = start
self._end = start + size
self._pos = 0
def __repr__(self) -> str:
return f"<fileview [{self._start:d}, {self._end:d}] {self._file_obj!r}>"
def tell(self):
return self._pos
def _checkwindow(self, seek_to, op):
if not (self._start <= seek_to <= self._end):
msg = f"{op} to offset {seek_to:d} is outside window [{self._start:d}, {self._end:d}]"
raise OSError(msg)
def seek(self, offset, whence=0):
seek_to = offset
if whence == os.SEEK_SET:
seek_to += self._start
elif whence == os.SEEK_CUR:
seek_to += self._start + self._pos
elif whence == os.SEEK_END:
seek_to += self._end
else:
msg = f"Invalid whence argument to seek: {whence!r}"
raise OSError(msg)
self._checkwindow(seek_to, "seek")
self._file_obj.seek(seek_to)
self._pos = seek_to - self._start
def write(self, content):
here = self._start + self._pos
self._checkwindow(here, "write")
self._checkwindow(here + len(content), "write")
self._file_obj.seek(here, os.SEEK_SET)
self._file_obj.write(content)
self._pos += len(content)
def read(self, size=maxint):
assert size >= 0 # noqa: S101
here = self._start + self._pos
self._checkwindow(here, "read")
size = min(size, self._end - here)
self._file_obj.seek(here, os.SEEK_SET)
read_bytes = self._file_obj.read(size)
self._pos += len(read_bytes)
return read_bytes
def read_data(file, endian, num=1):
"""Read a given number of 32-bits unsigned integers from the given file with the given endianness."""
res = struct.unpack(endian + "L" * num, file.read(num * 4))
if len(res) == 1:
return res[0]
return res
def mach_o_change(at_path, what, value): # noqa: C901
"""
Replace a given name (what) in any LC_LOAD_DYLIB command found in the given binary with a new name (value),
provided it's shorter.
""" # noqa: D205
def do_macho(file, bits, endian):
# Read Mach-O header (the magic number is assumed read by the caller)
_cpu_type, _cpu_sub_type, _file_type, n_commands, _size_of_commands, _flags = read_data(file, endian, 6)
# 64-bits header has one more field.
if bits == 64: # noqa: PLR2004
read_data(file, endian)
# The header is followed by n commands
for _ in range(n_commands):
where = file.tell()
# Read command header
cmd, cmd_size = read_data(file, endian, 2)
if cmd == LC_LOAD_DYLIB:
# The first data field in LC_LOAD_DYLIB commands is the offset of the name, starting from the
# beginning of the command.
name_offset = read_data(file, endian)
file.seek(where + name_offset, os.SEEK_SET)
# Read the NUL terminated string
load = file.read(cmd_size - name_offset).decode()
load = load[: load.index("\0")]
# If the string is what is being replaced, overwrite it.
if load == what:
file.seek(where + name_offset, os.SEEK_SET)
file.write(value.encode() + b"\0")
# Seek to the next command
file.seek(where + cmd_size, os.SEEK_SET)
def do_file(file, offset=0, size=maxint):
file = FileView(file, offset, size)
# Read magic number
magic = read_data(file, BIG_ENDIAN)
if magic == FAT_MAGIC:
# Fat binaries contain nfat_arch Mach-O binaries
n_fat_arch = read_data(file, BIG_ENDIAN)
for _ in range(n_fat_arch):
# Read arch header
_cpu_type, _cpu_sub_type, offset, size, _align = read_data(file, BIG_ENDIAN, 5)
do_file(file, offset, size)
elif magic == MH_MAGIC:
do_macho(file, 32, BIG_ENDIAN)
elif magic == MH_CIGAM:
do_macho(file, 32, LITTLE_ENDIAN)
elif magic == MH_MAGIC_64:
do_macho(file, 64, BIG_ENDIAN)
elif magic == MH_CIGAM_64:
do_macho(file, 64, LITTLE_ENDIAN)
assert len(what) >= len(value) # noqa: S101
with open(at_path, "r+b") as f:
do_file(f)
return mach_o_change
class CPython3macOsBrew(CPython3, CPythonPosix):
@classmethod
def can_describe(cls, interpreter):
return is_macos_brew(interpreter) and super().can_describe(interpreter)
@classmethod
def setup_meta(cls, interpreter): # noqa: ARG003
meta = BuiltinViaGlobalRefMeta()
meta.copy_error = "Brew disables copy creation: https://github.com/Homebrew/homebrew-core/issues/138159"
return meta
__all__ = [
"CPython3macOsBrew",
"CPython3macOsFramework",
"CPythonmacOsFramework",
]

View file

@ -0,0 +1,53 @@
from __future__ import annotations
import abc
from pathlib import Path
from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest, RefMust, RefWhen
from virtualenv.create.via_global_ref.builtin.via_global_self_do import ViaGlobalRefVirtualenvBuiltin
class PyPy(ViaGlobalRefVirtualenvBuiltin, abc.ABC):
@classmethod
def can_describe(cls, interpreter):
return interpreter.implementation == "PyPy" and super().can_describe(interpreter)
@classmethod
def _executables(cls, interpreter):
host = Path(interpreter.system_executable)
targets = sorted(f"{name}{PyPy.suffix}" for name in cls.exe_names(interpreter))
yield host, targets, RefMust.NA, RefWhen.ANY
@classmethod
def executables(cls, interpreter):
yield from super().sources(interpreter)
@classmethod
def exe_names(cls, interpreter):
return {
cls.exe_stem(),
"python",
f"python{interpreter.version_info.major}",
f"python{interpreter.version_info.major}.{interpreter.version_info.minor}",
}
@classmethod
def sources(cls, interpreter):
yield from cls.executables(interpreter)
for host in cls._add_shared_libs(interpreter):
yield PathRefToDest(host, dest=lambda self, s: self.bin_dir / s.name)
@classmethod
def _add_shared_libs(cls, interpreter):
# https://bitbucket.org/pypy/pypy/issue/1922/future-proofing-virtualenv
python_dir = Path(interpreter.system_executable).resolve().parent
yield from cls._shared_libs(python_dir)
@classmethod
def _shared_libs(cls, python_dir):
raise NotImplementedError
__all__ = [
"PyPy",
]

View file

@ -0,0 +1,76 @@
from __future__ import annotations
import abc
from pathlib import Path
from virtualenv.create.describe import PosixSupports, Python3Supports, WindowsSupports
from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest
from .common import PyPy
class PyPy3(PyPy, Python3Supports, abc.ABC):
@classmethod
def exe_stem(cls):
return "pypy3"
@classmethod
def exe_names(cls, interpreter):
return super().exe_names(interpreter) | {"pypy"}
class PyPy3Posix(PyPy3, PosixSupports):
"""PyPy 3 on POSIX."""
@classmethod
def _shared_libs(cls, python_dir):
# glob for libpypy3-c.so, libpypy3-c.dylib, libpypy3.9-c.so ...
return python_dir.glob("libpypy3*.*")
def to_lib(self, src):
return self.dest / "lib" / src.name
@classmethod
def sources(cls, interpreter):
yield from super().sources(interpreter)
# PyPy >= 3.8 supports a standard prefix installation, where older
# versions always used a portable/development style installation.
# If this is a standard prefix installation, skip the below:
if interpreter.system_prefix == "/usr":
return
# Also copy/symlink anything under prefix/lib, which, for "portable"
# PyPy builds, includes the tk,tcl runtime and a number of shared
# objects. In distro-specific builds or on conda this should be empty
# (on PyPy3.8+ it will, like on CPython, hold the stdlib).
host_lib = Path(interpreter.system_prefix) / "lib"
stdlib = Path(interpreter.system_stdlib)
if host_lib.exists() and host_lib.is_dir():
for path in host_lib.iterdir():
if stdlib == path:
# For PyPy3.8+ the stdlib lives in lib/pypy3.8
# We need to avoid creating a symlink to it since that
# will defeat the purpose of a virtualenv
continue
yield PathRefToDest(path, dest=cls.to_lib)
class Pypy3Windows(PyPy3, WindowsSupports):
"""PyPy 3 on Windows."""
@property
def less_v37(self):
return self.interpreter.version_info.minor < 7 # noqa: PLR2004
@classmethod
def _shared_libs(cls, python_dir):
# glob for libpypy*.dll and libffi*.dll
for pattern in ["libpypy*.dll", "libffi*.dll"]:
srcs = python_dir.glob(pattern)
yield from srcs
__all__ = [
"PyPy3",
"PyPy3Posix",
"Pypy3Windows",
]

View file

@ -0,0 +1,178 @@
"""
Virtual environments in the traditional sense are built as reference to the host python. This file allows declarative
references to elements on the file system, allowing our system to automatically detect what modes it can support given
the constraints: e.g. can the file system symlink, can the files be read, executed, etc.
""" # noqa: D205
from __future__ import annotations
import os
from abc import ABC, abstractmethod
from collections import OrderedDict
from stat import S_IXGRP, S_IXOTH, S_IXUSR
from virtualenv.info import fs_is_case_sensitive, fs_supports_symlink
from virtualenv.util.path import copy, make_exe, symlink
class RefMust:
NA = "NA"
COPY = "copy"
SYMLINK = "symlink"
class RefWhen:
ANY = "ANY"
COPY = "copy"
SYMLINK = "symlink"
class PathRef(ABC):
"""Base class that checks if a file reference can be symlink/copied."""
FS_SUPPORTS_SYMLINK = fs_supports_symlink()
FS_CASE_SENSITIVE = fs_is_case_sensitive()
def __init__(self, src, must=RefMust.NA, when=RefWhen.ANY) -> None:
self.must = must
self.when = when
self.src = src
try:
self.exists = src.exists()
except OSError:
self.exists = False
self._can_read = None if self.exists else False
self._can_copy = None if self.exists else False
self._can_symlink = None if self.exists else False
def __repr__(self) -> str:
return f"{self.__class__.__name__}(src={self.src})"
@property
def can_read(self):
if self._can_read is None:
if self.src.is_file():
try:
with self.src.open("rb"):
self._can_read = True
except OSError:
self._can_read = False
else:
self._can_read = os.access(str(self.src), os.R_OK)
return self._can_read
@property
def can_copy(self):
if self._can_copy is None:
if self.must == RefMust.SYMLINK:
self._can_copy = self.can_symlink
else:
self._can_copy = self.can_read
return self._can_copy
@property
def can_symlink(self):
if self._can_symlink is None:
if self.must == RefMust.COPY:
self._can_symlink = self.can_copy
else:
self._can_symlink = self.FS_SUPPORTS_SYMLINK and self.can_read
return self._can_symlink
@abstractmethod
def run(self, creator, symlinks):
raise NotImplementedError
def method(self, symlinks):
if self.must == RefMust.SYMLINK:
return symlink
if self.must == RefMust.COPY:
return copy
return symlink if symlinks else copy
class ExePathRef(PathRef, ABC):
"""Base class that checks if a executable can be references via symlink/copy."""
def __init__(self, src, must=RefMust.NA, when=RefWhen.ANY) -> None:
super().__init__(src, must, when)
self._can_run = None
@property
def can_symlink(self):
if self.FS_SUPPORTS_SYMLINK:
return self.can_run
return False
@property
def can_run(self):
if self._can_run is None:
mode = self.src.stat().st_mode
for key in [S_IXUSR, S_IXGRP, S_IXOTH]:
if mode & key:
self._can_run = True
break
else:
self._can_run = False
return self._can_run
class PathRefToDest(PathRef):
"""Link a path on the file system."""
def __init__(self, src, dest, must=RefMust.NA, when=RefWhen.ANY) -> None:
super().__init__(src, must, when)
self.dest = dest
def run(self, creator, symlinks):
dest = self.dest(creator, self.src)
method = self.method(symlinks)
dest_iterable = dest if isinstance(dest, list) else (dest,)
if not dest.parent.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
for dst in dest_iterable:
method(self.src, dst)
class ExePathRefToDest(PathRefToDest, ExePathRef):
"""Link a exe path on the file system."""
def __init__(self, src, targets, dest, must=RefMust.NA, when=RefWhen.ANY) -> None:
ExePathRef.__init__(self, src, must, when)
PathRefToDest.__init__(self, src, dest, must, when)
if not self.FS_CASE_SENSITIVE:
targets = list(OrderedDict((i.lower(), None) for i in targets).keys())
self.base = targets[0]
self.aliases = targets[1:]
self.dest = dest
def run(self, creator, symlinks):
bin_dir = self.dest(creator, self.src).parent
dest = bin_dir / self.base
method = self.method(symlinks)
method(self.src, dest)
if not symlinks:
make_exe(dest)
for extra in self.aliases:
link_file = bin_dir / extra
if link_file.exists():
link_file.unlink()
if symlinks:
link_file.symlink_to(self.base)
else:
copy(self.src, link_file)
if not symlinks:
make_exe(link_file)
def __repr__(self) -> str:
return f"{self.__class__.__name__}(src={self.src}, alias={self.aliases})"
__all__ = [
"ExePathRef",
"ExePathRefToDest",
"PathRef",
"PathRefToDest",
"RefMust",
"RefWhen",
]

View file

@ -0,0 +1,118 @@
from __future__ import annotations
from abc import ABC
from virtualenv.create.via_global_ref.api import ViaGlobalRefApi, ViaGlobalRefMeta
from virtualenv.create.via_global_ref.builtin.ref import (
ExePathRefToDest,
RefMust,
RefWhen,
)
from virtualenv.util.path import ensure_dir
from .builtin_way import VirtualenvBuiltin
class BuiltinViaGlobalRefMeta(ViaGlobalRefMeta):
def __init__(self) -> None:
super().__init__()
self.sources = []
class ViaGlobalRefVirtualenvBuiltin(ViaGlobalRefApi, VirtualenvBuiltin, ABC):
def __init__(self, options, interpreter) -> None:
super().__init__(options, interpreter)
self._sources = getattr(options.meta, "sources", None) # if we're created as a describer this might be missing
@classmethod
def can_create(cls, interpreter):
"""By default, all built-in methods assume that if we can describe it we can create it."""
# first we must be able to describe it
if not cls.can_describe(interpreter):
return None
meta = cls.setup_meta(interpreter)
if meta is not None and meta:
cls._sources_can_be_applied(interpreter, meta)
return meta
@classmethod
def _sources_can_be_applied(cls, interpreter, meta):
for src in cls.sources(interpreter):
if src.exists:
if meta.can_copy and not src.can_copy:
meta.copy_error = f"cannot copy {src}"
if meta.can_symlink and not src.can_symlink:
meta.symlink_error = f"cannot symlink {src}"
else:
msg = f"missing required file {src}"
if src.when == RefMust.NA:
meta.error = msg
elif src.when == RefMust.COPY:
meta.copy_error = msg
elif src.when == RefMust.SYMLINK:
meta.symlink_error = msg
if not meta.can_copy and not meta.can_symlink:
meta.error = f"neither copy or symlink supported, copy: {meta.copy_error} symlink: {meta.symlink_error}"
if meta.error:
break
meta.sources.append(src)
@classmethod
def setup_meta(cls, interpreter): # noqa: ARG003
return BuiltinViaGlobalRefMeta()
@classmethod
def sources(cls, interpreter):
for host_exe, targets, must, when in cls._executables(interpreter):
yield ExePathRefToDest(host_exe, dest=cls.to_bin, targets=targets, must=must, when=when)
def to_bin(self, src):
return self.bin_dir / src.name
@classmethod
def _executables(cls, interpreter):
raise NotImplementedError
def create(self):
dirs = self.ensure_directories()
for directory in list(dirs):
if any(i for i in dirs if i is not directory and directory.parts == i.parts[: len(directory.parts)]):
dirs.remove(directory)
for directory in sorted(dirs):
ensure_dir(directory)
self.set_pyenv_cfg()
self.pyenv_cfg.write()
true_system_site = self.enable_system_site_package
try:
self.enable_system_site_package = False
for src in self._sources:
if (
src.when == RefWhen.ANY
or (src.when == RefWhen.SYMLINK and self.symlinks is True)
or (src.when == RefWhen.COPY and self.symlinks is False)
):
src.run(self, self.symlinks)
finally:
if true_system_site != self.enable_system_site_package:
self.enable_system_site_package = true_system_site
super().create()
def ensure_directories(self):
return {self.dest, self.bin_dir, self.script_dir, self.stdlib} | set(self.libs)
def set_pyenv_cfg(self):
"""
We directly inject the base prefix and base exec prefix to avoid site.py needing to discover these
from home (which usually is done within the interpreter itself).
""" # noqa: D205
super().set_pyenv_cfg()
self.pyenv_cfg["base-prefix"] = self.interpreter.system_prefix
self.pyenv_cfg["base-exec-prefix"] = self.interpreter.system_exec_prefix
self.pyenv_cfg["base-executable"] = self.interpreter.system_executable
__all__ = [
"BuiltinViaGlobalRefMeta",
"ViaGlobalRefVirtualenvBuiltin",
]

View file

@ -0,0 +1,26 @@
from __future__ import annotations
from pathlib import Path
def handle_store_python(meta, interpreter):
if is_store_python(interpreter):
meta.symlink_error = "Windows Store Python does not support virtual environments via symlink"
return meta
def is_store_python(interpreter):
parts = Path(interpreter.system_executable).parts
return (
len(parts) > 4 # noqa: PLR2004
and parts[-4] == "Microsoft"
and parts[-3] == "WindowsApps"
and parts[-2].startswith("PythonSoftwareFoundation.Python.3.")
and parts[-1].startswith("python")
)
__all__ = [
"handle_store_python",
"is_store_python",
]

View file

@ -0,0 +1,102 @@
from __future__ import annotations # noqa: A005
import logging
from copy import copy
from virtualenv.create.via_global_ref.store import handle_store_python
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.util.error import ProcessCallFailedError
from virtualenv.util.path import ensure_dir
from virtualenv.util.subprocess import run_cmd
from .api import ViaGlobalRefApi, ViaGlobalRefMeta
from .builtin.cpython.mac_os import CPython3macOsBrew
from .builtin.pypy.pypy3 import Pypy3Windows
class Venv(ViaGlobalRefApi):
def __init__(self, options, interpreter) -> None:
self.describe = options.describe
super().__init__(options, interpreter)
current = PythonInfo.current()
self.can_be_inline = interpreter is current and interpreter.executable == interpreter.system_executable
self._context = None
def _args(self):
return super()._args() + ([("describe", self.describe.__class__.__name__)] if self.describe else [])
@classmethod
def can_create(cls, interpreter):
if interpreter.has_venv:
if CPython3macOsBrew.can_describe(interpreter):
return CPython3macOsBrew.setup_meta(interpreter)
meta = ViaGlobalRefMeta()
if interpreter.platform == "win32":
meta = handle_store_python(meta, interpreter)
return meta
return None
def create(self):
if self.can_be_inline:
self.create_inline()
else:
self.create_via_sub_process()
for lib in self.libs:
ensure_dir(lib)
super().create()
self.executables_for_win_pypy_less_v37()
def executables_for_win_pypy_less_v37(self):
"""
PyPy <= 3.6 (v7.3.3) for Windows contains only pypy3.exe and pypy3w.exe
Venv does not handle non-existing exe sources, e.g. python.exe, so this
patch does it.
""" # noqa: D205
creator = self.describe
if isinstance(creator, Pypy3Windows) and creator.less_v37:
for exe in creator.executables(self.interpreter):
exe.run(creator, self.symlinks)
def create_inline(self):
from venv import EnvBuilder # noqa: PLC0415
builder = EnvBuilder(
system_site_packages=self.enable_system_site_package,
clear=False,
symlinks=self.symlinks,
with_pip=False,
)
builder.create(str(self.dest))
def create_via_sub_process(self):
cmd = self.get_host_create_cmd()
logging.info("using host built-in venv to create via %s", " ".join(cmd))
code, out, err = run_cmd(cmd)
if code != 0:
raise ProcessCallFailedError(code, out, err, cmd)
def get_host_create_cmd(self):
cmd = [self.interpreter.system_executable, "-m", "venv", "--without-pip"]
if self.enable_system_site_package:
cmd.append("--system-site-packages")
cmd.extend(("--symlinks" if self.symlinks else "--copies", str(self.dest)))
return cmd
def set_pyenv_cfg(self):
# prefer venv options over ours, but keep our extra
venv_content = copy(self.pyenv_cfg.refresh())
super().set_pyenv_cfg()
self.pyenv_cfg.update(venv_content)
def __getattribute__(self, item):
describe = object.__getattribute__(self, "describe")
if describe is not None and hasattr(describe, item):
element = getattr(describe, item)
if not callable(element) or item == "script":
return element
return object.__getattribute__(self, item)
__all__ = [
"Venv",
]