Skip to content

Commit 2847e9a

Browse files
committed
Have the Chef return Paths instead of Links
1 parent 6da7a67 commit 2847e9a

File tree

6 files changed

+29
-63
lines changed

6 files changed

+29
-63
lines changed

src/poetry/installation/chef.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ def __init__(self, config: Config, env: Env) -> None:
2525
Path(config.get("cache-dir")).expanduser().joinpath("artifacts")
2626
)
2727

28-
def get_cached_archive_for_link(self, link: Link) -> Link | None:
28+
def get_cached_archive_for_link(self, link: Link) -> Path | None:
2929
archives = self.get_cached_archives_for_link(link)
3030
if not archives:
3131
return None
3232

33-
candidates: list[tuple[float | None, Link]] = []
33+
candidates: list[tuple[float | None, Path]] = []
3434
for archive in archives:
35-
if not archive.is_wheel:
35+
if archive.suffix != ".whl":
3636
candidates.append((float("inf"), archive))
3737
continue
3838

3939
try:
40-
wheel = Wheel(archive.filename)
40+
wheel = Wheel(archive.name)
4141
except InvalidWheelName:
4242
continue
4343

@@ -53,16 +53,16 @@ def get_cached_archive_for_link(self, link: Link) -> Link | None:
5353

5454
return min(candidates)[1]
5555

56-
def get_cached_archives_for_link(self, link: Link) -> list[Link]:
56+
def get_cached_archives_for_link(self, link: Link) -> list[Path]:
5757
cache_dir = self.get_cache_directory_for_link(link)
5858

5959
archive_types = ["whl", "tar.gz", "tar.bz2", "bz2", "zip"]
60-
links = []
60+
paths = []
6161
for archive_type in archive_types:
6262
for archive in cache_dir.glob(f"*.{archive_type}"):
63-
links.append(Link(archive.as_uri()))
63+
paths.append(Path(archive))
6464

65-
return links
65+
return paths
6666

6767
def get_cache_directory_for_link(self, link: Link) -> Path:
6868
key_parts = {"url": link.url_without_fragment}

src/poetry/installation/executor.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from cleo.io.null_io import NullIO
1818
from poetry.core.packages.file_dependency import FileDependency
1919
from poetry.core.packages.utils.link import Link
20-
from poetry.core.packages.utils.utils import url_to_path
2120
from poetry.core.pyproject.toml import PyProjectTOML
2221

2322
from poetry.installation.chef import Chef
@@ -114,7 +113,7 @@ def verbose(self, verbose: bool = True) -> Executor:
114113
return self
115114

116115
def pip_install(
117-
self, req: Path | Link, upgrade: bool = False, editable: bool = False
116+
self, req: Path, upgrade: bool = False, editable: bool = False
118117
) -> int:
119118
try:
120119
pip_install(req, self._env, upgrade=upgrade, editable=editable)
@@ -463,7 +462,7 @@ def _install(self, operation: Install | Update) -> int:
463462
if package.source_type == "git":
464463
return self._install_git(operation)
465464

466-
archive: Link | Path
465+
archive: Path
467466
if package.source_type == "file":
468467
archive = self._prepare_file(operation)
469468
elif package.source_type == "url":
@@ -606,15 +605,15 @@ def _install_git(self, operation: Install | Update) -> int:
606605

607606
return status_code
608607

609-
def _download(self, operation: Install | Update) -> Link | Path:
608+
def _download(self, operation: Install | Update) -> Path:
610609
link = self._chooser.choose_for(operation.package)
611610

612611
return self._download_link(operation, link)
613612

614-
def _download_link(self, operation: Install | Update, link: Link) -> Link | Path:
613+
def _download_link(self, operation: Install | Update, link: Link) -> Path:
615614
package = operation.package
616615

617-
archive: Link | Path | None
616+
archive: Path | None
618617
archive = self._chef.get_cached_archive_for_link(link)
619618
if archive is None:
620619
# No cached distributions was found, so we download and prepare it
@@ -638,20 +637,14 @@ def _download_link(self, operation: Install | Update, link: Link) -> Link | Path
638637
return archive
639638

640639
@staticmethod
641-
def _validate_archive_hash(archive: Path | Link, package: Package) -> str:
642-
archive_path = (
643-
url_to_path(archive.url) if isinstance(archive, Link) else archive
644-
)
645-
file_dep = FileDependency(
646-
package.name,
647-
archive_path,
648-
)
640+
def _validate_archive_hash(archive: Path, package: Package) -> str:
641+
file_dep = FileDependency(package.name, archive)
649642
archive_hash: str = "sha256:" + file_dep.hash()
650643
known_hashes = {f["hash"] for f in package.files}
651644

652645
if archive_hash not in known_hashes:
653646
raise RuntimeError(
654-
f"Hash for {package} from archive {archive_path.name} not found in"
647+
f"Hash for {package} from archive {archive.name} not found in"
655648
f" known hashes (was: {archive_hash})"
656649
)
657650

src/poetry/utils/pip.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from poetry.core.packages.utils.link import Link
6-
from poetry.core.packages.utils.utils import url_to_path
7-
85
from poetry.exceptions import PoetryException
96
from poetry.utils.env import EnvCommandError
107

@@ -16,13 +13,12 @@
1613

1714

1815
def pip_install(
19-
path: Path | Link,
16+
path: Path,
2017
environment: Env,
2118
editable: bool = False,
2219
deps: bool = False,
2320
upgrade: bool = False,
2421
) -> int | str:
25-
path = url_to_path(path.url) if isinstance(path, Link) else path
2622
is_wheel = path.suffix == ".whl"
2723

2824
# We disable version check here as we are already pinning to version available in

tests/installation/test_chef.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
[
2424
(
2525
"https://files.python-poetry.org/demo-0.1.0.tar.gz",
26-
"file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl",
26+
"/cache/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl",
2727
),
2828
(
2929
"https://example.com/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl",
30-
"file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl",
30+
"/cache/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl",
3131
),
3232
],
3333
)
@@ -50,16 +50,16 @@ def test_get_cached_archive_for_link(
5050
chef,
5151
"get_cached_archives_for_link",
5252
return_value=[
53-
Link("file:///foo/demo-0.1.0-py2.py3-none-any"),
54-
Link("file:///foo/demo-0.1.0.tar.gz"),
55-
Link("file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl"),
56-
Link("file:///foo/demo-0.1.0-cp37-cp37-macosx_10_15_x86_64.whl"),
53+
Path("/cache/demo-0.1.0-py2.py3-none-any"),
54+
Path("/cache/demo-0.1.0.tar.gz"),
55+
Path("/cache/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl"),
56+
Path("/cache/demo-0.1.0-cp37-cp37-macosx_10_15_x86_64.whl"),
5757
],
5858
)
5959

6060
archive = chef.get_cached_archive_for_link(Link(link))
6161

62-
assert Link(cached) == archive
62+
assert Path(cached) == archive
6363

6464

6565
def test_get_cached_archives_for_link(config: Config, mocker: MockerFixture):
@@ -82,9 +82,7 @@ def test_get_cached_archives_for_link(config: Config, mocker: MockerFixture):
8282
)
8383

8484
assert archives
85-
assert set(archives) == {
86-
Link(path.as_uri()) for path in distributions.glob("demo-0.1.0*")
87-
}
85+
assert set(archives) == {Path(path) for path in distributions.glob("demo-0.1.0*")}
8886

8987

9088
def test_get_cache_directory_for_link(config: Config, config_cache_dir: Path):

tests/installation/test_executor.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,8 @@ def test_executor_should_not_write_pep610_url_references_for_cached_package(
436436
config: Config,
437437
io: BufferedIO,
438438
):
439-
link_cached = Link(
440-
fixture_dir("distributions")
441-
.joinpath("demo-0.1.0-py2.py3-none-any.whl")
442-
.as_uri()
443-
)
439+
link_cached = fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"
440+
444441
mocker.patch(
445442
"poetry.installation.executor.Executor._download", return_value=link_cached
446443
)
@@ -564,12 +561,8 @@ def test_executor_should_use_cached_link_and_hash(
564561
mocker: MockerFixture,
565562
fixture_dir: FixtureDirGetter,
566563
):
567-
# Produce a file:/// URI that is a valid link
568-
link_cached = Link(
569-
fixture_dir("distributions")
570-
.joinpath("demo-0.1.0-py2.py3-none-any.whl")
571-
.as_uri()
572-
)
564+
link_cached = fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"
565+
573566
mocker.patch(
574567
"poetry.installation.chef.Chef.get_cached_archive_for_link",
575568
return_value=link_cached,

tests/utils/test_pip.py

-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
import pytest
88

9-
from poetry.core.packages.utils.link import Link
10-
from poetry.core.packages.utils.utils import path_to_url
11-
129
from poetry.utils.pip import pip_install
1310

1411

@@ -28,17 +25,6 @@ def test_pip_install_successful(
2825
assert "Successfully installed demo-0.1.0" in result
2926

3027

31-
def test_pip_install_link(
32-
tmp_dir: str, tmp_venv: VirtualEnv, fixture_dir: FixtureDirGetter
33-
):
34-
file_path = Link(
35-
path_to_url(fixture_dir("distributions/demo-0.1.0-py2.py3-none-any.whl"))
36-
)
37-
result = pip_install(file_path, tmp_venv)
38-
39-
assert "Successfully installed demo-0.1.0" in result
40-
41-
4228
def test_pip_install_with_keyboard_interrupt(
4329
tmp_dir: str,
4430
tmp_venv: VirtualEnv,

0 commit comments

Comments
 (0)