Skip to content

Commit

Permalink
Use locally cached wheels during install
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Jun 18, 2022
1 parent 84b7571 commit 9972074
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
11 changes: 3 additions & 8 deletions src/poetry/installation/chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,10 @@ def should_prepare(self, archive: Path) -> bool:
def is_wheel(self, archive: Path) -> bool:
return archive.suffix == ".whl"

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:
Expand All @@ -70,7 +65,7 @@ def get_cached_archive_for_link(self, link: Link) -> Link:
)

if not candidates:
return link
return None

return min(candidates)[1]

Expand Down
4 changes: 2 additions & 2 deletions src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,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)
Expand Down
25 changes: 20 additions & 5 deletions tests/installation/test_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9972074

Please sign in to comment.