wheel.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Represents a wheel file and provides access to the various parts of the
  2. name that have meaning.
  3. """
  4. from __future__ import annotations
  5. from collections.abc import Iterable
  6. from pip._vendor.packaging.tags import Tag
  7. from pip._vendor.packaging.utils import (
  8. InvalidWheelFilename as _PackagingInvalidWheelFilename,
  9. )
  10. from pip._vendor.packaging.utils import parse_wheel_filename
  11. from pip._internal.exceptions import InvalidWheelFilename
  12. class Wheel:
  13. """A wheel file"""
  14. def __init__(self, filename: str) -> None:
  15. self.filename = filename
  16. try:
  17. wheel_info = parse_wheel_filename(filename)
  18. except _PackagingInvalidWheelFilename as e:
  19. raise InvalidWheelFilename(e.args[0]) from None
  20. self.name, _version, self.build_tag, self.file_tags = wheel_info
  21. self.version = str(_version)
  22. def get_formatted_file_tags(self) -> list[str]:
  23. """Return the wheel's tags as a sorted list of strings."""
  24. return sorted(str(tag) for tag in self.file_tags)
  25. def support_index_min(self, tags: list[Tag]) -> int:
  26. """Return the lowest index that one of the wheel's file_tag combinations
  27. achieves in the given list of supported tags.
  28. For example, if there are 8 supported tags and one of the file tags
  29. is first in the list, then return 0.
  30. :param tags: the PEP 425 tags to check the wheel against, in order
  31. with most preferred first.
  32. :raises ValueError: If none of the wheel's file tags match one of
  33. the supported tags.
  34. """
  35. try:
  36. return next(i for i, t in enumerate(tags) if t in self.file_tags)
  37. except StopIteration:
  38. raise ValueError()
  39. def find_most_preferred_tag(
  40. self, tags: list[Tag], tag_to_priority: dict[Tag, int]
  41. ) -> int:
  42. """Return the priority of the most preferred tag that one of the wheel's file
  43. tag combinations achieves in the given list of supported tags using the given
  44. tag_to_priority mapping, where lower priorities are more-preferred.
  45. This is used in place of support_index_min in some cases in order to avoid
  46. an expensive linear scan of a large list of tags.
  47. :param tags: the PEP 425 tags to check the wheel against.
  48. :param tag_to_priority: a mapping from tag to priority of that tag, where
  49. lower is more preferred.
  50. :raises ValueError: If none of the wheel's file tags match one of
  51. the supported tags.
  52. """
  53. return min(
  54. tag_to_priority[tag] for tag in self.file_tags if tag in tag_to_priority
  55. )
  56. def supported(self, tags: Iterable[Tag]) -> bool:
  57. """Return whether the wheel is compatible with one of the given tags.
  58. :param tags: the PEP 425 tags to check the wheel against.
  59. """
  60. return not self.file_tags.isdisjoint(tags)