trererere

This commit is contained in:
2026-09-18 21:32:27 -03:00
parent f632a89c2e
commit df0de2c695
430 changed files with 39 additions and 70148 deletions
@@ -1,13 +0,0 @@
from __future__ import annotations
__version__ = "26.2.1"
def main(args: list[str] | None = None) -> int:
"""This is an internal API only meant for use by pip's own console scripts.
For additional details, see https://github.com/pypa/pip/issues/7498.
"""
from pip._internal.utils.entrypoints import _wrapper
return _wrapper(args)
@@ -1,24 +0,0 @@
import os
import sys
# Remove '' and current working directory from the first entry
# of sys.path, if present to avoid using current directory
# in pip commands check, freeze, install, list and show,
# when invoked as python -m pip <command>
if sys.path[0] in ("", os.getcwd()):
sys.path.pop(0)
# If we are running from a wheel, add the wheel to sys.path
# This allows the usage python pip-*.whl/pip install pip-*.whl
if not __spec__ or __spec__.parent == "":
# __file__ is pip-*.whl/pip/__main__.py
# first dirname call strips of '/__main__.py', second strips off '/pip'
# Resulting path is the name of the wheel itself
# Add that to sys.path so we can import pip
path = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, path)
if __name__ == "__main__":
from pip._internal.cli.main import main as _main
sys.exit(_main())
@@ -1,50 +0,0 @@
"""Execute exactly this copy of pip, within a different environment.
This file is named as it is, to ensure that this module can't be imported via
an import statement.
"""
# /!\ This version compatibility check section must be Python 2 compatible. /!\
import sys
# Copied from pyproject.toml
PYTHON_REQUIRES = (3, 10)
def version_str(version): # type: ignore
return ".".join(str(v) for v in version)
if sys.version_info[:2] < PYTHON_REQUIRES:
raise SystemExit(
"This version of pip does not support python {} (requires >={}).".format(
version_str(sys.version_info[:2]), version_str(PYTHON_REQUIRES)
)
)
# From here on, we can use Python 3 features, but the syntax must remain
# Python 2 compatible.
import runpy # noqa: E402
from importlib.machinery import PathFinder # noqa: E402
from os.path import dirname # noqa: E402
PIP_SOURCES_ROOT = dirname(dirname(__file__))
class PipImportRedirectingFinder:
@classmethod
def find_spec(self, fullname, path=None, target=None): # type: ignore
if fullname != "pip":
return None
spec = PathFinder.find_spec(fullname, [PIP_SOURCES_ROOT], target)
assert spec, (PIP_SOURCES_ROOT, fullname)
return spec
sys.meta_path.insert(0, PipImportRedirectingFinder())
assert __name__ == "__main__", "Cannot run __pip-runner__.py as a non-main module"
runpy.run_module("pip", run_name="__main__", alter_sys=True)
@@ -1,18 +0,0 @@
from __future__ import annotations
from pip._internal.utils import _log
# init_logging() must be called before any call to logging.getLogger()
# which happens at import of most modules.
_log.init_logging()
def main(args: list[str] | None = None) -> int:
"""This is preserved for old console scripts that may still be referencing
it.
For additional details, see https://github.com/pypa/pip/issues/7498.
"""
from pip._internal.utils.entrypoints import _wrapper
return _wrapper(args)
@@ -1,25 +0,0 @@
"""Build environments used for isolation during build backend calls."""
from pip._internal.build_env.base import (
BuildEnvironment,
BuildEnvironmentInstaller,
BuildIsolationMode,
)
from pip._internal.build_env.installer import (
InprocessBuildEnvironmentInstaller,
SubprocessBuildEnvironmentInstaller,
)
from pip._internal.build_env.noop import NoOpBuildEnvironment
from pip._internal.build_env.venv import VenvBuildEnvironment
from pip._internal.build_env.virtual import VirtualBuildEnvironment
__all__ = [
"BuildEnvironment",
"BuildEnvironmentInstaller",
"BuildIsolationMode",
"InprocessBuildEnvironmentInstaller",
"NoOpBuildEnvironment",
"SubprocessBuildEnvironmentInstaller",
"VenvBuildEnvironment",
"VirtualBuildEnvironment",
]
@@ -1,103 +0,0 @@
from __future__ import annotations
import abc
from collections.abc import Iterable
from contextlib import AbstractContextManager as ContextManager
from typing import TYPE_CHECKING, Literal, Protocol
from pip._vendor.packaging.version import Version
from pip._internal.locations import get_scheme
from pip._internal.metadata import get_default_environment, get_environment
from pip._internal.utils.packaging import get_requirement
if TYPE_CHECKING:
from pip._internal.req.req_install import InstallRequirement
BuildIsolationMode = Literal["off", "virtual", "venv"]
def _dedup(a: str, b: str) -> tuple[str] | tuple[str, str]:
return (a, b) if a != b else (a,)
class Prefix:
# TODO: simplify this data model when the legacy subprocess installer is removed
def __init__(self, path: str, *, venv_executable: str | None = None) -> None:
self.path = path
self.setup = False
scheme = get_scheme("", prefix=path)
self.bin_dir = scheme.scripts
self.lib_dirs = _dedup(scheme.purelib, scheme.platlib)
self.venv_executable = venv_executable
class BuildEnvironmentInstaller(Protocol):
"""
Interface for installing build dependencies into an isolated build
environment.
"""
def install(
self,
requirements: Iterable[str],
prefix: Prefix,
*,
kind: str,
for_req: InstallRequirement | None,
) -> None: ...
class BuildEnvironment(ContextManager[None], metaclass=abc.ABCMeta):
"""Creates and manages an isolated environment to install build deps"""
lib_dirs: list[str]
python_executable: str
@abc.abstractmethod
def __init__(self, installer: BuildEnvironmentInstaller): ...
def check_requirements(
self, reqs: Iterable[str]
) -> tuple[set[tuple[str, str]], set[str]]:
"""Return 2 sets:
- conflicting requirements: set of (installed, wanted) reqs tuples
- missing requirements: set of reqs
"""
missing = set()
conflicting = set()
if reqs:
env = (
get_environment(self.lib_dirs)
if hasattr(self, "lib_dirs")
else get_default_environment()
)
for req_str in reqs:
req = get_requirement(req_str)
# We're explicitly evaluating with an empty extra value, since build
# environments are not provided any mechanism to select specific extras.
if req.marker is not None and not req.marker.evaluate({"extra": ""}):
continue
dist = env.get_distribution(req.name)
if not dist:
missing.add(req_str)
continue
if isinstance(dist.version, Version):
installed_req_str = f"{req.name}=={dist.version}"
else:
installed_req_str = f"{req.name}==={dist.version}"
if not req.specifier.contains(dist.version, prereleases=True):
conflicting.add((installed_req_str, req_str))
# FIXME: Consider direct URL?
return conflicting, missing
@abc.abstractmethod
def install_requirements(
self,
requirements: Iterable[str],
prefix_as_string: str,
*,
kind: str,
for_req: InstallRequirement | None = None,
) -> None: ...
@@ -1,333 +0,0 @@
from __future__ import annotations
import logging
import sys
import textwrap
from collections.abc import Iterable, Sequence
from contextlib import AbstractContextManager as ContextManager
from contextlib import nullcontext
from io import StringIO
from typing import TYPE_CHECKING
from pip._internal.build_env.base import Prefix
from pip._internal.cli.spinners import open_rich_spinner, open_spinner
from pip._internal.exceptions import (
BuildDependencyInstallError,
DiagnosticPipError,
InstallWheelBuildError,
PipError,
)
from pip._internal.metadata import get_environment
from pip._internal.utils.logging import VERBOSE, capture_logging
from pip._internal.utils.misc import get_runnable_pip
from pip._internal.utils.subprocess import call_subprocess
from pip._internal.utils.temp_dir import TempDirectory
if TYPE_CHECKING:
from pip._internal.cache import WheelCache
from pip._internal.index.package_finder import PackageFinder
from pip._internal.operations.build.build_tracker import BuildTracker
from pip._internal.req.req_install import InstallRequirement
from pip._internal.resolution.base import BaseResolver
logger = logging.getLogger(__name__)
class SubprocessBuildEnvironmentInstaller:
"""
Install build dependencies by calling pip in a subprocess.
"""
def __init__(
self,
finder: PackageFinder,
build_constraints: list[str] | None = None,
) -> None:
self.finder = finder
self._build_constraints = build_constraints or []
def install(
self,
requirements: Iterable[str],
prefix: Prefix,
*,
kind: str,
for_req: InstallRequirement | None,
) -> None:
finder = self.finder
args: list[str] = [
get_runnable_pip(),
"install",
# HACK: --prefix shouldn't be necessary for venv environments, but
# we set it anyway so if it's set via an envvar or configuration
# file, it won't break things, *sigh*.
"--prefix",
prefix.path,
"--no-user",
"--no-warn-script-location",
"--disable-pip-version-check",
# As the build environment is ephemeral, it's wasteful to
# pre-compile everything, especially as not every Python
# module will be used/compiled in most cases.
"--no-compile",
# The prefix specified two lines above, thus
# target from config file or env var should be ignored
"--target",
"",
]
if prefix.venv_executable:
args.insert(0, prefix.venv_executable)
else:
args.insert(0, sys.executable)
args.append("--ignore-installed")
if logger.getEffectiveLevel() <= logging.DEBUG:
args.append("-vv")
elif logger.getEffectiveLevel() <= VERBOSE:
args.append("-v")
for format_control in ("no_binary", "only_binary"):
formats = getattr(finder.format_control, format_control)
args.extend(
(
"--" + format_control.replace("_", "-"),
",".join(sorted(formats or {":none:"})),
)
)
if finder.release_control is not None:
# Use ordered args to preserve the user's original command-line order
# This is important because later flags can override earlier ones
for attr_name, value in finder.release_control.get_ordered_args():
args.extend(("--" + attr_name.replace("_", "-"), value))
index_urls = finder.index_urls
if index_urls:
args.extend(["-i", index_urls[0]])
for extra_index in index_urls[1:]:
args.extend(["--extra-index-url", extra_index])
else:
args.append("--no-index")
for link in finder.find_links:
args.extend(["--find-links", link])
# is not None: forward an empty --proxy "" (disable proxying) too.
if finder.proxy is not None:
args.extend(["--proxy", finder.proxy])
if finder.no_proxy_env:
args.append("--no-proxy-env")
for host in finder.trusted_hosts:
args.extend(["--trusted-host", host])
if finder.custom_cert:
args.extend(["--cert", finder.custom_cert])
if finder.client_cert:
args.extend(["--client-cert", finder.client_cert])
if finder.prefer_binary:
args.append("--prefer-binary")
if finder.refresh_package:
args.extend(["--refresh-package", ",".join(finder.refresh_package)])
# Only build constraints apply in the isolated build environment.
# _PIP_IN_BUILD_IGNORE_CONSTRAINTS tells the subprocess to ignore the
# regular constraints it inherits (via PIP_CONSTRAINT or config files).
# Build constraints reach it through --build-constraint, which also
# constrains any nested builds.
for constraint_file in self._build_constraints:
args.extend(["--build-constraint", constraint_file])
if finder.uploaded_prior_to:
args.extend(["--uploaded-prior-to", finder.uploaded_prior_to.isoformat()])
args.append("--")
args.extend(requirements)
identify_requirement = (
f" for {for_req.name}" if for_req and for_req.name else ""
)
with open_spinner(f"Installing {kind}") as spinner:
call_subprocess(
args,
command_desc=f"installing {kind}{identify_requirement}",
spinner=spinner,
extra_environ={"_PIP_IN_BUILD_IGNORE_CONSTRAINTS": "1"},
)
class InprocessBuildEnvironmentInstaller:
"""
Install build dependencies via the already running pip process.
This contains a stripped down version of the install command with
only the logic necessary for installing build dependencies. The
finder, session, build tracker, and wheel cache are reused, but new
instances of everything else are created as needed.
Options are inherited from the parent install command unless
they don't make sense for build dependencies (in which case, they
are hard-coded, see comments below).
"""
# TODO: this plays poorly with venv-based build environments, but cannot be
# fixed until pip gains better support for operating within a Python
# environment that isn't the running environment.
def __init__(
self,
*,
finder: PackageFinder,
build_tracker: BuildTracker,
wheel_cache: WheelCache,
build_constraints: Sequence[InstallRequirement] = (),
verbosity: int = 0,
) -> None:
from pip._internal.operations.prepare import RequirementPreparer
self._finder = finder
self._build_constraints = build_constraints
self._wheel_cache = wheel_cache
self._level = 0
build_dir = TempDirectory(kind="build-env-install", globally_managed=True)
self._preparer = RequirementPreparer(
build_isolation_installer=self,
# Inherited options or state.
finder=finder,
session=finder._link_collector.session,
build_dir=build_dir.path,
build_tracker=build_tracker,
verbosity=verbosity,
# This is irrelevant as it only applies to editable requirements.
src_dir="",
# Hard-coded options (that should NOT be inherited).
download_dir=None,
build_isolation="virtual",
check_build_deps=False,
progress_bar="off",
# TODO: hash-checking should be extended to build deps, but that is
# deferred for later as it'd be a breaking change.
require_hashes=False,
use_user_site=False,
lazy_wheel=False,
legacy_resolver=False,
allow_editables=True,
)
def install(
self,
requirements: Iterable[str],
prefix: Prefix,
*,
kind: str,
for_req: InstallRequirement | None,
) -> None:
"""Install entrypoint. Manages output capturing and error handling."""
capture_logs = not logger.isEnabledFor(VERBOSE) and self._level == 0
if capture_logs:
# Hide the logs from the installation of build dependencies.
# They will be shown only if an error occurs.
capture_ctx: ContextManager[StringIO] = capture_logging()
spinner: ContextManager[None] = open_rich_spinner(f"Installing {kind}")
else:
# Otherwise, pass-through all logs (with a header).
capture_ctx, spinner = nullcontext(StringIO()), nullcontext()
logger.info("Installing %s ...", kind)
try:
self._level += 1
with spinner, capture_ctx as stream:
self._install_impl(requirements, prefix)
except DiagnosticPipError as exc:
# Format similar to a nested subprocess error, where the
# causing error is shown first, followed by the build error.
logger.info(textwrap.dedent(stream.getvalue()))
logger.error("%s", exc, extra={"rich": True})
logger.info("")
raise BuildDependencyInstallError(
for_req, requirements, cause=exc, log_lines=None
)
except Exception as exc:
logs: list[str] | None = textwrap.dedent(stream.getvalue()).splitlines()
if not capture_logs:
# If logs aren't being captured, then display the error inline
# with the rest of the logs.
logs = None
if isinstance(exc, PipError):
logger.error("%s", exc)
else:
logger.exception("pip crashed unexpectedly")
raise BuildDependencyInstallError(
for_req, requirements, cause=exc, log_lines=logs
)
finally:
self._level -= 1
def _install_impl(self, requirements: Iterable[str], prefix: Prefix) -> None:
"""Core build dependency install logic."""
from pip._internal.commands.install import installed_packages_summary
from pip._internal.req import install_given_reqs
from pip._internal.req.constructors import install_req_from_line
from pip._internal.wheel_builder import build
ireqs = [install_req_from_line(req, user_supplied=True) for req in requirements]
ireqs.extend(self._build_constraints)
resolver = self._make_resolver()
resolved_set = resolver.resolve(ireqs, check_supported_wheels=True)
self._preparer.prepare_linked_requirements_more(
resolved_set.requirements.values()
)
reqs_to_build = [
r for r in resolved_set.requirements_to_install if not r.is_wheel
]
_, build_failures = build(
reqs_to_build, self._wheel_cache, verify=True, allow_editables=True
)
if build_failures:
raise InstallWheelBuildError(build_failures)
installed = install_given_reqs(
resolver.get_installation_order(resolved_set),
prefix=prefix.path,
# Hard-coded options (that should NOT be inherited).
root=None,
home=None,
warn_script_location=False,
use_user_site=False,
# As the build environment is ephemeral, it's wasteful to
# pre-compile everything since not all modules will be used.
pycompile=False,
progress_bar="off",
# Link console scripts to the build env's interpreter, not pip's.
script_executable=prefix.venv_executable,
)
env = get_environment(list(prefix.lib_dirs))
if summary := installed_packages_summary(installed, env):
logger.info(summary)
def _make_resolver(self) -> BaseResolver:
"""Create a new resolver for one time use."""
# Legacy installer never used the legacy resolver so create a
# resolvelib resolver directly. Yuck.
from pip._internal.req.constructors import install_req_from_req_string
from pip._internal.resolution.resolvelib.resolver import Resolver
return Resolver(
make_install_req=install_req_from_req_string,
# Inherited state.
preparer=self._preparer,
finder=self._finder,
wheel_cache=self._wheel_cache,
# Hard-coded options (that should NOT be inherited).
ignore_requires_python=False,
use_user_site=False,
ignore_dependencies=False,
only_dependencies=False,
ignore_installed=True,
force_reinstall=False,
upgrade_strategy="to-satisfy-only",
py_version_info=None,
)
@@ -1,42 +0,0 @@
from __future__ import annotations
import sys
from collections.abc import Iterable
from types import TracebackType
from typing import TYPE_CHECKING
from pip._internal.build_env.base import BuildEnvironment
if TYPE_CHECKING:
from pip._internal.req.req_install import InstallRequirement
class NoOpBuildEnvironment(BuildEnvironment):
"""A no-op drop-in replacement for BuildEnvironment"""
def __init__(self) -> None:
self.python_executable = sys.executable
def __enter__(self) -> None:
pass
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
pass
def cleanup(self) -> None:
pass
def install_requirements(
self,
requirements: Iterable[str],
prefix_as_string: str,
*,
kind: str,
for_req: InstallRequirement | None = None,
) -> None:
raise NotImplementedError()
@@ -1,145 +0,0 @@
from __future__ import annotations
import os
import sys
import sysconfig
from collections.abc import Iterable
from types import TracebackType
from typing import TYPE_CHECKING
from pip._internal.build_env.base import (
BuildEnvironment,
BuildEnvironmentInstaller,
Prefix,
)
from pip._internal.exceptions import VenvCreationError, VenvImportError
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
if TYPE_CHECKING:
from pip._internal.req.req_install import InstallRequirement
def _get_venv_path_from_sysconfig(name: str, env_dir: str) -> str:
vars = {
"base": env_dir,
"platbase": env_dir,
}
return sysconfig.get_path(name, scheme="venv", vars=vars)
class VenvBuildEnvironment(BuildEnvironment):
"""A venv-based build environment."""
def __init__(self, installer: BuildEnvironmentInstaller) -> None:
# We defer this import because certain distributions of Python do not include
# a functional venv out of the box.
try:
import venv
except ImportError:
raise VenvImportError
self._env_path = TempDirectory(
kind=tempdir_kinds.BUILD_ENV, globally_managed=True
).path
# Use symlinks to support relocatable Python installations on POSIX, including
# python-build-standalone. This matches upstream venv CLI's behaviour.
env = venv.EnvBuilder(symlinks=(os.name != "nt"))
try:
context = env.ensure_directories(self._env_path)
env.create(self._env_path)
except OSError as e:
raise VenvCreationError(str(e))
if sys.version_info >= (3, 12):
# The context object was only documented in Python 3.12
self.lib_dirs = [context.lib_path]
self._bin_path = context.bin_path
elif sys.version_info[:2] == (3, 11):
# On Python 3.11, we can use sysconfig.
self.lib_dirs = [_get_venv_path_from_sysconfig("purelib", self._env_path)]
self._bin_path = _get_venv_path_from_sysconfig("scripts", self._env_path)
else:
# Otherwise, we need to manually construct all the paths... sigh.
if sys.platform == "win32":
libpath = os.path.join(self._env_path, "Lib", "site-packages")
else:
python = "pypy" if sys.implementation.name == "pypy" else "python"
libpath = os.path.join(
self._env_path,
"lib",
f"{python}{sys.version_info.major}.{sys.version_info.minor}",
"site-packages",
)
self.lib_dirs = [libpath]
# Same reasoning for try-except as for python_executable below.
try:
self._bin_path = context.bin_path
except AttributeError:
scripts_dir = "Scripts" if os.name == "nt" else "bin"
self._bin_path = os.path.join(self._env_path, scripts_dir)
# There are enough ways trying to construct the Python executable path can go
# wrong that we're better off assuming that the context object has the right
# attributes, and only when they don't exist do we try to guess.
#
# These attributes seem to exist in every CPython version after 3.10.1 and
# are documented to exist on 3.12 and higher.
try:
self.python_executable = context.env_exec_cmd
except AttributeError:
try:
self.python_executable = context.env_exe
except AttributeError:
executable_name = "python.exe" if os.name == "nt" else "python"
self.python_executable = os.path.join(self._bin_path, executable_name)
self._save_env: dict[str, str | None] = {}
self._installer = installer
if not os.path.exists(self.python_executable):
# This error is only likely on Windows due to interference from AV software.
raise VenvCreationError(
f"Python executable failed to copy to {self.python_executable}"
)
def __enter__(self) -> None:
# We want backend calls to be able to use binaries installed as if this
# virtual environment was "activated".
self._save_env = {
name: os.environ.get(name, None) for name in ("PATH", "PYTHONPATH")
}
new_path = [self._bin_path]
if old_path := self._save_env["PATH"]:
new_path.extend(old_path.split(os.pathsep))
# However, we don't want a pre-existing PYTHONPATH to influence the
# backend calls.
os.environ.update({"PATH": os.pathsep.join(new_path), "PYTHONPATH": ""})
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
for varname, old_value in self._save_env.items():
if old_value is None:
os.environ.pop(varname, None)
else:
os.environ[varname] = old_value
def install_requirements(
self,
requirements: Iterable[str],
prefix_as_string: str,
*,
kind: str,
for_req: InstallRequirement | None = None,
) -> None:
if not requirements:
return
# TODO: when better support for installing to arbitrary Python environments
# is added, replace this prefix hack with that.
prefix = Prefix(self._env_path, venv_executable=self.python_executable)
self._installer.install(requirements, prefix, kind=kind, for_req=for_req)
@@ -1,139 +0,0 @@
from __future__ import annotations
import os
import site
import sys
import textwrap
from collections import OrderedDict
from collections.abc import Iterable
from types import TracebackType
from typing import TYPE_CHECKING
from pip._internal.build_env.base import (
BuildEnvironment,
BuildEnvironmentInstaller,
Prefix,
)
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
if TYPE_CHECKING:
from pip._internal.req.req_install import InstallRequirement
def get_system_sitepackages() -> set[str]:
"""Get system site packages, as a normalized set of strings."""
system_sites = site.getsitepackages()
return {os.path.normcase(path) for path in system_sites}
class VirtualBuildEnvironment(BuildEnvironment):
"""Legacy build environment implementation.
Patches sys.path and uses sitecustomize.py to isolate Python processes. It has
known bugs and weird edge cases, but it exists as a fallback for platforms where
the venv module is unavailable or dysfunctional.
"""
def __init__(self, installer: BuildEnvironmentInstaller) -> None:
self.python_executable = sys.executable
self.installer = installer
temp_dir = TempDirectory(kind=tempdir_kinds.BUILD_ENV, globally_managed=True)
self._prefixes = OrderedDict(
(name, Prefix(os.path.join(temp_dir.path, name)))
for name in ("normal", "overlay")
)
self._bin_dirs: list[str] = []
self.lib_dirs: list[str] = []
for prefix in reversed(list(self._prefixes.values())):
self._bin_dirs.append(prefix.bin_dir)
self.lib_dirs.extend(prefix.lib_dirs)
# Customize site to:
# - ensure .pth files are honored
# - prevent access to system site packages
system_sites = get_system_sitepackages()
self._site_dir = os.path.join(temp_dir.path, "site")
if not os.path.exists(self._site_dir):
os.mkdir(self._site_dir)
with open(
os.path.join(self._site_dir, "sitecustomize.py"), "w", encoding="utf-8"
) as fp:
fp.write(textwrap.dedent("""
import os, site, sys
# First, discover all system-sites related paths.
original_sys_path = sys.path[:]
# Clear sys.path so addsitedir() will add system site paths and paths
# added by contained .pth files to sys.path reliably. This is necessary
# since Python 3.15, which notably no longer re-executes .pth files for
# known paths.
sys.path = []
known_paths = set()
for path in {system_sites!r}:
site.addsitedir(path, known_paths=known_paths)
system_paths = set(os.path.normcase(path) for path in sys.path)
# Drop discovered system-sites related paths.
original_sys_path = [
path for path in original_sys_path
if os.path.normcase(path) not in system_paths
]
sys.path = original_sys_path
# Second, add lib directories.
# ensuring .pth file are processed.
for path in {lib_dirs!r}:
assert not path in sys.path
site.addsitedir(path)
""").format(system_sites=system_sites, lib_dirs=self.lib_dirs))
def __enter__(self) -> None:
self._save_env = {
name: os.environ.get(name, None)
for name in ("PATH", "PYTHONNOUSERSITE", "PYTHONPATH")
}
path = self._bin_dirs[:]
old_path = self._save_env["PATH"]
if old_path:
path.extend(old_path.split(os.pathsep))
pythonpath = [self._site_dir]
os.environ.update(
{
"PATH": os.pathsep.join(path),
"PYTHONNOUSERSITE": "1",
"PYTHONPATH": os.pathsep.join(pythonpath),
}
)
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
for varname, old_value in self._save_env.items():
if old_value is None:
os.environ.pop(varname, None)
else:
os.environ[varname] = old_value
def install_requirements(
self,
requirements: Iterable[str],
prefix_as_string: str,
*,
kind: str,
for_req: InstallRequirement | None = None,
) -> None:
prefix = self._prefixes[prefix_as_string]
assert not prefix.setup
prefix.setup = True
if not requirements:
return
self.installer.install(requirements, prefix, kind=kind, for_req=for_req)
@@ -1,294 +0,0 @@
"""Cache Management"""
from __future__ import annotations
import hashlib
import json
import logging
import os
from pathlib import Path
from typing import Any
from pip._vendor.packaging.tags import Tag, interpreter_name, interpreter_version
from pip._vendor.packaging.utils import canonicalize_name
from pip._internal.exceptions import InvalidWheelFilename
from pip._internal.models.direct_url import DirectUrl
from pip._internal.models.link import Link
from pip._internal.models.wheel import Wheel
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
from pip._internal.utils.urls import path_to_url
logger = logging.getLogger(__name__)
ORIGIN_JSON_NAME = "origin.json"
def _hash_dict(d: dict[str, str]) -> str:
"""Return a stable sha224 of a dictionary."""
s = json.dumps(d, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
return hashlib.sha224(s.encode("ascii")).hexdigest()
class Cache:
"""An abstract class - provides cache directories for data from links
:param cache_dir: The root of the cache.
"""
def __init__(self, cache_dir: str) -> None:
super().__init__()
assert not cache_dir or os.path.isabs(cache_dir)
self.cache_dir = cache_dir or None
def _get_cache_path_parts(self, link: Link) -> list[str]:
"""Get parts of part that must be os.path.joined with cache_dir"""
# We want to generate an url to use as our cache key, we don't want to
# just reuse the URL because it might have other items in the fragment
# and we don't care about those.
key_parts = {"url": link.url_without_fragment}
if link.hash_name is not None and link.hash is not None:
key_parts[link.hash_name] = link.hash
if link.subdirectory_fragment:
key_parts["subdirectory"] = link.subdirectory_fragment
# Include interpreter name, major and minor version in cache key
# to cope with ill-behaved sdists that build a different wheel
# depending on the python version their setup.py is being run on,
# and don't encode the difference in compatibility tags.
# https://github.com/pypa/pip/issues/7296
key_parts["interpreter_name"] = interpreter_name()
key_parts["interpreter_version"] = interpreter_version()
# Encode our key url with sha224, we'll use this because it has similar
# security properties to sha256, but with a shorter total output (and
# thus less secure). However the differences don't make a lot of
# difference for our use case here.
hashed = _hash_dict(key_parts)
# We want to nest the directories some to prevent having a ton of top
# level directories where we might run out of sub directories on some
# FS.
parts = [hashed[:2], hashed[2:4], hashed[4:6], hashed[6:]]
return parts
def _get_candidates(self, link: Link, canonical_package_name: str) -> list[Any]:
can_not_cache = not self.cache_dir or not canonical_package_name or not link
if can_not_cache:
return []
path = self.get_path_for_link(link)
if os.path.isdir(path):
return [(candidate, path) for candidate in os.listdir(path)]
return []
def get_path_for_link(self, link: Link) -> str:
"""Return a directory to store cached items in for link."""
raise NotImplementedError()
def get(
self,
link: Link,
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
"""Returns a link to a cached item if it exists, otherwise returns the
passed link.
"""
raise NotImplementedError()
class SimpleWheelCache(Cache):
"""A cache of wheels for future installs."""
def __init__(self, cache_dir: str) -> None:
super().__init__(cache_dir)
def get_path_for_link(self, link: Link) -> str:
"""Return a directory to store cached wheels for link
Because there are M wheels for any one sdist, we provide a directory
to cache them in, and then consult that directory when looking up
cache hits.
We only insert things into the cache if they have plausible version
numbers, so that we don't contaminate the cache with things that were
not unique. E.g. ./package might have dozens of installs done for it
and build a version of 0.0...and if we built and cached a wheel, we'd
end up using the same wheel even if the source has been edited.
:param link: The link of the sdist for which this will cache wheels.
"""
parts = self._get_cache_path_parts(link)
assert self.cache_dir
# Store wheels within the root cache_dir
return os.path.join(self.cache_dir, "wheels", *parts)
def get(
self,
link: Link,
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
candidates = []
if link.is_existing_dir():
return link
if not package_name:
return link
canonical_package_name = canonicalize_name(package_name)
supported_tags_set = set(supported_tags)
for wheel_name, wheel_dir in self._get_candidates(link, canonical_package_name):
try:
wheel = Wheel(wheel_name)
except InvalidWheelFilename:
continue
if wheel.name != canonical_package_name:
logger.debug(
"Ignoring cached wheel %s for %s as it "
"does not match the expected distribution name %s.",
wheel_name,
link,
package_name,
)
continue
if not wheel.supported(supported_tags_set):
# Built for a different python/arch/etc
continue
candidates.append(
(
wheel.support_index_min(supported_tags),
wheel_name,
wheel_dir,
)
)
if not candidates:
return link
_, wheel_name, wheel_dir = min(candidates)
return Link(path_to_url(os.path.join(wheel_dir, wheel_name)))
class EphemWheelCache(SimpleWheelCache):
"""A SimpleWheelCache that creates it's own temporary cache directory"""
def __init__(self) -> None:
self._temp_dir = TempDirectory(
kind=tempdir_kinds.EPHEM_WHEEL_CACHE,
globally_managed=True,
)
super().__init__(self._temp_dir.path)
class CacheEntry:
def __init__(
self,
link: Link,
persistent: bool,
):
self.link = link
self.persistent = persistent
self.origin: DirectUrl | None = None
origin_direct_url_path = Path(self.link.file_path).parent / ORIGIN_JSON_NAME
if origin_direct_url_path.exists():
try:
self.origin = DirectUrl.from_json(
origin_direct_url_path.read_text(encoding="utf-8")
)
except Exception as e:
logger.warning(
"Ignoring invalid cache entry origin file %s for %s (%s)",
origin_direct_url_path,
link.filename,
e,
)
class WheelCache(Cache):
"""Wraps EphemWheelCache and SimpleWheelCache into a single Cache
This Cache allows for gracefully degradation, using the ephem wheel cache
when a certain link is not found in the simple wheel cache first.
"""
def __init__(self, cache_dir: str) -> None:
super().__init__(cache_dir)
self._wheel_cache = SimpleWheelCache(cache_dir)
self._ephem_cache = EphemWheelCache()
def get_path_for_link(self, link: Link) -> str:
return self._wheel_cache.get_path_for_link(link)
def get_ephem_path_for_link(self, link: Link) -> str:
return self._ephem_cache.get_path_for_link(link)
def get(
self,
link: Link,
package_name: str | None,
supported_tags: list[Tag],
) -> Link:
cache_entry = self.get_cache_entry(link, package_name, supported_tags)
if cache_entry is None:
return link
return cache_entry.link
def get_cache_entry(
self,
link: Link,
package_name: str | None,
supported_tags: list[Tag],
) -> CacheEntry | None:
"""Returns a CacheEntry with a link to a cached item if it exists or
None. The cache entry indicates if the item was found in the persistent
or ephemeral cache.
"""
retval = self._wheel_cache.get(
link=link,
package_name=package_name,
supported_tags=supported_tags,
)
if retval is not link:
return CacheEntry(retval, persistent=True)
retval = self._ephem_cache.get(
link=link,
package_name=package_name,
supported_tags=supported_tags,
)
if retval is not link:
return CacheEntry(retval, persistent=False)
return None
@staticmethod
def record_download_origin(cache_dir: str, download_info: DirectUrl) -> None:
origin_path = Path(cache_dir) / ORIGIN_JSON_NAME
if origin_path.exists():
try:
origin = DirectUrl.from_json(origin_path.read_text(encoding="utf-8"))
except Exception as e:
logger.warning(
"Could not read origin file %s in cache entry (%s). "
"Will attempt to overwrite it.",
origin_path,
e,
)
else:
# TODO: use DirectUrl.equivalent when
# https://github.com/pypa/pip/pull/10564 is merged.
if origin.url != download_info.url:
logger.warning(
"Origin URL %s in cache entry %s does not match download URL "
"%s. This is likely a pip bug or a cache corruption issue. "
"Will overwrite it with the new value.",
origin.url,
cache_dir,
download_info.url,
)
origin_path.write_text(download_info.to_json(), encoding="utf-8")
@@ -1,3 +0,0 @@
"""Subpackage containing all of pip's command line interface related code"""
# This file intentionally does not import submodules
@@ -1,187 +0,0 @@
"""Logic that powers autocompletion installed by ``pip completion``."""
from __future__ import annotations
import optparse
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
def autocomplete() -> None:
"""Entry Point for completion of main and subcommand options."""
# Don't complete if user hasn't sourced bash_completion file.
if "PIP_AUTO_COMPLETE" not in os.environ:
return
# Don't complete if autocompletion environment variables
# are not present
if not os.environ.get("COMP_WORDS") or not os.environ.get("COMP_CWORD"):
return
cwords = os.environ["COMP_WORDS"].split()[1:]
cword = int(os.environ["COMP_CWORD"])
try:
current = cwords[cword - 1]
except IndexError:
current = ""
parser = create_main_parser()
subcommands = list(commands_dict)
options = []
# subcommand
subcommand_name: str | None = None
for word in cwords:
if word in subcommands:
subcommand_name = word
break
# subcommand options
if subcommand_name is not None:
# special case: 'help' subcommand has no options
if subcommand_name == "help":
sys.exit(1)
# special case: list locally installed dists for show and uninstall
should_list_installed = not current.startswith("-") and subcommand_name in [
"show",
"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 = [
dist.canonical_name
for dist in env.iter_installed_distributions(local_only=True)
if dist.canonical_name.startswith(lc)
and dist.canonical_name not in cwords[1:]
]
# if there are no dists installed, fall back to option completion
if installed:
for dist in installed:
print(dist)
sys.exit(1)
should_list_installables = (
not current.startswith("-") and subcommand_name == "install"
)
if should_list_installables:
for path in auto_complete_paths(current, "path"):
print(path)
sys.exit(1)
subcommand = create_command(subcommand_name)
for opt in subcommand.parser.option_list_all:
if opt.help != optparse.SUPPRESS_HELP:
options += [
(opt_str, opt.nargs) for opt_str in opt._long_opts + opt._short_opts
]
# filter out previously specified options from available options
prev_opts = [x.split("=")[0] for x in cwords[1 : cword - 1]]
options = [(x, v) for (x, v) in options if x not in prev_opts]
# filter options by current input
options = [(k, v) for k, v in options if k.startswith(current)]
# get completion type given cwords and available subcommand options
completion_type = get_path_completion_type(
cwords,
cword,
subcommand.parser.option_list_all,
)
# get completion files and directories if ``completion_type`` is
# ``<file>``, ``<dir>`` or ``<path>``
if completion_type:
paths = auto_complete_paths(current, completion_type)
options = [(path, 0) for path in paths]
for option in options:
opt_label = option[0]
# append '=' to options which require args
if option[1] and option[0][:2] == "--":
opt_label += "="
print(opt_label)
# Complete sub-commands (unless one is already given).
if not any(name in cwords for name in subcommand.handler_map()):
for handler_name in subcommand.handler_map():
if handler_name.startswith(current):
print(handler_name)
else:
# show main parser options only when necessary
opts = [i.option_list for i in parser.option_groups]
opts.append(parser.option_list)
flattened_opts = chain.from_iterable(opts)
if current.startswith("-"):
for opt in flattened_opts:
if opt.help != optparse.SUPPRESS_HELP:
subcommands += opt._long_opts + opt._short_opts
else:
# get completion type given cwords and all available options
completion_type = get_path_completion_type(cwords, cword, flattened_opts)
if completion_type:
subcommands = list(auto_complete_paths(current, completion_type))
print(" ".join([x for x in subcommands if x.startswith(current)]))
sys.exit(1)
def get_path_completion_type(
cwords: list[str], cword: int, opts: Iterable[Any]
) -> 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 opts: The available options to check
:return: path completion type (``file``, ``dir``, ``path`` or None)
"""
if cword < 2 or not cwords[cword - 2].startswith("-"):
return None
for opt in opts:
if opt.help == optparse.SUPPRESS_HELP:
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
return None
def auto_complete_paths(current: str, completion_type: str) -> Iterable[str]:
"""If ``completion_type`` is ``file`` or ``path``, list all regular files
and directories starting with ``current``; otherwise only list directories
starting with ``current``.
:param current: The word to be completed
:param completion_type: path completion type(``file``, ``path`` or ``dir``)
:return: A generator of regular files and/or directories
"""
directory, filename = os.path.split(current)
current_path = os.path.abspath(directory)
# Don't complete paths if they can't be accessed
if not os.access(current_path, os.R_OK):
return
filename = os.path.normcase(filename)
# list all files that start with ``filename``
file_list = (
x for x in os.listdir(current_path) if os.path.normcase(x).startswith(filename)
)
for f in file_list:
opt = os.path.join(current_path, f)
comp_file = os.path.normcase(os.path.join(directory, f))
# complete regular files when there is not ``<dir>`` after option
# complete directories when there is ``<file>``, ``<path>`` or
# ``<dir>``after option
if completion_type != "dir" and os.path.isfile(opt):
yield comp_file
elif os.path.isdir(opt):
yield os.path.join(comp_file, "")
@@ -1,261 +0,0 @@
"""Base Command class, and related routines"""
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 pip._vendor.rich import reconfigure
from pip._vendor.rich import traceback as rich_traceback
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,
VIRTUALENV_NOT_FOUND,
)
from pip._internal.exceptions import (
BadCommand,
CommandError,
DiagnosticPipError,
InstallationError,
NetworkConnectionError,
PreviousBuildDirError,
)
from pip._internal.utils.filesystem import check_path_owner
from pip._internal.utils.logging import BrokenStdoutLoggingError, setup_logging
from pip._internal.utils.misc import get_prog, normalize_path
from pip._internal.utils.temp_dir import TempDirectoryTypeRegistry as TempDirRegistry
from pip._internal.utils.temp_dir import global_tempdir_manager, tempdir_registry
from pip._internal.utils.virtualenv import running_under_virtualenv
__all__ = ["Command"]
logger = logging.getLogger(__name__)
class Command(CommandContextMixIn):
usage: str = ""
ignore_require_venv: bool = False
def __init__(self, name: str, summary: str, isolated: bool = False) -> None:
super().__init__()
self.name = name
self.summary = summary
self.parser = ConfigOptionParser(
usage=self.usage,
prog=f"{get_prog()} {name}",
formatter=UpdatingDefaultsHelpFormatter(),
add_help_option=False,
name=name,
description=self.__doc__,
isolated=isolated,
)
self.tempdir_registry: TempDirRegistry | None = None
# Commands should add options to this option group
optgroup_name = f"{self.name.capitalize()} Options"
self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name)
# Add the general options
gen_opts = cmdoptions.make_option_group(
cmdoptions.general_group,
self.parser,
)
self.parser.add_option_group(gen_opts)
self.add_options()
def add_options(self) -> None:
pass
@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.
"""
# 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:
with self.pip_version_check(options, args):
return self.run(options, args)
if options.debug_mode:
rich_traceback.install(show_locals=True)
return _inner_run()
try:
status = _inner_run()
assert isinstance(status, int)
return status
except DiagnosticPipError as exc:
logger.error("%s", exc, extra={"rich": True})
logger.debug("Exception information:", exc_info=True)
return ERROR
except PreviousBuildDirError as exc:
logger.critical(str(exc))
logger.debug("Exception information:", exc_info=True)
return PREVIOUS_BUILD_DIR_ERROR
except (
InstallationError,
BadCommand,
NetworkConnectionError,
) as exc:
logger.critical(str(exc))
logger.debug("Exception information:", exc_info=True)
return ERROR
except CommandError as exc:
logger.critical("%s", exc)
logger.debug("Exception information:", exc_info=True)
return ERROR
except BrokenStdoutLoggingError:
# 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
# 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)
return ERROR
except BaseException:
logger.critical("Exception:", exc_info=True)
return UNKNOWN_ERROR
def parse_args(self, args: list[str]) -> tuple[Values, list[str]]:
# factored out for testability
return self.parser.parse_args(args)
def main(self, args: list[str]) -> int:
try:
with self.main_context():
return self._main(args)
finally:
logging.shutdown()
def _main(self, args: list[str]) -> int:
# We must initialize this before the tempdir manager, otherwise the
# configuration would not be accessible by the time we clean up the
# tempdir manager.
self.tempdir_registry = self.enter_context(tempdir_registry())
# Intentionally set as early as possible so globally-managed temporary
# directories are available to the rest of the code.
self.enter_context(global_tempdir_manager())
options, args = self.parse_args(args)
# Set verbosity so that it can be used elsewhere.
self.verbosity = options.verbose - options.quiet
if options.debug_mode:
self.verbosity = 2
if hasattr(options, "progress_bar") and options.progress_bar == "auto":
options.progress_bar = "on" if self.verbosity >= 0 else "off"
reconfigure(no_color=options.no_color)
level_number = setup_logging(
verbosity=self.verbosity,
no_color=options.no_color,
user_log_file=options.log,
)
always_enabled_features = set(options.features_enabled) & set(
cmdoptions.ALWAYS_ENABLED_FEATURES
)
if always_enabled_features:
logger.warning(
"The following features are always enabled: %s. ",
", ".join(sorted(always_enabled_features)),
)
# Make sure that the --python argument isn't specified after the
# subcommand. We can tell, because if --python was specified,
# we should only reach this point if we're running in the created
# subprocess, which has the _PIP_RUNNING_IN_SUBPROCESS environment
# variable set.
if options.python and "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
logger.critical(
"The --python option must be placed before the pip subcommand name"
)
sys.exit(ERROR)
# TODO: Try to get these passing down from the command?
# without resorting to os.environ to hold these.
# This also affects isolated builds and it should.
if options.no_input:
os.environ["PIP_NO_INPUT"] = "1"
if options.exists_action:
os.environ["PIP_EXISTS_ACTION"] = " ".join(options.exists_action)
if options.require_venv and not self.ignore_require_venv:
# If a venv is required check if it can really be found
if not running_under_virtualenv():
logger.critical("Could not find an activated virtualenv (required).")
sys.exit(VIRTUALENV_NOT_FOUND)
if options.cache_dir:
options.cache_dir = normalize_path(options.cache_dir)
if not check_path_owner(options.cache_dir):
logger.warning(
"The directory '%s' or its parent directory is not owned "
"or is not writable by the current user. The cache "
"has been disabled. Check the permissions and owner of "
"that directory. If executing pip with sudo, you should "
"use sudo's -H flag.",
options.cache_dir,
)
options.cache_dir = None
return self._run_wrapper(level_number, options, args)
def handler_map(self) -> dict[str, Callable[[Values, list[str]], None]]:
"""
map of names to handler actions for commands with sub-actions
"""
return {}
File diff suppressed because it is too large Load Diff
@@ -1,28 +0,0 @@
from collections.abc import Generator
from contextlib import AbstractContextManager, ExitStack, contextmanager
from typing import TypeVar
_T = TypeVar("_T", covariant=True)
class CommandContextMixIn:
def __init__(self) -> None:
super().__init__()
self._in_main_context = False
self._main_context = ExitStack()
@contextmanager
def main_context(self) -> Generator[None, None, None]:
assert not self._in_main_context
self._in_main_context = True
try:
with self._main_context:
yield
finally:
self._in_main_context = False
def enter_context(self, context_provider: AbstractContextManager[_T]) -> _T:
assert self._in_main_context
return self._main_context.enter_context(context_provider)
@@ -1,217 +0,0 @@
"""
Contains command classes which may interact with an index / the network.
Unlike its sister module, req_command, this module still uses lazy imports
so commands which don't always hit the network (e.g. list w/o --outdated or
--uptodate) don't need waste time importing PipSession and friends.
"""
from __future__ import annotations
import contextlib
import logging
import os
from collections.abc import Iterator
from functools import lru_cache
from optparse import Values
from typing import TYPE_CHECKING
from pip._vendor import certifi
from pip._internal.cli.base_command import Command
from pip._internal.cli.command_context import CommandContextMixIn
if TYPE_CHECKING:
from ssl import SSLContext
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:
try:
import ssl
except ImportError:
logger.warning("Disabling truststore since ssl support is missing")
return None
try:
from pip._vendor import truststore
except ImportError:
logger.warning("Disabling truststore because platform isn't supported")
return None
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.load_verify_locations(certifi.where())
return ctx
class SessionCommandMixin(CommandContextMixIn):
"""
A class mixin for command classes needing _build_session().
"""
def __init__(self) -> None:
super().__init__()
self._session: PipSession | None = None
@classmethod
def _get_index_urls(cls, options: Values) -> list[str] | None:
"""Return a list of index urls from user-provided options."""
index_urls = []
if not getattr(options, "no_index", False):
url = getattr(options, "index_url", None)
if url:
index_urls.append(url)
urls = getattr(options, "extra_index_urls", None)
if urls:
index_urls.extend(urls)
# Return None rather than an empty list
return index_urls or None
def get_default_session(self, options: Values) -> PipSession:
"""Get a default-managed session."""
if self._session is None:
self._session = self.enter_context(self._build_session(options))
# there's no type annotation on requests.Session, so it's
# automatically ContextManager[Any] and self._session becomes Any,
# then https://github.com/python/mypy/issues/7696 kicks in
assert self._session is not None
return self._session
def _build_session(
self,
options: Values,
retries: int | None = None,
timeout: int | None = None,
) -> PipSession:
from pip._internal.network.session import PipSession
cache_dir = options.cache_dir
assert not cache_dir or os.path.isabs(cache_dir)
if "legacy-certs" not in options.deprecated_features_enabled:
ssl_context = _create_truststore_ssl_context()
else:
ssl_context = None
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,
resume_retries=options.resume_retries,
trusted_hosts=options.trusted_hosts,
index_urls=self._get_index_urls(options),
ssl_context=ssl_context,
refresh_package=getattr(options, "refresh_package", set()),
)
# Handle custom ca-bundles from the user
if options.cert:
session.verify = options.cert
# Handle SSL client certificate
if options.client_cert:
session.cert = options.client_cert
# Handle timeouts
if options.timeout or timeout:
session.timeout = timeout if timeout is not None else options.timeout
# 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
session.auth.keyring_provider = options.keyring_provider
return session
def _pip_self_version_check_fetch(
session: PipSession, options: Values
) -> UpgradePrompt | None:
from pip._internal.self_outdated_check import pip_self_version_check_fetch
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):
"""
Abstract base class for commands with the index_group options.
This also corresponds to the commands that permit the pip version check.
"""
def should_exclude_prerelease(
self, options: Values, package_name: NormalizedName
) -> bool:
"""
Determine if pre-releases should be excluded for a package.
"""
# Check per-package release control settings
if options.release_control:
allow_prereleases = options.release_control.allows_prereleases(package_name)
if allow_prereleases is True:
return False # Include pre-releases
elif allow_prereleases is False:
return True # Exclude pre-releases
# No specific setting: exclude prereleases by default
return True
@contextlib.contextmanager
def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:
"""
Do the pip version check if not disabled.
This overrides the default behavior of not doing the check.
"""
# Make sure the index_group options are present.
assert hasattr(options, "no_index")
if options.disable_pip_version_check or options.no_index:
yield
return
upgrade_prompt: UpgradePrompt | None = None
try:
session = self._build_session(
options,
retries=0,
timeout=min(5, options.timeout),
)
with session:
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)
@@ -1,86 +0,0 @@
"""Primary application entrypoint."""
from __future__ import annotations
import locale
import logging
import os
import sys
import warnings
logger = logging.getLogger(__name__)
# Do not import and use main() directly! Using it directly is actively
# discouraged by pip's maintainers. The name, location and behavior of
# this function is subject to change, so calling it directly is not
# portable across different pip versions.
# In addition, running pip in-process is unsupported and unsafe. This is
# elaborated in detail at
# https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.
# That document also provides suggestions that should work for nearly
# all users that are considering importing and using main() directly.
# However, we know that certain users will still want to invoke pip
# in-process. If you understand and accept the implications of using pip
# in an unsupported manner, the best approach is to use runpy to avoid
# depending on the exact location of this entry point.
# The following example shows how to use runpy to invoke pip in that
# case:
#
# sys.argv = ["pip", your, args, here]
# runpy.run_module("pip", run_name="__main__")
#
# Note that this will exit the process after running, unlike a direct
# call to main. As it is not safe to do any processing after calling
# main, this should not be an issue in practice.
def main(args: list[str] | None = None) -> 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
# if we use ``multiprocessing`` module,
# which imports __main__ for each spawned subprocess.
from pip._internal.cli.autocompletion import autocomplete
from pip._internal.cli.main_parser import parse_command
from pip._internal.commands import create_command
from pip._internal.exceptions import PipError
from pip._internal.utils import deprecation
if args is None:
args = sys.argv[1:]
# Suppress the pkg_resources deprecation warning
# Note - we use a module of .*pkg_resources to cover
# the normal case (pip._vendor.pkg_resources) and the
# devendored case (a bare pkg_resources)
warnings.filterwarnings(
action="ignore", category=DeprecationWarning, module=".*pkg_resources"
)
# Configure our deprecation warnings to be sent through loggers
deprecation.install_warning_logger()
autocomplete()
try:
cmd_name, cmd_args = parse_command(args)
except PipError as exc:
sys.stderr.write(f"ERROR: {exc}")
sys.stderr.write(os.linesep)
sys.exit(1)
# 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))
return command.main(cmd_args)
@@ -1,135 +0,0 @@
"""A single place for constructing and exposing the main parser"""
from __future__ import annotations
import os
import subprocess
import sys
from pip._vendor.rich.markup import escape
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, get_runnable_pip
__all__ = ["create_main_parser", "parse_command"]
def create_main_parser() -> ConfigOptionParser:
"""Creates and returns the main parser for pip's CLI"""
parser = ConfigOptionParser(
usage="\n%prog <command> [options]",
add_help_option=False,
formatter=UpdatingDefaultsHelpFormatter(),
name="global",
prog=get_prog(),
)
parser.disable_interspersed_args()
parser.version = get_pip_version()
# add the general options
gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser)
parser.add_option_group(gen_opts)
# so the help formatter knows
parser.main = True # type: ignore
# create command listing for description
description = [""] + [
f"[optparse.longargs]{name:27}[/] {escape(command_info.summary)}"
for name, command_info in commands_dict.items()
]
parser.description = "\n".join(description)
return parser
def identify_python_interpreter(python: str) -> str | None:
# If the named file exists, use it.
# If it's a directory, assume it's a virtual environment and
# look for the environment's Python executable.
if os.path.exists(python):
if os.path.isdir(python):
# bin/python for Unix, Scripts/python.exe for Windows
# Try both in case of odd cases like cygwin.
for exe in ("bin/python", "Scripts/python.exe"):
py = os.path.join(python, exe)
if os.path.exists(py):
return py
else:
return python
# Could not find the interpreter specified
return None
def parse_command(args: list[str]) -> tuple[str, list[str]]:
parser = create_main_parser()
# Note: parser calls disable_interspersed_args(), so the result of this
# call is to split the initial args into the general options before the
# subcommand and everything else.
# For example:
# args: ['--timeout=5', 'install', '--user', 'INITools']
# general_options: ['--timeout==5']
# args_else: ['install', '--user', 'INITools']
general_options, args_else = parser.parse_args(args)
# --python
if general_options.python and "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
# Re-invoke pip using the specified Python interpreter
interpreter = identify_python_interpreter(general_options.python)
if interpreter is None:
raise CommandError(
f"Could not locate Python interpreter {general_options.python}"
)
pip_cmd = [
interpreter,
get_runnable_pip(),
]
pip_cmd.extend(args)
# Set a flag so the child doesn't re-invoke itself, causing
# an infinite loop.
os.environ["_PIP_RUNNING_IN_SUBPROCESS"] = "1"
returncode = 0
try:
proc = subprocess.run(pip_cmd)
returncode = proc.returncode
except (subprocess.SubprocessError, OSError) as exc:
raise CommandError(f"Failed to run pip under {interpreter}: {exc}")
sys.exit(returncode)
# --version
if general_options.version:
sys.stdout.write(parser.version)
sys.stdout.write(os.linesep)
sys.exit()
# pip || pip help -> print_help()
if not args_else or (args_else[0] == "help" and len(args_else) == 1):
parser.print_help()
sys.exit()
# the subcommand name
cmd_name = args_else[0]
if cmd_name not in commands_dict:
guess = get_similar_commands(cmd_name)
msg = [f'unknown command "{cmd_name}"']
if guess:
msg.append(f'maybe you meant "{guess}"')
raise CommandError(" - ".join(msg))
# all the args without the subcommand
cmd_args = args[:]
cmd_args.remove(cmd_name)
return cmd_name, cmd_args
@@ -1,366 +0,0 @@
"""Base option parser setup"""
from __future__ import annotations
import logging
import optparse
import os
import re
import shutil
import sys
import textwrap
from collections.abc import Generator
from contextlib import suppress
from typing import 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
logger = logging.getLogger(__name__)
class PrettyHelpFormatter(optparse.IndentedHelpFormatter):
"""A prettier/less verbose help formatter for optparse."""
styles = {
"optparse.shortargs": "green",
"optparse.longargs": "cyan",
"optparse.groups": "bold blue",
"optparse.metavar": "yellow",
}
highlights = {
r"\s(-{1}[\w]+[\w-]*)": "shortargs", # highlight -letter as short args
r"\s(-{2}[\w]+[\w-]*)": "longargs", # highlight --words as long args
}
def __init__(self, *args: Any, **kwargs: Any) -> None:
# help position must be aligned with __init__.parseopts.description
kwargs["max_help_position"] = 30
kwargs["indent_increment"] = 1
kwargs["width"] = shutil.get_terminal_size()[0] - 2
super().__init__(*args, **kwargs)
def format_option_strings(self, option: optparse.Option) -> str:
"""Return a comma-separated list of option strings and metavars."""
opts = []
if option._short_opts:
opts.append(f"[optparse.shortargs]{option._short_opts[0]}[/]")
if option._long_opts:
opts.append(f"[optparse.longargs]{option._long_opts[0]}[/]")
if len(opts) > 1:
opts.insert(1, ", ")
if option.takes_value():
assert option.dest is not None
metavar = option.metavar or option.dest.lower()
opts.append(f" [optparse.metavar]<{escape(metavar.lower())}>[/]")
return "".join(opts)
def format_option(self, option: optparse.Option) -> str:
"""Overridden method with Rich support."""
# fmt: off
result = []
opts = self.option_strings[option]
opt_width = self.help_position - self.current_indent - 2
# Remove the rich style tags before calculating width during
# text wrap calculations. Also store the length removed to adjust
# the padding in the else branch.
stripped = re.sub(r"(\[[a-z.]+\])|(\[\/\])", "", opts)
style_tag_length = len(opts) - len(stripped)
if len(stripped) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts) # noqa: UP031
indent_first = self.help_position
else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", # noqa: UP031
opt_width + style_tag_length, opts)
indent_first = 0
result.append(opts)
if option.help:
help_text = self.expand_default(option)
help_lines = textwrap.wrap(help_text, self.help_width)
result.append("%*s%s\n" % (indent_first, "", help_lines[0])) # noqa: UP031
result.extend(["%*s%s\n" % (self.help_position, "", line) # noqa: UP031
for line in help_lines[1:]])
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)
# fmt: on
def format_heading(self, heading: str) -> str:
if heading == "Options":
return ""
return "[optparse.groups]" + escape(heading) + ":[/]\n"
def format_usage(self, usage: str) -> str:
"""
Ensure there is only one newline between usage and the first heading
if there is no description.
"""
contents = self.indent_lines(textwrap.dedent(usage), " ")
msg = f"\n[optparse.groups]Usage:[/] {escape(contents)}\n"
return msg
def format_description(self, description: str | None) -> str:
# leave full control over description to us
if description:
if hasattr(self.parser, "main"):
label = "[optparse.groups]Commands:[/]"
else:
label = "[optparse.groups]Description:[/]"
# some doc strings have initial newlines, some don't
description = description.lstrip("\n")
# some doc strings have final newlines and spaces, some don't
description = description.rstrip()
# dedent, then reindent
description = self.indent_lines(textwrap.dedent(description), " ")
description = f"{label}\n{description}\n"
return description
else:
return ""
def format_epilog(self, epilog: str | None) -> str:
# leave full control over epilog to us
if epilog:
return escape(epilog)
else:
return ""
def expand_default(self, option: optparse.Option) -> str:
"""Overridden HelpFormatter.expand_default() which colorizes flags."""
help = escape(super().expand_default(option))
for regex, style in self.highlights.items():
help = re.sub(regex, rf"[optparse.{style}] \1[/]", help)
return help
def indent_lines(self, text: str, indent: str) -> str:
new_lines = [indent + line for line in text.split("\n")]
return "\n".join(new_lines)
class UpdatingDefaultsHelpFormatter(PrettyHelpFormatter):
"""Custom help formatter for use in ConfigOptionParser.
This is updates the defaults before expanding them, allowing
them to show up correctly in the help listing.
Also redact auth from url type options
"""
def expand_default(self, option: optparse.Option) -> str:
default_values = None
if self.parser is not None:
assert isinstance(self.parser, ConfigOptionParser)
self.parser._update_defaults(self.parser.defaults)
assert option.dest is not None
default_values = self.parser.defaults.get(option.dest)
help_text = super().expand_default(option)
if default_values and option.metavar == "URL":
if isinstance(default_values, str):
default_values = [default_values]
# If its not a list, we should abort and just return the help text
if not isinstance(default_values, list):
default_values = []
for val in default_values:
help_text = help_text.replace(val, redact_auth_from_url(val))
return help_text
class CustomOptionParser(optparse.OptionParser):
def insert_option_group(
self, idx: int, *args: Any, **kwargs: Any
) -> optparse.OptionGroup:
"""Insert an OptionGroup at a given position."""
group = self.add_option_group(*args, **kwargs)
self.option_groups.pop()
self.option_groups.insert(idx, group)
return group
@property
def option_list_all(self) -> list[optparse.Option]:
"""Get a list of all options, including those in option groups."""
res = self.option_list[:]
for i in self.option_groups:
res.extend(i.option_list)
return res
class ConfigOptionParser(CustomOptionParser):
"""Custom option parser which updates its defaults by checking the
configuration files and environmental variables"""
def __init__(
self,
*args: Any,
name: str,
isolated: bool = False,
**kwargs: Any,
) -> None:
self.name = name
self.config = Configuration(isolated)
assert self.name
super().__init__(*args, **kwargs)
def check_default(self, option: optparse.Option, key: str, val: Any) -> Any:
try:
return option.check_value(key, val)
except optparse.OptionValueError as exc:
print(f"An error occurred during configuration: {exc}")
sys.exit(3)
def _get_ordered_configuration_items(
self,
) -> Generator[tuple[str, Any], None, None]:
# Configuration gives keys in an unordered manner. Order them.
override_order = ["global", self.name, ":env:"]
# Pool the options into different groups
# Use a dict because we need to implement the fallthrough logic after PR 12201
# was merged which removed the fallthrough logic for options
section_items_dict: dict[str, dict[str, Any]] = {
name: {} for name in override_order
}
for _, value in self.config.items():
for section_key, val in value.items():
section, key = section_key.split(".", 1)
if section in override_order:
section_items_dict[section][key] = val
# Now that we a dict of items per section, convert to list of tuples
# Make sure we completely remove empty values again
section_items = {
name: [(k, v) for k, v in section_items_dict[name].items() if v]
for name in override_order
}
# Yield each group in their override order
for section in override_order:
yield from section_items[section]
def _update_defaults(self, defaults: dict[str, Any]) -> dict[str, Any]:
"""Updates the given defaults with values from the config files and
the environ. Does a little special handling for certain types of
options (lists)."""
# Accumulate complex default state.
self.values = optparse.Values(self.defaults)
late_eval = set()
# Then set the options with those values
for key, val in self._get_ordered_configuration_items():
# '--' because configuration supports only long names
option = self.get_option("--" + key)
# Ignore options not present in this parser. E.g. non-globals put
# in [global] by users that want them to apply to all applicable
# commands.
if option is None:
continue
assert option.dest is not None
if option.action in ("store_true", "store_false"):
try:
val = strtobool(val)
except ValueError:
self.error(
f"{val} is not a valid value for {key} option, "
"please specify a boolean value like yes/no, "
"true/false or 1/0 instead."
)
elif option.action == "count":
with suppress(ValueError):
val = strtobool(val)
with suppress(ValueError):
val = int(val)
if not isinstance(val, int) or val < 0:
self.error(
f"{val} is not a valid value for {key} option, "
"please instead specify either a non-negative integer "
"or a boolean value like yes/no or false/true "
"which is equivalent to 1/0."
)
elif option.action == "append":
val = val.split()
val = [self.check_default(option, key, v) for v in val]
elif option.action == "callback":
assert option.callback is not None
late_eval.add(option.dest)
opt_str = option.get_opt_string()
val = option.convert_value(opt_str, val)
# From take_action
args = option.callback_args or ()
kwargs = option.callback_kwargs or {}
option.callback(option, opt_str, val, self, *args, **kwargs)
else:
val = self.check_default(option, key, val)
defaults[option.dest] = val
for key in late_eval:
defaults[key] = getattr(self.values, key)
self.values = None
return defaults
def get_default_values(self) -> optparse.Values:
"""Overriding to make updating the defaults after instantiation of
the option parser possible, _update_defaults() does the dirty work."""
if not self.process_default_values:
# Old, pre-Optik 1.5 behaviour.
return optparse.Values(self.defaults)
# Load the configuration, or error out in case of an error
try:
self.config.load()
except ConfigurationError as err:
self.exit(UNKNOWN_ERROR, str(err))
defaults = self._update_defaults(self.defaults.copy()) # ours
for option in self._get_all_options():
assert option.dest is not None
default = defaults.get(option.dest)
if isinstance(default, str):
opt_str = option.get_opt_string()
defaults[option.dest] = option.check_value(opt_str, default)
return optparse.Values(defaults)
def error(self, msg: str) -> NoReturn:
self.print_usage(sys.stderr)
self.exit(UNKNOWN_ERROR, f"{msg}\n")
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 = (
"--no-color" in sys.argv
or bool(strtobool(os.environ.get("PIP_NO_COLOR", "no") or "no"))
or "NO_COLOR" in os.environ
)
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)
@@ -1,153 +0,0 @@
from __future__ import annotations
import functools
import sys
from collections.abc import Callable, Generator, Iterable, Iterator
from typing import TYPE_CHECKING, Literal, TypeVar
from pip._vendor.rich.progress import (
BarColumn,
DownloadColumn,
FileSizeColumn,
MofNCompleteColumn,
Progress,
ProgressColumn,
SpinnerColumn,
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
TransferSpeedColumn,
)
from pip._internal.cli.spinners import RateLimiter
from pip._internal.utils.logging import get_console, get_indentation
if TYPE_CHECKING:
from pip._internal.req.req_install import InstallRequirement
T = TypeVar("T")
ProgressRenderer = Callable[[Iterable[T]], Iterator[T]]
BarType = Literal["on", "off", "raw"]
def _rich_download_progress_bar(
iterable: Iterable[bytes],
*,
bar_type: BarType,
size: int | None,
initial_progress: int | None = None,
) -> Generator[bytes, None, None]:
assert bar_type == "on", "This should only be used in the default mode."
if not size:
total = float("inf")
columns: tuple[ProgressColumn, ...] = (
TextColumn("[progress.description]{task.description}"),
SpinnerColumn("line", speed=1.5),
FileSizeColumn(),
TransferSpeedColumn(),
TimeElapsedColumn(),
)
else:
total = size
columns = (
TextColumn("[progress.description]{task.description}"),
BarColumn(),
DownloadColumn(),
TransferSpeedColumn(),
TextColumn("{task.fields[time_description]}"),
TimeRemainingColumn(elapsed_when_finished=True),
)
progress = Progress(*columns, refresh_per_second=5)
task_id = progress.add_task(
" " * (get_indentation() + 2), total=total, time_description="eta"
)
if initial_progress is not None:
progress.update(task_id, advance=initial_progress)
with progress:
for chunk in iterable:
yield chunk
progress.update(task_id, advance=len(chunk))
progress.update(task_id, time_description="")
def _rich_install_progress_bar(
iterable: Iterable[InstallRequirement], *, total: int
) -> Iterator[InstallRequirement]:
columns = (
TextColumn("{task.fields[indent]}"),
BarColumn(),
MofNCompleteColumn(),
TextColumn("{task.description}"),
)
console = get_console()
bar = Progress(*columns, refresh_per_second=6, console=console, transient=True)
# Hiding the progress bar at initialization forces a refresh cycle to occur
# until the bar appears, avoiding very short flashes.
task = bar.add_task("", total=total, indent=" " * get_indentation(), visible=False)
with bar:
for req in iterable:
bar.update(task, description=rf"\[{req.name}]", visible=True)
yield req
bar.advance(task)
def _raw_progress_bar(
iterable: Iterable[bytes],
*,
size: int | None,
initial_progress: int | None = None,
) -> Generator[bytes, None, None]:
def write_progress(current: int, total: int) -> None:
sys.stdout.write(f"Progress {current} of {total}\n")
sys.stdout.flush()
current = initial_progress or 0
total = size or 0
rate_limiter = RateLimiter(0.25)
write_progress(current, total)
for chunk in iterable:
current += len(chunk)
if rate_limiter.ready() or current == total:
write_progress(current, total)
rate_limiter.reset()
yield chunk
def get_download_progress_renderer(
*, bar_type: BarType, size: int | None = None, initial_progress: int | None = None
) -> ProgressRenderer[bytes]:
"""Get an object that can be used to render the download progress.
Returns a callable, that takes an iterable to "wrap".
"""
if bar_type == "on":
return functools.partial(
_rich_download_progress_bar,
bar_type=bar_type,
size=size,
initial_progress=initial_progress,
)
elif bar_type == "raw":
return functools.partial(
_raw_progress_bar,
size=size,
initial_progress=initial_progress,
)
else:
return iter # no-op, when passed an iterator
def get_install_progress_renderer(
*, bar_type: BarType, total: int
) -> ProgressRenderer[InstallRequirement]:
"""Get an object that can be used to render the install progress.
Returns a callable, that takes an iterable to "wrap".
"""
if bar_type == "on":
return functools.partial(_rich_install_progress_bar, total=total)
else:
return iter
@@ -1,504 +0,0 @@
"""Contains the RequirementCommand base class.
This class is in a separate module so the commands that do not always
need PackageFinder capability don't unnecessarily import the
PackageFinder machinery and all its vendored dependencies, etc.
"""
from __future__ import annotations
import logging
import os
from collections.abc import Callable
from functools import partial
from optparse import Values
from typing import Any, TypeVar
from pip._internal.build_env import (
BuildEnvironmentInstaller,
BuildIsolationMode,
InprocessBuildEnvironmentInstaller,
SubprocessBuildEnvironmentInstaller,
)
from pip._internal.cache import WheelCache
from pip._internal.cli import cmdoptions
from pip._internal.cli.cmdoptions import make_target_python
from pip._internal.cli.index_command import IndexGroupCommand
from pip._internal.cli.index_command import SessionCommandMixin as SessionCommandMixin
from pip._internal.exceptions import (
CommandError,
PreviousBuildDirError,
UnsupportedPythonVersion,
)
from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.models.target_python import TargetPython
from pip._internal.network.session import PipSession
from pip._internal.operations.build.build_tracker import BuildTracker
from pip._internal.operations.prepare import RequirementPreparer
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_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,
TempDirectoryTypeRegistry,
tempdir_kinds,
)
logger = logging.getLogger(__name__)
def should_ignore_regular_constraints() -> bool:
"""
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,
]
_CommandT = TypeVar("_CommandT", bound="RequirementCommand")
def with_cleanup(
func: Callable[[_CommandT, Values, list[str]], int],
) -> Callable[[_CommandT, Values, list[str]], int]:
"""Decorator for common logic related to managing temporary
directories.
"""
def configure_tempdir_registry(registry: TempDirectoryTypeRegistry) -> None:
for t in KEEPABLE_TEMPDIR_TYPES:
registry.set_delete(t, False)
def wrapper(self: _CommandT, options: Values, args: list[str]) -> int:
assert self.tempdir_registry is not None
if options.no_clean:
configure_tempdir_registry(self.tempdir_registry)
try:
return func(self, options, args)
except PreviousBuildDirError:
# This kind of conflict can occur when the user passes an explicit
# build directory with a pre-existing folder. In that case we do
# not want to accidentally remove it.
configure_tempdir_registry(self.tempdir_registry)
raise
return wrapper
def parse_constraint_files(
constraint_files: list[str],
finder: PackageFinder,
options: Values,
session: PipSession,
) -> list[InstallRequirement]:
requirements = []
for filename in constraint_files:
for parsed_req in parse_requirements(
filename,
constraint=True,
finder=finder,
options=options,
session=session,
):
req_to_add = install_req_from_parsed_requirement(
parsed_req,
isolated=options.isolated_mode,
user_supplied=False,
)
requirements.append(req_to_add)
return requirements
class RequirementCommand(IndexGroupCommand):
def __init__(self, *args: Any, **kw: Any) -> None:
super().__init__(*args, **kw)
self.cmd_opts.add_option(cmdoptions.dependency_groups())
self.cmd_opts.add_option(cmdoptions.no_clean())
@staticmethod
def determine_resolver_variant(options: Values) -> str:
"""Determines which resolver should be used, based on the given options."""
if "legacy-resolver" in options.deprecated_features_enabled:
return "legacy"
return "resolvelib"
@classmethod
def make_requirement_preparer(
cls,
temp_build_dir: TempDirectory,
options: Values,
build_tracker: BuildTracker,
session: PipSession,
finder: PackageFinder,
*,
allow_editables: bool,
use_user_site: bool,
download_dir: str | None = None,
verbosity: int = 0,
) -> RequirementPreparer:
"""
Create a RequirementPreparer instance for the given parameters.
"""
temp_build_dir_path = temp_build_dir.path
assert temp_build_dir_path is not None
legacy_resolver = False
resolver_variant = cls.determine_resolver_variant(options)
if resolver_variant == "resolvelib":
lazy_wheel = "fast-deps" in options.features_enabled
if lazy_wheel:
logger.warning(
"pip is using lazily downloaded wheels using HTTP "
"range requests to obtain dependency information. "
"This experimental feature is enabled through "
"--use-feature=fast-deps and it is not ready for "
"production."
)
else:
legacy_resolver = True
lazy_wheel = False
if "fast-deps" in options.features_enabled:
logger.warning(
"fast-deps has no effect when used with the legacy resolver."
)
# Handle build constraints
build_constraints = getattr(options, "build_constraints", [])
env_installer: BuildEnvironmentInstaller
if "inprocess-build-deps" in options.features_enabled:
build_constraint_reqs = parse_constraint_files(
build_constraints, finder, options, session
)
env_installer = InprocessBuildEnvironmentInstaller(
finder=finder,
build_tracker=build_tracker,
build_constraints=build_constraint_reqs,
verbosity=verbosity,
wheel_cache=WheelCache(options.cache_dir),
)
else:
env_installer = SubprocessBuildEnvironmentInstaller(
finder,
build_constraints=build_constraints,
)
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=build_isolation,
build_isolation_installer=env_installer,
check_build_deps=options.check_build_deps,
build_tracker=build_tracker,
session=session,
progress_bar=options.progress_bar,
finder=finder,
require_hashes=options.require_hashes,
use_user_site=use_user_site,
lazy_wheel=lazy_wheel,
verbosity=verbosity,
legacy_resolver=legacy_resolver,
allow_editables=allow_editables,
)
@classmethod
def make_resolver(
cls,
preparer: RequirementPreparer,
finder: PackageFinder,
options: Values,
wheel_cache: WheelCache | None = None,
use_user_site: bool = False,
ignore_installed: bool = True,
ignore_requires_python: bool = False,
force_reinstall: bool = False,
upgrade_strategy: str = "to-satisfy-only",
py_version_info: tuple[int, ...] | None = None,
) -> BaseResolver:
"""
Create a Resolver instance for the given parameters.
"""
make_install_req = partial(
install_req_from_req_string,
isolated=options.isolated_mode,
)
resolver_variant = cls.determine_resolver_variant(options)
# The long import name and duplicated invocation is needed to convince
# Mypy into correctly typechecking. Otherwise it would complain the
# "Resolver" class being redefined.
if resolver_variant == "resolvelib":
import pip._internal.resolution.resolvelib.resolver
return pip._internal.resolution.resolvelib.resolver.Resolver(
preparer=preparer,
finder=finder,
wheel_cache=wheel_cache,
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,
upgrade_strategy=upgrade_strategy,
py_version_info=py_version_info,
)
import pip._internal.resolution.legacy.resolver
return pip._internal.resolution.legacy.resolver.Resolver(
preparer=preparer,
finder=finder,
wheel_cache=wheel_cache,
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,
upgrade_strategy=upgrade_strategy,
py_version_info=py_version_info,
)
def get_requirements(
self,
args: list[str],
options: Values,
finder: PackageFinder,
session: PipSession,
) -> list[InstallRequirement]:
"""
Parse command-line arguments into the corresponding requirements.
"""
requirements: list[InstallRequirement] = []
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():
continue
req_to_add = install_req_from_line(
req,
comes_from=None,
isolated=options.isolated_mode,
user_supplied=True,
config_settings=getattr(options, "config_settings", None),
)
requirements.append(req_to_add)
if options.dependency_groups:
for req in parse_dependency_groups(options.dependency_groups):
req_to_add = install_req_from_req_string(
req,
isolated=options.isolated_mode,
user_supplied=True,
)
requirements.append(req_to_add)
for req in options.editables:
req_to_add = install_req_from_editable(
req,
user_supplied=True,
isolated=options.isolated_mode,
config_settings=getattr(options, "config_settings", None),
)
requirements.append(req_to_add)
# NOTE: options.require_hashes may be set if --require-hashes is True
for filename in options.requirements:
if is_valid_pylock_filename(filename):
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(
filename, session=session
):
req_to_add, locked_link = install_req_from_pylock_package(
package,
package_dist,
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
):
req_to_add = install_req_from_parsed_requirement(
parsed_req,
isolated=options.isolated_mode,
user_supplied=True,
config_settings=(
parsed_req.options.get("config_settings")
if parsed_req.options
else None
),
)
requirements.append(req_to_add)
if options.requirements_from_scripts:
if len(options.requirements_from_scripts) > 1:
raise CommandError("--requirements-from-script can only be given once")
script = options.requirements_from_scripts[0]
try:
script_metadata = pep723_metadata(script)
except PEP723Exception as exc:
raise CommandError(exc.msg)
script_requires_python = script_metadata.get("requires-python", "")
if script_requires_python and not options.ignore_requires_python:
target_python = make_target_python(options)
if not check_requires_python(
requires_python=script_requires_python,
version_info=target_python.py_version_info,
):
raise UnsupportedPythonVersion(
f"Script {script!r} requires a different Python: "
f"{target_python.py_version} not in {script_requires_python!r}"
)
for req in script_metadata.get("dependencies", []):
req_to_add = install_req_from_req_string(
req,
isolated=options.isolated_mode,
user_supplied=True,
)
requirements.append(req_to_add)
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 (
args
or options.editables
or options.requirements
or options.dependency_groups
or options.requirements_from_scripts
):
opts = {"name": self.name}
if options.find_links:
raise CommandError(
"You must give at least one requirement to {name} "
'(maybe you meant "pip {name} {links}"?)'.format(
**dict(opts, links=" ".join(options.find_links))
)
)
else:
raise CommandError(
"You must give at least one requirement to {name} "
'(see "pip help {name}")'.format(**opts)
)
return requirements
@staticmethod
def trace_basic_info(finder: PackageFinder) -> None:
"""
Trace basic information about the provided objects.
"""
# Display where finder is looking for packages
search_scope = finder.search_scope
locations = search_scope.get_formatted_locations()
if locations:
logger.info(locations)
def _build_package_finder(
self,
options: Values,
session: PipSession,
target_python: TargetPython | None = None,
ignore_requires_python: bool = False,
) -> PackageFinder:
"""
Create a package finder appropriate to this requirement command.
:param ignore_requires_python: Whether to ignore incompatible
"Requires-Python" values in links. Defaults to False.
"""
link_collector = LinkCollector.create(session, options=options)
selection_prefs = SelectionPreferences(
allow_yanked=True,
format_control=options.format_control,
release_control=options.release_control,
prefer_binary=options.prefer_binary,
ignore_requires_python=ignore_requires_python,
)
return PackageFinder.create(
link_collector=link_collector,
selection_prefs=selection_prefs,
target_python=target_python,
uploaded_prior_to=options.uploaded_prior_to,
)
@@ -1,235 +0,0 @@
from __future__ import annotations
import contextlib
import itertools
import logging
import sys
import time
from collections.abc import Generator
from typing import IO, Final
from pip._vendor.rich.console import (
Console,
ConsoleOptions,
RenderableType,
RenderResult,
)
from pip._vendor.rich.live import Live
from pip._vendor.rich.measure import Measurement
from pip._vendor.rich.text import Text
from pip._internal.utils.compat import WINDOWS
from pip._internal.utils.logging import get_console, get_indentation
logger = logging.getLogger(__name__)
SPINNER_CHARS: Final = r"-\|/"
SPINS_PER_SECOND: Final = 8
class SpinnerInterface:
def spin(self) -> None:
raise NotImplementedError()
def finish(self, final_status: str) -> None:
raise NotImplementedError()
class InteractiveSpinner(SpinnerInterface):
def __init__(
self,
message: str,
file: IO[str] | None = None,
spin_chars: str = SPINNER_CHARS,
# Empirically, 8 updates/second looks nice
min_update_interval_seconds: float = 1 / SPINS_PER_SECOND,
):
self._message = message
if file is None:
file = sys.stdout
self._file = file
self._rate_limiter = RateLimiter(min_update_interval_seconds)
self._finished = False
self._spin_cycle = itertools.cycle(spin_chars)
self._file.write(" " * get_indentation() + self._message + " ... ")
self._width = 0
def _write(self, status: str) -> None:
assert not self._finished
# Erase what we wrote before by backspacing to the beginning, writing
# spaces to overwrite the old text, and then backspacing again
backup = "\b" * self._width
self._file.write(backup + " " * self._width + backup)
# Now we have a blank slate to add our status
self._file.write(status)
self._width = len(status)
self._file.flush()
self._rate_limiter.reset()
def spin(self) -> None:
if self._finished:
return
if not self._rate_limiter.ready():
return
self._write(next(self._spin_cycle))
def finish(self, final_status: str) -> None:
if self._finished:
return
self._write(final_status)
self._file.write("\n")
self._file.flush()
self._finished = True
# Used for dumb terminals, non-interactive installs (no tty), etc.
# We still print updates occasionally (once every 60 seconds by default) to
# act as a keep-alive for systems like Travis-CI that take lack-of-output as
# an indication that a task has frozen.
class NonInteractiveSpinner(SpinnerInterface):
def __init__(self, message: str, min_update_interval_seconds: float = 60.0) -> None:
self._message = message
self._finished = False
self._rate_limiter = RateLimiter(min_update_interval_seconds)
self._update("started")
def _update(self, status: str) -> None:
assert not self._finished
self._rate_limiter.reset()
logger.info("%s: %s", self._message, status)
def spin(self) -> None:
if self._finished:
return
if not self._rate_limiter.ready():
return
self._update("still running...")
def finish(self, final_status: str) -> None:
if self._finished:
return
self._update(f"finished with status '{final_status}'")
self._finished = True
class RateLimiter:
def __init__(self, min_update_interval_seconds: float) -> None:
self._min_update_interval_seconds = min_update_interval_seconds
self._last_update: float = 0
def ready(self) -> bool:
now = time.time()
delta = now - self._last_update
return delta >= self._min_update_interval_seconds
def reset(self) -> None:
self._last_update = time.time()
@contextlib.contextmanager
def open_spinner(message: str) -> Generator[SpinnerInterface, None, None]:
# Interactive spinner goes directly to sys.stdout rather than being routed
# through the logging system, but it acts like it has level INFO,
# i.e. it's only displayed if we're at level INFO or better.
# Non-interactive spinner goes through the logging system, so it is always
# in sync with logging configuration.
if sys.stdout.isatty() and logger.getEffectiveLevel() <= logging.INFO:
spinner: SpinnerInterface = InteractiveSpinner(message)
else:
spinner = NonInteractiveSpinner(message)
try:
with hidden_cursor(sys.stdout):
yield spinner
except KeyboardInterrupt:
spinner.finish("canceled")
raise
except Exception:
spinner.finish("error")
raise
else:
spinner.finish("done")
class _PipRichSpinner:
"""
Custom rich spinner that matches the style of the legacy spinners.
(*) Updates will be handled in a background thread by a rich live panel
which will call render() automatically at the appropriate time.
"""
def __init__(self, label: str) -> None:
self.label = label
self._spin_cycle = itertools.cycle(SPINNER_CHARS)
self._spinner_text = ""
self._finished = False
self._indent = get_indentation() * " "
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
yield self.render()
def __rich_measure__(
self, console: Console, options: ConsoleOptions
) -> Measurement:
text = self.render()
return Measurement.get(console, options, text)
def render(self) -> RenderableType:
if not self._finished:
self._spinner_text = next(self._spin_cycle)
return Text.assemble(self._indent, self.label, " ... ", self._spinner_text)
def finish(self, status: str) -> None:
"""Stop spinning and set a final status message."""
self._spinner_text = status
self._finished = True
@contextlib.contextmanager
def open_rich_spinner(label: str, console: Console | None = None) -> Generator[None]:
if not logger.isEnabledFor(logging.INFO):
# Don't show spinner if --quiet is given.
yield
return
console = console or get_console()
spinner = _PipRichSpinner(label)
with Live(spinner, refresh_per_second=SPINS_PER_SECOND, console=console):
try:
yield
except KeyboardInterrupt:
spinner.finish("canceled")
raise
except Exception:
spinner.finish("error")
raise
else:
spinner.finish("done")
HIDE_CURSOR = "\x1b[?25l"
SHOW_CURSOR = "\x1b[?25h"
@contextlib.contextmanager
def hidden_cursor(file: IO[str]) -> Generator[None, None, None]:
# The Windows terminal does not support the hide/show cursor ANSI codes,
# even via colorama. So don't even try.
if WINDOWS:
yield
# We don't want to clutter the output with control characters if we're
# writing to a file, or if the user is running with --quiet.
# See https://github.com/pypa/pip/issues/3418
elif not file.isatty() or logger.getEffectiveLevel() > logging.INFO:
yield
else:
file.write(HIDE_CURSOR)
try:
yield
finally:
file.write(SHOW_CURSOR)
@@ -1,7 +0,0 @@
SUCCESS = 0
ERROR = 1
UNKNOWN_ERROR = 2
VIRTUALENV_NOT_FOUND = 3
PREVIOUS_BUILD_DIR_ERROR = 4
NO_MATCHES_FOUND = 23
BROKEN_STDOUT = 120
@@ -1,139 +0,0 @@
"""
Package containing all pip commands
"""
from __future__ import annotations
import importlib
from collections import namedtuple
from typing import Any
from pip._internal.cli.base_command import Command
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`.
# - The ordering matters for help display.
#
# Even though the module path starts with the same "pip._internal.commands"
# prefix, the full path makes testing easier (specifically when modifying
# `commands_dict` in test setup / teardown).
commands_dict: dict[str, CommandInfo] = {
"install": CommandInfo(
"pip._internal.commands.install",
"InstallCommand",
"Install packages.",
),
"lock": CommandInfo(
"pip._internal.commands.lock",
"LockCommand",
"Generate a lock file.",
),
"download": CommandInfo(
"pip._internal.commands.download",
"DownloadCommand",
"Download packages.",
),
"uninstall": CommandInfo(
"pip._internal.commands.uninstall",
"UninstallCommand",
"Uninstall packages.",
),
"freeze": CommandInfo(
"pip._internal.commands.freeze",
"FreezeCommand",
"Output installed packages in requirements format.",
),
"inspect": CommandInfo(
"pip._internal.commands.inspect",
"InspectCommand",
"Inspect the python environment.",
),
"list": CommandInfo(
"pip._internal.commands.list",
"ListCommand",
"List installed packages.",
),
"show": CommandInfo(
"pip._internal.commands.show",
"ShowCommand",
"Show information about installed packages.",
),
"check": CommandInfo(
"pip._internal.commands.check",
"CheckCommand",
"Verify installed packages have compatible dependencies.",
),
"config": CommandInfo(
"pip._internal.commands.configuration",
"ConfigurationCommand",
"Manage local and global configuration.",
),
"search": CommandInfo(
"pip._internal.commands.search",
"SearchCommand",
"Search PyPI for packages.",
),
"cache": CommandInfo(
"pip._internal.commands.cache",
"CacheCommand",
"Inspect and manage pip's wheel cache.",
),
"index": CommandInfo(
"pip._internal.commands.index",
"IndexCommand",
"Inspect information available from package indexes.",
),
"wheel": CommandInfo(
"pip._internal.commands.wheel",
"WheelCommand",
"Build wheels from your requirements.",
),
"hash": CommandInfo(
"pip._internal.commands.hash",
"HashCommand",
"Compute hashes of package archives.",
),
"completion": CommandInfo(
"pip._internal.commands.completion",
"CompletionCommand",
"A helper command used for command completion.",
),
"debug": CommandInfo(
"pip._internal.commands.debug",
"DebugCommand",
"Show information useful for debugging.",
),
"help": CommandInfo(
"pip._internal.commands.help",
"HelpCommand",
"Show help for commands.",
),
}
def create_command(name: str, **kwargs: Any) -> Command:
"""
Create an instance of the Command class with the given name.
"""
module_path, class_name, summary = commands_dict[name]
module = importlib.import_module(module_path)
command_class = getattr(module, class_name)
command = command_class(name=name, summary=summary, **kwargs)
return command
def get_similar_commands(name: str) -> str | None:
"""Command name auto-correct."""
from difflib import get_close_matches
name = name.lower()
close_commands = get_close_matches(name, commands_dict.keys())
if close_commands:
return close_commands[0]
else:
return None