wheel.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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._internal.exceptions import InvalidWheelFilename
  8. class Wheel:
  9. """A wheel file"""
  10. wheel_file_re = re.compile(
  11. r"""^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))
  12. ((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
  13. \.whl|\.dist-info)$""",
  14. re.VERBOSE,
  15. )
  16. def __init__(self, filename: str) -> None:
  17. """
  18. :raises InvalidWheelFilename: when the filename is invalid for a wheel
  19. """
  20. wheel_info = self.wheel_file_re.match(filename)
  21. if not wheel_info:
  22. raise InvalidWheelFilename(f"{filename} is not a valid wheel filename.")
  23. self.filename = filename
  24. self.name = wheel_info.group("name").replace("_", "-")
  25. # we'll assume "_" means "-" due to wheel naming scheme
  26. # (https://github.com/pypa/pip/issues/1150)
  27. self.version = wheel_info.group("ver").replace("_", "-")
  28. self.build_tag = wheel_info.group("build")
  29. self.pyversions = wheel_info.group("pyver").split(".")
  30. self.abis = wheel_info.group("abi").split(".")
  31. self.plats = wheel_info.group("plat").split(".")
  32. # All the tag combinations from this file
  33. self.file_tags = {
  34. Tag(x, y, z) for x in self.pyversions for y in self.abis for z in self.plats
  35. }
  36. def get_formatted_file_tags(self) -> List[str]:
  37. """Return the wheel's tags as a sorted list of strings."""
  38. return sorted(str(tag) for tag in self.file_tags)
  39. def support_index_min(self, tags: List[Tag]) -> int:
  40. """Return the lowest index that one of the wheel's file_tag combinations
  41. achieves in the given list of supported tags.
  42. For example, if there are 8 supported tags and one of the file tags
  43. is first in the list, then return 0.
  44. :param tags: the PEP 425 tags to check the wheel against, in order
  45. with most preferred first.
  46. :raises ValueError: If none of the wheel's file tags match one of
  47. the supported tags.
  48. """
  49. return min(tags.index(tag) for tag in self.file_tags if tag in tags)
  50. def find_most_preferred_tag(
  51. self, tags: List[Tag], tag_to_priority: Dict[Tag, int]
  52. ) -> int:
  53. """Return the priority of the most preferred tag that one of the wheel's file
  54. tag combinations achieves in the given list of supported tags using the given
  55. tag_to_priority mapping, where lower priorities are more-preferred.
  56. This is used in place of support_index_min in some cases in order to avoid
  57. an expensive linear scan of a large list of tags.
  58. :param tags: the PEP 425 tags to check the wheel against.
  59. :param tag_to_priority: a mapping from tag to priority of that tag, where
  60. lower is more preferred.
  61. :raises ValueError: If none of the wheel's file tags match one of
  62. the supported tags.
  63. """
  64. return min(
  65. tag_to_priority[tag] for tag in self.file_tags if tag in tag_to_priority
  66. )
  67. def supported(self, tags: Iterable[Tag]) -> bool:
  68. """Return whether the wheel is compatible with one of the given tags.
  69. :param tags: the PEP 425 tags to check the wheel against.
  70. """
  71. return not self.file_tags.isdisjoint(tags)