virtualenv.py 3.5 KB

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