found_candidates.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import functools
  10. from collections.abc import Sequence
  11. from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, Set, Tuple
  12. from pip._vendor.packaging.version import _BaseVersion
  13. from .base import Candidate
  14. IndexCandidateInfo = Tuple[_BaseVersion, Callable[[], Optional[Candidate]]]
  15. if TYPE_CHECKING:
  16. SequenceCandidate = Sequence[Candidate]
  17. else:
  18. # For compatibility: Python before 3.9 does not support using [] on the
  19. # Sequence class.
  20. #
  21. # >>> from collections.abc import Sequence
  22. # >>> Sequence[str]
  23. # Traceback (most recent call last):
  24. # File "<stdin>", line 1, in <module>
  25. # TypeError: 'ABCMeta' object is not subscriptable
  26. #
  27. # TODO: Remove this block after dropping Python 3.8 support.
  28. SequenceCandidate = Sequence
  29. def _iter_built(infos: Iterator[IndexCandidateInfo]) -> Iterator[Candidate]:
  30. """Iterator for ``FoundCandidates``.
  31. This iterator is used when the package is not already installed. Candidates
  32. from index come later in their normal ordering.
  33. """
  34. versions_found: Set[_BaseVersion] = set()
  35. for version, func in infos:
  36. if version in versions_found:
  37. continue
  38. candidate = func()
  39. if candidate is None:
  40. continue
  41. yield candidate
  42. versions_found.add(version)
  43. def _iter_built_with_prepended(
  44. installed: Candidate, infos: Iterator[IndexCandidateInfo]
  45. ) -> Iterator[Candidate]:
  46. """Iterator for ``FoundCandidates``.
  47. This iterator is used when the resolver prefers the already-installed
  48. candidate and NOT to upgrade. The installed candidate is therefore
  49. always yielded first, and candidates from index come later in their
  50. normal ordering, except skipped when the version is already installed.
  51. """
  52. yield installed
  53. versions_found: Set[_BaseVersion] = {installed.version}
  54. for version, func in infos:
  55. if version in versions_found:
  56. continue
  57. candidate = func()
  58. if candidate is None:
  59. continue
  60. yield candidate
  61. versions_found.add(version)
  62. def _iter_built_with_inserted(
  63. installed: Candidate, infos: Iterator[IndexCandidateInfo]
  64. ) -> Iterator[Candidate]:
  65. """Iterator for ``FoundCandidates``.
  66. This iterator is used when the resolver prefers to upgrade an
  67. already-installed package. Candidates from index are returned in their
  68. normal ordering, except replaced when the version is already installed.
  69. The implementation iterates through and yields other candidates, inserting
  70. the installed candidate exactly once before we start yielding older or
  71. equivalent candidates, or after all other candidates if they are all newer.
  72. """
  73. versions_found: Set[_BaseVersion] = set()
  74. for version, func in infos:
  75. if version in versions_found:
  76. continue
  77. # If the installed candidate is better, yield it first.
  78. if installed.version >= version:
  79. yield installed
  80. versions_found.add(installed.version)
  81. candidate = func()
  82. if candidate is None:
  83. continue
  84. yield candidate
  85. versions_found.add(version)
  86. # If the installed candidate is older than all other candidates.
  87. if installed.version not in versions_found:
  88. yield installed
  89. class FoundCandidates(SequenceCandidate):
  90. """A lazy sequence to provide candidates to the resolver.
  91. The intended usage is to return this from `find_matches()` so the resolver
  92. can iterate through the sequence multiple times, but only access the index
  93. page when remote packages are actually needed. This improve performances
  94. when suitable candidates are already installed on disk.
  95. """
  96. def __init__(
  97. self,
  98. get_infos: Callable[[], Iterator[IndexCandidateInfo]],
  99. installed: Optional[Candidate],
  100. prefers_installed: bool,
  101. incompatible_ids: Set[int],
  102. ):
  103. self._get_infos = get_infos
  104. self._installed = installed
  105. self._prefers_installed = prefers_installed
  106. self._incompatible_ids = incompatible_ids
  107. def __getitem__(self, index: Any) -> Any:
  108. # Implemented to satisfy the ABC check. This is not needed by the
  109. # resolver, and should not be used by the provider either (for
  110. # performance reasons).
  111. raise NotImplementedError("don't do this")
  112. def __iter__(self) -> Iterator[Candidate]:
  113. infos = self._get_infos()
  114. if not self._installed:
  115. iterator = _iter_built(infos)
  116. elif self._prefers_installed:
  117. iterator = _iter_built_with_prepended(self._installed, infos)
  118. else:
  119. iterator = _iter_built_with_inserted(self._installed, infos)
  120. return (c for c in iterator if id(c) not in self._incompatible_ids)
  121. def __len__(self) -> int:
  122. # Implemented to satisfy the ABC check. This is not needed by the
  123. # resolver, and should not be used by the provider either (for
  124. # performance reasons).
  125. raise NotImplementedError("don't do this")
  126. @functools.lru_cache(maxsize=1)
  127. def __bool__(self) -> bool:
  128. if self._prefers_installed and self._installed:
  129. return True
  130. return any(self)