found_candidates.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Utilities to lazily create and visit candidates found.
  2. Creating and visiting a candidate is a *very* costly operation. It involves
  3. fetching, extracting, potentially building modules from source, and verifying
  4. distribution metadata. It is therefore crucial for performance to keep
  5. everything here lazy all the way down, so we only touch candidates that we
  6. absolutely need, and not "download the world" when we only need one version of
  7. something.
  8. """
  9. from __future__ import annotations
  10. import logging
  11. from collections.abc import Iterator, Sequence
  12. from typing import Any, Callable, Optional
  13. from pip._vendor.packaging.version import _BaseVersion
  14. from pip._internal.exceptions import MetadataInvalid
  15. from .base import Candidate
  16. logger = logging.getLogger(__name__)
  17. IndexCandidateInfo = tuple[_BaseVersion, Callable[[], Optional[Candidate]]]
  18. def _iter_built(infos: Iterator[IndexCandidateInfo]) -> Iterator[Candidate]:
  19. """Iterator for ``FoundCandidates``.
  20. This iterator is used when the package is not already installed. Candidates
  21. from index come later in their normal ordering.
  22. """
  23. versions_found: set[_BaseVersion] = set()
  24. for version, func in infos:
  25. if version in versions_found:
  26. continue
  27. try:
  28. candidate = func()
  29. except MetadataInvalid as e:
  30. logger.warning(
  31. "Ignoring version %s of %s since it has invalid metadata:\n"
  32. "%s\n"
  33. "Please use pip<24.1 if you need to use this version.",
  34. version,
  35. e.ireq.name,
  36. e,
  37. )
  38. # Mark version as found to avoid trying other candidates with the same
  39. # version, since they most likely have invalid metadata as well.
  40. versions_found.add(version)
  41. else:
  42. if candidate is None:
  43. continue
  44. yield candidate
  45. versions_found.add(version)
  46. def _iter_built_with_prepended(
  47. installed: Candidate, infos: Iterator[IndexCandidateInfo]
  48. ) -> Iterator[Candidate]:
  49. """Iterator for ``FoundCandidates``.
  50. This iterator is used when the resolver prefers the already-installed
  51. candidate and NOT to upgrade. The installed candidate is therefore
  52. always yielded first, and candidates from index come later in their
  53. normal ordering, except skipped when the version is already installed.
  54. """
  55. yield installed
  56. versions_found: set[_BaseVersion] = {installed.version}
  57. for version, func in infos:
  58. if version in versions_found:
  59. continue
  60. candidate = func()
  61. if candidate is None:
  62. continue
  63. yield candidate
  64. versions_found.add(version)
  65. def _iter_built_with_inserted(
  66. installed: Candidate, infos: Iterator[IndexCandidateInfo]
  67. ) -> Iterator[Candidate]:
  68. """Iterator for ``FoundCandidates``.
  69. This iterator is used when the resolver prefers to upgrade an
  70. already-installed package. Candidates from index are returned in their
  71. normal ordering, except replaced when the version is already installed.
  72. The implementation iterates through and yields other candidates, inserting
  73. the installed candidate exactly once before we start yielding older or
  74. equivalent candidates, or after all other candidates if they are all newer.
  75. """
  76. versions_found: set[_BaseVersion] = set()
  77. for version, func in infos:
  78. if version in versions_found:
  79. continue
  80. # If the installed candidate is better, yield it first.
  81. if installed.version >= version:
  82. yield installed
  83. versions_found.add(installed.version)
  84. candidate = func()
  85. if candidate is None:
  86. continue
  87. yield candidate
  88. versions_found.add(version)
  89. # If the installed candidate is older than all other candidates.
  90. if installed.version not in versions_found:
  91. yield installed
  92. class FoundCandidates(Sequence[Candidate]):
  93. """A lazy sequence to provide candidates to the resolver.
  94. The intended usage is to return this from `find_matches()` so the resolver
  95. can iterate through the sequence multiple times, but only access the index
  96. page when remote packages are actually needed. This improve performances
  97. when suitable candidates are already installed on disk.
  98. """
  99. def __init__(
  100. self,
  101. get_infos: Callable[[], Iterator[IndexCandidateInfo]],
  102. installed: Candidate | None,
  103. prefers_installed: bool,
  104. incompatible_ids: set[int],
  105. ):
  106. self._get_infos = get_infos
  107. self._installed = installed
  108. self._prefers_installed = prefers_installed
  109. self._incompatible_ids = incompatible_ids
  110. self._bool: bool | None = None
  111. def __getitem__(self, index: Any) -> Any:
  112. # Implemented to satisfy the ABC check. This is not needed by the
  113. # resolver, and should not be used by the provider either (for
  114. # performance reasons).
  115. raise NotImplementedError("don't do this")
  116. def __iter__(self) -> Iterator[Candidate]:
  117. infos = self._get_infos()
  118. if not self._installed:
  119. iterator = _iter_built(infos)
  120. elif self._prefers_installed:
  121. iterator = _iter_built_with_prepended(self._installed, infos)
  122. else:
  123. iterator = _iter_built_with_inserted(self._installed, infos)
  124. return (c for c in iterator if id(c) not in self._incompatible_ids)
  125. def __len__(self) -> int:
  126. # Implemented to satisfy the ABC check. This is not needed by the
  127. # resolver, and should not be used by the provider either (for
  128. # performance reasons).
  129. raise NotImplementedError("don't do this")
  130. def __bool__(self) -> bool:
  131. if self._bool is not None:
  132. return self._bool
  133. if self._prefers_installed and self._installed:
  134. self._bool = True
  135. return True
  136. self._bool = any(self)
  137. return self._bool