1from __future__ import annotations
2
3import logging
4import os
5import re
6import site
7import sys
8
9logger = logging.getLogger(__name__)
10_INCLUDE_SYSTEM_SITE_PACKAGES_REGEX = re.compile(
11 r"include-system-site-packages\s*=\s*(?P<value>true|false)"
12)
13
14
15def _running_under_venv() -> bool:
16 """Checks if sys.base_prefix and sys.prefix match.
17
18 This handles PEP 405 compliant virtual environments.
19 """
20 return sys.prefix != getattr(sys, "base_prefix", sys.prefix)
21
22
23def _running_under_legacy_virtualenv() -> bool:
24 """Checks if sys.real_prefix is set.
25
26 This handles virtual environments created with pypa's virtualenv.
27 """
28 # pypa/virtualenv case
29 return hasattr(sys, "real_prefix")
30
31
32def running_under_virtualenv() -> bool:
33 """True if we're running inside a virtual environment, False otherwise."""
34 return _running_under_venv() or _running_under_legacy_virtualenv()
35
36
37def _get_pyvenv_cfg_lines() -> list[str] | None:
38 """Reads {sys.prefix}/pyvenv.cfg and returns its contents as list of lines
39
40 Returns None, if it could not read/access the file.
41 """
42 pyvenv_cfg_file = os.path.join(sys.prefix, "pyvenv.cfg")
43 try:
44 # Although PEP 405 does not specify, the built-in venv module always
45 # writes with UTF-8. (pypa/pip#8717)
46 with open(pyvenv_cfg_file, encoding="utf-8") as f:
47 return f.read().splitlines() # avoids trailing newlines
48 except OSError:
49 return None
50
51
52def _no_global_under_venv() -> bool:
53 """Check `{sys.prefix}/pyvenv.cfg` for system site-packages inclusion
54
55 PEP 405 specifies that when system site-packages are not supposed to be
56 visible from a virtual environment, `pyvenv.cfg` must contain the following
57 line:
58
59 include-system-site-packages = false
60
61 Additionally, log a warning if accessing the file fails.
62 """
63 cfg_lines = _get_pyvenv_cfg_lines()
64 if cfg_lines is None:
65 # We're not in a "sane" venv, so assume there is no system
66 # site-packages access (since that's PEP 405's default state).
67 logger.warning(
68 "Could not access 'pyvenv.cfg' despite a virtual environment "
69 "being active. Assuming global site-packages is not accessible "
70 "in this environment."
71 )
72 return True
73
74 for line in cfg_lines:
75 match = _INCLUDE_SYSTEM_SITE_PACKAGES_REGEX.match(line)
76 if match is not None and match.group("value") == "false":
77 return True
78 return False
79
80
81def _no_global_under_legacy_virtualenv() -> bool:
82 """Check if "no-global-site-packages.txt" exists beside site.py
83
84 This mirrors logic in pypa/virtualenv for determining whether system
85 site-packages are visible in the virtual environment.
86 """
87 site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
88 no_global_site_packages_file = os.path.join(
89 site_mod_dir,
90 "no-global-site-packages.txt",
91 )
92 return os.path.exists(no_global_site_packages_file)
93
94
95def virtualenv_no_global() -> bool:
96 """Returns a boolean, whether running in venv with no system site-packages."""
97 # PEP 405 compliance needs to be checked first since virtualenv >=20 would
98 # return True for both checks, but is only able to use the PEP 405 config.
99 if _running_under_venv():
100 return _no_global_under_venv()
101
102 if _running_under_legacy_virtualenv():
103 return _no_global_under_legacy_virtualenv()
104
105 return False