provider.py 9.7 KB

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