This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -7,11 +7,9 @@ import os
import sys
from collections.abc import Iterable
from itertools import chain
from typing import Any
from pip._internal.cli.main_parser import create_main_parser
from pip._internal.commands import commands_dict, create_command
from pip._internal.metadata import get_default_environment
def autocomplete() -> None:
@@ -51,6 +49,10 @@ def autocomplete() -> None:
"uninstall",
]
if should_list_installed:
# NOTE: this import is deferred until absolutely necessary as it's slow,
# and it's important that autocompletion is fast.
from pip._internal.metadata import get_default_environment
env = get_default_environment()
lc = current.lower()
installed = [
@@ -130,26 +132,33 @@ def autocomplete() -> None:
def get_path_completion_type(
cwords: list[str], cword: int, opts: Iterable[Any]
cwords: list[str], cword: int, opts: Iterable[optparse.Option]
) -> str | None:
"""Get the type of path completion (``file``, ``dir``, ``path`` or None)
:param cwords: same as the environmental variable ``COMP_WORDS``
:param cword: same as the environmental variable ``COMP_CWORD``
:param cwords: Words from ``COMP_WORDS``, excluding the executable
:param cword: Index from ``COMP_CWORD``, including the executable
:param opts: The available options to check
:return: path completion type (``file``, ``dir``, ``path`` or None)
"""
if cword < 2 or not cwords[cword - 2].startswith("-"):
if cword < 2:
return None
# cwords omits the executable, so the previous word is at cword - 2.
previous_option = cwords[cword - 2].split("=", 1)[0]
if not previous_option.startswith("-"):
return None
for opt in opts:
if opt.help == optparse.SUPPRESS_HELP:
if opt.help == optparse.SUPPRESS_HELP or opt.metavar is None:
continue
for o in str(opt).split("/"):
if cwords[cword - 2].split("=")[0] == o:
if not opt.metavar or any(
x in ("path", "file", "dir") for x in opt.metavar.split("/")
):
return opt.metavar
if previous_option not in opt._long_opts + opt._short_opts:
continue
# Metavars such as 'path/url' include a local path alternative.
if any(part in ("path", "file", "dir") for part in opt.metavar.split("/")):
return opt.metavar
return None
@@ -2,14 +2,15 @@
from __future__ import annotations
import contextlib
import logging
import logging.config
import optparse
import os
import sys
import traceback
from collections.abc import Callable, Iterator
from optparse import Values
from typing import Callable
from pip._vendor.rich import reconfigure
from pip._vendor.rich import traceback as rich_traceback
@@ -18,6 +19,7 @@ from pip._internal.cli import cmdoptions
from pip._internal.cli.command_context import CommandContextMixIn
from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
from pip._internal.cli.status_codes import (
BROKEN_STDOUT,
ERROR,
PREVIOUS_BUILD_DIR_ERROR,
UNKNOWN_ERROR,
@@ -50,6 +52,7 @@ class Command(CommandContextMixIn):
def __init__(self, name: str, summary: str, isolated: bool = False) -> None:
super().__init__()
self._cli = False
self.name = name
self.summary = summary
self.parser = ConfigOptionParser(
@@ -80,7 +83,8 @@ class Command(CommandContextMixIn):
def add_options(self) -> None:
pass
def handle_pip_version_check(self, options: Values) -> None:
@contextlib.contextmanager
def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:
"""
This is a no-op so that commands by default do not do the pip version
check.
@@ -88,16 +92,15 @@ class Command(CommandContextMixIn):
# Make sure we do the pip version check if the index_group options
# are present.
assert not hasattr(options, "no_index")
yield
def run(self, options: Values, args: list[str]) -> int:
raise NotImplementedError
def _run_wrapper(self, level_number: int, options: Values, args: list[str]) -> int:
def _inner_run() -> int:
try:
with self.pip_version_check(options, args):
return self.run(options, args)
finally:
self.handle_pip_version_check(options)
if options.debug_mode:
rich_traceback.install(show_locals=True)
@@ -132,13 +135,28 @@ class Command(CommandContextMixIn):
return ERROR
except BrokenStdoutLoggingError:
# Bypass our logger and write any remaining messages to
# stderr because stdout no longer works.
print("ERROR: Pipe to stdout was broken", file=sys.stderr)
if level_number <= logging.DEBUG:
traceback.print_exc(file=sys.stderr)
# stdout is broken; write to stderr directly. Use os.write, not
# sys.stderr.write, so a full pipe buffer returns EPIPE instead
# of deadlocking (Windows anonymous pipes are ~4KB).
try:
os.write(2, b"ERROR: Pipe to stdout was broken\n")
if level_number <= logging.DEBUG:
encoding = getattr(sys.stderr, "encoding", None) or "utf-8"
os.write(
2, traceback.format_exc().encode(encoding, "backslashreplace")
)
except OSError:
pass
return ERROR
# redirect stdout to os.devnull so that the exit cleanup doesn't
# produce "BrokenPipeError" when exiting.
devnull = os.open(os.devnull, os.O_WRONLY)
try:
os.dup2(devnull, 1)
finally:
os.close(devnull)
return BROKEN_STDOUT
except KeyboardInterrupt:
logger.critical("Operation cancelled by user")
logger.debug("Exception information:", exc_info=True)
@@ -235,17 +253,6 @@ class Command(CommandContextMixIn):
)
options.cache_dir = None
if (
"inprocess-build-deps" in options.features_enabled
and os.environ.get("PIP_CONSTRAINT", "")
and "build-constraint" not in options.features_enabled
):
logger.warning(
"In-process build dependencies are enabled, "
"PIP_CONSTRAINT will have no effect for build dependencies"
)
options.features_enabled.append("build-constraint")
return self._run_wrapper(level_number, options, args)
def handler_map(self) -> dict[str, Callable[[Values, list[str]], None]]:
@@ -14,11 +14,14 @@ from __future__ import annotations
import logging
import os
import pathlib
import re
import textwrap
from collections.abc import Callable
from datetime import datetime, timedelta, timezone
from functools import partial
from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values
from textwrap import dedent
from typing import Any, Callable
from typing import Any
from pip._vendor.packaging.utils import canonicalize_name
@@ -32,6 +35,7 @@ from pip._internal.models.target_python import TargetPython
from pip._internal.utils.datetime import parse_iso_datetime
from pip._internal.utils.hashes import STRONG_HASHES
from pip._internal.utils.misc import strtobool
from pip._internal.utils.urls import strip_extras
logger = logging.getLogger(__name__)
@@ -62,6 +66,37 @@ def make_option_group(group: dict[str, Any], parser: ConfigOptionParser) -> Opti
return option_group
def check_only_deps_option_does_not_conflict(options: Values) -> None:
"""Function for determining if --only-deps and other incompatible options are
specified.
:param options: The OptionParser options.
"""
if not options.only_dependencies:
return
conflicts = []
if options.ignore_dependencies:
conflicts.append("'--no-deps'")
if "legacy-resolver" in options.deprecated_features_enabled:
conflicts.append("'--use-deprecated legacy-resolver'")
if options.requirements:
conflicts.append("'--requirement'")
if options.requirements_from_scripts:
conflicts.append("'--requirements-from-script'")
if options.dependency_groups:
conflicts.append("'--group'")
if conflicts:
if len(conflicts) > 1:
conflicts[-1] = "or " + conflicts[-1]
conflict_message = ", ".join(conflicts)
raise CommandError(
f"Cannot use '--only-dependencies' in combination with {conflict_message}. "
"If this is unexpected, please refer to the user guide:\n"
"\n"
" https://pip.pypa.io/en/stable/user_guide/#installing-only-dependencies"
)
def check_dist_restriction(options: Values, check_target: bool = False) -> None:
"""Function for determining if custom platform options are allowed.
@@ -101,28 +136,21 @@ def check_dist_restriction(options: Values, check_target: bool = False) -> None:
"installing via '--target' or using '--dry-run'"
)
if dist_restriction_set:
# Lazy import to keep CLI startup fast
from pip._internal.utils import pylock as pylock_utils
def check_build_constraints(options: Values) -> None:
"""Function for validating build constraints options.
:param options: The OptionParser options.
"""
if hasattr(options, "build_constraints") and options.build_constraints:
if not options.build_isolation:
raise CommandError(
"--build-constraint cannot be used with --no-build-isolation."
)
# Import here to avoid circular imports
from pip._internal.network.session import PipSession
from pip._internal.req.req_file import get_file_content
# Eagerly check build constraints file contents
# is valid so that we don't fail in when trying
# to check constraints in isolated build process
with PipSession() as session:
for constraint_file in options.build_constraints:
get_file_content(constraint_file, session)
filenames = list(options.requirements)
filenames.extend(path for path, _ in options.dependency_groups)
for filename in filenames:
filename_without_extras, _ = strip_extras(filename)
if pylock_utils.is_valid_pylock_filename(filename_without_extras):
raise CommandError(
"Platform and interpreter constraints using "
"--python-version, --platform, --abi, or --implementation, "
f"are not supported when selecting requirements "
f"from {filename_without_extras!r}"
)
def _path_option_check(option: Option, opt: str, value: str) -> str:
@@ -299,10 +327,19 @@ proxy: Callable[..., Option] = partial(
"--proxy",
dest="proxy",
type="str",
default="",
default=None,
help="Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.",
)
no_proxy_env: Callable[..., Option] = partial(
Option,
"--no-proxy-env",
dest="no_proxy_env",
action="store_true",
default=False,
help="Do not read proxy configuration from environment variables.",
)
retries: Callable[..., Option] = partial(
Option,
"--retries",
@@ -434,8 +471,9 @@ def _handle_uploaded_prior_to(
"""
This is an optparse.Option callback for the --uploaded-prior-to option.
Parses an ISO 8601 datetime string. If no timezone is specified in the string,
local timezone is used.
Accepts either an ISO 8601 datetime string (e.g., '2023-01-01T00:00:00Z')
or a strict subset of ISO 8601 durations: PnD where n is a number of days
(e.g., 'P7D' for 7 days ago).
Note: This option only works with indexes that provide upload-time metadata
as specified in the simple repository API:
@@ -444,6 +482,18 @@ def _handle_uploaded_prior_to(
if value is None:
return None
# Try ISO 8601 duration in PnD format. The leading 'P' disambiguates
# from absolute datetimes. Only whole days are supported; the format may
# be extended to more of the ISO 8601 duration syntax in the future if
# a real need is presented.
match = re.match(r"^P(\d+)D$", value, re.ASCII)
if match:
days = int(match.group(1))
parser.values.uploaded_prior_to = datetime.now(timezone.utc) - timedelta(
days=days
)
return
try:
uploaded_prior_to = parse_iso_datetime(value)
# Use local timezone if no offset is given in the ISO string.
@@ -453,8 +503,9 @@ def _handle_uploaded_prior_to(
except ValueError as exc:
msg = (
f"invalid value: {value!r}: {exc}. "
f"Expected an ISO 8601 datetime string, "
f"e.g '2023-01-01' or '2023-01-01T00:00:00Z'"
f"Expected an ISO 8601 datetime string "
f"(e.g., '2023-01-01' or '2023-01-01T00:00:00Z') "
f"or a duration in days (e.g., 'P3D')"
)
raise_option_error(parser, option=option, msg=msg)
@@ -463,15 +514,18 @@ def uploaded_prior_to() -> Option:
return Option(
"--uploaded-prior-to",
dest="uploaded_prior_to",
metavar="datetime",
metavar="datetime_or_duration",
action="callback",
callback=_handle_uploaded_prior_to,
type="str",
help=(
"Only consider packages uploaded prior to the given date time. "
"Accepts ISO 8601 strings (e.g., '2023-01-01T00:00:00Z'). "
"Uses local timezone if none specified. Only effective when "
"installing from indexes that provide upload-time metadata."
"Only consider packages uploaded prior to the given value. "
"Accepts an ISO 8601 datetime (e.g., '2023-01-01T00:00:00Z', "
"uses local timezone if none specified) "
"or duration in integer days with syntax `P<n>D` "
"(e.g., 'P3D' for packages uploaded at least 3 days ago). "
"Only effective when using indexes that provide "
"upload-time metadata."
),
)
@@ -524,8 +578,15 @@ def requirements() -> Option:
action="append",
default=[],
metavar="file",
help="Install from the given requirements file. "
"This option can be used multiple times.",
help=(
"Install from the given requirements file. "
"The file or URL can be in pip's requirements.txt format, "
"or pylock.toml format. "
"Extras may be selected from a pylock.toml file by "
"appending [extras] to the file name. "
"pylock.toml support is experimental. "
"This option can be used multiple times."
),
)
@@ -536,7 +597,7 @@ def requirements_from_scripts() -> Option:
default=[],
dest="requirements_from_scripts",
metavar="file",
help="Install dependencies of the given script file"
help="Install dependencies of the given script file "
"as defined by PEP 723 inline metadata. ",
)
@@ -615,12 +676,13 @@ def no_binary() -> Option:
callback=_handle_no_binary,
type="str",
default=format_control,
help="Do not use binary packages. Can be supplied multiple times, and "
'each time adds to the existing value. Accepts either ":all:" to '
'disable all binary packages, ":none:" to empty the set (notice '
"the colons), or one or more package names with commas between "
"them (no colons). Note that some packages are tricky to compile "
"and may fail to install when this option is used on them.",
help="Do not download binary packages. Cached binary packages may still "
"be used. Can be supplied multiple times, and each time adds to "
"the existing value. Accepts either ':all:' to disable all binary "
"packages, ':none:' to empty the set (notice the colons), or one "
"or more package names with commas between them (no colons). "
"Note that some packages are tricky to compile and may fail to "
"install when this option is used on them.",
)
@@ -790,15 +852,13 @@ python_version: Callable[..., Option] = partial(
callback=_handle_python_version,
type="str",
default=None,
help=dedent(
"""\
help=dedent("""\
The Python interpreter version to use for wheel and "Requires-Python"
compatibility checks. Defaults to a version derived from the running
interpreter. The version can be specified using up to three dot-separated
integers (e.g. "3" for 3.0.0, "3.7" for 3.7.0, or "3.7.3"). A major-minor
version can also be given as a string without dots (e.g. "37" for 3.7.0).
"""
),
"""),
)
@@ -927,14 +987,67 @@ no_deps: Callable[..., Option] = partial(
)
def _handle_refresh_package(
option: Option, opt_str: str, value: str, parser: OptionParser
) -> None:
if value.startswith("-"):
raise CommandError("--refresh-package option requires 1 argument.")
existing: set[str] = getattr(parser.values, option.dest)
new = value.split(",")
while ":all:" in new:
existing.clear()
existing.add(":all:")
del new[: new.index(":all:") + 1]
if ":none:" not in new:
return
for name in new:
if name == ":none:":
existing.clear()
else:
existing.add(canonicalize_name(name))
def refresh_package() -> Option:
return Option(
"--refresh-package",
dest="refresh_package",
action="callback",
callback=_handle_refresh_package,
type="str",
default=set(),
help="Refresh package index information for the given packages instead "
"of using cached responses. Accepts ':all:' to apply "
"to all packages, or a comma-separated list of package names.",
)
only_deps: Callable[..., Option] = partial(
Option,
"--only-deps",
"--only-dependencies",
dest="only_dependencies",
action="store_true",
default=False,
help=(
"Take only the dependencies of the provided requirements into account, "
"not the requirements themselves. Cannot be used in combination with "
"--no-deps, --group, --requirement, or --requirements-from-script. "
"No user-supplied requirements will be handled, even if they were "
"dependencies of other user-supplied requirements."
),
)
def _handle_dependency_group(
option: Option, opt: str, value: str, parser: OptionParser
) -> None:
"""
Process a value provided for the --group option.
Splits on the rightmost ":", and validates that the path (if present) ends
in `pyproject.toml`. Defaults the path to `pyproject.toml` when one is not given.
Splits on the rightmost ":", and validates an explicit source filename.
`:` cannot appear in dependency group names, so this is a safe and simple parse.
@@ -944,10 +1057,15 @@ def _handle_dependency_group(
if not sep:
path = "pyproject.toml"
else:
# check for 'pyproject.toml' filenames using pathlib
# Local pyproject.toml paths may contain URL-like prefixes.
if pathlib.PurePath(path).name != "pyproject.toml":
msg = "group paths use 'pyproject.toml' filenames"
raise_option_error(parser, option=option, msg=msg)
# Keep lock parsing imports confined to commands that request it.
from pip._internal.utils.pylock import is_valid_pylock_filename
filename, _ = strip_extras(path)
if not is_valid_pylock_filename(filename, allow_urls=False):
msg = "group paths use local 'pyproject.toml' or 'pylock.toml' files"
raise_option_error(parser, option=option, msg=msg)
parser.values.dependency_groups.append((path, groupname))
@@ -961,8 +1079,8 @@ dependency_groups: Callable[..., Option] = partial(
action="callback",
callback=_handle_dependency_group,
metavar="[path:]group",
help='Install a named dependency-group from a "pyproject.toml" file. '
'If a path is given, the name of the file must be "pyproject.toml". '
help='Install a named dependency-group from a "pyproject.toml" or '
'"pylock.toml" file. Named "pylock.<name>.toml" files are also supported. '
'Defaults to using "pyproject.toml" in the current directory.',
)
@@ -1134,6 +1252,17 @@ require_hashes: Callable[..., Option] = partial(
)
no_require_hashes: Callable[..., Option] = partial(
Option,
"--no-require-hashes",
dest="no_require_hashes",
action="store_true",
default=False,
help="Do not automatically enable --require-hashes "
"when encountering a requirement with hashes.",
)
list_path: Callable[..., Option] = partial(
PipOption,
"--path",
@@ -1175,6 +1304,7 @@ no_python_version_warning: Callable[..., Option] = partial(
ALWAYS_ENABLED_FEATURES = [
"truststore", # always on since 24.2
"no-binary-enable-wheel-cache", # always on since 23.1
"build-constraint", # always on since 26.2
]
use_new_feature: Callable[..., Option] = partial(
@@ -1186,8 +1316,8 @@ use_new_feature: Callable[..., Option] = partial(
default=[],
choices=[
"fast-deps",
"build-constraint",
"inprocess-build-deps",
"venv-isolation",
]
+ ALWAYS_ENABLED_FEATURES,
help="Enable new functionality, that may be backward incompatible.",
@@ -1220,12 +1350,12 @@ general_group: dict[str, Any] = {
require_virtualenv,
python,
verbose,
version,
quiet,
log,
no_input,
keyring_provider,
proxy,
no_proxy_env,
retries,
timeout,
exists_action,
@@ -1249,6 +1379,7 @@ index_group: dict[str, Any] = {
index_url,
extra_index_url,
no_index,
refresh_package,
find_links,
uploaded_prior_to,
],
@@ -8,9 +8,10 @@ so commands which don't always hit the network (e.g. list w/o --outdated or
from __future__ import annotations
import contextlib
import logging
import os
import sys
from collections.abc import Iterator
from functools import lru_cache
from optparse import Values
from typing import TYPE_CHECKING
@@ -26,16 +27,13 @@ if TYPE_CHECKING:
from pip._vendor.packaging.utils import NormalizedName
from pip._internal.network.session import PipSession
from pip._internal.self_outdated_check import UpgradePrompt
logger = logging.getLogger(__name__)
@lru_cache
def _create_truststore_ssl_context() -> SSLContext | None:
if sys.version_info < (3, 10):
logger.debug("Disabling truststore because Python version isn't 3.10+")
return None
try:
import ssl
except ImportError:
@@ -102,6 +100,13 @@ class SessionCommandMixin(CommandContextMixIn):
else:
ssl_context = None
has_custom_ca = bool(options.cert)
if options.proxy is None and not options.no_proxy_env:
# Requests reads CA bundle environment variables only with trust_env.
has_custom_ca = has_custom_ca or bool(
os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("CURL_CA_BUNDLE")
)
session = PipSession(
cache=os.path.join(cache_dir, "http-v2") if cache_dir else None,
retries=retries if retries is not None else options.retries,
@@ -109,6 +114,8 @@ class SessionCommandMixin(CommandContextMixIn):
trusted_hosts=options.trusted_hosts,
index_urls=self._get_index_urls(options),
ssl_context=ssl_context,
use_ssl_context_for_proxy=not has_custom_ca,
refresh_package=getattr(options, "refresh_package", set()),
)
# Handle custom ca-bundles from the user
@@ -123,14 +130,18 @@ class SessionCommandMixin(CommandContextMixIn):
if options.timeout or timeout:
session.timeout = timeout if timeout is not None else options.timeout
# Handle configured proxies
if options.proxy:
session.proxies = {
"http": options.proxy,
"https": options.proxy,
}
# An explicit --proxy (including "" to disable proxying) or --no-proxy-env
# makes the session ignore environment proxies; unset --proxy is None.
if options.proxy is not None or options.no_proxy_env:
if options.proxy:
session.proxies = {
"http": options.proxy,
"https": options.proxy,
}
session.trust_env = False
# Keep "" so an empty --proxy reaches build subprocesses.
session.pip_proxy = options.proxy
session.pip_no_proxy_env = options.no_proxy_env
# Determine if we can prompt the user for authentication or not
session.auth.prompting = not options.no_input
@@ -139,10 +150,18 @@ class SessionCommandMixin(CommandContextMixIn):
return session
def _pip_self_version_check(session: PipSession, options: Values) -> None:
from pip._internal.self_outdated_check import pip_self_version_check as check
def _pip_self_version_check_fetch(
session: PipSession, options: Values
) -> UpgradePrompt | None:
from pip._internal.self_outdated_check import pip_self_version_check_fetch
check(session, options)
return pip_self_version_check_fetch(session, options)
def _pip_self_version_check_emit(upgrade_prompt: UpgradePrompt | None) -> None:
from pip._internal.self_outdated_check import pip_self_version_check_emit
pip_self_version_check_emit(upgrade_prompt)
class IndexGroupCommand(Command, SessionCommandMixin):
@@ -169,7 +188,8 @@ class IndexGroupCommand(Command, SessionCommandMixin):
# No specific setting: exclude prereleases by default
return True
def handle_pip_version_check(self, options: Values) -> None:
@contextlib.contextmanager
def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:
"""
Do the pip version check if not disabled.
@@ -179,17 +199,27 @@ class IndexGroupCommand(Command, SessionCommandMixin):
assert hasattr(options, "no_index")
if options.disable_pip_version_check or options.no_index:
yield
return
upgrade_prompt: UpgradePrompt | None = None
try:
# Otherwise, check if we're using the latest version of pip available.
session = self._build_session(
options,
retries=0,
timeout=min(5, options.timeout),
)
with session:
_pip_self_version_check(session, options)
upgrade_prompt = _pip_self_version_check_fetch(session, options)
except Exception:
logger.warning("There was an error checking the latest version of pip.")
logger.debug("See below for error", exc_info=True)
try:
yield
finally:
try:
_pip_self_version_check_emit(upgrade_prompt)
except Exception:
logger.warning("There was an error checking the latest version of pip.")
logger.debug("See below for error", exc_info=True)
@@ -38,7 +38,16 @@ logger = logging.getLogger(__name__)
# main, this should not be an issue in practice.
def cli(args: list[str] | None = None) -> int:
"""Run the CLI with imports blocked until process exit after installation."""
return _main(args, cli=True)
def main(args: list[str] | None = None) -> int:
return _main(args, cli=False)
def _main(args: list[str] | None, *, cli: bool) -> int:
# NOTE: Lazy imports to speed up import of this module,
# which is imported from the pip console script. This doesn't
# speed up normal pip execution, but might be important in the future
@@ -73,13 +82,16 @@ def main(args: list[str] | None = None) -> int:
sys.stderr.write(os.linesep)
sys.exit(1)
# Needed for locale.getpreferredencoding(False) to work
# Needed for locale encoding detection to work
# in pip._internal.utils.encoding.auto_decode
# TODO: Re-evaluate whether this is still needed once pip drops Python 3.10.
try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error as e:
# setlocale can apparently crash if locale are uninitialized
logger.debug("Ignoring error %s when setting locale", e)
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
command._cli = cli
return command.main(cmd_args)
@@ -8,12 +8,11 @@ import sys
from pip._vendor.rich.markup import escape
from pip._internal.build_env import get_runnable_pip
from pip._internal.cli import cmdoptions
from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
from pip._internal.commands import commands_dict, get_similar_commands
from pip._internal.exceptions import CommandError
from pip._internal.utils.misc import get_pip_version, get_prog
from pip._internal.utils.misc import get_pip_version, get_prog, get_runnable_pip
__all__ = ["create_main_parser", "parse_command"]
@@ -31,6 +30,7 @@ def create_main_parser() -> ConfigOptionParser:
parser.disable_interspersed_args()
parser.version = get_pip_version()
parser.add_option(cmdoptions.version())
# add the general options
gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser)
@@ -97,13 +97,23 @@ def parse_command(args: list[str]) -> tuple[str, list[str]]:
# Set a flag so the child doesn't re-invoke itself, causing
# an infinite loop.
os.environ["_PIP_RUNNING_IN_SUBPROCESS"] = "1"
returncode = 0
subprocess_env = os.environ.copy()
subprocess_env["_PIP_RUNNING_IN_SUBPROCESS"] = "1"
try:
proc = subprocess.run(pip_cmd)
returncode = proc.returncode
# Based on CPython's test-runner interrupt cleanup:
# https://github.com/python/cpython/blob/6c695bfad45ddde73d0d674286800fbe295f0627/Lib/test/libregrtest/main.py#L728-L739
with subprocess.Popen(pip_cmd, env=subprocess_env) as proc:
try:
returncode = proc.wait()
except KeyboardInterrupt:
# Reap before warning or traceback rendering reads replaced files.
proc.kill()
proc.wait()
returncode = 1
except (subprocess.SubprocessError, OSError) as exc:
raise CommandError(f"Failed to run pip under {interpreter}: {exc}")
sys.exit(returncode)
# --version
@@ -11,16 +11,18 @@ import sys
import textwrap
from collections.abc import Generator
from contextlib import suppress
from typing import Any, NoReturn
from typing import TYPE_CHECKING, Any, NoReturn
from pip._vendor.rich.markup import escape
from pip._vendor.rich.theme import Theme
from pip._internal.cli.status_codes import UNKNOWN_ERROR
from pip._internal.configuration import Configuration, ConfigurationError
from pip._internal.utils.logging import PipConsole
from pip._internal.utils.misc import redact_auth_from_url, strtobool
if TYPE_CHECKING:
from pip._internal.utils.logging import PipConsole
logger = logging.getLogger(__name__)
@@ -212,7 +214,6 @@ class ConfigOptionParser(CustomOptionParser):
) -> None:
self.name = name
self.config = Configuration(isolated)
assert self.name
super().__init__(*args, **kwargs)
@@ -220,7 +221,14 @@ class ConfigOptionParser(CustomOptionParser):
try:
return option.check_value(key, val)
except optparse.OptionValueError as exc:
print(f"An error occurred during configuration: {exc}")
logger.error(
"An error occurred during configuration: %s. "
"Check your pip configuration files or "
"environment variables for this value. "
"See https://pip.pypa.io/en/stable/topics/configuration/ "
"for more information.",
exc,
)
sys.exit(3)
def _get_ordered_configuration_items(
@@ -344,7 +352,7 @@ class ConfigOptionParser(CustomOptionParser):
self.print_usage(sys.stderr)
self.exit(UNKNOWN_ERROR, f"{msg}\n")
def print_help(self, file: Any = None) -> None:
def _create_console_for_help(self, file: Any = None) -> PipConsole:
# This is unfortunate but necessary since arguments may have not been
# parsed yet at this point, so detect --no-color manually.
no_color = (
@@ -352,7 +360,19 @@ class ConfigOptionParser(CustomOptionParser):
or bool(strtobool(os.environ.get("PIP_NO_COLOR", "no") or "no"))
or "NO_COLOR" in os.environ
)
console = PipConsole(
# Imported lazily: rich's console stack is only needed when help is
# actually rendered, not when this module is imported for `--version`.
from pip._internal.utils.logging import PipConsole
return PipConsole(
theme=Theme(PrettyHelpFormatter.styles), no_color=no_color, file=file
)
def print_usage(self, file: Any = None) -> None:
if self.usage:
console = self._create_console_for_help(file)
console.print(self.get_usage(), highlight=False)
def print_help(self, file: Any = None) -> None:
console = self._create_console_for_help(file)
console.print(self.format_help().rstrip(), highlight=False)
@@ -2,8 +2,8 @@ from __future__ import annotations
import functools
import sys
from collections.abc import Generator, Iterable, Iterator
from typing import TYPE_CHECKING, Callable, Literal, TypeVar
from collections.abc import Callable, Generator, Iterable, Iterator
from typing import TYPE_CHECKING, Literal, TypeVar
from pip._vendor.rich.progress import (
BarColumn,
@@ -9,12 +9,17 @@ from __future__ import annotations
import logging
import os
from collections.abc import Callable
from dataclasses import dataclass, field
from functools import partial
from itertools import chain
from optparse import Values
from typing import Any, Callable, TypeVar
from pathlib import Path
from typing import Any, TypeVar
from pip._internal.build_env import (
BuildEnvironmentInstaller,
BuildIsolationMode,
InprocessBuildEnvironmentInstaller,
SubprocessBuildEnvironmentInstaller,
)
@@ -39,37 +44,98 @@ from pip._internal.req.constructors import (
install_req_from_editable,
install_req_from_line,
install_req_from_parsed_requirement,
install_req_from_pylock_package,
install_req_from_req_string,
)
from pip._internal.req.pep723 import PEP723Exception, pep723_metadata
from pip._internal.req.req_dependency_group import parse_dependency_groups
from pip._internal.req.req_file import parse_requirements
from pip._internal.req.req_file import get_file_content, parse_requirements
from pip._internal.req.req_install import InstallRequirement
from pip._internal.resolution.base import BaseResolver
from pip._internal.utils.packaging import check_requires_python
from pip._internal.utils.pylock import (
is_valid_pylock_filename,
select_from_pylock_path_or_url,
)
from pip._internal.utils.temp_dir import (
TempDirectory,
TempDirectoryKind,
TempDirectoryTypeRegistry,
tempdir_kinds,
)
from pip._internal.utils.urls import strip_extras
logger = logging.getLogger(__name__)
def should_ignore_regular_constraints(options: Values) -> bool:
@dataclass
class _PylockRequest:
"""Accumulate one lock's selectors before evaluating its package markers."""
filename: str
extras: set[str] = field(default_factory=set)
dependency_groups: set[str] = field(default_factory=set)
include_default_groups: bool = False
def _pylock_source_key(filename: str) -> tuple[str, str, str]:
"""Identify a source together with the base used for its relative artifacts."""
if filename.lower().startswith(("http:", "https:", "file:")):
return "url", filename, ""
# Preserve case and parent components because either can change which file opens.
source = str(Path(filename).absolute())
artifact_base = str(Path(os.path.dirname(filename)).absolute())
return "path", source, artifact_base
def _collect_requirement_sources(
requirements: list[str], groups: list[tuple[str, str]]
) -> tuple[list[str | _PylockRequest], list[tuple[str, str]]]:
"""Combine selectors for each lock while retaining separate project inputs."""
pylocks: dict[tuple[str, str, str], _PylockRequest] = {}
requirement_files: list[str | _PylockRequest] = []
project_groups: list[tuple[str, str]] = []
sources = chain(((filename, None) for filename in requirements), groups)
for source, group in sources:
filename, extras = strip_extras(source)
if not is_valid_pylock_filename(filename):
if group is None:
requirement_files.append(source)
else:
project_groups.append((source, group))
continue
key = _pylock_source_key(filename)
if key not in pylocks:
pylocks[key] = _PylockRequest(filename)
requirement_files.append(pylocks[key])
request = pylocks[key]
request.extras.update(extras)
if group is None:
request.include_default_groups = True
else:
request.dependency_groups.add(group)
return requirement_files, project_groups
def should_ignore_regular_constraints() -> bool:
"""
Check if regular constraints should be ignored because
we are in a isolated build process and build constraints
feature is enabled but no build constraints were passed.
Whether this process should ignore the regular constraints it inherits.
The parent pip sets ``_PIP_IN_BUILD_IGNORE_CONSTRAINTS`` whenever it starts
an isolated build in a subprocess, so that regular constraints the build
inherits (such as via ``PIP_CONSTRAINT`` or config files) do not affect it.
Only build constraints apply there.
"""
return os.environ.get("_PIP_IN_BUILD_IGNORE_CONSTRAINTS") == "1"
KEEPABLE_TEMPDIR_TYPES = [
tempdir_kinds.BUILD_ENV,
tempdir_kinds.EPHEM_WHEEL_CACHE,
tempdir_kinds.REQ_BUILD,
TempDirectoryKind.BUILD_ENV,
TempDirectoryKind.EPHEM_WHEEL_CACHE,
TempDirectoryKind.REQ_BUILD,
]
@@ -136,6 +202,20 @@ class RequirementCommand(IndexGroupCommand):
self.cmd_opts.add_option(cmdoptions.dependency_groups())
self.cmd_opts.add_option(cmdoptions.no_clean())
def check_build_constraints(self, options: Values) -> None:
"""Check build constraints with the command's network session."""
if not options.build_constraints:
return
if not options.build_isolation:
raise CommandError(
"--build-constraint cannot be used with --no-build-isolation."
)
session = self.get_default_session(options)
for constraint_file in options.build_constraints:
get_file_content(constraint_file, session)
@staticmethod
def determine_resolver_variant(options: Values) -> str:
"""Determines which resolver should be used, based on the given options."""
@@ -152,6 +232,8 @@ class RequirementCommand(IndexGroupCommand):
build_tracker: BuildTracker,
session: PipSession,
finder: PackageFinder,
*,
allow_editables: bool,
use_user_site: bool,
download_dir: str | None = None,
verbosity: int = 0,
@@ -184,9 +266,6 @@ class RequirementCommand(IndexGroupCommand):
# Handle build constraints
build_constraints = getattr(options, "build_constraints", [])
build_constraint_feature_enabled = (
"build-constraint" in options.features_enabled
)
env_installer: BuildEnvironmentInstaller
if "inprocess-build-deps" in options.features_enabled:
@@ -204,14 +283,26 @@ class RequirementCommand(IndexGroupCommand):
env_installer = SubprocessBuildEnvironmentInstaller(
finder,
build_constraints=build_constraints,
build_constraint_feature_enabled=build_constraint_feature_enabled,
)
if not options.build_isolation:
build_isolation: BuildIsolationMode = "off"
elif "venv-isolation" in options.features_enabled:
build_isolation = "venv"
if "inprocess-build-deps" in options.features_enabled:
logger.warning(
"Using --use-feature 'inprocess-build-deps' and 'venv-isolation'"
" together is currently partially supported. If you encounter"
" broken behaviour, use only one feature."
)
else:
build_isolation = "virtual"
return RequirementPreparer(
build_dir=temp_build_dir_path,
src_dir=options.src_dir,
download_dir=download_dir,
build_isolation=options.build_isolation,
build_isolation=build_isolation,
build_isolation_installer=env_installer,
check_build_deps=options.check_build_deps,
build_tracker=build_tracker,
@@ -223,6 +314,7 @@ class RequirementCommand(IndexGroupCommand):
lazy_wheel=lazy_wheel,
verbosity=verbosity,
legacy_resolver=legacy_resolver,
allow_editables=allow_editables,
)
@classmethod
@@ -260,6 +352,7 @@ class RequirementCommand(IndexGroupCommand):
make_install_req=make_install_req,
use_user_site=use_user_site,
ignore_dependencies=options.ignore_dependencies,
only_dependencies=options.only_dependencies,
ignore_installed=ignore_installed,
ignore_requires_python=ignore_requires_python,
force_reinstall=force_reinstall,
@@ -275,6 +368,7 @@ class RequirementCommand(IndexGroupCommand):
make_install_req=make_install_req,
use_user_site=use_user_site,
ignore_dependencies=options.ignore_dependencies,
only_dependencies=options.only_dependencies,
ignore_installed=ignore_installed,
ignore_requires_python=ignore_requires_python,
force_reinstall=force_reinstall,
@@ -293,12 +387,19 @@ class RequirementCommand(IndexGroupCommand):
Parse command-line arguments into the corresponding requirements.
"""
requirements: list[InstallRequirement] = []
requirement_sources, project_groups = _collect_requirement_sources(
options.requirements, options.dependency_groups
)
if not should_ignore_regular_constraints(options):
constraints = parse_constraint_files(
options.constraints, finder, options, session
)
requirements.extend(constraints)
if should_ignore_regular_constraints():
# Inside an isolated build subprocess: apply the build constraints
# (forwarded via --build-constraint) instead of the inherited ones.
constraint_files = getattr(options, "build_constraints", [])
else:
constraint_files = options.constraints
constraints = parse_constraint_files(constraint_files, finder, options, session)
requirements.extend(constraints)
for req in args:
if not req.strip():
@@ -312,8 +413,8 @@ class RequirementCommand(IndexGroupCommand):
)
requirements.append(req_to_add)
if options.dependency_groups:
for req in parse_dependency_groups(options.dependency_groups):
if project_groups:
for req in parse_dependency_groups(project_groups):
req_to_add = install_req_from_req_string(
req,
isolated=options.isolated_mode,
@@ -331,9 +432,34 @@ class RequirementCommand(IndexGroupCommand):
requirements.append(req_to_add)
# NOTE: options.require_hashes may be set if --require-hashes is True
for filename in options.requirements:
for source in requirement_sources:
if isinstance(source, _PylockRequest):
logger.warning(
"Using pylock.toml as a requirements source "
"is an experimental feature. "
"It may be removed/changed in a future release "
"without prior warning."
)
for package, package_dist in select_from_pylock_path_or_url(
source.filename,
session=session,
format_control=options.format_control,
extras=source.extras,
dependency_groups=source.dependency_groups,
include_default_groups=source.include_default_groups,
):
req_to_add, locked_link = install_req_from_pylock_package(
package,
package_dist,
source.filename,
user_supplied=True,
)
requirements.append(req_to_add)
if locked_link:
finder.add_locked_link(package.name, locked_link)
continue
for parsed_req in parse_requirements(
filename, finder=finder, options=options, session=session
source, finder=finder, options=options, session=session
):
req_to_add = install_req_from_parsed_requirement(
parsed_req,
@@ -379,8 +505,17 @@ class RequirementCommand(IndexGroupCommand):
)
requirements.append(req_to_add)
# If any requirement has hash options, enable hash checking.
if any(req.has_hash_options for req in requirements):
if options.require_hashes and options.no_require_hashes:
raise CommandError(
"--require-hashes and --no-require-hashes are mutually exclusive"
)
# If any requirement has hash options, enable hash checking for all
# requirements, unless this mechanism has been explicitly disabled
# with --no-require-hashes.
if not options.no_require_hashes and any(
req.has_hash_options for req in requirements
):
options.require_hashes = True
if not (
@@ -422,7 +557,7 @@ class RequirementCommand(IndexGroupCommand):
options: Values,
session: PipSession,
target_python: TargetPython | None = None,
ignore_requires_python: bool | None = None,
ignore_requires_python: bool = False,
) -> PackageFinder:
"""
Create a package finder appropriate to this requirement command.
@@ -4,3 +4,4 @@ UNKNOWN_ERROR = 2
VIRTUALENV_NOT_FOUND = 3
PREVIOUS_BUILD_DIR_ERROR = 4
NO_MATCHES_FOUND = 23
BROKEN_STDOUT = 120