Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/virtualenv.py
4804 views
import logging1import os2import re3import site4import sys5from typing import List, Optional67logger = logging.getLogger(__name__)8_INCLUDE_SYSTEM_SITE_PACKAGES_REGEX = re.compile(9r"include-system-site-packages\s*=\s*(?P<value>true|false)"10)111213def _running_under_venv() -> bool:14"""Checks if sys.base_prefix and sys.prefix match.1516This handles PEP 405 compliant virtual environments.17"""18return sys.prefix != getattr(sys, "base_prefix", sys.prefix)192021def _running_under_regular_virtualenv() -> bool:22"""Checks if sys.real_prefix is set.2324This handles virtual environments created with pypa's virtualenv.25"""26# pypa/virtualenv case27return hasattr(sys, "real_prefix")282930def running_under_virtualenv() -> bool:31"""Return True if we're running inside a virtualenv, False otherwise."""32return _running_under_venv() or _running_under_regular_virtualenv()333435def _get_pyvenv_cfg_lines() -> Optional[List[str]]:36"""Reads {sys.prefix}/pyvenv.cfg and returns its contents as list of lines3738Returns None, if it could not read/access the file.39"""40pyvenv_cfg_file = os.path.join(sys.prefix, "pyvenv.cfg")41try:42# Although PEP 405 does not specify, the built-in venv module always43# writes with UTF-8. (pypa/pip#8717)44with open(pyvenv_cfg_file, encoding="utf-8") as f:45return f.read().splitlines() # avoids trailing newlines46except OSError:47return None484950def _no_global_under_venv() -> bool:51"""Check `{sys.prefix}/pyvenv.cfg` for system site-packages inclusion5253PEP 405 specifies that when system site-packages are not supposed to be54visible from a virtual environment, `pyvenv.cfg` must contain the following55line:5657include-system-site-packages = false5859Additionally, log a warning if accessing the file fails.60"""61cfg_lines = _get_pyvenv_cfg_lines()62if cfg_lines is None:63# We're not in a "sane" venv, so assume there is no system64# site-packages access (since that's PEP 405's default state).65logger.warning(66"Could not access 'pyvenv.cfg' despite a virtual environment "67"being active. Assuming global site-packages is not accessible "68"in this environment."69)70return True7172for line in cfg_lines:73match = _INCLUDE_SYSTEM_SITE_PACKAGES_REGEX.match(line)74if match is not None and match.group("value") == "false":75return True76return False777879def _no_global_under_regular_virtualenv() -> bool:80"""Check if "no-global-site-packages.txt" exists beside site.py8182This mirrors logic in pypa/virtualenv for determining whether system83site-packages are visible in the virtual environment.84"""85site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))86no_global_site_packages_file = os.path.join(87site_mod_dir,88"no-global-site-packages.txt",89)90return os.path.exists(no_global_site_packages_file)919293def virtualenv_no_global() -> bool:94"""Returns a boolean, whether running in venv with no system site-packages."""95# PEP 405 compliance needs to be checked first since virtualenv >=20 would96# return True for both checks, but is only able to use the PEP 405 config.97if _running_under_venv():98return _no_global_under_venv()99100if _running_under_regular_virtualenv():101return _no_global_under_regular_virtualenv()102103return False104105106