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

No files matched your search

@@ -6,20 +6,22 @@ from pip._vendor.packaging.version import parse as parse_version
from pip._internal.models.link import Link
@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class InstallationCandidate:
"""Represents a potential "candidate" for installation."""
__slots__ = ["name", "version", "link"]
name: str
version: Version
link: Link
locked: bool
def __init__(self, name: str, version: str, link: Link) -> None:
def __init__(
self, name: str, version: str, link: Link, locked: bool = False
) -> None:
object.__setattr__(self, "name", name)
object.__setattr__(self, "version", parse_version(version))
object.__setattr__(self, "link", link)
object.__setattr__(self, "locked", locked)
def __str__(self) -> str:
return f"{self.name!r} candidate (version {self.version} at {self.link})"
@@ -3,225 +3,40 @@
from __future__ import annotations
import json
import re
import urllib.parse
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any, ClassVar, TypeVar, Union
from typing import Any
from pip._vendor.packaging.direct_url import (
ArchiveInfo,
DirectUrlValidationError,
DirInfo,
VcsInfo,
)
from pip._vendor.packaging.direct_url import (
DirectUrl as PackagingDirectUrl,
)
__all__ = [
"ArchiveInfo",
"DirInfo",
"DirectUrl",
"DirectUrlValidationError",
"DirInfo",
"ArchiveInfo",
"DIRECT_URL_METADATA_NAME",
"VcsInfo",
]
T = TypeVar("T")
DIRECT_URL_METADATA_NAME = "direct_url.json"
ENV_VAR_RE = re.compile(r"^\$\{[A-Za-z0-9-_]+\}(:\$\{[A-Za-z0-9-_]+\})?$")
class DirectUrlValidationError(Exception):
pass
def _get(
d: dict[str, Any], expected_type: type[T], key: str, default: T | None = None
) -> T | None:
"""Get value from dictionary and verify expected type."""
if key not in d:
return default
value = d[key]
if not isinstance(value, expected_type):
raise DirectUrlValidationError(
f"{value!r} has unexpected type for {key} (expected {expected_type})"
)
return value
def _get_required(
d: dict[str, Any], expected_type: type[T], key: str, default: T | None = None
) -> T:
value = _get(d, expected_type, key, default)
if value is None:
raise DirectUrlValidationError(f"{key} must have a value")
return value
def _exactly_one_of(infos: Iterable[InfoType | None]) -> InfoType:
infos = [info for info in infos if info is not None]
if not infos:
raise DirectUrlValidationError(
"missing one of archive_info, dir_info, vcs_info"
)
if len(infos) > 1:
raise DirectUrlValidationError(
"more than one of archive_info, dir_info, vcs_info"
)
assert infos[0] is not None
return infos[0]
def _filter_none(**kwargs: Any) -> dict[str, Any]:
"""Make dict excluding None values."""
return {k: v for k, v in kwargs.items() if v is not None}
@dataclass
class VcsInfo:
name: ClassVar = "vcs_info"
vcs: str
commit_id: str
requested_revision: str | None = None
@classmethod
def _from_dict(cls, d: dict[str, Any] | None) -> VcsInfo | None:
if d is None:
return None
return cls(
vcs=_get_required(d, str, "vcs"),
commit_id=_get_required(d, str, "commit_id"),
requested_revision=_get(d, str, "requested_revision"),
)
def _to_dict(self) -> dict[str, Any]:
return _filter_none(
vcs=self.vcs,
requested_revision=self.requested_revision,
commit_id=self.commit_id,
)
class ArchiveInfo:
name = "archive_info"
def __init__(
self,
hash: str | None = None,
hashes: dict[str, str] | None = None,
) -> None:
# set hashes before hash, since the hash setter will further populate hashes
self.hashes = hashes
self.hash = hash
@property
def hash(self) -> str | None:
return self._hash
@hash.setter
def hash(self, value: str | None) -> None:
if value is not None:
# Auto-populate the hashes key to upgrade to the new format automatically.
# We don't back-populate the legacy hash key from hashes.
try:
hash_name, hash_value = value.split("=", 1)
except ValueError:
raise DirectUrlValidationError(
f"invalid archive_info.hash format: {value!r}"
)
if self.hashes is None:
self.hashes = {hash_name: hash_value}
elif hash_name not in self.hashes:
self.hashes = self.hashes.copy()
self.hashes[hash_name] = hash_value
self._hash = value
@classmethod
def _from_dict(cls, d: dict[str, Any] | None) -> ArchiveInfo | None:
if d is None:
return None
return cls(hash=_get(d, str, "hash"), hashes=_get(d, dict, "hashes"))
def _to_dict(self) -> dict[str, Any]:
return _filter_none(hash=self.hash, hashes=self.hashes)
@dataclass
class DirInfo:
name: ClassVar = "dir_info"
editable: bool = False
@classmethod
def _from_dict(cls, d: dict[str, Any] | None) -> DirInfo | None:
if d is None:
return None
return cls(editable=_get_required(d, bool, "editable", default=False))
def _to_dict(self) -> dict[str, Any]:
return _filter_none(editable=self.editable or None)
InfoType = Union[ArchiveInfo, DirInfo, VcsInfo]
@dataclass
class DirectUrl:
url: str
info: InfoType
subdirectory: str | None = None
def _remove_auth_from_netloc(self, netloc: str) -> str:
if "@" not in netloc:
return netloc
user_pass, netloc_no_user_pass = netloc.split("@", 1)
if (
isinstance(self.info, VcsInfo)
and self.info.vcs == "git"
and user_pass == "git"
):
return netloc
if ENV_VAR_RE.match(user_pass):
return netloc
return netloc_no_user_pass
@property
def redacted_url(self) -> str:
"""url with user:password part removed unless it is formed with
environment variables as specified in PEP 610, or it is ``git``
in the case of a git URL.
"""
purl = urllib.parse.urlsplit(self.url)
netloc = self._remove_auth_from_netloc(purl.netloc)
surl = urllib.parse.urlunsplit(
(purl.scheme, netloc, purl.path, purl.query, purl.fragment)
)
return surl
def validate(self) -> None:
self.from_dict(self.to_dict())
@classmethod
def from_dict(cls, d: dict[str, Any]) -> DirectUrl:
return DirectUrl(
url=_get_required(d, str, "url"),
subdirectory=_get(d, str, "subdirectory"),
info=_exactly_one_of(
[
ArchiveInfo._from_dict(_get(d, dict, "archive_info")),
DirInfo._from_dict(_get(d, dict, "dir_info")),
VcsInfo._from_dict(_get(d, dict, "vcs_info")),
]
),
)
def to_dict(self) -> dict[str, Any]:
res = _filter_none(
url=self.redacted_url,
subdirectory=self.subdirectory,
)
res[self.info.name] = self.info._to_dict()
return res
class DirectUrl(PackagingDirectUrl):
def to_dict_compat(self) -> dict[str, Any]:
return dict(super().to_dict(generate_legacy_hash=True))
@classmethod
def from_json(cls, s: str) -> DirectUrl:
return cls.from_dict(json.loads(s))
def to_json(self) -> str:
return json.dumps(self.to_dict(), sort_keys=True)
return json.dumps(self.to_dict_compat(), sort_keys=True)
def is_local_editable(self) -> bool:
return isinstance(self.info, DirInfo) and self.info.editable
return bool(self.dir_info and self.dir_info.editable)
@@ -19,7 +19,7 @@ class InstallationReport:
# be absent when the requirement was installed from the wheel cache
# and the cache entry was populated by an older pip version that did not
# record origin.json.
"download_info": ireq.download_info.to_dict(),
"download_info": ireq.download_info.to_dict_compat(),
# is_direct is true if the requirement was a direct URL reference (which
# includes editable requirements), and false if the requirement was
# downloaded from a PEP 503 index or --find-links.
@@ -8,12 +8,12 @@ import os
import posixpath
import re
import urllib.parse
import urllib.request
from collections.abc import Mapping
from dataclasses import dataclass
from typing import (
Any,
NamedTuple,
NewType,
)
from pip._internal.exceptions import InvalidEggFragment
@@ -31,6 +31,49 @@ from pip._internal.utils.urls import path_to_url, url_to_path
logger = logging.getLogger(__name__)
# A single path component: percent-decoded once and reduced to a basename, so it
# contains no path separator and is not a ``.`` or ``..`` reference. The empty
# string means "no component".
PathComponent = NewType("PathComponent", str)
def _to_path_component(name: str) -> PathComponent:
"""Reduce ``name`` to a single path component, or ``""`` if it has none.
``os.path.basename`` drops any directory part, drive letter, or separator;
a ``.``, ``..``, or empty result is not a component and becomes ``""``.
"""
name = os.path.basename(name)
if name in ("", os.curdir, os.pardir):
return PathComponent("")
return PathComponent(name)
def as_path_component(name: str) -> PathComponent:
"""Like ``_to_path_component`` but reject the empty result.
Use where a file is about to be written, so a missing name is an error
rather than a silent fallback to the directory itself.
"""
component = _to_path_component(name)
if not component:
raise ValueError(f"Unexpected file name derived from URL: {name!r}")
return component
def join_within_directory(directory: str, component: PathComponent) -> str:
"""Join a single path ``component`` onto ``directory``.
``component`` is a :data:`PathComponent`, so by type it has no separator and
is not a ``.`` or ``..`` reference; the result can never escape ``directory``.
Requiring ``PathComponent`` rather than ``str`` lets the type checker enforce
at the call site that the name was reduced to a safe component beforehand.
"""
return os.path.join(directory, component)
# Order matters, earlier hashes have a precedence over later hashes for what
# we will pick to use.
_SUPPORTED_HASHES = ("sha512", "sha384", "sha256", "sha224", "sha1", "md5")
@@ -125,6 +168,8 @@ def _clean_file_url_path(part: str) -> str:
Clean the first part of a URL path that corresponds to a local
filesystem path (i.e. the first part after splitting on "@" characters).
"""
import urllib.request
# We unquote prior to quoting to make sure nothing is double quoted.
# Also, on Windows the path part might contain a drive letter which
# should not be quoted. On Linux where drive letters do not
@@ -423,18 +468,13 @@ class Link:
return redact_auth_from_url(self.url)
@property
def filename(self) -> str:
path = self.path.rstrip("/")
name = posixpath.basename(path)
if not name:
# Make sure we don't leak auth information if the netloc
# includes a username and password.
netloc, user_pass = split_auth_from_netloc(self.netloc)
return netloc
def filename(self) -> PathComponent:
name = _to_path_component(posixpath.basename(self.path.rstrip("/")))
if name:
return name
name = urllib.parse.unquote(name)
assert name, f"URL {self._url!r} produced no filename"
return name
# No component in the path; fall back to the netloc, dropping any auth.
return _to_path_component(split_auth_from_netloc(self.netloc)[0])
@property
def file_path(self) -> str:
@@ -7,8 +7,7 @@ from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._internal.exceptions import CommandError
# TODO: add slots=True when Python 3.9 is dropped
@dataclass
@dataclass(slots=True)
class ReleaseControl:
"""Helper for managing which release types can be installed."""
@@ -10,14 +10,12 @@ from dataclasses import dataclass
SCHEME_KEYS = ["platlib", "purelib", "headers", "scripts", "data"]
@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class Scheme:
"""A Scheme holds paths which are used as the base directories for
artifacts associated with a Python package.
"""
__slots__ = SCHEME_KEYS
platlib: str
purelib: str
headers: str
@@ -14,14 +14,12 @@ from pip._internal.utils.misc import normalize_path, redact_auth_from_url
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class SearchScope:
"""
Encapsulates the locations that pip is configured to search.
"""
__slots__ = ["find_links", "index_urls", "no_index"]
find_links: list[str]
index_urls: list[str]
no_index: bool
@@ -1,56 +1,36 @@
from __future__ import annotations
from dataclasses import dataclass
from pip._internal.models.format_control import FormatControl
from pip._internal.models.release_control import ReleaseControl
# TODO: This needs Python 3.10's improved slots support for dataclasses
# to be converted into a dataclass.
@dataclass(slots=True)
class SelectionPreferences:
"""
Encapsulates the candidate selection preferences for downloading
and installing files.
"""
__slots__ = [
"allow_yanked",
"release_control",
"format_control",
"prefer_binary",
"ignore_requires_python",
]
:param allow_yanked: Whether files marked as yanked (in the sense
of PEP 592) are permitted to be candidates for install.
:param release_control: A ReleaseControl object or None. Used to control
whether pre-releases are allowed for specific packages.
:param format_control: A FormatControl object or None. Used to control
the selection of source packages / binary packages when consulting
the index and links.
:param prefer_binary: Whether to prefer an old, but valid, binary
dist over a new source dist.
:param ignore_requires_python: Whether to ignore incompatible
"Requires-Python" values in links. Defaults to False.
"""
# Don't include an allow_yanked default value to make sure each call
# site considers whether yanked releases are allowed. This also causes
# that decision to be made explicit in the calling code, which helps
# people when reading the code.
def __init__(
self,
allow_yanked: bool,
release_control: ReleaseControl | None = None,
format_control: FormatControl | None = None,
prefer_binary: bool = False,
ignore_requires_python: bool | None = None,
) -> None:
"""Create a SelectionPreferences object.
:param allow_yanked: Whether files marked as yanked (in the sense
of PEP 592) are permitted to be candidates for install.
:param release_control: A ReleaseControl object or None. Used to control
whether pre-releases are allowed for specific packages.
:param format_control: A FormatControl object or None. Used to control
the selection of source packages / binary packages when consulting
the index and links.
:param prefer_binary: Whether to prefer an old, but valid, binary
dist over a new source dist.
:param ignore_requires_python: Whether to ignore incompatible
"Requires-Python" values in links. Defaults to False.
"""
if ignore_requires_python is None:
ignore_requires_python = False
self.allow_yanked = allow_yanked
self.release_control = release_control
self.format_control = format_control
self.prefer_binary = prefer_binary
self.ignore_requires_python = ignore_requires_python
allow_yanked: bool
release_control: ReleaseControl | None = None
format_control: FormatControl | None = None
prefer_binary: bool = False
ignore_requires_python: bool = False