From f3f859c6812c8a489ac0701da5ceb52919a922e7 Mon Sep 17 00:00:00 2001 From: Kevin Wojkovich Date: Sat, 8 Apr 2023 09:17:02 -0500 Subject: [PATCH 1/2] allow multiple indexes to be trusted hosts --- src/poetry_plugin_export/exporter.py | 25 ++++--- tests/test_exporter.py | 103 +++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 9 deletions(-) diff --git a/src/poetry_plugin_export/exporter.py b/src/poetry_plugin_export/exporter.py index 2cd3d7b..6343738 100644 --- a/src/poetry_plugin_export/exporter.py +++ b/src/poetry_plugin_export/exporter.py @@ -170,6 +170,7 @@ def _export_generic_txt( if indexes and self._with_urls: # If we have extra indexes, we add them to the beginning of the output indexes_header = "" + trusted_hosts = [] for index in sorted(indexes): repositories = [ r @@ -189,19 +190,25 @@ def _export_generic_txt( else repository.url ) indexes_header += f"--index-url {url}\n" - continue + else: + url = ( + repository.authenticated_url + if self._with_credentials + else repository.url + ) - url = ( - repository.authenticated_url - if self._with_credentials - else repository.url - ) parsed_url = urllib.parse.urlsplit(url) if parsed_url.scheme == "http": - indexes_header += f"--trusted-host {parsed_url.netloc}\n" - indexes_header += f"--extra-index-url {url}\n" + trusted_hosts.append(parsed_url.netloc) + + if repository is not self._poetry.pool.repositories[0]: + indexes_header += f"--extra-index-url {url}\n" + + trusted_cmdopts = "" + for host in trusted_hosts: + trusted_cmdopts += f"--trusted-host {host}\n" - content = indexes_header + "\n" + content + content = trusted_cmdopts + indexes_header + "\n" + content if isinstance(output, IO): output.write(content) diff --git a/tests/test_exporter.py b/tests/test_exporter.py index 5f8d0a1..79776e0 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -1785,6 +1785,109 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_so assert content == expected +def test_exporter_exports_requirements_txt_with_default_trusted_host( + tmp_path: Path, poetry: Poetry +) -> None: + poetry.pool.remove_repository("PyPI") + poetry.config.merge( + { + "repositories": { + "custom-a": {"url": "http://a.example.com/simple"}, + "custom-b": {"url": "http://b.example.com/simple"}, + }, + } + ) + poetry.pool.add_repository( + LegacyRepository( + "custom-b", + "http://b.example.com/simple", + config=poetry.config, + ), + default=True, + ) + poetry.pool.add_repository( + LegacyRepository( + "custom-a", + "http://a.example.com/simple", + config=poetry.config, + ), + secondary=True, + ) + poetry.locker.mock_lock_data( # type: ignore[attr-defined] + { + "package": [ + { + "name": "foo", + "version": "1.2.3", + "optional": False, + "python-versions": "*", + "source": { + "type": "legacy", + "url": "http://a.example.com/simple", + "reference": "", + }, + }, + { + "name": "bar", + "version": "4.5.6", + "optional": False, + "python-versions": "*", + "source": { + "type": "legacy", + "url": "http://b.example.com/simple", + "reference": "", + }, + }, + { + "name": "baz", + "version": "7.8.9", + "optional": False, + "python-versions": "*", + "source": { + "type": "legacy", + "url": "http://b.example.com/simple", + "reference": "", + }, + }, + ], + "metadata": { + "python-versions": "*", + "content-hash": "123456789", + "files": { + "foo": [{"name": "foo.whl", "hash": "12345"}], + "bar": [{"name": "bar.whl", "hash": "67890"}], + "baz": [{"name": "baz.whl", "hash": "24680"}], + }, + }, + } + ) + set_package_requires(poetry, dev={"bar", "baz"}) + + exporter = Exporter(poetry, NullIO()) + exporter.only_groups([MAIN_GROUP, "dev"]) + exporter.with_credentials() + exporter.export("requirements.txt", tmp_path, "requirements.txt") + + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: + content = f.read() + + expected = f"""\ +--trusted-host a.example.com +--trusted-host b.example.com +--extra-index-url http://a.example.com/simple +--index-url http://b.example.com/simple + +bar==4.5.6 ; {MARKER_PY} \\ + --hash=sha256:67890 +baz==7.8.9 ; {MARKER_PY} \\ + --hash=sha256:24680 +foo==1.2.3 ; {MARKER_PY} \\ + --hash=sha256:12345 +""" + + assert content == expected + + def test_exporter_exports_requirements_txt_with_default_and_secondary_sources( tmp_path: Path, poetry: Poetry ) -> None: From cf164b1603abef643c48db52331df26b5ca1c252 Mon Sep 17 00:00:00 2001 From: Kevin Wojkovich Date: Sun, 9 Apr 2023 09:09:45 -0500 Subject: [PATCH 2/2] low quality check --- src/poetry_plugin_export/exporter.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/poetry_plugin_export/exporter.py b/src/poetry_plugin_export/exporter.py index 6343738..6577bd7 100644 --- a/src/poetry_plugin_export/exporter.py +++ b/src/poetry_plugin_export/exporter.py @@ -199,7 +199,12 @@ def _export_generic_txt( parsed_url = urllib.parse.urlsplit(url) if parsed_url.scheme == "http": - trusted_hosts.append(parsed_url.netloc) + netloc = ( + parsed_url.netloc.split("@")[1] + if "@" in parsed_url + else parsed_url.netloc + ) + trusted_hosts.append(netloc) if repository is not self._poetry.pool.repositories[0]: indexes_header += f"--extra-index-url {url}\n"