target_python.py 3.7 KB

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