wheel.py 3.5 KB

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