From d9eab03d97c88129943964cdb1001a088ec042c1 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 18 Jun 2022 15:19:57 +0100 Subject: [PATCH] Use locally cached wheels during install --- src/poetry/installation/chef.py | 11 +++-------- src/poetry/installation/executor.py | 4 ++-- tests/installation/test_chef.py | 25 ++++++++++++++++++++----- tests/installation/test_executor.py | 2 +- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/poetry/installation/chef.py b/src/poetry/installation/chef.py index dea433142ec..ed41610e871 100644 --- a/src/poetry/installation/chef.py +++ b/src/poetry/installation/chef.py @@ -25,15 +25,10 @@ def __init__(self, config: Config, env: Env) -> None: Path(config.get("cache-dir")).expanduser().joinpath("artifacts") ) - def get_cached_archive_for_link(self, link: Link) -> Link: - # If the archive is already a wheel, there is no need to cache it. - if link.is_wheel: - return link - + def get_cached_archive_for_link(self, link: Link) -> Link | None: archives = self.get_cached_archives_for_link(link) - if not archives: - return link + return None candidates: list[tuple[float | None, Link]] = [] for archive in archives: @@ -54,7 +49,7 @@ def get_cached_archive_for_link(self, link: Link) -> Link: ) if not candidates: - return link + return None return min(candidates)[1] diff --git a/src/poetry/installation/executor.py b/src/poetry/installation/executor.py index 3cf3d11d6fd..03d549282ef 100644 --- a/src/poetry/installation/executor.py +++ b/src/poetry/installation/executor.py @@ -614,9 +614,9 @@ def _download(self, operation: Install | Update) -> Link | Path: def _download_link(self, operation: Install | Update, link: Link) -> Link | Path: package = operation.package - archive: Link | Path + archive: Link | Path | None archive = self._chef.get_cached_archive_for_link(link) - if archive is link: + if archive is None: # No cached distributions was found, so we download and prepare it try: archive = self._download_archive(operation, link) diff --git a/tests/installation/test_chef.py b/tests/installation/test_chef.py index ccd6666f7ee..0822fee1f73 100644 --- a/tests/installation/test_chef.py +++ b/tests/installation/test_chef.py @@ -3,6 +3,8 @@ from pathlib import Path from typing import TYPE_CHECKING +import pytest + from packaging.tags import Tag from poetry.core.packages.utils.link import Link @@ -16,7 +18,22 @@ from tests.conftest import Config -def test_get_cached_archive_for_link(config: Config, mocker: MockerFixture): +@pytest.mark.parametrize( + ("link", "cached"), + [ + ( + "https://files.python-poetry.org/demo-0.1.0.tar.gz", + "file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl", + ), + ( + "https://example.com/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl", + "file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl", + ), + ], +) +def test_get_cached_archive_for_link( + config: Config, mocker: MockerFixture, link: str, cached: str +): chef = Chef( config, MockEnv( @@ -40,11 +57,9 @@ def test_get_cached_archive_for_link(config: Config, mocker: MockerFixture): ], ) - archive = chef.get_cached_archive_for_link( - Link("https://files.python-poetry.org/demo-0.1.0.tar.gz") - ) + archive = chef.get_cached_archive_for_link(Link(link)) - assert Link("file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl") == archive + assert Link(cached) == archive def test_get_cached_archives_for_link(config: Config, mocker: MockerFixture): diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index 9178019963b..1179cf19dc1 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -369,7 +369,7 @@ def test_executor_should_delete_incomplete_downloads( ) mocker.patch( "poetry.installation.chef.Chef.get_cached_archive_for_link", - side_effect=lambda link: link, + side_effect=lambda link: None, ) mocker.patch( "poetry.installation.chef.Chef.get_cache_directory_for_link",