Skip to content

Commit

Permalink
installer: support for unnormalized dist-info
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Mar 17, 2023
1 parent 40061f9 commit c1f8dbd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/poetry/installation/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from installer import install
from installer.destinations import SchemeDictionaryDestination
from installer.sources import WheelFile
from packaging.utils import canonicalize_name

from poetry.__version__ import __version__
from poetry.utils._compat import WINDOWS
Expand All @@ -25,6 +26,37 @@
from poetry.utils.env import Env


class PatchedWheelFile(WheelFile):
# TODO: Patch from https://github.com/pypa/installer/issues/134
# can be removed if new installer release is available
@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"
dist_info_dir = dist_infos[0]

# NAME-VER.dist-info
di_dname = dist_info_dir.rsplit("-", 2)[0]
norm_di_dname = canonicalize_name(di_dname)
norm_file_dname = canonicalize_name(self.distribution)
assert (
norm_di_dname == norm_file_dname
), "Wheel .dist-info directory doesn't match wheel filename"

self._dist_info_dir = dist_info_dir
return self._dist_info_dir


class WheelDestination(SchemeDictionaryDestination):
""" """

Expand Down Expand Up @@ -97,7 +129,7 @@ def enable_bytecode_compilation(self, enable: bool = True) -> None:
self._destination.bytecode_optimization_levels = (1,) if enable else ()

def install(self, wheel: Path) -> None:
with WheelFile.open(Path(wheel.as_posix())) as source:
with PatchedWheelFile.open(Path(wheel.as_posix())) as source:
install(
source=source,
destination=self._destination.for_source(source),
Expand Down
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/installation/test_wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ def test_enable_bytecode_compilation(
assert list(cache_dir.glob("*.pyc"))
else:
assert not cache_dir.exists()


def test_install_wheel_with_unnormalized_dist_info(
fixture_dir: FixtureDirGetter, tmp_path: Path
) -> None:
wheel = fixture_dir("distributions/un_normalized_dist_info-0.1.0-py3-none-any.whl")
env = MockEnv(path=tmp_path)
installer = WheelInstaller(env)
installer.install(wheel)
dist_info_dir = (
Path(env.paths["purelib"]) / "Un.normalized_dist-info-0.1.0.dist-info"
)
assert dist_info_dir.exists()
assert (dist_info_dir / "INSTALLER").exists()
assert (dist_info_dir / "METADATA").exists()
assert (dist_info_dir / "RECORD").exists()
assert (dist_info_dir / "WHEEL").exists()

0 comments on commit c1f8dbd

Please sign in to comment.