"""Patches that are applied at runtime to the virtual environment""" # -*- coding: utf-8 -*- 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. """ # 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 = "install_{}".format(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" if sys.version_info > (3, 4): # https://docs.python.org/3/library/importlib.html#setting-up-an-importer from importlib.abc import MetaPathFinder from importlib.util import find_spec from threading import Lock from functools import partial class _Finder(MetaPathFinder): """A meta path finder that allows patching the imported distutils modules""" fullname = None lock = Lock() def find_spec(self, fullname, path, target=None): if fullname in _DISTUTILS_PATCH and self.fullname is None: with self.lock: 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: setattr(spec.loader, func_name, partial(func, old)) except AttributeError: pass # C-Extension loaders are r/o such as zipimporter with