Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of wheel version numbers #183

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+wheel-parsing-prerelease-versions.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where parsing of wheel filenames in test support code would miss prerelease version numbers
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ testing =
python_version < "3.8"
importlib-metadata; \
python_version < "3.8"
wheel-filename
# no version of pyproject-metadata supports Python 3.6
pyproject-metadata; \
python_version >= "3.7"
Expand Down
35 changes: 26 additions & 9 deletions test_support/test_support/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from abc import ABC, abstractmethod
from test_support import importlib_metadata, Project
from test_support.metadata import parse_core_metadata
from typing import Any, IO, Iterable, List, Optional, Sequence
from typing import Any, IO, Iterable, List, NamedTuple, Optional, Sequence, Union
from wheel_filename import ParsedWheelFilename, parse_wheel_filename

try:
from pyproject_metadata import RFC822Message, StandardMetadata
Expand Down Expand Up @@ -95,18 +96,28 @@ def __init__(self, package_url: str, package_hash_spec: str, metadata_hash_spec:
self.metadata_hasher = None


class ParsedSdistFilename(NamedTuple):
project: str
version: str


def _parse_package_filename(filename: str) -> Union[ParsedSdistFilename, ParsedWheelFilename]:
if filename.endswith(".whl"):
return parse_wheel_filename(filename)
elif filename.endswith(".tar.gz"):
name, _, version = filename[:-7].partition("-")
return ParsedSdistFilename(name, version)
else:
raise ValueError(f"Not a valid Python package filename: {filename}")


class SimplePackageListingParser(html.parser.HTMLParser):
"""
A bare-bones parser for the list of versions of a given package offered by
the simple repository API. It will select releases of the given version of
the package.
"""

_filename_regex = re.compile(
r"(?P<name>[\w.-]+)-(?P<version>{}).+\.(?:whl|tar\.gz)".format(packaging.version.VERSION_PATTERN),
flags=re.VERBOSE | re.IGNORECASE,
)

@staticmethod
def _normalize_package_name(name: str):
"""
Expand Down Expand Up @@ -139,10 +150,16 @@ def handle_starttag(self, tag: Any, attrs: Any):
parsed_url: urllib.parse.ParseResult = urllib.parse.urlparse(href)
filename: str
_, _, filename = parsed_url.path.rpartition("/")
m = self._filename_regex.match(filename)
if not m or self.version != packaging.version.parse(m.group("version")):
try:
parsed_filename = _parse_package_filename(filename)
except ValueError:
_logger.debug("no filename match")
return

if self.version != (ver := packaging.version.parse(parsed_filename.version)):
_logger.debug("version mismatch: %s vs %s", self.version, ver)
return
assert self.normalized_name == self._normalize_package_name(m.group("name"))
assert self.normalized_name == self._normalize_package_name(parsed_filename.project)
no_fragment_url: str = urllib.parse.urlunparse(list(parsed_url[:5]) + [""])
self.releases.append(PackageInfo(no_fragment_url, parsed_url.fragment, data_dist_info_metadata))

Expand Down
82 changes: 82 additions & 0 deletions tests/test_filename_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Tests that verify the logic we use to match sdist and wheel filenames is
consistent with the packaging standards.
"""

import pytest

from test_support.distribution import ParsedSdistFilename, _parse_package_filename
from typing import List, Optional, Tuple
from wheel_filename import ParsedWheelFilename

SDIST: List[Tuple[str, str]] = [
("p", "1.0"),
("package", "1.0"),
("package_name", "1.0"),
("package", "0.1"),
("package", "0.0.1"),
("package", "0.0.100"),
("package", "1.post0"),
("package", "2024.09.06"),
("package", "20240906"),
("package", "1a"),
("package", "1a0"),
("package", "1a1"),
("package", "1a10"),
("package", "1b"),
("package", "1b0"),
("package", "1b1"),
("package", "1rc"),
("package", "1rc0"),
("package", "1rc1"),
]


@pytest.mark.parametrize(("name", "version"), SDIST)
class TestSDistFilename:
@pytest.fixture
def match_filename(self, name: str, version: str) -> ParsedSdistFilename:
filename = f"{name}-{version}.tar.gz"
parsed = _parse_package_filename(filename)
assert isinstance(parsed, ParsedSdistFilename)
return parsed

def test_project(self, name: str, version: str, match_filename: ParsedSdistFilename) -> None:
assert match_filename.project == name

def test_version(self, name: str, version: str, match_filename: ParsedSdistFilename) -> None:
assert match_filename.version == version


WHEEL_TAGS: List[Tuple[Optional[str], str, str, str]] = [
(None, "py3", "none", "any"),
("1", "py3", "none", "any"),
(None, "py38", "cp38", "any"),
(None, "py3", "none", "linux_86_64"),
]


@pytest.mark.parametrize(("name", "version"), SDIST)
@pytest.mark.parametrize(("build", "python", "abi", "platform"), WHEEL_TAGS)
class TestWheelFilename:
@pytest.fixture
def match_filename(
self, name: str, version: str, build: str, python: str, abi: str, platform: str
) -> ParsedWheelFilename:
filename = f"{name}-{version}"
if build:
filename += f"-{build}"
filename += f"-{python}-{abi}-{platform}.whl"
parsed = _parse_package_filename(filename)
assert isinstance(parsed, ParsedWheelFilename)
return parsed

def test_name(
self, name: str, version: str, python: str, abi: str, platform: str, match_filename: ParsedWheelFilename
) -> None:
assert match_filename.project == name

def test_version(
self, name: str, version: str, python: str, abi: str, platform: str, match_filename: ParsedWheelFilename
) -> None:
assert match_filename.version == version
Loading