wheel.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Represents a wheel file and provides access to the various parts of the
  2. name that have meaning.
  3. """
  4. import re
  5. from typing import Dict, Iterable, List
  6. from pip._vendor.packaging.tags import Tag
  7. from pip._vendor.packaging.utils import (
  8. InvalidWheelFilename as PackagingInvalidWheelName,
  9. )
  10. from pip._vendor.packaging.utils import parse_wheel_filename
  11. from pip._internal.exceptions import InvalidWheelFilename
  12. from pip._internal.utils.deprecation import deprecated
  13. class Wheel:
  14. """A wheel file"""
  15. wheel_file_re = re.compile(
  16. r"""^(?P<namever>(?P<name>[^\s-]+?)-(?P<ver>[^\s-]*?))
  17. ((-(?P<build>\d[^-]*?))?-(?P<pyver>[^\s-]+?)-(?P<abi>[^\s-]+?)-(?P<plat>[^\s-]+?)
  18. \.whl|\.dist-info)$""",
  19. re.VERBOSE,
  20. )
  21. def __init__(self, filename: str) -> None:
  22. """
  23. :raises InvalidWheelFilename: when the filename is invalid for a wheel
  24. """
  25. wheel_info = self.wheel_file_re.match(filename)
  26. if not wheel_info:
  27. raise InvalidWheelFilename(f"{filename} is not a valid wheel filename.")
  28. self.filename = filename
  29. self.name = wheel_info.group("name").replace("_", "-")
  30. _version = wheel_info.group("ver")
  31. if "_" in _version:
  32. try:
  33. parse_wheel_filename(filename)
  34. except PackagingInvalidWheelName as e:
  35. deprecated(
  36. reason=(
  37. f"Wheel filename {filename!r} is not correctly normalised. "
  38. "Future versions of pip will raise the following error:\n"
  39. f"{e.args[0]}\n\n"
  40. ),
  41. replacement=(
  42. "to rename the wheel to use a correctly normalised "
  43. "name (this may require updating the version in "
  44. "the project metadata)"
  45. ),
  46. gone_in="25.1",
  47. issue=12938,
  48. )
  49. _version = _version.replace("_", "-")
  50. self.version = _version
  51. self.build_tag = wheel_info.group("build")
  52. self.pyversions = wheel_info.group("pyver").split(".")
  53. self.abis = wheel_info.group("abi").split(".")
  54. self.plats = wheel_info.group("plat").split(".")
  55. # All the tag combinations from this file
  56. self.file_tags = {
  57. Tag(x, y, z) for x in self.pyversions for y in self.abis for z in self.plats
  58. }
  59. def get_formatted_file_tags(self) -> List[str]:
  60. """Return the wheel's tags as a sorted list of strings."""
  61. return sorted(str(tag) for tag in self.file_tags)
  62. def support_index_min(self, tags: List[Tag]) -> int:
  63. """Return the lowest index that one of the wheel's file_tag combinations
  64. achieves in the given list of supported tags.
  65. For example, if there are 8 supported tags and one of the file tags
  66. is first in the list, then return 0.
  67. :param tags: the PEP 425 tags to check the wheel against, in order
  68. with most preferred first.
  69. :raises ValueError: If none of the wheel's file tags match one of
  70. the supported tags.
  71. """
  72. try:
  73. return next(i for i, t in enumerate(tags) if t in self.file_tags)
  74. except StopIteration:
  75. raise ValueError()
  76. def find_most_preferred_tag(
  77. self, tags: List[Tag], tag_to_priority: Dict[Tag, int]
  78. ) -> int:
  79. """Return the priority of the most preferred tag that one of the wheel's file
  80. tag combinations achieves in the given list of supported tags using the given
  81. tag_to_priority mapping, where lower priorities are more-preferred.
  82. This is used in place of support_index_min in some cases in order to avoid
  83. an expensive linear scan of a large list of tags.
  84. :param tags: the PEP 425 tags to check the wheel against.
  85. :param tag_to_priority: a mapping from tag to priority of that tag, where
  86. lower is more preferred.
  87. :raises ValueError: If none of the wheel's file tags match one of
  88. the supported tags.
  89. """
  90. return min(
  91. tag_to_priority[tag] for tag in self.file_tags if tag in tag_to_priority
  92. )
  93. def supported(self, tags: Iterable[Tag]) -> bool:
  94. """Return whether the wheel is compatible with one of the given tags.
  95. :param tags: the PEP 425 tags to check the wheel against.
  96. """
  97. return not self.file_tags.isdisjoint(tags)