target_python.py 4.0 KB

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