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

@@ -5,12 +5,17 @@ Package containing all pip commands
from __future__ import annotations
import importlib
from collections import namedtuple
from typing import Any
from typing import TYPE_CHECKING, Any, NamedTuple
from pip._internal.cli.base_command import Command
if TYPE_CHECKING:
from pip._internal.cli.base_command import Command
class CommandInfo(NamedTuple):
module_path: str
class_name: str
summary: str
CommandInfo = namedtuple("CommandInfo", "module_path, class_name, summary")
# This dictionary does a bunch of heavy lifting for help output:
# - Enables avoiding additional (costly) imports for presenting `--help`.
@@ -119,7 +124,9 @@ def create_command(name: str, **kwargs: Any) -> Command:
"""
module_path, class_name, summary = commands_dict[name]
module = importlib.import_module(module_path)
command_class = getattr(module, class_name)
# Registry entries identify Command subclasses by module and class name.
command_class: type[Command] = getattr(module, class_name)
command = command_class(name=name, summary=summary, **kwargs)
return command
@@ -1,7 +1,7 @@
import os
import textwrap
from collections.abc import Callable
from optparse import Values
from typing import Callable
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
@@ -107,8 +107,7 @@ class CacheCommand(Command):
wheels_cache_size = filesystem.format_directory_size(wheels_cache_location)
message = (
textwrap.dedent(
"""
textwrap.dedent("""
Package index page cache location (pip v23.3+): {http_cache_location}
Package index page cache location (older pips): {old_http_cache_location}
Package index page cache size: {http_cache_size}
@@ -116,8 +115,7 @@ class CacheCommand(Command):
Locally built wheels location: {wheels_cache_location}
Locally built wheels size: {wheels_cache_size}
Number of locally built wheels: {package_count}
""" # noqa: E501
)
""") # noqa: E501
.format(
http_cache_location=http_cache_location,
old_http_cache_location=old_http_cache_location,
@@ -191,10 +189,13 @@ class CacheCommand(Command):
logger.verbose("Removed %s", filename)
http_dirs = filesystem.subdirs_without_files(self._cache_dir(options, "http"))
http_v2_dirs = filesystem.subdirs_without_files(
self._cache_dir(options, "http-v2")
)
wheel_dirs = filesystem.subdirs_without_wheels(
self._cache_dir(options, "wheels")
)
dirs = [*http_dirs, *wheel_dirs]
dirs = [*http_dirs, *http_v2_dirs, *wheel_dirs]
for subdir in dirs:
try:
@@ -1,10 +1,11 @@
from __future__ import annotations
import inspect
import logging
import os
import subprocess
from collections.abc import Callable
from optparse import Values
from typing import Any, Callable
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
@@ -12,7 +13,6 @@ from pip._internal.configuration import (
Configuration,
Kind,
get_configuration_files,
kinds,
)
from pip._internal.exceptions import PipError
from pip._internal.utils.logging import indent_log
@@ -21,6 +21,30 @@ from pip._internal.utils.misc import get_prog, write_output
logger = logging.getLogger(__name__)
def _config_handler(
name: str, handler: Callable[..., None]
) -> Callable[[Values, list[str]], None]:
"""Adapt a configuration action's positional parameters to CLI arguments."""
signature = inspect.signature(handler)
def invoke(options: Values, args: list[str]) -> None:
try:
signature.bind(options, *args)
except TypeError:
# Skip options; these action handlers are bound methods.
arguments = list(signature.parameters)[1:]
example = " ".join([name, *(f"[{argument}]" for argument in arguments)])
raise PipError(
f"Got unexpected number of arguments, expected {len(arguments)}. "
f'(example: "{get_prog()} config {example}")'
) from None
# TypeErrors raised by the action itself are programming errors.
handler(options, *args)
return invoke
class ConfigurationCommand(Command):
"""
Manage local and global configuration.
@@ -96,7 +120,7 @@ class ConfigurationCommand(Command):
self.parser.insert_option_group(0, self.cmd_opts)
def handler_map(self) -> dict[str, Callable[[Values, list[str]], None]]:
return {
handlers: dict[str, Callable[..., None]] = {
"list": self.list_values,
"edit": self.open_in_editor,
"get": self.get_name,
@@ -105,6 +129,10 @@ class ConfigurationCommand(Command):
"debug": self.list_config_values,
}
return {
name: _config_handler(name, handler) for name, handler in handlers.items()
}
def run(self, options: Values, args: list[str]) -> int:
handler_map = self.handler_map()
@@ -147,9 +175,9 @@ class ConfigurationCommand(Command):
file_options = [
key
for key, value in (
(kinds.USER, options.user_file),
(kinds.GLOBAL, options.global_file),
(kinds.SITE, options.site_file),
(Kind.USER, options.user_file),
(Kind.GLOBAL, options.global_file),
(Kind.SITE, options.site_file),
)
if value
]
@@ -160,11 +188,11 @@ class ConfigurationCommand(Command):
# Default to user, unless there's a site file.
elif any(
os.path.exists(site_config_file)
for site_config_file in get_configuration_files()[kinds.SITE]
for site_config_file in get_configuration_files()[Kind.SITE]
):
return kinds.SITE
return Kind.SITE
else:
return kinds.USER
return Kind.USER
elif len(file_options) == 1:
return file_options[0]
@@ -173,34 +201,28 @@ class ConfigurationCommand(Command):
"(--user, --site, --global) to perform."
)
def list_values(self, options: Values, args: list[str]) -> None:
self._get_n_args(args, "list", n=0)
def list_values(self, options: Values) -> None:
for key, value in sorted(self.configuration.items()):
for key, value in sorted(value.items()):
write_output("%s=%r", key, value)
def get_name(self, options: Values, args: list[str]) -> None:
key = self._get_n_args(args, "get [name]", n=1)
value = self.configuration.get_value(key)
def get_name(self, options: Values, name: str) -> None:
value = self.configuration.get_value(name)
write_output("%s", value)
def set_name_value(self, options: Values, args: list[str]) -> None:
key, value = self._get_n_args(args, "set [name] [value]", n=2)
self.configuration.set_value(key, value)
def set_name_value(self, options: Values, name: str, value: str) -> None:
self.configuration.set_value(name, value)
self._save_configuration()
def unset_name(self, options: Values, args: list[str]) -> None:
key = self._get_n_args(args, "unset [name]", n=1)
self.configuration.unset_value(key)
def unset_name(self, options: Values, name: str) -> None:
self.configuration.unset_value(name)
self._save_configuration()
def list_config_values(self, options: Values, args: list[str]) -> None:
def list_config_values(self, options: Values) -> None:
"""List config key-value pairs across different config files"""
self._get_n_args(args, "debug", n=0)
self.print_env_var_values()
# Iterate over config files and print if they exist, and the
@@ -230,7 +252,7 @@ class ConfigurationCommand(Command):
env_var = f"PIP_{key.upper()}"
write_output("%s=%r", env_var, value)
def open_in_editor(self, options: Values, args: list[str]) -> None:
def open_in_editor(self, options: Values) -> None:
editor = self._determine_editor(options)
fname = self.configuration.get_file_to_edit()
@@ -252,20 +274,6 @@ class ConfigurationCommand(Command):
except subprocess.CalledProcessError as e:
raise PipError(f"Editor Subprocess exited with exit code {e.returncode}")
def _get_n_args(self, args: list[str], example: str, n: int) -> Any:
"""Helper to make sure the command got the right number of arguments"""
if len(args) != n:
msg = (
f"Got unexpected number of arguments, expected {n}. "
f'(example: "{get_prog()} config {example}")'
)
raise PipError(msg)
if n == 1:
return args[0]
else:
return args
def _save_configuration(self) -> None:
# We successfully ran a modifying command. Need to save the
# configuration.
@@ -1,6 +1,6 @@
from __future__ import annotations
import locale
import importlib
import logging
import os
import sys
@@ -18,7 +18,7 @@ from pip._internal.cli.cmdoptions import make_target_python
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.configuration import Configuration
from pip._internal.metadata import get_environment
from pip._internal.utils.compat import open_text_resource
from pip._internal.utils.compat import get_locale_encoding, open_text_resource
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import get_pip_version
@@ -48,29 +48,21 @@ def create_vendor_txt_map() -> dict[str, str]:
return dict(line.split("==", 1) for line in lines)
def get_module_from_module_name(module_name: str) -> ModuleType | None:
def get_module_from_module_name(module_name: str) -> ModuleType:
# Module name can be uppercase in vendor.txt for some reason...
module_name = module_name.lower().replace("-", "_")
# PATCH: setuptools is actually only pkg_resources.
if module_name == "setuptools":
module_name = "pkg_resources"
try:
__import__(f"pip._vendor.{module_name}", globals(), locals(), level=0)
return getattr(pip._vendor, module_name)
except ImportError:
# We allow 'truststore' to fail to import due
# to being unavailable on Python 3.9 and earlier.
if module_name == "truststore" and sys.version_info < (3, 10):
return None
raise
return importlib.import_module(f"pip._vendor.{module_name}")
def get_vendor_version_from_module(module_name: str) -> str | None:
module = get_module_from_module_name(module_name)
version = getattr(module, "__version__", None)
if module and not version:
if not version:
# Try to find version in debundled module info.
assert module.__file__ is not None
env = get_environment([os.path.dirname(module.__file__)])
@@ -141,7 +133,7 @@ def show_tags(options: Values) -> None:
def ca_bundle_info(config: Configuration) -> str:
levels = {key.split(".", 1)[0] for key, _ in config.items()}
levels = {key.split(".", 1)[0] for _, options in config.items() for key in options}
if not levels:
return "Not specified"
@@ -184,8 +176,8 @@ class DebugCommand(Command):
show_value("sys.getdefaultencoding", sys.getdefaultencoding())
show_value("sys.getfilesystemencoding", sys.getfilesystemencoding())
show_value(
"locale.getpreferredencoding",
locale.getpreferredencoding(),
"locale.getencoding",
get_locale_encoding(),
)
show_value("sys.platform", sys.platform)
show_sys_implementation()
@@ -39,8 +39,10 @@ class DownloadCommand(RequirementCommand):
self.cmd_opts.add_option(cmdoptions.requirements())
self.cmd_opts.add_option(cmdoptions.requirements_from_scripts())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.only_deps())
self.cmd_opts.add_option(cmdoptions.src())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.no_require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.no_build_isolation())
self.cmd_opts.add_option(cmdoptions.use_pep517())
@@ -82,8 +84,9 @@ class DownloadCommand(RequirementCommand):
options.editables = []
cmdoptions.check_dist_restriction(options)
cmdoptions.check_build_constraints(options)
self.check_build_constraints(options)
cmdoptions.check_release_control_exclusive(options)
cmdoptions.check_only_deps_option_does_not_conflict(options)
options.download_dir = normalize_path(options.download_dir)
ensure_dir(options.download_dir)
@@ -117,6 +120,7 @@ class DownloadCommand(RequirementCommand):
download_dir=options.download_dir,
use_user_site=False,
verbosity=self.verbosity,
allow_editables=False,
)
resolver = self.make_resolver(
@@ -4,8 +4,8 @@ from optparse import Values
from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.metadata.base import stdlib_pkgs
from pip._internal.operations.freeze import freeze
from pip._internal.utils.compat import stdlib_pkgs
def _should_suppress_build_backends() -> bool:
@@ -2,9 +2,9 @@ from __future__ import annotations
import json
import logging
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from optparse import Values
from typing import Any, Callable
from typing import Any
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version
@@ -16,7 +16,12 @@ from pip._internal.commands.search import (
get_installed_distribution,
print_dist_installation_info,
)
from pip._internal.exceptions import CommandError, DistributionNotFound, PipError
from pip._internal.exceptions import (
CommandError,
DiagnosticPipError,
DistributionNotFound,
PipError,
)
from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.selection_prefs import SelectionPreferences
@@ -80,6 +85,8 @@ class IndexCommand(IndexGroupCommand):
# Error handling happens here, not in the action-handlers.
try:
handler_map[action](options, args[1:])
except DiagnosticPipError:
raise
except PipError as e:
logger.error(e.args[0])
return ERROR
@@ -91,7 +98,7 @@ class IndexCommand(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 the index command.
@@ -10,7 +10,7 @@ from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.metadata import BaseDistribution, get_environment
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.metadata.base import stdlib_pkgs
from pip._internal.utils.urls import path_to_url
logger = logging.getLogger(__name__)
@@ -71,7 +71,7 @@ class InspectCommand(Command):
# report) since it is not recorded in installed metadata.
direct_url = dist.direct_url
if direct_url is not None:
res["direct_url"] = direct_url.to_dict()
res["direct_url"] = direct_url.to_dict_compat()
else:
# Emulate direct_url for legacy editable installs.
editable_project_location = dist.editable_project_location
@@ -1,17 +1,29 @@
from __future__ import annotations
import contextlib
import errno
import functools
import inspect
import json
import operator
import os
import shutil
import site
import sys
import traceback
import types
from collections.abc import Iterable, Iterator, Sequence
from optparse import SUPPRESS_HELP, Values
from pathlib import Path
from typing import Any
from pip._vendor import pygments, rich
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.requests.exceptions import InvalidProxyURL
from pip._vendor.rich import print_json
from pip._vendor.rich.text import Text
import pip
# Eagerly import self_outdated_check to avoid crashes. Otherwise,
# this module would be imported *after* pip was replaced, resulting
@@ -19,7 +31,7 @@ from pip._vendor.rich import print_json
# with the rest of pip that's already imported, or allowing a
# wheel to execute arbitrary code on install by replacing
# self_outdated_check.
import pip._internal.self_outdated_check # noqa: F401
import pip._internal.self_outdated_check
from pip._internal.cache import WheelCache
from pip._internal.cli import cmdoptions
from pip._internal.cli.cmdoptions import make_target_python
@@ -29,6 +41,7 @@ from pip._internal.cli.req_command import (
)
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.exceptions import (
BlockedImportError,
CommandError,
InstallationError,
InstallWheelBuildError,
@@ -63,6 +76,242 @@ from pip._internal.wheel_builder import build
logger = getLogger(__name__)
_IMPORT_AUDIT_HOOK_INSTALLED = False
_ACTIVE_INSTALLATIONS: set[object] = set()
_WARNED_IMPORTS: set[str] = set()
_IMPORT_ERRORS: dict[str, ImportError] = {}
# Import dependencies directly where possible; otherwise list them here.
_INTERNAL_EAGER_IMPORTS: tuple[str, ...] = (
"pip._internal.operations.install.wheel",
# Rich loads the Python lexer when rendering debug tracebacks.
f"{pygments.__name__}.lexers.python",
# Preload Python 3.13+ traceback helpers to preserve the original error.
"traceback",
"_colorize",
"_suggestions",
"ast",
"difflib",
"unicodedata",
# Preserve cmd's fallback when readline is unavailable.
"readline",
# weakref.finalize imports gc when running shutdown callbacks.
"gc",
# Requests imports netrc lazily when looking up credentials.
"netrc",
# Use Rich's actual namespace when pip has been de-vendored.
# The renderer is used for output to a legacy Windows console.
f"{rich.__name__}._windows_renderer",
# zipfile may import these backends when reading compressed wheel contents.
"_bz2",
"_lzma",
"_zstd",
)
def _warn_unsafe_import(module: str) -> None:
"""Warn once per module without letting logging block the import."""
if module in _WARNED_IMPORTS:
return
# Mark before logging: a handler may itself import this module.
_WARNED_IMPORTS.add(module)
try:
logger.warning(
"Unsafe import of %r after pip install completed: packages may have "
"changed. Callers should import all required modules before running "
"pip install, or run pip in a subprocess. "
"See https://pip.pypa.io/en/stable/user_guide/"
"#using-pip-from-your-program",
module,
)
except Exception:
# The caller's logging handlers must not turn warnings into import errors.
pass
def _prevent_import_hook(name: str, args: tuple[Any, ...]) -> None:
if name == "import":
_check_import(args[0])
def _check_import(module: str) -> None:
"""Apply the installation import policy to audit events and module lookups."""
if not _ACTIVE_INSTALLATIONS:
_warn_unsafe_import(module)
return
if module in _IMPORT_ERRORS:
raise _IMPORT_ERRORS[module].with_traceback(None)
raise BlockedImportError(
f"Blocked import of {module!r} after pip install started. Hook authors"
" must import required modules before installation starts."
)
class _ImportBlocker:
"""Cover module lookups, including reloads, without import audit events."""
def find_spec(
self,
fullname: str,
path: Sequence[str] | None = None,
target: types.ModuleType | None = None,
) -> None:
_check_import(fullname)
_IMPORT_BLOCKER = _ImportBlocker()
def _eagerly_import_modules(module_names: Iterable[str]) -> None:
"""Import selected dependencies before installation starts."""
# Load Rich's lazy emoji and width data before installation starts.
_ = Text.from_markup(":package:").cell_len
_IMPORT_ERRORS.clear()
for module in dict.fromkeys(module_names):
try:
__import__(module)
except ImportError as error:
_IMPORT_ERRORS[module] = error.with_traceback(None)
def _referenced_modules(module: types.ModuleType) -> Iterator[types.ModuleType]:
"""Find dependencies referenced by modules and callables."""
for value in tuple(vars(module).values()):
if isinstance(value, types.ModuleType):
yield value
continue
# Standard callable adapters keep dependency globals on their function.
while True:
if type(value) is functools.partial:
value = value.func
elif type(value) is types.MethodType:
value = value.__func__
else:
break
names: tuple[object, ...]
if type(value) is types.FunctionType:
# Decorators can preserve the original module while changing globals.
names = (value.__globals__.get("__name__"), value.__module__)
elif callable(value):
# Read callable metadata without invoking descriptors.
names = (inspect.getattr_static(value, "__module__", None),)
else:
continue
for name in names:
dependency = sys.modules.get(name) if isinstance(name, str) else None
if isinstance(dependency, types.ModuleType):
yield dependency
def _cache_lazy_import_error(error: ImportError) -> None:
"""Preserve failures for both a missing dependency and its failed importers."""
# Import machinery removes failed modules before propagating their error.
for frame, _ in traceback.walk_tb(error.__traceback__):
module_name = frame.f_globals.get("__name__")
if isinstance(module_name, str) and module_name not in sys.modules:
_IMPORT_ERRORS[module_name] = error
if error.name is not None:
_IMPORT_ERRORS[error.name] = error
# Avoid retaining the import traceback in the cache.
error.with_traceback(None)
def _reify_lazy_imports(module_names: Iterable[str]) -> None:
"""Resolve lazy bindings in installation dependencies before imports are blocked."""
if sys.version_info < (3, 15):
return
modules = {
name: module
for name in module_names
if isinstance(module := sys.modules.get(name), types.ModuleType)
}
attempted: set[object] = set()
while True:
# A from-import can already hold an object from a lazy dependency.
dependencies = {
dependency.__name__: dependency
for module in modules.values()
for dependency in _referenced_modules(module)
if dependency.__name__ not in modules
}
modules.update(dependencies)
# Resolving a binding can add lazy imports to modules already scanned.
pending = [
(module, name, value)
for module in modules.values()
for name, value in tuple(vars(module).items())
if type(value) is types.LazyImportType and value not in attempted
]
if not pending and not dependencies:
break
for module, name, value in pending:
attempted.add(value)
before = set(sys.modules)
try:
resolved = getattr(module, name)
# The binding may resolve to an already-loaded module.
if isinstance(resolved, types.ModuleType):
modules[resolved.__name__] = resolved
except ImportError as error:
_cache_lazy_import_error(error)
except Exception:
# Optional lazy imports need not succeed for installation to proceed.
pass
# Include dependencies loaded indirectly, even by a failed import.
for dependency in sys.modules.keys() - before:
imported = sys.modules[dependency]
if isinstance(imported, types.ModuleType):
modules[dependency] = imported
@contextlib.contextmanager
def _prevent_further_imports(*, until_process_exit: bool = False) -> Iterator[None]:
"""Block late imports until process exit or control returns to a caller."""
global _IMPORT_AUDIT_HOOK_INSTALLED
if not _IMPORT_AUDIT_HOOK_INSTALLED:
sys.addaudithook(_prevent_import_hook)
_IMPORT_AUDIT_HOOK_INSTALLED = True
# Callers may add finders between installations; reloads bypass audit events.
sys.meta_path[:] = [
_IMPORT_BLOCKER,
*(finder for finder in sys.meta_path if finder is not _IMPORT_BLOCKER),
]
token = object()
_ACTIVE_INSTALLATIONS.add(token)
try:
yield
finally:
# CLI exception hooks and exit callbacks run after command cleanup.
if not until_process_exit:
_ACTIVE_INSTALLATIONS.discard(token)
def _arg_refers_to_pip(arg: str) -> bool:
try:
req = Requirement(arg)
except InvalidRequirement:
return False
return canonicalize_name(req.name) == "pip"
class InstallCommand(RequirementCommand):
"""
Install packages from:
@@ -83,12 +332,18 @@ class InstallCommand(RequirementCommand):
%prog [options] [-e] <local project path> ...
%prog [options] <archive url/path> ..."""
def main(self, args: list[str]) -> int:
# Keep imports blocked through command cleanup and logging shutdown.
with contextlib.ExitStack() as self._import_guard:
return super().main(args)
def add_options(self) -> None:
self.cmd_opts.add_option(cmdoptions.requirements())
self.cmd_opts.add_option(cmdoptions.constraints())
self.cmd_opts.add_option(cmdoptions.build_constraints())
self.cmd_opts.add_option(cmdoptions.requirements_from_scripts())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.only_deps())
self.cmd_opts.add_option(cmdoptions.editable())
self.cmd_opts.add_option(
@@ -245,6 +500,7 @@ class InstallCommand(RequirementCommand):
help="Do not warn about broken dependencies",
)
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.no_require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.root_user_action())
@@ -278,6 +534,17 @@ class InstallCommand(RequirementCommand):
),
)
@contextlib.contextmanager
def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:
# Skip the self-version check when pip itself is a requirement. The
# running pip may be replaced mid-command, and the upgrade prompt
# is redundant.
if any(_arg_refers_to_pip(arg) for arg in args):
yield
return
with super().pip_version_check(options, args):
yield
@with_cleanup
def run(self, options: Values, args: list[str]) -> int:
if options.use_user_site and options.target_dir is not None:
@@ -304,7 +571,8 @@ class InstallCommand(RequirementCommand):
if options.upgrade:
upgrade_strategy = options.upgrade_strategy
cmdoptions.check_build_constraints(options)
cmdoptions.check_only_deps_option_does_not_conflict(options)
self.check_build_constraints(options)
cmdoptions.check_dist_restriction(options, check_target=True)
cmdoptions.check_release_control_exclusive(options)
@@ -359,12 +627,6 @@ class InstallCommand(RequirementCommand):
wheel_cache = WheelCache(options.cache_dir)
# Only when installing is it permitted to use PEP 660.
# In other circumstances (pip wheel, pip download) we generate
# regular (i.e. non editable) metadata and wheels.
for req in reqs:
req.permit_editable_wheels = True
preparer = self.make_requirement_preparer(
temp_build_dir=directory,
options=options,
@@ -373,6 +635,7 @@ class InstallCommand(RequirementCommand):
finder=finder,
use_user_site=options.use_user_site,
verbosity=self.verbosity,
allow_editables=True,
)
resolver = self.make_resolver(
preparer=preparer,
@@ -396,7 +659,7 @@ class InstallCommand(RequirementCommand):
if options.json_report_file:
report = InstallationReport(requirement_set.requirements_to_install)
if options.json_report_file == "-":
print_json(data=report.to_dict())
rich.print_json(data=report.to_dict())
else:
with open(options.json_report_file, "w", encoding="utf-8") as f:
json.dump(report.to_dict(), f, indent=2, ensure_ascii=False)
@@ -438,6 +701,7 @@ class InstallCommand(RequirementCommand):
reqs_to_build,
wheel_cache=wheel_cache,
verify=True,
allow_editables=True,
)
if build_failures:
@@ -459,6 +723,15 @@ class InstallCommand(RequirementCommand):
if options.target_dir or options.prefix_path:
warn_script_location = False
# Block late imports so we don't silently pick up a module
# from a distribution pip is about to install.
module_names = (*_INTERNAL_EAGER_IMPORTS, *pip._EAGER_IMPORTS)
_eagerly_import_modules(module_names)
_reify_lazy_imports(module_names)
self._import_guard.enter_context(
_prevent_further_imports(until_process_exit=self._cli)
)
installed = install_given_reqs(
to_install,
root=options.root_path,
@@ -503,8 +776,10 @@ class InstallCommand(RequirementCommand):
self._handle_target_dir(
options.target_dir, target_temp_dir, options.upgrade
)
if options.root_user_action == "warn":
warn_if_run_as_root()
return SUCCESS
def _handle_target_dir(
@@ -512,8 +787,6 @@ class InstallCommand(RequirementCommand):
) -> None:
ensure_dir(target_dir)
# Checking both purelib and platlib directories for installed
# packages to be moved to target directory
lib_dir_list = []
# Checking both purelib and platlib directories for installed
@@ -532,12 +805,16 @@ class InstallCommand(RequirementCommand):
for lib_dir in lib_dir_list:
for item in os.listdir(lib_dir):
# Do not move library directories a second time as data.
if lib_dir == data_dir:
ddir = os.path.join(data_dir, item)
if any(s.startswith(ddir) for s in lib_dir_list[:-1]):
continue
target_item_dir = os.path.join(target_dir, item)
if os.path.exists(target_item_dir):
# Keep dangling links subject to the same replacement policy.
if os.path.lexists(target_item_dir):
if not upgrade:
logger.warning(
"Target directory %s already exists. Specify "
@@ -545,6 +822,7 @@ class InstallCommand(RequirementCommand):
target_item_dir,
)
continue
if os.path.islink(target_item_dir):
logger.warning(
"Target directory %s already exists and is "
@@ -554,6 +832,7 @@ class InstallCommand(RequirementCommand):
target_item_dir,
)
continue
if os.path.isdir(target_item_dir):
shutil.rmtree(target_item_dir)
else:
@@ -1,8 +1,9 @@
from __future__ import annotations
import contextlib
import json
import logging
from collections.abc import Generator, Sequence
from collections.abc import Generator, Iterator, Sequence
from email.parser import Parser
from optparse import Values
from typing import TYPE_CHECKING, cast
@@ -15,8 +16,8 @@ from pip._internal.cli.index_command import IndexGroupCommand
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import CommandError
from pip._internal.metadata import BaseDistribution, get_environment
from pip._internal.metadata.base import stdlib_pkgs
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.utils.misc import tabulate, write_output
if TYPE_CHECKING:
@@ -135,9 +136,13 @@ class ListCommand(IndexGroupCommand):
self.parser.insert_option_group(0, selection_opts)
self.parser.insert_option_group(0, self.cmd_opts)
def handle_pip_version_check(self, options: Values) -> None:
if options.outdated or options.uptodate:
super().handle_pip_version_check(options)
@contextlib.contextmanager
def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:
if not (options.outdated or options.uptodate):
yield
return
with super().pip_version_check(options, args):
yield
def _build_package_finder(
self, options: Values, session: PipSession
@@ -155,11 +160,14 @@ class ListCommand(IndexGroupCommand):
selection_prefs = SelectionPreferences(
allow_yanked=False,
release_control=options.release_control,
format_control=options.format_control,
prefer_binary=options.prefer_binary,
)
return PackageFinder.create(
link_collector=link_collector,
selection_prefs=selection_prefs,
uploaded_prior_to=options.uploaded_prior_to,
)
def run(self, options: Values, args: list[str]) -> int:
@@ -175,10 +183,6 @@ class ListCommand(IndexGroupCommand):
cmdoptions.check_list_path_option(options)
skip = set(stdlib_pkgs)
if options.excludes:
skip.update(canonicalize_name(n) for n in options.excludes)
packages: _ProcessedDists = [
cast("_DistWithLatestInfo", d)
for d in get_environment(options.path).iter_installed_distributions(
@@ -186,7 +190,7 @@ class ListCommand(IndexGroupCommand):
user_only=options.user,
editables_only=options.editable,
include_editables=options.include_editable,
skip=skip,
skip=set(stdlib_pkgs),
)
]
@@ -197,6 +201,12 @@ class ListCommand(IndexGroupCommand):
if options.not_required:
packages = self.get_not_required(packages, options)
if options.excludes:
excluded_names = {canonicalize_name(n) for n in options.excludes}
packages = [
dist for dist in packages if dist.canonical_name not in excluded_names
]
if options.outdated:
packages = self.get_outdated(packages, options)
elif options.uptodate:
@@ -62,6 +62,7 @@ class LockCommand(RequirementCommand):
self.cmd_opts.add_option(cmdoptions.constraints())
self.cmd_opts.add_option(cmdoptions.build_constraints())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.only_deps())
self.cmd_opts.add_option(cmdoptions.editable())
@@ -75,6 +76,7 @@ class LockCommand(RequirementCommand):
self.cmd_opts.add_option(cmdoptions.config_settings())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.no_require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
index_opts = cmdoptions.make_option_group(
@@ -101,8 +103,9 @@ class LockCommand(RequirementCommand):
"without prior warning."
)
cmdoptions.check_build_constraints(options)
self.check_build_constraints(options)
cmdoptions.check_release_control_exclusive(options)
cmdoptions.check_only_deps_option_does_not_conflict(options)
session = self.get_default_session(options)
@@ -123,12 +126,6 @@ class LockCommand(RequirementCommand):
wheel_cache = WheelCache(options.cache_dir)
# Only when installing is it permitted to use PEP 660.
# In other circumstances (pip wheel, pip download) we generate
# regular (i.e. non editable) metadata and wheels.
for req in reqs:
req.permit_editable_wheels = True
preparer = self.make_requirement_preparer(
temp_build_dir=directory,
options=options,
@@ -137,6 +134,7 @@ class LockCommand(RequirementCommand):
finder=finder,
use_user_site=False,
verbosity=self.verbosity,
allow_editables=True,
)
resolver = self.make_resolver(
preparer=preparer,
@@ -116,18 +116,14 @@ def transform_hits(hits: list[dict[str, str]]) -> list[TransformedHit]:
def print_dist_installation_info(latest: str, dist: BaseDistribution | None) -> None:
if dist is not None:
with indent_log():
if dist.version == latest:
write_output("INSTALLED: %s (latest)", dist.version)
write_output("INSTALLED: %s", dist.version)
if parse_version(latest).pre:
write_output(
"LATEST: %s (pre-release; install with `pip install --pre`)",
latest,
)
else:
write_output("INSTALLED: %s", dist.version)
if parse_version(latest).pre:
write_output(
"LATEST: %s (pre-release; install"
" with `pip install --pre`)",
latest,
)
else:
write_output("LATEST: %s", latest)
write_output("LATEST: %s", latest)
def get_installed_distribution(name: str) -> BaseDistribution | None:
@@ -53,7 +53,7 @@ class ShowCommand(Command):
return ERROR
query = args
results = search_packages_info(query)
results = search_packages_info(query, include_files=options.files)
if not print_results(
results, list_files=options.files, verbose=options.verbose
):
@@ -82,7 +82,10 @@ class _PackageInfo(NamedTuple):
files: list[str] | None
def search_packages_info(query: list[str]) -> Generator[_PackageInfo, None, None]:
def search_packages_info(
query: list[str],
include_files: bool,
) -> Generator[_PackageInfo, None, None]:
"""
Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
@@ -133,11 +136,11 @@ def search_packages_info(query: list[str]) -> Generator[_PackageInfo, None, None
except FileNotFoundError:
entry_points = []
files_iter = dist.iter_declared_entries()
if files_iter is None:
files: list[str] | None = None
else:
files = sorted(files_iter)
files: list[str] | None = None
if include_files:
files_iter = dist.iter_declared_entries()
if files_iter is not None:
files = sorted(files_iter)
metadata = dist.metadata
@@ -189,7 +192,10 @@ def print_results(
if i > 0:
write_output("---")
metadata_version_tuple = tuple(map(int, dist.metadata_version.split(".")))
metadata_version = dist.metadata_version
metadata_version_tuple = (
tuple(map(int, metadata_version.split("."))) if metadata_version else ()
)
write_output("Name: %s", dist.name)
write_output("Version: %s", dist.version)
@@ -62,6 +62,7 @@ class WheelCommand(RequirementCommand):
self.cmd_opts.add_option(cmdoptions.src())
self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.only_deps())
self.cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(
@@ -75,6 +76,7 @@ class WheelCommand(RequirementCommand):
self.cmd_opts.add_option(cmdoptions.config_settings())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.no_require_hashes())
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
@@ -92,8 +94,9 @@ class WheelCommand(RequirementCommand):
@with_cleanup
def run(self, options: Values, args: list[str]) -> int:
cmdoptions.check_build_constraints(options)
self.check_build_constraints(options)
cmdoptions.check_release_control_exclusive(options)
cmdoptions.check_only_deps_option_does_not_conflict(options)
session = self.get_default_session(options)
@@ -123,6 +126,7 @@ class WheelCommand(RequirementCommand):
download_dir=options.wheel_dir,
use_user_site=False,
verbosity=self.verbosity,
allow_editables=False,
)
resolver = self.make_resolver(
@@ -151,6 +155,7 @@ class WheelCommand(RequirementCommand):
reqs_to_build,
wheel_cache=wheel_cache,
verify=(not options.no_verify),
allow_editables=False,
)
for req in build_successes:
assert req.link and req.link.is_wheel
@@ -160,8 +165,9 @@ class WheelCommand(RequirementCommand):
shutil.copy(req.local_file_path, options.wheel_dir)
except OSError as e:
logger.warning(
"Building wheel for %s failed: %s",
"Failed to copy built wheel %s to %s: %s",
req.name,
options.wheel_dir,
e,
)
build_failures.append(req)