Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/models/wheel.py
4804 views
"""Represents a wheel file and provides access to the various parts of the1name that have meaning.2"""3import re4from typing import Dict, Iterable, List56from pip._vendor.packaging.tags import Tag78from pip._internal.exceptions import InvalidWheelFilename91011class Wheel:12"""A wheel file"""1314wheel_file_re = re.compile(15r"""^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))16((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)17\.whl|\.dist-info)$""",18re.VERBOSE,19)2021def __init__(self, filename: str) -> None:22"""23:raises InvalidWheelFilename: when the filename is invalid for a wheel24"""25wheel_info = self.wheel_file_re.match(filename)26if not wheel_info:27raise InvalidWheelFilename(f"{filename} is not a valid wheel filename.")28self.filename = filename29self.name = wheel_info.group("name").replace("_", "-")30# we'll assume "_" means "-" due to wheel naming scheme31# (https://github.com/pypa/pip/issues/1150)32self.version = wheel_info.group("ver").replace("_", "-")33self.build_tag = wheel_info.group("build")34self.pyversions = wheel_info.group("pyver").split(".")35self.abis = wheel_info.group("abi").split(".")36self.plats = wheel_info.group("plat").split(".")3738# All the tag combinations from this file39self.file_tags = {40Tag(x, y, z) for x in self.pyversions for y in self.abis for z in self.plats41}4243def get_formatted_file_tags(self) -> List[str]:44"""Return the wheel's tags as a sorted list of strings."""45return sorted(str(tag) for tag in self.file_tags)4647def support_index_min(self, tags: List[Tag]) -> int:48"""Return the lowest index that one of the wheel's file_tag combinations49achieves in the given list of supported tags.5051For example, if there are 8 supported tags and one of the file tags52is first in the list, then return 0.5354:param tags: the PEP 425 tags to check the wheel against, in order55with most preferred first.5657:raises ValueError: If none of the wheel's file tags match one of58the supported tags.59"""60return min(tags.index(tag) for tag in self.file_tags if tag in tags)6162def find_most_preferred_tag(63self, tags: List[Tag], tag_to_priority: Dict[Tag, int]64) -> int:65"""Return the priority of the most preferred tag that one of the wheel's file66tag combinations achieves in the given list of supported tags using the given67tag_to_priority mapping, where lower priorities are more-preferred.6869This is used in place of support_index_min in some cases in order to avoid70an expensive linear scan of a large list of tags.7172:param tags: the PEP 425 tags to check the wheel against.73:param tag_to_priority: a mapping from tag to priority of that tag, where74lower is more preferred.7576:raises ValueError: If none of the wheel's file tags match one of77the supported tags.78"""79return min(80tag_to_priority[tag] for tag in self.file_tags if tag in tag_to_priority81)8283def supported(self, tags: Iterable[Tag]) -> bool:84"""Return whether the wheel is compatible with one of the given tags.8586:param tags: the PEP 425 tags to check the wheel against.87"""88return not self.file_tags.isdisjoint(tags)899091