m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.9 KiB

6 months ago
  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. paths = d.find_config_files()
  50. logger.warning(
  51. "Ignore distutils configs in %s due to encoding errors.",
  52. ", ".join(os.path.basename(p) for p in paths),
  53. )
  54. obj: Optional[DistutilsCommand] = None
  55. obj = d.get_command_obj("install", create=True)
  56. assert obj is not None
  57. i = cast(distutils_install_command, obj)
  58. # NOTE: setting user or home has the side-effect of creating the home dir
  59. # or user base for installations during finalize_options()
  60. # ideally, we'd prefer a scheme class that has no side-effects.
  61. assert not (user and prefix), f"user={user} prefix={prefix}"
  62. assert not (home and prefix), f"home={home} prefix={prefix}"
  63. i.user = user or i.user
  64. if user or home:
  65. i.prefix = ""
  66. i.prefix = prefix or i.prefix
  67. i.home = home or i.home
  68. i.root = root or i.root
  69. i.finalize_options()
  70. scheme = {}
  71. for key in SCHEME_KEYS:
  72. scheme[key] = getattr(i, "install_" + key)
  73. # install_lib specified in setup.cfg should install *everything*
  74. # into there (i.e. it takes precedence over both purelib and
  75. # platlib). Note, i.install_lib is *always* set after
  76. # finalize_options(); we only want to override here if the user
  77. # has explicitly requested it hence going back to the config
  78. if "install_lib" in d.get_option_dict("install"):
  79. scheme.update({"purelib": i.install_lib, "platlib": i.install_lib})
  80. if running_under_virtualenv():
  81. if home:
  82. prefix = home
  83. elif user:
  84. prefix = i.install_userbase
  85. else:
  86. prefix = i.prefix
  87. scheme["headers"] = os.path.join(
  88. prefix,
  89. "include",
  90. "site",
  91. f"python{get_major_minor_version()}",
  92. dist_name,
  93. )
  94. if root is not None:
  95. path_no_drive = os.path.splitdrive(os.path.abspath(scheme["headers"]))[1]
  96. scheme["headers"] = os.path.join(root, path_no_drive[1:])
  97. return scheme
  98. def get_scheme(
  99. dist_name: str,
  100. user: bool = False,
  101. home: Optional[str] = None,
  102. root: Optional[str] = None,
  103. isolated: bool = False,
  104. prefix: Optional[str] = None,
  105. ) -> Scheme:
  106. """
  107. Get the "scheme" corresponding to the input parameters. The distutils
  108. documentation provides the context for the available schemes:
  109. https://docs.python.org/3/install/index.html#alternate-installation
  110. :param dist_name: the name of the package to retrieve the scheme for, used
  111. in the headers scheme path
  112. :param user: indicates to use the "user" scheme
  113. :param home: indicates to use the "home" scheme and provides the base
  114. directory for the same
  115. :param root: root under which other directories are re-based
  116. :param isolated: equivalent to --no-user-cfg, i.e. do not consider
  117. ~/.pydistutils.cfg (posix) or ~/pydistutils.cfg (non-posix) for
  118. scheme paths
  119. :param prefix: indicates to use the "prefix" scheme and provides the
  120. base directory for the same
  121. """
  122. scheme = distutils_scheme(dist_name, user, home, root, isolated, prefix)
  123. return Scheme(
  124. platlib=scheme["platlib"],
  125. purelib=scheme["purelib"],
  126. headers=scheme["headers"],
  127. scripts=scheme["scripts"],
  128. data=scheme["data"],
  129. )
  130. def get_bin_prefix() -> str:
  131. # XXX: In old virtualenv versions, sys.prefix can contain '..' components,
  132. # so we need to call normpath to eliminate them.
  133. prefix = os.path.normpath(sys.prefix)
  134. if WINDOWS:
  135. bin_py = os.path.join(prefix, "Scripts")
  136. # buildout uses 'bin' on Windows too?
  137. if not os.path.exists(bin_py):
  138. bin_py = os.path.join(prefix, "bin")
  139. return bin_py
  140. # Forcing to use /usr/local/bin for standard macOS framework installs
  141. # Also log to ~/Library/Logs/ for use with the Console.app log viewer
  142. if sys.platform[:6] == "darwin" and prefix[:16] == "/System/Library/":
  143. return "/usr/local/bin"
  144. return os.path.join(prefix, "bin")
  145. def get_purelib() -> str:
  146. return get_python_lib(plat_specific=False)
  147. def get_platlib() -> str:
  148. return get_python_lib(plat_specific=True)