Skip to content

Commit

Permalink
Search weels for .dist-info directories
Browse files Browse the repository at this point in the history
Some wheels don't use normalized names for their .dist-info directories,
so search the wheel for them.

Fixes: pypa#134
  • Loading branch information
stefanor committed Aug 16, 2022
1 parent 4b3af0c commit 115a879
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/installer/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ def open(cls, path: "os.PathLike[str]") -> Iterator["WheelFile"]:
with zipfile.ZipFile(path) as f:
yield cls(f)

@property
def dist_info_dir(self) -> str:
"""Name of the dist-info directory."""
if not hasattr(self, "_dist_info_dir"):
top_level_directories = {
path.split("/", 1)[0] for path in self._zipfile.namelist()
}
dist_infos = [
name for name in top_level_directories if name.endswith(".dist-info")
]
assert (
len(dist_infos) == 1
), "Wheel doesn't contain exactly one .dist-info directory"
self._dist_info_dir = dist_infos[0]
return self._dist_info_dir

@property
def dist_info_filenames(self) -> List[str]:
"""Get names of all files in the dist-info directory."""
Expand Down
42 changes: 42 additions & 0 deletions tests/test_sources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import posixpath
import textwrap
import zipfile

import pytest
Expand Down Expand Up @@ -92,3 +93,44 @@ def test_provides_correct_contents(self, fancy_wheel):

assert sorted(got_records) == sorted(expected_records)
assert got_files == files

def test_finds_dist_info(self, tmp_path):
# Create a wheel with a misnamed .dist-info
path = tmp_path / "denorm-1.0-py3-none-any.whl"
files = {
"denorm.py": b"",
"misnamed-1.0.0.dist-info/top_level.txt": b"""\
denorm
""",
"misnamed-1.0.0.dist-info/WHEEL": b"""\
Wheel-Version: 1.0
Generator: bad-magic (1.0.0)
Root-Is-Purelib: true
Tag: py3-none-any
""",
"misnamed-1.0.0.dist-info/METADATA": b"""\
Metadata-Version: 2.1
Name: denorm
Version: 1.0.0
""",
"misnamed-1.0.0.dist-info/RECORD": b"""\
fancy.py,,
misnamed-1.0.0.dist-info/top_level.txt,,
misnamed-1.0.0.dist-info/WHEEL,,
misnamed-1.0.0.dist-info/METADATA,,
misnamed-1.0.0.dist-info/RECORD,,
""",
}
with zipfile.ZipFile(path, "w") as archive:
for name, indented_content in files.items():
archive.writestr(
name,
textwrap.dedent(indented_content.decode("utf-8")).encode("utf-8"),
)
with WheelFile.open(path) as source:
assert sorted(source.dist_info_filenames) == [
"METADATA",
"RECORD",
"WHEEL",
"top_level.txt",
]

0 comments on commit 115a879

Please sign in to comment.