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

Normalize wheel filenames #135

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 11 additions & 1 deletion src/installer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def parse_metadata_file(contents: str) -> Message:
return feed_parser.close()


def normalize_distribution_name(name: str) -> str:
"""Normalize a project name according to PEP-503.

:param name: The project name to normalize
"""
return re.sub(r"[-_.]+", "-", name).lower()


def parse_wheel_filename(filename: str) -> WheelFilename:
"""Parse a wheel filename, into it's various components.

Expand All @@ -101,7 +109,9 @@ def parse_wheel_filename(filename: str) -> WheelFilename:
wheel_info = _WHEEL_FILENAME_REGEX.match(filename)
if not wheel_info:
raise ValueError(f"Not a valid wheel filename: {filename}")
return WheelFilename(*wheel_info.groups())
parsed = wheel_info.groups()
normalized_name = normalize_distribution_name(parsed[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, we don't know for sure if the name inside the wheel will be canonicalized.

For example, zope.interface's wheels contain zope.interface-5.4.0.dist-info (and not zope-interface) thanks, in no small part, to setuptools' behaviour in this area.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait, that's what this is doing. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we're going to either need to probe a few names, or search the directory listing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see pip searches the zip for a .dist-info. So I guess we should do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Let's close this out, since we've established that merely canonicalising the name isn't sufficient.

return WheelFilename(normalized_name, *parsed[1:])


def copyfileobj_with_hashing(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
construct_record_file,
copyfileobj_with_hashing,
fix_shebang,
normalize_distribution_name,
parse_entrypoints,
parse_metadata_file,
parse_wheel_filename,
Expand All @@ -40,6 +41,27 @@ def test_basics(self):
assert result.get_all("MULTI-USE-FIELD") == ["1", "2", "3"]


class TestNormalizeDistributionName:
@pytest.mark.parametrize(
"string, expected",
[
# Noop
(
"package-1",
"package-1",
),
# PEP 508 normalization
(
"ABC..12",
"abc-12",
),
],
)
def test_valid_cases(self, string, expected):
got = normalize_distribution_name(string)
assert expected == got, (expected, got)


class TestParseWheelFilename:
@pytest.mark.parametrize(
"string, expected",
Expand Down Expand Up @@ -71,6 +93,11 @@ class TestParseWheelFilename:
"tensorflow-2.3.0-cp38-cp38-win_amd64.whl",
WheelFilename("tensorflow", "2.3.0", None, "cp38-cp38-win_amd64"),
),
# Non-canonicalized real wheel names
(
"Quart-0.18.0-py3-none-any.whl",
WheelFilename("quart", "0.18.0", None, "py3-none-any"),
),
],
)
def test_valid_cases(self, string, expected):
Expand Down