_distutils.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """Locations where we look for configs, install stuff, etc"""
  2. # The following comment should be removed at some point in the future.
  3. # mypy: strict-optional=False
  4. # If pip's going to use distutils, it should not be using the copy that setuptools
  5. # might have injected into the environment. This is done by removing the injected
  6. # shim, if it's injected.
  7. #
  8. # See https://github.com/pypa/pip/issues/8761 for the original discussion and
  9. # rationale for why this is done within pip.
  10. try:
  11. __import__("_distutils_hack").remove_shim()
  12. except (ImportError, AttributeError):
  13. pass
  14. import logging
  15. import os
  16. import sys
  17. from distutils.cmd import Command as DistutilsCommand
  18. from distutils.command.install import SCHEME_KEYS
  19. from distutils.command.install import install as distutils_install_command
  20. from distutils.sysconfig import get_python_lib
  21. from typing import Dict, List, Optional, Union, cast
  22. from pip._internal.models.scheme import Scheme
  23. from pip._internal.utils.compat import WINDOWS
  24. from pip._internal.utils.virtualenv import running_under_virtualenv
  25. from .base import get_major_minor_version
  26. logger = logging.getLogger(__name__)
  27. def distutils_scheme(
  28. dist_name: str,
  29. user: bool = False,
  30. home: Optional[str] = None,
  31. root: Optional[str] = None,
  32. isolated: bool = False,
  33. prefix: Optional[str] = None,
  34. *,
  35. ignore_config_files: bool = False,
  36. ) -> Dict[str, str]:
  37. """
  38. Return a distutils install scheme
  39. """
  40. from distutils.dist import Distribution
  41. dist_args: Dict[str, Union[str, List[str]]] = {"name": dist_name}
  42. if isolated:
  43. dist_args["script_args"] = ["--no-user-cfg"]
  44. d = Distribution(dist_args)
  45. if not ignore_config_files:
  46. try:
  47. d.parse_config_files()
  48. except UnicodeDecodeError:
  49. # Typeshed does not include find_config_files() for some reason.
  50. paths = d.find_config_files() # type: ignore
  51. logger.warning(
  52. "Ignore distutils configs in %s due to encoding errors.",
  53. ", ".join(os.path.basename(p) for p in paths),
  54. )
  55. obj: Optional[DistutilsCommand] = None
  56. obj = d.get_command_obj("install", create=True)
  57. assert obj is not None
  58. i = cast(distutils_install_command, obj)
  59. # NOTE: setting user or home has the side-effect of creating the home dir
  60. # or user base for installations during finalize_options()
  61. # ideally, we'd prefer a scheme class that has no side-effects.
  62. assert not (user and prefix), f"user={user} prefix={prefix}"
  63. assert not (home and prefix), f"home={home} prefix={prefix}"
  64. i.user = user or i.user
  65. if user or home:
  66. i.prefix = ""
  67. i.prefix = prefix or i.prefix
  68. i.home = home or i.home
  69. i.root = root or i.root
  70. i.finalize_options()
  71. scheme = {}
  72. for key in SCHEME_KEYS:
  73. scheme[key] = getattr(i, "install_" + key)
  74. # install_lib specified in setup.cfg should install *everything*
  75. # into there (i.e. it takes precedence over both purelib and
  76. # platlib). Note, i.install_lib is *always* set after
  77. # finalize_options(); we only want to override here if the user
  78. # has explicitly requested it hence going back to the config
  79. if "install_lib" in d.get_option_dict("install"):
  80. scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
  81. if running_under_virtualenv():
  82. if home:
  83. prefix = home
  84. elif user:
  85. prefix = i.install_userbase
  86. else:
  87. prefix = i.prefix
  88. scheme["headers"] = os.path.join(
  89. prefix,
  90. "include",
  91. "site",
  92. f"python{get_major_minor_version()}",
  93. dist_name,
  94. )
  95. if root is not None:
  96. path_no_drive = os.path.splitdrive(os.path.abspath(scheme["headers"]))[1]
  97. scheme["headers"] = os.path.join(root, path_no_drive[1:])
  98. return scheme
  99. def get_scheme(
  100. dist_name: str,
  101. user: bool = False,
  102. home: Optional[str] = None,
  103. root: Optional[str] = None,
  104. isolated: bool = False,
  105. prefix: Optional[str] = None,
  106. ) -> Scheme:
  107. """
  108. Get the "scheme" corresponding to the input parameters. The distutils
  109. documentation provides the context for the available schemes:
  110. https://docs.python.org/3/install/index.html#alternate-installation
  111. :param dist_name: the name of the package to retrieve the scheme for, used
  112. in the headers scheme path
  113. :param user: indicates to use the "user" scheme
  114. :param home: indicates to use the "home" scheme and provides the base
  115. directory for the same
  116. :param root: root under which other directories are re-based
  117. :param isolated: equivalent to --no-user-cfg, i.e. do not consider
  118. ~/.pydistutils.cfg (posix) or ~/pydistutils.cfg (non-posix) for
  119. scheme paths
  120. :param prefix: indicates to use the "prefix" scheme and provides the
  121. base directory for the same
  122. """
  123. scheme = distutils_scheme(dist_name, user, home, root, isolated, prefix)
  124. return Scheme(
  125. platlib=scheme["platlib"],
  126. purelib=scheme["purelib"],
  127. headers=scheme["headers"],
  128. scripts=scheme["scripts"],
  129. data=scheme["data"],
  130. )
  131. def get_bin_prefix() -> str:
  132. # XXX: In old virtualenv versions, sys.prefix can contain '..' components,
  133. # so we need to call normpath to eliminate them.
  134. prefix = os.path.normpath(sys.prefix)
  135. if WINDOWS:
  136. bin_py = os.path.join(prefix, "Scripts")
  137. # buildout uses 'bin' on Windows too?
  138. if not os.path.exists(bin_py):
  139. bin_py = os.path.join(prefix, "bin")
  140. return bin_py
  141. # Forcing to use /usr/local/bin for standard macOS framework installs
  142. # Also log to ~/Library/Logs/ for use with the Console.app log viewer
  143. if sys.platform[:6] == "darwin" and prefix[:16] == "/System/Library/":
  144. return "/usr/local/bin"
  145. return os.path.join(prefix, "bin")
  146. def get_purelib() -> str:
  147. return get_python_lib(plat_specific=False)
  148. def get_platlib() -> str:
  149. return get_python_lib(plat_specific=True)