provider.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import collections
  2. import math
  3. from typing import TYPE_CHECKING, Dict, Iterable, Iterator, Mapping, Sequence, Union
  4. from pip._vendor.resolvelib.providers import AbstractProvider
  5. from .base import Candidate, Constraint, Requirement
  6. from .candidates import REQUIRES_PYTHON_IDENTIFIER
  7. from .factory import Factory
  8. if TYPE_CHECKING:
  9. from pip._vendor.resolvelib.providers import Preference
  10. from pip._vendor.resolvelib.resolvers import RequirementInformation
  11. PreferenceInformation = RequirementInformation[Requirement, Candidate]
  12. _ProviderBase = AbstractProvider[Requirement, Candidate, str]
  13. else:
  14. _ProviderBase = AbstractProvider
  15. # Notes on the relationship between the provider, the factory, and the
  16. # candidate and requirement classes.
  17. #
  18. # The provider is a direct implementation of the resolvelib class. Its role
  19. # is to deliver the API that resolvelib expects.
  20. #
  21. # Rather than work with completely abstract "requirement" and "candidate"
  22. # concepts as resolvelib does, pip has concrete classes implementing these two
  23. # ideas. The API of Requirement and Candidate objects are defined in the base
  24. # classes, but essentially map fairly directly to the equivalent provider
  25. # methods. In particular, `find_matches` and `is_satisfied_by` are
  26. # requirement methods, and `get_dependencies` is a candidate method.
  27. #
  28. # The factory is the interface to pip's internal mechanisms. It is stateless,
  29. # and is created by the resolver and held as a property of the provider. It is
  30. # responsible for creating Requirement and Candidate objects, and provides
  31. # services to those objects (access to pip's finder and preparer).
  32. class PipProvider(_ProviderBase):
  33. """Pip's provider implementation for resolvelib.
  34. :params constraints: A mapping of constraints specified by the user. Keys
  35. are canonicalized project names.
  36. :params ignore_dependencies: Whether the user specified ``--no-deps``.
  37. :params upgrade_strategy: The user-specified upgrade strategy.
  38. :params user_requested: A set of canonicalized package names that the user
  39. supplied for pip to install/upgrade.
  40. """
  41. def __init__(
  42. self,
  43. factory: Factory,
  44. constraints: Dict[str, Constraint],
  45. ignore_dependencies: bool,
  46. upgrade_strategy: str,
  47. user_requested: Dict[str, int],
  48. ) -> None:
  49. self._factory = factory
  50. self._constraints = constraints
  51. self._ignore_dependencies = ignore_dependencies
  52. self._upgrade_strategy = upgrade_strategy
  53. self._user_requested = user_requested
  54. self._known_depths: Dict[str, float] = collections.defaultdict(lambda: math.inf)
  55. def identify(self, requirement_or_candidate: Union[Requirement, Candidate]) -> str:
  56. return requirement_or_candidate.name
  57. def get_preference(
  58. self,
  59. identifier: str,
  60. resolutions: Mapping[str, Candidate],
  61. candidates: Mapping[str, Iterator[Candidate]],
  62. information: Mapping[str, Iterator["PreferenceInformation"]],
  63. ) -> "Preference":
  64. """Produce a sort key for given requirement based on preference.
  65. The lower the return value is, the more preferred this group of
  66. arguments is.
  67. Currently pip considers the followings in order:
  68. * Prefer if any of the known requirements is "direct", e.g. points to an
  69. explicit URL.
  70. * If equal, prefer if any requirement is "pinned", i.e. contains
  71. operator ``===`` or ``==``.
  72. * If equal, calculate an approximate "depth" and resolve requirements
  73. closer to the user-specified requirements first.
  74. * Order user-specified requirements by the order they are specified.
  75. * If equal, prefers "non-free" requirements, i.e. contains at least one
  76. operator, such as ``>=`` or ``<``.
  77. * If equal, order alphabetically for consistency (helps debuggability).
  78. """
  79. lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
  80. candidate, ireqs = zip(*lookups)
  81. operators = [
  82. specifier.operator
  83. for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
  84. for specifier in specifier_set
  85. ]
  86. direct = candidate is not None
  87. pinned = any(op[:2] == "==" for op in operators)
  88. unfree = bool(operators)
  89. try:
  90. requested_order: Union[int, float] = self._user_requested[identifier]
  91. except KeyError:
  92. requested_order = math.inf
  93. parent_depths = (
  94. self._known_depths[parent.name] if parent is not None else 0.0
  95. for _, parent in information[identifier]
  96. )
  97. inferred_depth = min(d for d in parent_depths) + 1.0
  98. self._known_depths[identifier] = inferred_depth
  99. else:
  100. inferred_depth = 1.0
  101. requested_order = self._user_requested.get(identifier, math.inf)
  102. # Requires-Python has only one candidate and the check is basically
  103. # free, so we always do it first to avoid needless work if it fails.
  104. requires_python = identifier == REQUIRES_PYTHON_IDENTIFIER
  105. # HACK: Setuptools have a very long and solid backward compatibility
  106. # track record, and extremely few projects would request a narrow,
  107. # non-recent version range of it since that would break a lot things.
  108. # (Most projects specify it only to request for an installer feature,
  109. # which does not work, but that's another topic.) Intentionally
  110. # delaying Setuptools helps reduce branches the resolver has to check.
  111. # This serves as a temporary fix for issues like "apache-airlfow[all]"
  112. # while we work on "proper" branch pruning techniques.
  113. delay_this = identifier == "setuptools"
  114. return (
  115. not requires_python,
  116. delay_this,
  117. not direct,
  118. not pinned,
  119. inferred_depth,
  120. requested_order,
  121. not unfree,
  122. identifier,
  123. )
  124. def _get_constraint(self, identifier: str) -> Constraint:
  125. if identifier in self._constraints:
  126. return self._constraints[identifier]
  127. # HACK: Theoratically we should check whether this identifier is a valid
  128. # "NAME[EXTRAS]" format, and parse out the name part with packaging or
  129. # some regular expression. But since pip's resolver only spits out
  130. # three kinds of identifiers: normalized PEP 503 names, normalized names
  131. # plus extras, and Requires-Python, we can cheat a bit here.
  132. name, open_bracket, _ = identifier.partition("[")
  133. if open_bracket and name in self._constraints:
  134. return self._constraints[name]
  135. return Constraint.empty()
  136. def find_matches(
  137. self,
  138. identifier: str,
  139. requirements: Mapping[str, Iterator[Requirement]],
  140. incompatibilities: Mapping[str, Iterator[Candidate]],
  141. ) -> Iterable[Candidate]:
  142. def _eligible_for_upgrade(name: str) -> bool:
  143. """Are upgrades allowed for this project?
  144. This checks the upgrade strategy, and whether the project was one
  145. that the user specified in the command line, in order to decide
  146. whether we should upgrade if there's a newer version available.
  147. (Note that we don't need access to the `--upgrade` flag, because
  148. an upgrade strategy of "to-satisfy-only" means that `--upgrade`
  149. was not specified).
  150. """
  151. if self._upgrade_strategy == "eager":
  152. return True
  153. elif self._upgrade_strategy == "only-if-needed":
  154. return name in self._user_requested
  155. return False
  156. return self._factory.find_candidates(
  157. identifier=identifier,
  158. requirements=requirements,
  159. constraint=self._get_constraint(identifier),
  160. prefers_installed=(not _eligible_for_upgrade(identifier)),
  161. incompatibilities=incompatibilities,
  162. )
  163. def is_satisfied_by(self, requirement: Requirement, candidate: Candidate) -> bool:
  164. return requirement.is_satisfied_by(candidate)
  165. def get_dependencies(self, candidate: Candidate) -> Sequence[Requirement]:
  166. with_requires = not self._ignore_dependencies
  167. return [r for r in candidate.iter_dependencies(with_requires) if r is not None]