_distutils.py 5.7 KB

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