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.

122 lines
4.2 KiB

6 months ago
  1. import sys
  2. from typing import List, Optional, Set, Tuple
  3. from pip._vendor.packaging.tags import Tag
  4. from pip._internal.utils.compatibility_tags import get_supported, version_info_to_nodot
  5. from pip._internal.utils.misc import normalize_version_info
  6. class TargetPython:
  7. """
  8. Encapsulates the properties of a Python interpreter one is targeting
  9. for a package install, download, etc.
  10. """
  11. __slots__ = [
  12. "_given_py_version_info",
  13. "abis",
  14. "implementation",
  15. "platforms",
  16. "py_version",
  17. "py_version_info",
  18. "_valid_tags",
  19. "_valid_tags_set",
  20. ]
  21. def __init__(
  22. self,
  23. platforms: Optional[List[str]] = None,
  24. py_version_info: Optional[Tuple[int, ...]] = None,
  25. abis: Optional[List[str]] = None,
  26. implementation: Optional[str] = None,
  27. ) -> None:
  28. """
  29. :param platforms: A list of strings or None. If None, searches for
  30. packages that are supported by the current system. Otherwise, will
  31. find packages that can be built on the platforms passed in. These
  32. packages will only be downloaded for distribution: they will
  33. not be built locally.
  34. :param py_version_info: An optional tuple of ints representing the
  35. Python version information to use (e.g. `sys.version_info[:3]`).
  36. This can have length 1, 2, or 3 when provided.
  37. :param abis: A list of strings or None. This is passed to
  38. compatibility_tags.py's get_supported() function as is.
  39. :param implementation: A string or None. This is passed to
  40. compatibility_tags.py's get_supported() function as is.
  41. """
  42. # Store the given py_version_info for when we call get_supported().
  43. self._given_py_version_info = py_version_info
  44. if py_version_info is None:
  45. py_version_info = sys.version_info[:3]
  46. else:
  47. py_version_info = normalize_version_info(py_version_info)
  48. py_version = ".".join(map(str, py_version_info[:2]))
  49. self.abis = abis
  50. self.implementation = implementation
  51. self.platforms = platforms
  52. self.py_version = py_version
  53. self.py_version_info = py_version_info
  54. # This is used to cache the return value of get_(un)sorted_tags.
  55. self._valid_tags: Optional[List[Tag]] = None
  56. self._valid_tags_set: Optional[Set[Tag]] = None
  57. def format_given(self) -> str:
  58. """
  59. Format the given, non-None attributes for display.
  60. """
  61. display_version = None
  62. if self._given_py_version_info is not None:
  63. display_version = ".".join(
  64. str(part) for part in self._given_py_version_info
  65. )
  66. key_values = [
  67. ("platforms", self.platforms),
  68. ("version_info", display_version),
  69. ("abis", self.abis),
  70. ("implementation", self.implementation),
  71. ]
  72. return " ".join(
  73. f"{key}={value!r}" for key, value in key_values if value is not None
  74. )
  75. def get_sorted_tags(self) -> List[Tag]:
  76. """
  77. Return the supported PEP 425 tags to check wheel candidates against.
  78. The tags are returned in order of preference (most preferred first).
  79. """
  80. if self._valid_tags is None:
  81. # Pass versions=None if no py_version_info was given since
  82. # versions=None uses special default logic.
  83. py_version_info = self._given_py_version_info
  84. if py_version_info is None:
  85. version = None
  86. else:
  87. version = version_info_to_nodot(py_version_info)
  88. tags = get_supported(
  89. version=version,
  90. platforms=self.platforms,
  91. abis=self.abis,
  92. impl=self.implementation,
  93. )
  94. self._valid_tags = tags
  95. return self._valid_tags
  96. def get_unsorted_tags(self) -> Set[Tag]:
  97. """Exactly the same as get_sorted_tags, but returns a set.
  98. This is important for performance.
  99. """
  100. if self._valid_tags_set is None:
  101. self._valid_tags_set = set(self.get_sorted_tags())
  102. return self._valid_tags_set