wheel.py 3.5 KB

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