| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import optparse |
| 4 |
from contextlib import contextmanager |
| 5 |
from typing import Iterator, Mapping, cast |
| 6 |
|
| 7 |
from pip._internal.commands.install import InstallCommand |
| 8 |
from pip._internal.index.package_finder import PackageFinder |
| 9 |
from pip._internal.models.candidate import InstallationCandidate |
| 10 |
from pip._internal.network.session import PipSession |
| 11 |
from pip._internal.req import InstallRequirement |
| 12 |
from pip._internal.utils.hashes import FAVORITE_HASH |
| 13 |
|
| 14 |
from piptools.utils import as_tuple, key_from_ireq, make_install_requirement |
| 15 |
|
| 16 |
from .base import BaseRepository |
| 17 |
from .pypi import PyPIRepository |
| 18 |
|
| 19 |
|
| 20 |
def ireq_satisfied_by_existing_pin( |
| 21 |
ireq: InstallRequirement, existing_pin: InstallationCandidate |
| 22 |
) -> bool: |
| 23 |
""" |
| 24 |
Return True if the given InstallationRequirement is satisfied by the |
| 25 |
previously encountered version pin. |
| 26 |
""" |
| 27 |
version = next(iter(existing_pin.req.specifier)).version |
| 28 |
result = ireq.req.specifier.contains( |
| 29 |
version, prereleases=existing_pin.req.specifier.prereleases |
| 30 |
) |
| 31 |
return cast(bool, result) |
| 32 |
|
| 33 |
|
| 34 |
class LocalRequirementsRepository(BaseRepository): |
| 35 |
""" |
| 36 |
The LocalRequirementsRepository proxied the _real_ repository by first |
| 37 |
checking if a requirement can be satisfied by existing pins (i.e. the |
| 38 |
result of a previous compile step). |
| 39 |
|
| 40 |
In effect, if a requirement can be satisfied with a version pinned in the |
| 41 |
requirements file, we prefer that version over the best match found in |
| 42 |
PyPI. This keeps updates to the requirements.txt down to a minimum. |
| 43 |
""" |
| 44 |
|
| 45 |
def __init__( |
| 46 |
self, |
| 47 |
existing_pins: Mapping[str, InstallationCandidate], |
| 48 |
proxied_repository: PyPIRepository, |
| 49 |
reuse_hashes: bool = True, |
| 50 |
): |
| 51 |
self._reuse_hashes = reuse_hashes |
| 52 |
self.repository = proxied_repository |
| 53 |
self.existing_pins = existing_pins |
| 54 |
|
| 55 |
@property |
| 56 |
def options(self) -> optparse.Values: |
| 57 |
return self.repository.options |
| 58 |
|
| 59 |
@property |
| 60 |
def finder(self) -> PackageFinder: |
| 61 |
return self.repository.finder |
| 62 |
|
| 63 |
@property |
| 64 |
def session(self) -> PipSession: |
| 65 |
return self.repository.session |
| 66 |
|
| 67 |
@property |
| 68 |
def command(self) -> InstallCommand: |
| 69 |
"""Return an install command instance.""" |
| 70 |
return self.repository.command |
| 71 |
|
| 72 |
def clear_caches(self) -> None: |
| 73 |
self.repository.clear_caches() |
| 74 |
|
| 75 |
def find_best_match( |
| 76 |
self, ireq: InstallRequirement, prereleases: bool | None = None |
| 77 |
) -> InstallationCandidate: |
| 78 |
key = key_from_ireq(ireq) |
| 79 |
existing_pin = self.existing_pins.get(key) |
| 80 |
if existing_pin and ireq_satisfied_by_existing_pin(ireq, existing_pin): |
| 81 |
project, version, _ = as_tuple(existing_pin) |
| 82 |
return make_install_requirement(project, version, ireq) |
| 83 |
else: |
| 84 |
return self.repository.find_best_match(ireq, prereleases) |
| 85 |
|
| 86 |
def get_dependencies(self, ireq: InstallRequirement) -> set[InstallRequirement]: |
| 87 |
return self.repository.get_dependencies(ireq) |
| 88 |
|
| 89 |
def get_hashes(self, ireq: InstallRequirement) -> set[str]: |
| 90 |
existing_pin = self._reuse_hashes and self.existing_pins.get( |
| 91 |
key_from_ireq(ireq) |
| 92 |
) |
| 93 |
if existing_pin and ireq_satisfied_by_existing_pin(ireq, existing_pin): |
| 94 |
hashes = existing_pin.hash_options |
| 95 |
hexdigests = hashes.get(FAVORITE_HASH) |
| 96 |
if hexdigests: |
| 97 |
return { |
| 98 |
":".join([FAVORITE_HASH, hexdigest]) for hexdigest in hexdigests |
| 99 |
} |
| 100 |
return self.repository.get_hashes(ireq) |
| 101 |
|
| 102 |
@contextmanager |
| 103 |
def allow_all_wheels(self) -> Iterator[None]: |
| 104 |
with self.repository.allow_all_wheels(): |
| 105 |
yield |