From a1dac02704b097a564433efc429ee9bb17ffabe3 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Thu, 23 Sep 2021 08:12:41 +0800 Subject: [PATCH 1/7] fix: cache URLs on Windows --- poetry/installation/executor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index 79bf4e34a49..00c11136828 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -11,6 +11,7 @@ from poetry.core.packages.file_dependency import FileDependency from poetry.core.packages.utils.link import Link +from poetry.core.packages.utils.utils import url_to_path from poetry.core.pyproject.toml import PyProjectTOML from poetry.io.null_io import NullIO from poetry.utils._compat import PY2 @@ -617,7 +618,7 @@ def _download_link(self, operation, link): hash_type, FileDependency( package.name, - Path(archive.path) + url_to_path(archive.url) if isinstance(archive, Link) else archive, ).hash(hash_type), From fd004a5c7f62b0f6551cfb534fa76b4a884e7789 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 10 Oct 2021 01:20:53 +0800 Subject: [PATCH 2/7] test: Ensure executor accepts links --- tests/installation/test_executor.py | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index c892b92ee3e..2aaf1cedf55 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -327,3 +327,60 @@ def test_executor_should_check_every_possible_hash_types_before_failing( Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl"), ) + + +def test_executor_should_hash_links(config, io, pool, mocker, fixture_dir, tmp_dir): + # Produce a file:/// URI that is a valid link + link = Link( + fixture_dir("distributions") + .joinpath("demo-0.1.0-py2.py3-none-any.whl") + .as_uri() + ) + mocker.patch.object( + Chef, + "get_cached_archive_for_link", + side_effect=lambda _: link, + ) + + env = MockEnv(path=Path(tmp_dir)) + executor = Executor(env, pool, config, io) + + package = Package("demo", "0.1.0") + package.files = [ + { + "file": "demo-0.1.0-py2.py3-none-any.whl", + "hash": "md5:15507846fd4299596661d0197bfb4f90", + } + ] + + archive = executor._download_link( + Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl") + ) + + assert archive == link + + +def test_executor_should_hash_paths(config, io, pool, mocker, fixture_dir, tmp_dir): + link = fixture_dir("distributions").joinpath("demo-0.1.0-py2.py3-none-any.whl") + mocker.patch.object( + Chef, + "get_cached_archive_for_link", + side_effect=lambda _: link, + ) + + env = MockEnv(path=Path(tmp_dir)) + executor = Executor(env, pool, config, io) + + package = Package("demo", "0.1.0") + package.files = [ + { + "file": "demo-0.1.0-py2.py3-none-any.whl", + "hash": "md5:15507846fd4299596661d0197bfb4f90", + } + ] + + archive = executor._download_link( + Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl") + ) + + assert archive == link From fa31f54bf639ca6b9a6f01d71878b0646193a47d Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 10 Oct 2021 02:05:56 +0800 Subject: [PATCH 3/7] fix: Error message --- poetry/installation/executor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index 00c11136828..2cc86e20cc5 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -612,15 +612,14 @@ def _download_link(self, operation, link): hashes = {f["hash"] for f in package.files} hash_types = {h.split(":")[0] for h in hashes} archive_hashes = set() + path = url_to_path(archive.url) if isinstance(archive, Link) else archive for hash_type in hash_types: archive_hashes.add( "{}:{}".format( hash_type, FileDependency( package.name, - url_to_path(archive.url) - if isinstance(archive, Link) - else archive, + path, ).hash(hash_type), ) ) @@ -630,7 +629,7 @@ def _download_link(self, operation, link): "Invalid hashes ({}) for {} using archive {}. Expected one of {}.".format( ", ".join(sorted(archive_hashes)), package, - archive.name, + path.name, ", ".join(sorted(hashes)), ) ) From 755eb6456be59eed97222b12ae81eb1e8e6733ce Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 10 Oct 2021 02:49:16 +0800 Subject: [PATCH 4/7] style: Format code --- poetry/installation/executor.py | 6 +----- tests/installation/test_executor.py | 12 ++---------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index 2cc86e20cc5..caa5fdd13d8 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -616,11 +616,7 @@ def _download_link(self, operation, link): for hash_type in hash_types: archive_hashes.add( "{}:{}".format( - hash_type, - FileDependency( - package.name, - path, - ).hash(hash_type), + hash_type, FileDependency(package.name, path).hash(hash_type), ) ) diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index 2aaf1cedf55..6bef9c44c95 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -336,11 +336,7 @@ def test_executor_should_hash_links(config, io, pool, mocker, fixture_dir, tmp_d .joinpath("demo-0.1.0-py2.py3-none-any.whl") .as_uri() ) - mocker.patch.object( - Chef, - "get_cached_archive_for_link", - side_effect=lambda _: link, - ) + mocker.patch.object(Chef, "get_cached_archive_for_link", side_effect=lambda _: link) env = MockEnv(path=Path(tmp_dir)) executor = Executor(env, pool, config, io) @@ -362,11 +358,7 @@ def test_executor_should_hash_links(config, io, pool, mocker, fixture_dir, tmp_d def test_executor_should_hash_paths(config, io, pool, mocker, fixture_dir, tmp_dir): link = fixture_dir("distributions").joinpath("demo-0.1.0-py2.py3-none-any.whl") - mocker.patch.object( - Chef, - "get_cached_archive_for_link", - side_effect=lambda _: link, - ) + mocker.patch.object(Chef, "get_cached_archive_for_link", side_effect=lambda _: link) env = MockEnv(path=Path(tmp_dir)) executor = Executor(env, pool, config, io) From a91f08a2c8551395072862e1a3641dc735a804ee Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Thu, 21 Oct 2021 08:35:27 +0800 Subject: [PATCH 5/7] fix: Clean up unnecessary tests --- tests/installation/test_executor.py | 32 +++++++---------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index 6bef9c44c95..fb504dbcb8b 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -329,37 +329,19 @@ def test_executor_should_check_every_possible_hash_types_before_failing( ) -def test_executor_should_hash_links(config, io, pool, mocker, fixture_dir, tmp_dir): +def test_executor_should_use_cached_link_and_hash( + config, io, pool, mocker, fixture_dir, tmp_dir +): # Produce a file:/// URI that is a valid link - link = Link( + link_cached = Link( fixture_dir("distributions") .joinpath("demo-0.1.0-py2.py3-none-any.whl") .as_uri() ) - mocker.patch.object(Chef, "get_cached_archive_for_link", side_effect=lambda _: link) - - env = MockEnv(path=Path(tmp_dir)) - executor = Executor(env, pool, config, io) - - package = Package("demo", "0.1.0") - package.files = [ - { - "file": "demo-0.1.0-py2.py3-none-any.whl", - "hash": "md5:15507846fd4299596661d0197bfb4f90", - } - ] - - archive = executor._download_link( - Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl") + mocker.patch.object( + Chef, "get_cached_archive_for_link", side_effect=lambda _: link_cached ) - assert archive == link - - -def test_executor_should_hash_paths(config, io, pool, mocker, fixture_dir, tmp_dir): - link = fixture_dir("distributions").joinpath("demo-0.1.0-py2.py3-none-any.whl") - mocker.patch.object(Chef, "get_cached_archive_for_link", side_effect=lambda _: link) - env = MockEnv(path=Path(tmp_dir)) executor = Executor(env, pool, config, io) @@ -375,4 +357,4 @@ def test_executor_should_hash_paths(config, io, pool, mocker, fixture_dir, tmp_d Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl") ) - assert archive == link + assert archive == link_cached From 51b7598406e63e6ed907fbb8376f8f208330e575 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Tue, 26 Oct 2021 10:51:40 +0800 Subject: [PATCH 6/7] refactor: Rename variable --- poetry/installation/executor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index caa5fdd13d8..1787c87c913 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -612,11 +612,11 @@ def _download_link(self, operation, link): hashes = {f["hash"] for f in package.files} hash_types = {h.split(":")[0] for h in hashes} archive_hashes = set() - path = url_to_path(archive.url) if isinstance(archive, Link) else archive + archive_path = url_to_path(archive.url) if isinstance(archive, Link) else archive for hash_type in hash_types: archive_hashes.add( "{}:{}".format( - hash_type, FileDependency(package.name, path).hash(hash_type), + hash_type, FileDependency(package.name, archive_path).hash(hash_type), ) ) @@ -625,7 +625,7 @@ def _download_link(self, operation, link): "Invalid hashes ({}) for {} using archive {}. Expected one of {}.".format( ", ".join(sorted(archive_hashes)), package, - path.name, + archive_path.name, ", ".join(sorted(hashes)), ) ) From 1c87e5db86314f1c00135532fb882580fdbcf375 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Fri, 12 Nov 2021 23:18:10 +0800 Subject: [PATCH 7/7] style: Reformat code --- poetry/installation/executor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index 1787c87c913..d90895db551 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -612,11 +612,14 @@ def _download_link(self, operation, link): hashes = {f["hash"] for f in package.files} hash_types = {h.split(":")[0] for h in hashes} archive_hashes = set() - archive_path = url_to_path(archive.url) if isinstance(archive, Link) else archive + archive_path = ( + url_to_path(archive.url) if isinstance(archive, Link) else archive + ) for hash_type in hash_types: archive_hashes.add( "{}:{}".format( - hash_type, FileDependency(package.name, archive_path).hash(hash_type), + hash_type, + FileDependency(package.name, archive_path).hash(hash_type), ) )