Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple indexes to be trusted hosts #194

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/poetry_plugin_export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -189,19 +190,30 @@ 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"
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"

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)
Expand Down
103 changes: 103 additions & 0 deletions tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down