locations.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. from __future__ import absolute_import
  5. import os
  6. import os.path
  7. import platform
  8. import site
  9. import sys
  10. import sysconfig
  11. from distutils import sysconfig as distutils_sysconfig
  12. from distutils.command.install import SCHEME_KEYS # type: ignore
  13. from distutils.command.install import install as distutils_install_command
  14. from pip._internal.models.scheme import Scheme
  15. from pip._internal.utils import appdirs
  16. from pip._internal.utils.compat import WINDOWS
  17. from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
  18. from pip._internal.utils.virtualenv import running_under_virtualenv
  19. if MYPY_CHECK_RUNNING:
  20. from typing import Dict, List, Optional, Union
  21. from distutils.cmd import Command as DistutilsCommand
  22. # Application Directories
  23. USER_CACHE_DIR = appdirs.user_cache_dir("pip")
  24. def get_major_minor_version():
  25. # type: () -> str
  26. """
  27. Return the major-minor version of the current Python as a string, e.g.
  28. "3.7" or "3.10".
  29. """
  30. return '{}.{}'.format(*sys.version_info)
  31. def get_src_prefix():
  32. # type: () -> str
  33. if running_under_virtualenv():
  34. src_prefix = os.path.join(sys.prefix, 'src')
  35. else:
  36. # FIXME: keep src in cwd for now (it is not a temporary folder)
  37. try:
  38. src_prefix = os.path.join(os.getcwd(), 'src')
  39. except OSError:
  40. # In case the current working directory has been renamed or deleted
  41. sys.exit(
  42. "The folder you are executing pip from can no longer be found."
  43. )
  44. # under macOS + virtualenv sys.prefix is not properly resolved
  45. # it is something like /path/to/python/bin/..
  46. return os.path.abspath(src_prefix)
  47. # FIXME doesn't account for venv linked to global site-packages
  48. site_packages = sysconfig.get_path("purelib") # type: Optional[str]
  49. # This is because of a bug in PyPy's sysconfig module, see
  50. # https://bitbucket.org/pypy/pypy/issues/2506/sysconfig-returns-incorrect-paths
  51. # for more information.
  52. if platform.python_implementation().lower() == "pypy":
  53. site_packages = distutils_sysconfig.get_python_lib()
  54. try:
  55. # Use getusersitepackages if this is present, as it ensures that the
  56. # value is initialised properly.
  57. user_site = site.getusersitepackages()
  58. except AttributeError:
  59. user_site = site.USER_SITE
  60. if WINDOWS:
  61. bin_py = os.path.join(sys.prefix, 'Scripts')
  62. bin_user = os.path.join(user_site, 'Scripts')
  63. # buildout uses 'bin' on Windows too?
  64. if not os.path.exists(bin_py):
  65. bin_py = os.path.join(sys.prefix, 'bin')
  66. bin_user = os.path.join(user_site, 'bin')
  67. else:
  68. bin_py = os.path.join(sys.prefix, 'bin')
  69. bin_user = os.path.join(user_site, 'bin')
  70. # Forcing to use /usr/local/bin for standard macOS framework installs
  71. # Also log to ~/Library/Logs/ for use with the Console.app log viewer
  72. if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
  73. bin_py = '/usr/local/bin'
  74. def distutils_scheme(
  75. dist_name, user=False, home=None, root=None, isolated=False, prefix=None
  76. ):
  77. # type:(str, bool, str, str, bool, str) -> Dict[str, str]
  78. """
  79. Return a distutils install scheme
  80. """
  81. from distutils.dist import Distribution
  82. dist_args = {'name': dist_name} # type: Dict[str, Union[str, List[str]]]
  83. if isolated:
  84. dist_args["script_args"] = ["--no-user-cfg"]
  85. d = Distribution(dist_args)
  86. d.parse_config_files()
  87. obj = None # type: Optional[DistutilsCommand]
  88. obj = d.get_command_obj('install', create=True)
  89. assert obj is not None
  90. i = cast(distutils_install_command, obj)
  91. # NOTE: setting user or home has the side-effect of creating the home dir
  92. # or user base for installations during finalize_options()
  93. # ideally, we'd prefer a scheme class that has no side-effects.
  94. assert not (user and prefix), "user={} prefix={}".format(user, prefix)
  95. assert not (home and prefix), "home={} prefix={}".format(home, prefix)
  96. i.user = user or i.user
  97. if user or home:
  98. i.prefix = ""
  99. i.prefix = prefix or i.prefix
  100. i.home = home or i.home
  101. i.root = root or i.root
  102. i.finalize_options()
  103. scheme = {}
  104. for key in SCHEME_KEYS:
  105. scheme[key] = getattr(i, 'install_' + key)
  106. # install_lib specified in setup.cfg should install *everything*
  107. # into there (i.e. it takes precedence over both purelib and
  108. # platlib). Note, i.install_lib is *always* set after
  109. # finalize_options(); we only want to override here if the user
  110. # has explicitly requested it hence going back to the config
  111. if 'install_lib' in d.get_option_dict('install'):
  112. scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
  113. if running_under_virtualenv():
  114. scheme['headers'] = os.path.join(
  115. sys.prefix,
  116. 'include',
  117. 'site',
  118. 'python{}'.format(get_major_minor_version()),
  119. dist_name,
  120. )
  121. if root is not None:
  122. path_no_drive = os.path.splitdrive(
  123. os.path.abspath(scheme["headers"]))[1]
  124. scheme["headers"] = os.path.join(
  125. root,
  126. path_no_drive[1:],
  127. )
  128. return scheme
  129. def get_scheme(
  130. dist_name, # type: str
  131. user=False, # type: bool
  132. home=None, # type: Optional[str]
  133. root=None, # type: Optional[str]
  134. isolated=False, # type: bool
  135. prefix=None, # type: Optional[str]
  136. ):
  137. # type: (...) -> Scheme
  138. """
  139. Get the "scheme" corresponding to the input parameters. The distutils
  140. documentation provides the context for the available schemes:
  141. https://docs.python.org/3/install/index.html#alternate-installation
  142. :param dist_name: the name of the package to retrieve the scheme for, used
  143. in the headers scheme path
  144. :param user: indicates to use the "user" scheme
  145. :param home: indicates to use the "home" scheme and provides the base
  146. directory for the same
  147. :param root: root under which other directories are re-based
  148. :param isolated: equivalent to --no-user-cfg, i.e. do not consider
  149. ~/.pydistutils.cfg (posix) or ~/pydistutils.cfg (non-posix) for
  150. scheme paths
  151. :param prefix: indicates to use the "prefix" scheme and provides the
  152. base directory for the same
  153. """
  154. scheme = distutils_scheme(
  155. dist_name, user, home, root, isolated, prefix
  156. )
  157. return Scheme(
  158. platlib=scheme["platlib"],
  159. purelib=scheme["purelib"],
  160. headers=scheme["headers"],
  161. scripts=scheme["scripts"],
  162. data=scheme["data"],
  163. )