_sysconfig.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import logging
  2. import os
  3. import sys
  4. import sysconfig
  5. import typing
  6. from pip._internal.exceptions import InvalidSchemeCombination, UserInstallationInvalid
  7. from pip._internal.models.scheme import SCHEME_KEYS, Scheme
  8. from pip._internal.utils.virtualenv import running_under_virtualenv
  9. from .base import change_root, get_major_minor_version, is_osx_framework
  10. logger = logging.getLogger(__name__)
  11. # Notes on _infer_* functions.
  12. # Unfortunately ``get_default_scheme()`` didn't exist before 3.10, so there's no
  13. # way to ask things like "what is the '_prefix' scheme on this platform". These
  14. # functions try to answer that with some heuristics while accounting for ad-hoc
  15. # platforms not covered by CPython's default sysconfig implementation. If the
  16. # ad-hoc implementation does not fully implement sysconfig, we'll fall back to
  17. # a POSIX scheme.
  18. _AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names())
  19. _PREFERRED_SCHEME_API = getattr(sysconfig, "get_preferred_scheme", None)
  20. def _should_use_osx_framework_prefix() -> bool:
  21. """Check for Apple's ``osx_framework_library`` scheme.
  22. Python distributed by Apple's Command Line Tools has this special scheme
  23. that's used when:
  24. * This is a framework build.
  25. * We are installing into the system prefix.
  26. This does not account for ``pip install --prefix`` (also means we're not
  27. installing to the system prefix), which should use ``posix_prefix``, but
  28. logic here means ``_infer_prefix()`` outputs ``osx_framework_library``. But
  29. since ``prefix`` is not available for ``sysconfig.get_default_scheme()``,
  30. which is the stdlib replacement for ``_infer_prefix()``, presumably Apple
  31. wouldn't be able to magically switch between ``osx_framework_library`` and
  32. ``posix_prefix``. ``_infer_prefix()`` returning ``osx_framework_library``
  33. means its behavior is consistent whether we use the stdlib implementation
  34. or our own, and we deal with this special case in ``get_scheme()`` instead.
  35. """
  36. return (
  37. "osx_framework_library" in _AVAILABLE_SCHEMES
  38. and not running_under_virtualenv()
  39. and is_osx_framework()
  40. )
  41. def _infer_prefix() -> str:
  42. """Try to find a prefix scheme for the current platform.
  43. This tries:
  44. * A special ``osx_framework_library`` for Python distributed by Apple's
  45. Command Line Tools, when not running in a virtual environment.
  46. * Implementation + OS, used by PyPy on Windows (``pypy_nt``).
  47. * Implementation without OS, used by PyPy on POSIX (``pypy``).
  48. * OS + "prefix", used by CPython on POSIX (``posix_prefix``).
  49. * Just the OS name, used by CPython on Windows (``nt``).
  50. If none of the above works, fall back to ``posix_prefix``.
  51. """
  52. if _PREFERRED_SCHEME_API:
  53. return _PREFERRED_SCHEME_API("prefix")
  54. if _should_use_osx_framework_prefix():
  55. return "osx_framework_library"
  56. implementation_suffixed = f"{sys.implementation.name}_{os.name}"
  57. if implementation_suffixed in _AVAILABLE_SCHEMES:
  58. return implementation_suffixed
  59. if sys.implementation.name in _AVAILABLE_SCHEMES:
  60. return sys.implementation.name
  61. suffixed = f"{os.name}_prefix"
  62. if suffixed in _AVAILABLE_SCHEMES:
  63. return suffixed
  64. if os.name in _AVAILABLE_SCHEMES: # On Windows, prefx is just called "nt".
  65. return os.name
  66. return "posix_prefix"
  67. def _infer_user() -> str:
  68. """Try to find a user scheme for the current platform."""
  69. if _PREFERRED_SCHEME_API:
  70. return _PREFERRED_SCHEME_API("user")
  71. if is_osx_framework() and not running_under_virtualenv():
  72. suffixed = "osx_framework_user"
  73. else:
  74. suffixed = f"{os.name}_user"
  75. if suffixed in _AVAILABLE_SCHEMES:
  76. return suffixed
  77. if "posix_user" not in _AVAILABLE_SCHEMES: # User scheme unavailable.
  78. raise UserInstallationInvalid()
  79. return "posix_user"
  80. def _infer_home() -> str:
  81. """Try to find a home for the current platform."""
  82. if _PREFERRED_SCHEME_API:
  83. return _PREFERRED_SCHEME_API("home")
  84. suffixed = f"{os.name}_home"
  85. if suffixed in _AVAILABLE_SCHEMES:
  86. return suffixed
  87. return "posix_home"
  88. # Update these keys if the user sets a custom home.
  89. _HOME_KEYS = [
  90. "installed_base",
  91. "base",
  92. "installed_platbase",
  93. "platbase",
  94. "prefix",
  95. "exec_prefix",
  96. ]
  97. if sysconfig.get_config_var("userbase") is not None:
  98. _HOME_KEYS.append("userbase")
  99. def get_scheme(
  100. dist_name: str,
  101. user: bool = False,
  102. home: typing.Optional[str] = None,
  103. root: typing.Optional[str] = None,
  104. isolated: bool = False,
  105. prefix: typing.Optional[str] = None,
  106. ) -> Scheme:
  107. """
  108. Get the "scheme" corresponding to the input parameters.
  109. :param dist_name: the name of the package to retrieve the scheme for, used
  110. in the headers scheme path
  111. :param user: indicates to use the "user" scheme
  112. :param home: indicates to use the "home" scheme
  113. :param root: root under which other directories are re-based
  114. :param isolated: ignored, but kept for distutils compatibility (where
  115. this controls whether the user-site pydistutils.cfg is honored)
  116. :param prefix: indicates to use the "prefix" scheme and provides the
  117. base directory for the same
  118. """
  119. if user and prefix:
  120. raise InvalidSchemeCombination("--user", "--prefix")
  121. if home and prefix:
  122. raise InvalidSchemeCombination("--home", "--prefix")
  123. if home is not None:
  124. scheme_name = _infer_home()
  125. elif user:
  126. scheme_name = _infer_user()
  127. else:
  128. scheme_name = _infer_prefix()
  129. # Special case: When installing into a custom prefix, use posix_prefix
  130. # instead of osx_framework_library. See _should_use_osx_framework_prefix()
  131. # docstring for details.
  132. if prefix is not None and scheme_name == "osx_framework_library":
  133. scheme_name = "posix_prefix"
  134. if home is not None:
  135. variables = {k: home for k in _HOME_KEYS}
  136. elif prefix is not None:
  137. variables = {k: prefix for k in _HOME_KEYS}
  138. else:
  139. variables = {}
  140. paths = sysconfig.get_paths(scheme=scheme_name, vars=variables)
  141. # Logic here is very arbitrary, we're doing it for compatibility, don't ask.
  142. # 1. Pip historically uses a special header path in virtual environments.
  143. # 2. If the distribution name is not known, distutils uses 'UNKNOWN'. We
  144. # only do the same when not running in a virtual environment because
  145. # pip's historical header path logic (see point 1) did not do this.
  146. if running_under_virtualenv():
  147. if user:
  148. base = variables.get("userbase", sys.prefix)
  149. else:
  150. base = variables.get("base", sys.prefix)
  151. python_xy = f"python{get_major_minor_version()}"
  152. paths["include"] = os.path.join(base, "include", "site", python_xy)
  153. elif not dist_name:
  154. dist_name = "UNKNOWN"
  155. scheme = Scheme(
  156. platlib=paths["platlib"],
  157. purelib=paths["purelib"],
  158. headers=os.path.join(paths["include"], dist_name),
  159. scripts=paths["scripts"],
  160. data=paths["data"],
  161. )
  162. if root is not None:
  163. for key in SCHEME_KEYS:
  164. value = change_root(root, getattr(scheme, key))
  165. setattr(scheme, key, value)
  166. return scheme
  167. def get_bin_prefix() -> str:
  168. # Forcing to use /usr/local/bin for standard macOS framework installs.
  169. if sys.platform[:6] == "darwin" and sys.prefix[:16] == "/System/Library/":
  170. return "/usr/local/bin"
  171. return sysconfig.get_paths()["scripts"]
  172. def get_purelib() -> str:
  173. return sysconfig.get_paths()["purelib"]
  174. def get_platlib() -> str:
  175. return sysconfig.get_paths()["platlib"]