found_candidates.py 5.3 KB

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