target_python.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import sys
  2. from typing import List, Optional, 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. ]
  20. def __init__(
  21. self,
  22. platforms: Optional[List[str]] = None,
  23. py_version_info: Optional[Tuple[int, ...]] = None,
  24. abis: Optional[List[str]] = None,
  25. implementation: Optional[str] = None,
  26. ) -> None:
  27. """
  28. :param platforms: A list of strings or None. If None, searches for
  29. packages that are supported by the current system. Otherwise, will
  30. find packages that can be built on the platforms passed in. These
  31. packages will only be downloaded for distribution: they will
  32. not be built locally.
  33. :param py_version_info: An optional tuple of ints representing the
  34. Python version information to use (e.g. `sys.version_info[:3]`).
  35. This can have length 1, 2, or 3 when provided.
  36. :param abis: A list of strings or None. This is passed to
  37. compatibility_tags.py's get_supported() function as is.
  38. :param implementation: A string or None. This is passed to
  39. compatibility_tags.py's get_supported() function as is.
  40. """
  41. # Store the given py_version_info for when we call get_supported().
  42. self._given_py_version_info = py_version_info
  43. if py_version_info is None:
  44. py_version_info = sys.version_info[:3]
  45. else:
  46. py_version_info = normalize_version_info(py_version_info)
  47. py_version = ".".join(map(str, py_version_info[:2]))
  48. self.abis = abis
  49. self.implementation = implementation
  50. self.platforms = platforms
  51. self.py_version = py_version
  52. self.py_version_info = py_version_info
  53. # This is used to cache the return value of get_tags().
  54. self._valid_tags: Optional[List[Tag]] = None
  55. def format_given(self) -> str:
  56. """
  57. Format the given, non-None attributes for display.
  58. """
  59. display_version = None
  60. if self._given_py_version_info is not None:
  61. display_version = ".".join(
  62. str(part) for part in self._given_py_version_info
  63. )
  64. key_values = [
  65. ("platforms", self.platforms),
  66. ("version_info", display_version),
  67. ("abis", self.abis),
  68. ("implementation", self.implementation),
  69. ]
  70. return " ".join(
  71. f"{key}={value!r}" for key, value in key_values if value is not None
  72. )
  73. def get_tags(self) -> List[Tag]:
  74. """
  75. Return the supported PEP 425 tags to check wheel candidates against.
  76. The tags are returned in order of preference (most preferred first).
  77. """
  78. if self._valid_tags is None:
  79. # Pass versions=None if no py_version_info was given since
  80. # versions=None uses special default logic.
  81. py_version_info = self._given_py_version_info
  82. if py_version_info is None:
  83. version = None
  84. else:
  85. version = version_info_to_nodot(py_version_info)
  86. tags = get_supported(
  87. version=version,
  88. platforms=self.platforms,
  89. abis=self.abis,
  90. impl=self.implementation,
  91. )
  92. self._valid_tags = tags
  93. return self._valid_tags