Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_internal/utils/virtualenv.py @ 1:64071f2a4cf0 draft default tip
Deleted selected files
| author | guerler |
|---|---|
| date | Mon, 27 Jul 2020 03:55:49 -0400 |
| parents | 9e54283cc701 |
| children |
comparison
equal
deleted
inserted
replaced
| 0:9e54283cc701 | 1:64071f2a4cf0 |
|---|---|
| 1 from __future__ import absolute_import | |
| 2 | |
| 3 import logging | |
| 4 import os | |
| 5 import re | |
| 6 import site | |
| 7 import sys | |
| 8 | |
| 9 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
| 10 | |
| 11 if MYPY_CHECK_RUNNING: | |
| 12 from typing import List, Optional | |
| 13 | |
| 14 logger = logging.getLogger(__name__) | |
| 15 _INCLUDE_SYSTEM_SITE_PACKAGES_REGEX = re.compile( | |
| 16 r"include-system-site-packages\s*=\s*(?P<value>true|false)" | |
| 17 ) | |
| 18 | |
| 19 | |
| 20 def _running_under_venv(): | |
| 21 # type: () -> bool | |
| 22 """Checks if sys.base_prefix and sys.prefix match. | |
| 23 | |
| 24 This handles PEP 405 compliant virtual environments. | |
| 25 """ | |
| 26 return sys.prefix != getattr(sys, "base_prefix", sys.prefix) | |
| 27 | |
| 28 | |
| 29 def _running_under_regular_virtualenv(): | |
| 30 # type: () -> bool | |
| 31 """Checks if sys.real_prefix is set. | |
| 32 | |
| 33 This handles virtual environments created with pypa's virtualenv. | |
| 34 """ | |
| 35 # pypa/virtualenv case | |
| 36 return hasattr(sys, 'real_prefix') | |
| 37 | |
| 38 | |
| 39 def running_under_virtualenv(): | |
| 40 # type: () -> bool | |
| 41 """Return True if we're running inside a virtualenv, False otherwise. | |
| 42 """ | |
| 43 return _running_under_venv() or _running_under_regular_virtualenv() | |
| 44 | |
| 45 | |
| 46 def _get_pyvenv_cfg_lines(): | |
| 47 # type: () -> Optional[List[str]] | |
| 48 """Reads {sys.prefix}/pyvenv.cfg and returns its contents as list of lines | |
| 49 | |
| 50 Returns None, if it could not read/access the file. | |
| 51 """ | |
| 52 pyvenv_cfg_file = os.path.join(sys.prefix, 'pyvenv.cfg') | |
| 53 try: | |
| 54 with open(pyvenv_cfg_file) as f: | |
| 55 return f.read().splitlines() # avoids trailing newlines | |
| 56 except IOError: | |
| 57 return None | |
| 58 | |
| 59 | |
| 60 def _no_global_under_venv(): | |
| 61 # type: () -> bool | |
| 62 """Check `{sys.prefix}/pyvenv.cfg` for system site-packages inclusion | |
| 63 | |
| 64 PEP 405 specifies that when system site-packages are not supposed to be | |
| 65 visible from a virtual environment, `pyvenv.cfg` must contain the following | |
| 66 line: | |
| 67 | |
| 68 include-system-site-packages = false | |
| 69 | |
| 70 Additionally, log a warning if accessing the file fails. | |
| 71 """ | |
| 72 cfg_lines = _get_pyvenv_cfg_lines() | |
| 73 if cfg_lines is None: | |
| 74 # We're not in a "sane" venv, so assume there is no system | |
| 75 # site-packages access (since that's PEP 405's default state). | |
| 76 logger.warning( | |
| 77 "Could not access 'pyvenv.cfg' despite a virtual environment " | |
| 78 "being active. Assuming global site-packages is not accessible " | |
| 79 "in this environment." | |
| 80 ) | |
| 81 return True | |
| 82 | |
| 83 for line in cfg_lines: | |
| 84 match = _INCLUDE_SYSTEM_SITE_PACKAGES_REGEX.match(line) | |
| 85 if match is not None and match.group('value') == 'false': | |
| 86 return True | |
| 87 return False | |
| 88 | |
| 89 | |
| 90 def _no_global_under_regular_virtualenv(): | |
| 91 # type: () -> bool | |
| 92 """Check if "no-global-site-packages.txt" exists beside site.py | |
| 93 | |
| 94 This mirrors logic in pypa/virtualenv for determining whether system | |
| 95 site-packages are visible in the virtual environment. | |
| 96 """ | |
| 97 site_mod_dir = os.path.dirname(os.path.abspath(site.__file__)) | |
| 98 no_global_site_packages_file = os.path.join( | |
| 99 site_mod_dir, 'no-global-site-packages.txt', | |
| 100 ) | |
| 101 return os.path.exists(no_global_site_packages_file) | |
| 102 | |
| 103 | |
| 104 def virtualenv_no_global(): | |
| 105 # type: () -> bool | |
| 106 """Returns a boolean, whether running in venv with no system site-packages. | |
| 107 """ | |
| 108 | |
| 109 if _running_under_regular_virtualenv(): | |
| 110 return _no_global_under_regular_virtualenv() | |
| 111 | |
| 112 if _running_under_venv(): | |
| 113 return _no_global_under_venv() | |
| 114 | |
| 115 return False |
