target_python.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import sys
  2. from pip._internal.utils.compatibility_tags import (
  3. get_supported,
  4. version_info_to_nodot,
  5. )
  6. from pip._internal.utils.misc import normalize_version_info
  7. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  8. if MYPY_CHECK_RUNNING:
  9. from typing import List, Optional, Tuple
  10. from pip._vendor.packaging.tags import Tag
  11. class TargetPython(object):
  12. """
  13. Encapsulates the properties of a Python interpreter one is targeting
  14. for a package install, download, etc.
  15. """
  16. def __init__(
  17. self,
  18. platform=None, # type: Optional[str]
  19. py_version_info=None, # type: Optional[Tuple[int, ...]]
  20. abi=None, # type: Optional[str]
  21. implementation=None, # type: Optional[str]
  22. ):
  23. # type: (...) -> None
  24. """
  25. :param platform: A string or None. If None, searches for packages
  26. that are supported by the current system. Otherwise, will find
  27. packages that can be built on the platform passed in. These
  28. packages will only be downloaded for distribution: they will
  29. not be built locally.
  30. :param py_version_info: An optional tuple of ints representing the
  31. Python version information to use (e.g. `sys.version_info[:3]`).
  32. This can have length 1, 2, or 3 when provided.
  33. :param abi: A string or None. This is passed to compatibility_tags.py's
  34. get_supported() function as is.
  35. :param implementation: A string or None. This is passed to
  36. compatibility_tags.py's get_supported() function as is.
  37. """
  38. # Store the given py_version_info for when we call get_supported().
  39. self._given_py_version_info = py_version_info
  40. if py_version_info is None:
  41. py_version_info = sys.version_info[:3]
  42. else:
  43. py_version_info = normalize_version_info(py_version_info)
  44. py_version = '.'.join(map(str, py_version_info[:2]))
  45. self.abi = abi
  46. self.implementation = implementation
  47. self.platform = platform
  48. self.py_version = py_version
  49. self.py_version_info = py_version_info
  50. # This is used to cache the return value of get_tags().
  51. self._valid_tags = None # type: Optional[List[Tag]]
  52. def format_given(self):
  53. # type: () -> str
  54. """
  55. Format the given, non-None attributes for display.
  56. """
  57. display_version = None
  58. if self._given_py_version_info is not None:
  59. display_version = '.'.join(
  60. str(part) for part in self._given_py_version_info
  61. )
  62. key_values = [
  63. ('platform', self.platform),
  64. ('version_info', display_version),
  65. ('abi', self.abi),
  66. ('implementation', self.implementation),
  67. ]
  68. return ' '.join(
  69. '{}={!r}'.format(key, value) for key, value in key_values
  70. if value is not None
  71. )
  72. def get_tags(self):
  73. # type: () -> 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. platform=self.platform,
  89. abi=self.abi,
  90. impl=self.implementation,
  91. )
  92. self._valid_tags = tags
  93. return self._valid_tags