|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import re
|
6 |
| -from typing import Dict, Iterable, List |
| 6 | +from typing import Dict, Iterable, List, Optional |
7 | 7 |
|
8 | 8 | from pip._vendor.packaging.tags import Tag
|
| 9 | +from pip._vendor.packaging.utils import BuildTag, parse_wheel_filename |
9 | 10 | from pip._vendor.packaging.utils import (
|
10 |
| - InvalidWheelFilename as PackagingInvalidWheelName, |
| 11 | + InvalidWheelFilename as _PackagingInvalidWheelFilename, |
11 | 12 | )
|
12 |
| -from pip._vendor.packaging.utils import parse_wheel_filename |
13 | 13 |
|
14 | 14 | from pip._internal.exceptions import InvalidWheelFilename
|
15 | 15 | from pip._internal.utils.deprecation import deprecated
|
|
18 | 18 | class Wheel:
|
19 | 19 | """A wheel file"""
|
20 | 20 |
|
21 |
| - wheel_file_re = re.compile( |
| 21 | + legacy_wheel_file_re = re.compile( |
22 | 22 | r"""^(?P<namever>(?P<name>[^\s-]+?)-(?P<ver>[^\s-]*?))
|
23 | 23 | ((-(?P<build>\d[^-]*?))?-(?P<pyver>[^\s-]+?)-(?P<abi>[^\s-]+?)-(?P<plat>[^\s-]+?)
|
24 | 24 | \.whl|\.dist-info)$""",
|
25 | 25 | re.VERBOSE,
|
26 | 26 | )
|
27 | 27 |
|
28 | 28 | def __init__(self, filename: str) -> None:
|
29 |
| - """ |
30 |
| - :raises InvalidWheelFilename: when the filename is invalid for a wheel |
31 |
| - """ |
32 |
| - wheel_info = self.wheel_file_re.match(filename) |
33 |
| - if not wheel_info: |
34 |
| - raise InvalidWheelFilename(f"{filename} is not a valid wheel filename.") |
35 | 29 | self.filename = filename
|
36 |
| - self.name = wheel_info.group("name").replace("_", "-") |
37 |
| - _version = wheel_info.group("ver") |
38 |
| - if "_" in _version: |
39 |
| - try: |
40 |
| - parse_wheel_filename(filename) |
41 |
| - except PackagingInvalidWheelName as e: |
42 |
| - deprecated( |
43 |
| - reason=( |
44 |
| - f"Wheel filename {filename!r} is not correctly normalised. " |
45 |
| - "Future versions of pip will raise the following error:\n" |
46 |
| - f"{e.args[0]}\n\n" |
47 |
| - ), |
48 |
| - replacement=( |
49 |
| - "to rename the wheel to use a correctly normalised " |
50 |
| - "name (this may require updating the version in " |
51 |
| - "the project metadata)" |
52 |
| - ), |
53 |
| - gone_in="25.1", |
54 |
| - issue=12938, |
55 |
| - ) |
56 |
| - |
57 |
| - _version = _version.replace("_", "-") |
58 |
| - |
59 |
| - self.version = _version |
60 |
| - self.build_tag = wheel_info.group("build") |
61 |
| - self.pyversions = wheel_info.group("pyver").split(".") |
62 |
| - self.abis = wheel_info.group("abi").split(".") |
63 |
| - self.plats = wheel_info.group("plat").split(".") |
64 |
| - |
65 |
| - # All the tag combinations from this file |
66 |
| - self.file_tags = { |
67 |
| - Tag(x, y, z) for x in self.pyversions for y in self.abis for z in self.plats |
68 |
| - } |
| 30 | + |
| 31 | + # To make mypy happy specify type hints that can come from either |
| 32 | + # parse_wheel_filename or the legacy_wheel_file_re match. |
| 33 | + self.name: str |
| 34 | + self._build_tag: Optional[BuildTag] = None |
| 35 | + |
| 36 | + try: |
| 37 | + wheel_info = parse_wheel_filename(filename) |
| 38 | + self.name, _version, self._build_tag, self.file_tags = wheel_info |
| 39 | + self.version = str(_version) |
| 40 | + except _PackagingInvalidWheelFilename as e: |
| 41 | + # Check if the wheel filename is in the legacy format |
| 42 | + legacy_wheel_info = self.legacy_wheel_file_re.match(filename) |
| 43 | + if not legacy_wheel_info: |
| 44 | + raise InvalidWheelFilename(e.args[0]) from None |
| 45 | + |
| 46 | + deprecated( |
| 47 | + reason=( |
| 48 | + f"Wheel filename {filename!r} is not correctly normalised. " |
| 49 | + "Future versions of pip will raise the following error:\n" |
| 50 | + f"{e.args[0]}\n\n" |
| 51 | + ), |
| 52 | + replacement=( |
| 53 | + "to rename the wheel to use a correctly normalised " |
| 54 | + "name (this may require updating the version in " |
| 55 | + "the project metadata)" |
| 56 | + ), |
| 57 | + gone_in="25.3", |
| 58 | + issue=12938, |
| 59 | + ) |
| 60 | + |
| 61 | + self.name = legacy_wheel_info.group("name").replace("_", "-") |
| 62 | + self.version = legacy_wheel_info.group("ver").replace("_", "-") |
| 63 | + |
| 64 | + # Generate the file tags from the legacy wheel filename |
| 65 | + pyversions = legacy_wheel_info.group("pyver").split(".") |
| 66 | + abis = legacy_wheel_info.group("abi").split(".") |
| 67 | + plats = legacy_wheel_info.group("plat").split(".") |
| 68 | + self.file_tags = frozenset( |
| 69 | + Tag(interpreter=py, abi=abi, platform=plat) |
| 70 | + for py in pyversions |
| 71 | + for abi in abis |
| 72 | + for plat in plats |
| 73 | + ) |
| 74 | + |
| 75 | + @property |
| 76 | + def build_tag(self) -> BuildTag: |
| 77 | + if self._build_tag is not None: |
| 78 | + return self._build_tag |
| 79 | + |
| 80 | + # Parse the build tag from the legacy wheel filename |
| 81 | + legacy_wheel_info = self.legacy_wheel_file_re.match(self.filename) |
| 82 | + assert legacy_wheel_info is not None, "guaranteed by filename validation" |
| 83 | + build_tag = legacy_wheel_info.group("build") |
| 84 | + match = re.match(r"^(\d+)(.*)$", build_tag) |
| 85 | + assert match is not None, "guaranteed by filename validation" |
| 86 | + build_tag_groups = match.groups() |
| 87 | + self._build_tag = (int(build_tag_groups[0]), build_tag_groups[1]) |
| 88 | + |
| 89 | + return self._build_tag |
69 | 90 |
|
70 | 91 | def get_formatted_file_tags(self) -> List[str]:
|
71 | 92 | """Return the wheel's tags as a sorted list of strings."""
|
|
0 commit comments