_distutils.py 5.1 KB

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