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

feat: use pip sources config from poetry #887

Open
wants to merge 5 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
41 changes: 39 additions & 2 deletions src/nox_poetry/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Iterator
from typing import Optional
from typing import Tuple
from urllib.parse import urlparse

import nox
from packaging.requirements import InvalidRequirement
Expand Down Expand Up @@ -90,6 +91,38 @@ def _to_constraints() -> Iterator[str]:
return "\n".join(_to_constraints())


def to_global_pip_args(requirements: str) -> Tuple[Optional[str]]:
"""Convert the requirements to global pip arguments."""
index_args = set()
trusted_hosts = set()
for requirement in requirements.splitlines():
for re_pattern, kwarg_pattern in [
(re.compile(r"^(-i|--index-url)[ =]+(?P<url>.*)$"), "--index-url={url}"),
(
re.compile(r"^--extra-index-url[ =]+(?P<url>.*)$"),
"--extra-index-url={url}",
),
]:

match = re_pattern.match(requirement.strip())
if match:
index_args.add(kwarg_pattern.format(**match.groupdict()))

# if there is a url in the captured pattern, ensure the domain gets
# added as a trusted host
try:
trusted_hosts.add(urlparse(match.group("url")).netloc)
except IndexError:
pass

# add the trusted hosts
index_args = tuple(sorted(index_args))
for trusted_host in sorted(trusted_hosts):
index_args += (f"--trusted-host={trusted_host}",)

return index_args


class _PoetrySession:
"""Poetry-related utilities for session functions."""

Expand Down Expand Up @@ -148,6 +181,8 @@ def rewrite(arg: str, extras: Optional[str]) -> str:
except CommandSkippedError:
return

# args += to_global_pip_args(requirements)

Comment on lines +184 to +185
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I had the config being injected before. I'll update this based on your opinion of how to best proceed.

self.session.install(f"--constraint={requirements}", *args, **kwargs)

def installroot(
Expand Down Expand Up @@ -224,8 +259,10 @@ def export_requirements(self) -> Path:
digest = hashlib.blake2b(lockdata).hexdigest()

if not hashfile.is_file() or hashfile.read_text() != digest:
constraints = to_constraints(self.poetry.export())
path.write_text(constraints)
requirements_text = self.poetry.export()
constraints = to_constraints(requirements_text)
pip_args = "\n".join(to_global_pip_args(requirements_text))
path.write_text("\n".join([pip_args, constraints]))
Comment on lines +262 to +265
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this data should be here or sent as arguments in the install command. Thoughts?

hashfile.write_text(digest)

return path
Expand Down
69 changes: 46 additions & 23 deletions tests/functional/data/example/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/functional/data/example/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ pygments = ["pygments"]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[[tool.poetry.source]]
name = "pypi-all-source1"
url = "https://pypi.python.org/simple"
default = true

[[tool.poetry.source]]
name = "pypi-all-source2"
url = "https://pypi.python.org/all"
Comment on lines +27 to +30
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poetry export is not actually including this as an extra index, but that seems like an upstream issue.