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

Add support for --extra-pip-requirement. #2461

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

jsirois
Copy link
Member

@jsirois jsirois commented Jul 9, 2024

You can now inject extra requirements into the isolated Pip PEXes Pex
uses to resolve distributions. The motivating use case for this is to
use the feature Pip 23.1 introduced for forcing --keyring-provider import.

Pex already supported using a combination of the following to force
non-interactive use of the keyring:

  1. A keyring script installation that was on the PATH
  2. A --pip-version 23.1 or newer.
  3. Specifying --use-pip-config to pass --keyring-provider subprocess
    to Pip.

You could not force --keyring-provider import though since the Pips
Pex uses are themselves hermetic PEXes without access to extra installed
keyring requirements elsewhere on the system. With
--extra-pip-requirement you can do this with the primary benefit over
--keyring-provider subprocess being that you do not need to add the
username to index URLs. This is ultimately because the keyring CLI
requires username whereas the API does not; but see
https://pip.pypa.io/en/stable/topics/authentication/#keyring-support for
more information.

You can now inject extra requirements into the isolated Pip PEXes Pex
uses to resolve distributions. The motivating use case for this is to
use the feature Pip 23.1 introduced for forcing `--keyring-provider
import`.

Pex already supported using a combination of the following to force
non-interactive use of the keyring:
1. A `keyring` script installation that was on the `PATH`
2. A `--pip-version` 23.1 or newer.
3. Specifying `--use-pip-config` to pass `--keyring-provider subprocess`
   to Pip.

You could not force `--keyring-provider import` though since the Pips
Pex uses are themselves hermetic PEXes without access to extra installed
keyring requirements elsewhere on the system. With
`--extra-pip-requirement` you can do this with the primary benefit over
`--keyring-provider subprocess` being that you do not need to add the
username to index URLs. This is ultimately because the keyring CLI
requires username whereas the API does not; but see
https://pip.pypa.io/en/stable/topics/authentication/#keyring-support for
more information.
@@ -119,61 +117,11 @@ def mitmdump_venv(shared_integration_test_tmpdir):


@pytest.fixture
def run_proxy(
Copy link
Member Author

Choose a reason for hiding this comment

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

I factored this out to testing/mitmproxy.py to re-use mitmproxy in the new test in its reverse proxy mode to wrap PyPI / devpi with required auth.

@jsirois jsirois marked this pull request as ready for review July 11, 2024 15:09
Comment on lines +108 to 139
if not resolver:
raise ValueError(
"A resolver is required to install extra {requirements} for vendored Pip: "
"{extra_requirements}".format(
requirements=pluralize(extra_requirements, "requirement"),
extra_requirements=" ".join(map(str, extra_requirements)),
)
)

# This indirection works around MyPy type inference failing to see that
# `iter_distribution_locations` is only successfully defined when resolve is not None.
extra_requirement_resolver = resolver

def iter_distribution_locations():
# type: () -> Iterator[str]
for location in expose_vendored():
yield location

for resolved_distribution in extra_requirement_resolver.resolve_requirements(
requirements=tuple(map(str, extra_requirements)),
targets=Targets.from_target(LocalInterpreter.create(interpreter)),
pip_version=PipVersion.VENDORED,
extra_resolver_requirements=(),
).distributions:
yield resolved_distribution.distribution.location

return _pip_installation(
version=PipVersion.VENDORED,
iter_distribution_locations=lambda: third_party.expose(
("pip", "setuptools"), interpreter=interpreter
),
iter_distribution_locations=iter_distribution_locations,
interpreter=interpreter,
fingerprint=_fingerprint(extra_requirements),
)
Copy link
Member Author

Choose a reason for hiding this comment

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

N.B.: The PR was motivated by Pip keyring but is more generic, allowing extra pip requirements for any reason under the sun that comes up. As such, even though keyring support does not work with vendored Pip, --extra-pip-requirement should work with vendored Pip.

Copy link
Collaborator

@benjyw benjyw left a comment

Choose a reason for hiding this comment

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

Nice!

@jsirois jsirois merged commit 9471d7f into pex-tool:main Jul 11, 2024
26 checks passed
@jsirois jsirois deleted the pip/keyring/full-support branch July 11, 2024 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants