target_python.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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=None, # type: Optional[List[str]]
  23. py_version_info=None, # type: Optional[Tuple[int, ...]]
  24. abis=None, # type: Optional[List[str]]
  25. implementation=None, # type: Optional[str]
  26. ):
  27. # type: (...) -> 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_tags().
  55. self._valid_tags = None # type: Optional[List[Tag]]
  56. def format_given(self):
  57. # type: () -> 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
  74. if value is not None
  75. )
  76. def get_tags(self):
  77. # type: () -> List[Tag]
  78. """
  79. Return the supported PEP 425 tags to check wheel candidates against.
  80. The tags are returned in order of preference (most preferred first).
  81. """
  82. if self._valid_tags is None:
  83. # Pass versions=None if no py_version_info was given since
  84. # versions=None uses special default logic.
  85. py_version_info = self._given_py_version_info
  86. if py_version_info is None:
  87. version = None
  88. else:
  89. version = version_info_to_nodot(py_version_info)
  90. tags = get_supported(
  91. version=version,
  92. platforms=self.platforms,
  93. abis=self.abis,
  94. impl=self.implementation,
  95. )
  96. self._valid_tags = tags
  97. return self._valid_tags