virtualenv.py (7473B)
1 # mypy: allow-untyped-defs 2 3 import logging 4 import os 5 import shutil 6 import site 7 import sys 8 import sysconfig 9 from shutil import which 10 11 # The `pkg_resources` module is provided by `setuptools`, which is itself a 12 # dependency of `virtualenv`. Tolerate its absence so that this module may be 13 # evaluated when that module is not available. Because users may not recognize 14 # the `pkg_resources` module by name, raise a more descriptive error if it is 15 # referenced during execution. 16 try: 17 import pkg_resources as _pkg_resources 18 get_pkg_resources = lambda: _pkg_resources 19 except ImportError: 20 def get_pkg_resources(): 21 raise ValueError("The Python module `virtualenv` is not installed.") 22 23 from tools.wpt.utils import call 24 25 logger = logging.getLogger(__name__) 26 27 class Virtualenv: 28 def __init__(self, path, skip_virtualenv_setup): 29 self.path = path 30 self.skip_virtualenv_setup = skip_virtualenv_setup 31 if not skip_virtualenv_setup: 32 self.virtualenv = [sys.executable, "-m", "venv"] 33 self._working_set = None 34 35 @property 36 def exists(self): 37 # We need to check also for lib_path because different python versions 38 # create different library paths. 39 return os.path.isdir(self.path) and os.path.isdir(self.lib_path) 40 41 @property 42 def broken_link(self): 43 # We shouldn't ever be using virtualenv, but for historic reasons check this 44 virtualenv_python_link = os.path.join(self.path, ".Python") 45 if os.path.lexists(virtualenv_python_link) and not os.path.exists(virtualenv_python_link): 46 return True 47 48 # This isn't a broken link, but we can't run the below in this state 49 if not os.path.exists(self.bin_path): 50 return True 51 52 with os.scandir(self.bin_path) as it: 53 for entry in it: 54 # There is no entry.exists(), it's not quite Path-like enough. 55 if entry.is_symlink() and not os.path.exists(entry.path): 56 return True 57 58 return False 59 60 def create(self): 61 if os.path.exists(self.path): 62 logger.warning(f"Removing existing venv at {self.path!r}") 63 shutil.rmtree(self.path, ignore_errors=True) 64 self._working_set = None 65 logger.info(f"Creating new venv at {self.path!r}") 66 call(*self.virtualenv, self.path) 67 68 def get_paths(self): 69 """Wrapper around sysconfig.get_paths(), returning the appropriate paths for the env.""" 70 if "venv" in sysconfig.get_scheme_names(): 71 # This should always be used on Python 3.11 and above. 72 scheme = "venv" 73 elif os.name == "nt": 74 # This matches nt_venv, unless sysconfig has been modified. 75 scheme = "nt" 76 elif os.name == "posix": 77 # This matches posix_venv, unless sysconfig has been modified. 78 scheme = "posix_prefix" 79 elif sys.version_info >= (3, 10): 80 # Using the default scheme is somewhat fragile, as various Python 81 # distributors (e.g., what Debian and Fedora package, and what Xcode 82 # includes) change the default scheme away from the upstream 83 # defaults, but it's about as good as we can do. 84 scheme = sysconfig.get_default_scheme() 85 else: 86 # This is explicitly documented as having previously existed in the 3.10 87 # docs, and has existed since CPython 2.7 and 3.1 (but not 3.0). 88 scheme = sysconfig._get_default_scheme() 89 90 vars = { 91 "base": self.path, 92 "platbase": self.path, 93 "installed_base": self.path, 94 "installed_platbase": self.path, 95 } 96 97 return sysconfig.get_paths(scheme, vars) 98 99 @property 100 def bin_path(self): 101 return self.get_paths()["scripts"] 102 103 @property 104 def pip_path(self): 105 path = which("pip3", path=self.bin_path) 106 if path is None: 107 path = which("pip", path=self.bin_path) 108 if path is None: 109 raise ValueError("pip3 or pip not found") 110 return path 111 112 @property 113 def lib_path(self): 114 # We always return platlib here, even if it differs to purelib, because we can 115 # always install pure-Python code into the platlib safely too. It's also very 116 # unlikely to differ for a venv. 117 return self.get_paths()["platlib"] 118 119 @property 120 def working_set(self): 121 if not self.exists: 122 raise ValueError("trying to read working_set when venv doesn't exist") 123 124 if self._working_set is None: 125 self._working_set = get_pkg_resources().WorkingSet((self.lib_path,)) 126 127 return self._working_set 128 129 def activate(self): 130 if sys.platform == "darwin": 131 # The default Python on macOS sets a __PYVENV_LAUNCHER__ environment 132 # variable which affects invocation of python (e.g. via pip) in a 133 # virtualenv. Unset it if present to avoid this. More background: 134 # https://github.com/web-platform-tests/wpt/issues/27377 135 # https://github.com/python/cpython/pull/9516 136 os.environ.pop("__PYVENV_LAUNCHER__", None) 137 138 paths = self.get_paths() 139 140 # Setup the path and site packages as if we'd launched with the virtualenv active 141 bin_dir = paths["scripts"] 142 os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep)) 143 144 # While not required (`./venv/bin/python3` won't set it, but 145 # `source ./venv/bin/activate && python3` will), we have historically set this. 146 os.environ["VIRTUAL_ENV"] = self.path 147 148 prev_length = len(sys.path) 149 150 # Add the venv library paths as sitedirs. 151 for key in ["purelib", "platlib"]: 152 site.addsitedir(paths[key]) 153 154 # Rearrange the path 155 sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length] 156 157 # Change prefixes, similar to what initconfig/site does for venvs. 158 sys.exec_prefix = self.path 159 sys.prefix = self.path 160 161 def start(self): 162 if not self.exists or self.broken_link: 163 self.create() 164 self.activate() 165 166 def install(self, *requirements): 167 try: 168 self.working_set.require(*requirements) 169 except Exception: 170 pass 171 else: 172 return 173 174 # `--prefer-binary` guards against race conditions when installation 175 # occurs while packages are in the process of being published. 176 call(self.pip_path, "install", "--prefer-binary", *requirements) 177 178 def install_requirements(self, *requirements_paths): 179 install = [] 180 # Check which requirements are already satisfied, to skip calling pip 181 # at all in the case that we've already installed everything, and to 182 # minimise the installs in other cases. 183 for requirements_path in requirements_paths: 184 with open(requirements_path) as f: 185 try: 186 self.working_set.require(f.read()) 187 except Exception: 188 install.append(requirements_path) 189 190 if install: 191 # `--prefer-binary` guards against race conditions when installation 192 # occurs while packages are in the process of being published. 193 cmd = [self.pip_path, "install", "--prefer-binary"] 194 for path in install: 195 cmd.extend(["-r", path]) 196 call(*cmd)