diff options
| author | Bernát Gábor <bgabor8@bloomberg.net> | 2020-10-12 11:25:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-12 11:25:36 +0100 |
| commit | ced984abfd4f1531f9075113e77af7ec01479d29 (patch) | |
| tree | 7fa3c2264447c05cf535ec34f6d5c10b41542b4c /src | |
| parent | 2b0bbbabb45044c44d35e65bfc5aa60403b4906b (diff) | |
| download | virtualenv-ced984abfd4f1531f9075113e77af7ec01479d29.tar.gz | |
Align Windows 3.7 methodology and later with venv (#1976)
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
Diffstat (limited to 'src')
7 files changed, 111 insertions, 64 deletions
diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/common.py b/src/virtualenv/create/via_global_ref/builtin/cpython/common.py index ac251b9..39c56cb 100644 --- a/src/virtualenv/create/via_global_ref/builtin/cpython/common.py +++ b/src/virtualenv/create/via_global_ref/builtin/cpython/common.py @@ -6,6 +6,7 @@ from collections import OrderedDict from six import add_metaclass from virtualenv.create.describe import PosixSupports, WindowsSupports +from virtualenv.create.via_global_ref.builtin.ref import RefMust, RefWhen from virtualenv.util.path import Path from ..via_global_self_do import ViaGlobalRefVirtualenvBuiltin @@ -33,19 +34,26 @@ class CPythonPosix(CPython, PosixSupports): targets = OrderedDict( (i, None) for i in ["python", "python{}".format(major), "python{}.{}".format(major, minor), host_exe.name] ) - yield host_exe, list(targets.keys()) + must = RefMust.COPY if interpreter.version_info.major == 2 else RefMust.NA + yield host_exe, list(targets.keys()), must, RefWhen.ANY @add_metaclass(ABCMeta) class CPythonWindows(CPython, WindowsSupports): @classmethod def _executables(cls, interpreter): - host = Path(interpreter.system_executable) + executables = cls._win_executables(Path(interpreter.system_executable), interpreter, RefWhen.ANY) + for src, targets, must, when in executables: + yield src, targets, must, when + + @classmethod + def _win_executables(cls, host, interpreter, when): + must = RefMust.COPY if interpreter.version_info.major == 2 else RefMust.NA for path in (host.parent / n for n in {"python.exe", host.name}): - yield host, [path.name] + yield host, [path.name], must, when # for more info on pythonw.exe see https://stackoverflow.com/a/30313091 python_w = host.parent / "pythonw.exe" - yield python_w, [python_w.name] + yield python_w, [python_w.name], must, when def is_mac_os_framework(interpreter): diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py b/src/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py index dd21436..149e8ed 100644 --- a/src/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py +++ b/src/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py @@ -1,12 +1,13 @@ from __future__ import absolute_import, unicode_literals import abc +from itertools import chain from textwrap import dedent from six import add_metaclass from virtualenv.create.describe import Python3Supports -from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest +from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest, RefMust, RefWhen from virtualenv.create.via_global_ref.store import is_store_python from virtualenv.util.path import Path @@ -55,8 +56,29 @@ class CPython3Windows(CPythonWindows, CPython3): def sources(cls, interpreter): for src in super(CPython3Windows, cls).sources(interpreter): yield src - for src in cls.include_dll_and_pyd(interpreter): - yield src + if cls.venv_37p(interpreter): + for dll in (i for i in Path(interpreter.system_executable).parent.iterdir() if i.suffix == ".dll"): + yield PathRefToDest(dll, cls.to_bin, RefMust.SYMLINK, RefWhen.SYMLINK) + else: + for src in cls.include_dll_and_pyd(interpreter): + yield src + + @classmethod + def _executables(cls, interpreter): + system_exe = Path(interpreter.system_executable) + if cls.venv_37p(interpreter): + # starting with CPython 3.7 Windows ships with a venvlauncher.exe that avoids the need for dll/pyd copies + launcher = Path(interpreter.system_stdlib) / "venv" / "scripts" / "nt" / "python.exe" + executables = cls._win_executables(launcher, interpreter, RefWhen.COPY) + executables = chain(executables, cls._win_executables(system_exe, interpreter, RefWhen.SYMLINK)) + else: + executables = cls._win_executables(system_exe, interpreter, RefWhen.ANY) + for src, targets, must, when in executables: + yield src, targets, must, when + + @staticmethod + def venv_37p(interpreter): + return interpreter.version_info.minor > 6 @classmethod def include_dll_and_pyd(cls, interpreter): diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py index 1b971f3..53f65e3 100644 --- a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py +++ b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py @@ -9,7 +9,7 @@ from textwrap import dedent from six import add_metaclass -from virtualenv.create.via_global_ref.builtin.ref import ExePathRefToDest, PathRefToDest +from virtualenv.create.via_global_ref.builtin.ref import ExePathRefToDest, PathRefToDest, RefMust from virtualenv.util.path import Path from virtualenv.util.six import ensure_text @@ -29,7 +29,8 @@ class CPythonmacOsFramework(CPython): for src in super(CPythonmacOsFramework, cls).sources(interpreter): yield src # add a symlink to the host python image - ref = PathRefToDest(cls.image_ref(interpreter), dest=lambda self, _: self.dest / ".Python", must_symlink=True) + exe = cls.image_ref(interpreter) + ref = PathRefToDest(exe, dest=lambda self, _: self.dest / ".Python", must=RefMust.SYMLINK) yield ref def create(self): @@ -40,7 +41,7 @@ class CPythonmacOsFramework(CPython): current = self.current_mach_o_image_path() for src in self._sources: if isinstance(src, ExePathRefToDest): - if src.must_copy or not self.symlinks: + if 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) @@ -49,12 +50,12 @@ class CPythonmacOsFramework(CPython): @classmethod def _executables(cls, interpreter): - for _, targets in super(CPythonmacOsFramework, cls)._executables(interpreter): + for _, targets, must, when in super(CPythonmacOsFramework, cls)._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 + yield fixed_host_exe, targets, must, when @abstractmethod def current_mach_o_image_path(self): diff --git a/src/virtualenv/create/via_global_ref/builtin/pypy/common.py b/src/virtualenv/create/via_global_ref/builtin/pypy/common.py index 90da51f..cc03b42 100644 --- a/src/virtualenv/create/via_global_ref/builtin/pypy/common.py +++ b/src/virtualenv/create/via_global_ref/builtin/pypy/common.py @@ -4,7 +4,7 @@ import abc from six import add_metaclass -from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest +from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest, RefMust, RefWhen from virtualenv.util.path import Path from ..via_global_self_do import ViaGlobalRefVirtualenvBuiltin @@ -20,7 +20,8 @@ class PyPy(ViaGlobalRefVirtualenvBuiltin): def _executables(cls, interpreter): host = Path(interpreter.system_executable) targets = sorted("{}{}".format(name, PyPy.suffix) for name in cls.exe_names(interpreter)) - yield host, targets + must = RefMust.COPY if interpreter.version_info.major == 2 else RefMust.NA + yield host, targets, must, RefWhen.ANY @classmethod def exe_names(cls, interpreter): diff --git a/src/virtualenv/create/via_global_ref/builtin/ref.py b/src/virtualenv/create/via_global_ref/builtin/ref.py index 263da3b..69f243b 100644 --- a/src/virtualenv/create/via_global_ref/builtin/ref.py +++ b/src/virtualenv/create/via_global_ref/builtin/ref.py @@ -17,6 +17,18 @@ from virtualenv.util.path import copy, make_exe, symlink from virtualenv.util.six import ensure_text +class RefMust(object): + NA = "NA" + COPY = "copy" + SYMLINK = "symlink" + + +class RefWhen(object): + ANY = "ANY" + COPY = "copy" + SYMLINK = "symlink" + + @add_metaclass(ABCMeta) class PathRef(object): """Base class that checks if a file reference can be symlink/copied""" @@ -24,9 +36,9 @@ class PathRef(object): FS_SUPPORTS_SYMLINK = fs_supports_symlink() FS_CASE_SENSITIVE = fs_is_case_sensitive() - def __init__(self, src, must_symlink, must_copy): - self.must_symlink = must_symlink - self.must_copy = must_copy + def __init__(self, src, must=RefMust.NA, when=RefWhen.ANY): + self.must = must + self.when = when self.src = src try: self.exists = src.exists() @@ -35,8 +47,6 @@ class PathRef(object): 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 - if self.must_copy is True and self.must_symlink is True: - raise ValueError("can copy and symlink at the same time") def __repr__(self): return "{}(src={})".format(self.__class__.__name__, self.src) @@ -57,7 +67,7 @@ class PathRef(object): @property def can_copy(self): if self._can_copy is None: - if self.must_symlink: + if self.must == RefMust.SYMLINK: self._can_copy = self.can_symlink else: self._can_copy = self.can_read @@ -66,7 +76,7 @@ class PathRef(object): @property def can_symlink(self): if self._can_symlink is None: - if self.must_copy: + if self.must == RefMust.COPY: self._can_symlink = self.can_copy else: self._can_symlink = self.FS_SUPPORTS_SYMLINK and self.can_read @@ -77,9 +87,9 @@ class PathRef(object): raise NotImplementedError def method(self, symlinks): - if self.must_symlink: + if self.must == RefMust.SYMLINK: return symlink - if self.must_copy: + if self.must == RefMust.COPY: return copy return symlink if symlinks else copy @@ -88,8 +98,8 @@ class PathRef(object): class ExePathRef(PathRef): """Base class that checks if a executable can be references via symlink/copy""" - def __init__(self, src, must_symlink, must_copy): - super(ExePathRef, self).__init__(src, must_symlink, must_copy) + def __init__(self, src, must=RefMust.NA, when=RefWhen.ANY): + super(ExePathRef, self).__init__(src, must, when) self._can_run = None @property @@ -114,8 +124,8 @@ class ExePathRef(PathRef): class PathRefToDest(PathRef): """Link a path on the file system""" - def __init__(self, src, dest, must_symlink=False, must_copy=False): - super(PathRefToDest, self).__init__(src, must_symlink, must_copy) + def __init__(self, src, dest, must=RefMust.NA, when=RefWhen.ANY): + super(PathRefToDest, self).__init__(src, must, when) self.dest = dest def run(self, creator, symlinks): @@ -131,15 +141,14 @@ class PathRefToDest(PathRef): class ExePathRefToDest(PathRefToDest, ExePathRef): """Link a exe path on the file system""" - def __init__(self, src, targets, dest, must_symlink=False, must_copy=False): - ExePathRef.__init__(self, src, must_symlink, must_copy) - PathRefToDest.__init__(self, src, dest, must_symlink, must_copy) + def __init__(self, src, targets, dest, must=RefMust.NA, when=RefWhen.ANY): + 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 - self.must_copy = must_copy def run(self, creator, symlinks): bin_dir = self.dest(creator, self.src).parent diff --git a/src/virtualenv/create/via_global_ref/builtin/via_global_self_do.py b/src/virtualenv/create/via_global_ref/builtin/via_global_self_do.py index 7de4fe1..a00b97a 100644 --- a/src/virtualenv/create/via_global_ref/builtin/via_global_self_do.py +++ b/src/virtualenv/create/via_global_ref/builtin/via_global_self_do.py @@ -4,7 +4,7 @@ from abc import ABCMeta from six import add_metaclass -from virtualenv.create.via_global_ref.builtin.ref import ExePathRefToDest +from virtualenv.create.via_global_ref.builtin.ref import ExePathRefToDest, RefMust from virtualenv.util.path import ensure_dir from ..api import ViaGlobalRefApi, ViaGlobalRefMeta @@ -27,27 +27,37 @@ class ViaGlobalRefVirtualenvBuiltin(ViaGlobalRefApi, VirtualenvBuiltin): 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 cls.can_describe(interpreter): - meta = cls.setup_meta(interpreter) - if meta is not None and meta: - for src in cls.sources(interpreter): - if src.exists: - if meta.can_copy and not src.can_copy: - meta.copy_error = "cannot copy {}".format(src) - if meta.can_symlink and not src.can_symlink: - meta.symlink_error = "cannot symlink {}".format(src) - if not meta.can_copy and not meta.can_symlink: - meta.error = "neither copy or symlink supported, copy: {} symlink: {}".format( - meta.copy_error, - meta.symlink_error, - ) - else: - meta.error = "missing required file {}".format(src) - if meta.error: - break - meta.sources.append(src) - return meta - return None + 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 = "cannot copy {}".format(src) + if meta.can_symlink and not src.can_symlink: + meta.symlink_error = "cannot symlink {}".format(src) + else: + msg = "missing required file {}".format(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 = "neither copy or symlink supported, copy: {} symlink: {}".format( + meta.copy_error, + meta.symlink_error, + ) + if meta.error: + break + meta.sources.append(src) @classmethod def setup_meta(cls, interpreter): @@ -55,9 +65,8 @@ class ViaGlobalRefVirtualenvBuiltin(ViaGlobalRefApi, VirtualenvBuiltin): @classmethod def sources(cls, interpreter): - is_py2 = interpreter.version_info.major == 2 - for host_exe, targets in cls._executables(interpreter): - yield ExePathRefToDest(host_exe, dest=cls.to_bin, targets=targets, must_copy=is_py2) + 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 diff --git a/src/virtualenv/run/plugin/creators.py b/src/virtualenv/run/plugin/creators.py index 31d03cf..ef4177a 100644 --- a/src/virtualenv/run/plugin/creators.py +++ b/src/virtualenv/run/plugin/creators.py @@ -18,14 +18,14 @@ class CreatorSelector(ComponentBuilder): @classmethod def for_interpreter(cls, interpreter): key_to_class, key_to_meta, builtin_key, describe = OrderedDict(), {}, None, None - errored = defaultdict(list) + errors = defaultdict(list) for key, creator_class in cls.options("virtualenv.create").items(): if key == "builtin": raise RuntimeError("builtin creator is a reserved name") meta = creator_class.can_create(interpreter) if meta: if meta.error: - errored[meta.error].append(creator_class) + errors[meta.error].append(creator_class) else: if "builtin" not in key_to_class and issubclass(creator_class, VirtualenvBuiltin): builtin_key = key @@ -36,12 +36,9 @@ class CreatorSelector(ComponentBuilder): if describe is None and issubclass(creator_class, Describe) and creator_class.can_describe(interpreter): describe = creator_class if not key_to_meta: - if errored: - raise RuntimeError( - "\n".join( - "{} for creators {}".format(k, ", ".join(i.__name__ for i in v)) for k, v in errored.items() - ), - ) + if errors: + rows = ["{} for creators {}".format(k, ", ".join(i.__name__ for i in v)) for k, v in errors.items()] + raise RuntimeError("\n".join(rows)) else: raise RuntimeError("No virtualenv implementation for {}".format(interpreter)) return CreatorInfo( |
