resolvers.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import collections
  2. import itertools
  3. import operator
  4. from .providers import AbstractResolver
  5. from .structs import DirectedGraph, IteratorMapping, build_iter_view
  6. RequirementInformation = collections.namedtuple(
  7. "RequirementInformation", ["requirement", "parent"]
  8. )
  9. class ResolverException(Exception):
  10. """A base class for all exceptions raised by this module.
  11. Exceptions derived by this class should all be handled in this module. Any
  12. bubbling pass the resolver should be treated as a bug.
  13. """
  14. class RequirementsConflicted(ResolverException):
  15. def __init__(self, criterion):
  16. super(RequirementsConflicted, self).__init__(criterion)
  17. self.criterion = criterion
  18. def __str__(self):
  19. return "Requirements conflict: {}".format(
  20. ", ".join(repr(r) for r in self.criterion.iter_requirement()),
  21. )
  22. class InconsistentCandidate(ResolverException):
  23. def __init__(self, candidate, criterion):
  24. super(InconsistentCandidate, self).__init__(candidate, criterion)
  25. self.candidate = candidate
  26. self.criterion = criterion
  27. def __str__(self):
  28. return "Provided candidate {!r} does not satisfy {}".format(
  29. self.candidate,
  30. ", ".join(repr(r) for r in self.criterion.iter_requirement()),
  31. )
  32. class Criterion(object):
  33. """Representation of possible resolution results of a package.
  34. This holds three attributes:
  35. * `information` is a collection of `RequirementInformation` pairs.
  36. Each pair is a requirement contributing to this criterion, and the
  37. candidate that provides the requirement.
  38. * `incompatibilities` is a collection of all known not-to-work candidates
  39. to exclude from consideration.
  40. * `candidates` is a collection containing all possible candidates deducted
  41. from the union of contributing requirements and known incompatibilities.
  42. It should never be empty, except when the criterion is an attribute of a
  43. raised `RequirementsConflicted` (in which case it is always empty).
  44. .. note::
  45. This class is intended to be externally immutable. **Do not** mutate
  46. any of its attribute containers.
  47. """
  48. def __init__(self, candidates, information, incompatibilities):
  49. self.candidates = candidates
  50. self.information = information
  51. self.incompatibilities = incompatibilities
  52. def __repr__(self):
  53. requirements = ", ".join(
  54. "({!r}, via={!r})".format(req, parent)
  55. for req, parent in self.information
  56. )
  57. return "Criterion({})".format(requirements)
  58. def iter_requirement(self):
  59. return (i.requirement for i in self.information)
  60. def iter_parent(self):
  61. return (i.parent for i in self.information)
  62. class ResolutionError(ResolverException):
  63. pass
  64. class ResolutionImpossible(ResolutionError):
  65. def __init__(self, causes):
  66. super(ResolutionImpossible, self).__init__(causes)
  67. # causes is a list of RequirementInformation objects
  68. self.causes = causes
  69. class ResolutionTooDeep(ResolutionError):
  70. def __init__(self, round_count):
  71. super(ResolutionTooDeep, self).__init__(round_count)
  72. self.round_count = round_count
  73. # Resolution state in a round.
  74. State = collections.namedtuple("State", "mapping criteria backtrack_causes")
  75. class Resolution(object):
  76. """Stateful resolution object.
  77. This is designed as a one-off object that holds information to kick start
  78. the resolution process, and holds the results afterwards.
  79. """
  80. def __init__(self, provider, reporter):
  81. self._p = provider
  82. self._r = reporter
  83. self._states = []
  84. @property
  85. def state(self):
  86. try:
  87. return self._states[-1]
  88. except IndexError:
  89. raise AttributeError("state")
  90. def _push_new_state(self):
  91. """Push a new state into history.
  92. This new state will be used to hold resolution results of the next
  93. coming round.
  94. """
  95. base = self._states[-1]
  96. state = State(
  97. mapping=base.mapping.copy(),
  98. criteria=base.criteria.copy(),
  99. backtrack_causes=base.backtrack_causes[:],
  100. )
  101. self._states.append(state)
  102. def _add_to_criteria(self, criteria, requirement, parent):
  103. self._r.adding_requirement(requirement=requirement, parent=parent)
  104. identifier = self._p.identify(requirement_or_candidate=requirement)
  105. criterion = criteria.get(identifier)
  106. if criterion:
  107. incompatibilities = list(criterion.incompatibilities)
  108. else:
  109. incompatibilities = []
  110. matches = self._p.find_matches(
  111. identifier=identifier,
  112. requirements=IteratorMapping(
  113. criteria,
  114. operator.methodcaller("iter_requirement"),
  115. {identifier: [requirement]},
  116. ),
  117. incompatibilities=IteratorMapping(
  118. criteria,
  119. operator.attrgetter("incompatibilities"),
  120. {identifier: incompatibilities},
  121. ),
  122. )
  123. if criterion:
  124. information = list(criterion.information)
  125. information.append(RequirementInformation(requirement, parent))
  126. else:
  127. information = [RequirementInformation(requirement, parent)]
  128. criterion = Criterion(
  129. candidates=build_iter_view(matches),
  130. information=information,
  131. incompatibilities=incompatibilities,
  132. )
  133. if not criterion.candidates:
  134. raise RequirementsConflicted(criterion)
  135. criteria[identifier] = criterion
  136. def _remove_information_from_criteria(self, criteria, parents):
  137. """Remove information from parents of criteria.
  138. Concretely, removes all values from each criterion's ``information``
  139. field that have one of ``parents`` as provider of the requirement.
  140. :param criteria: The criteria to update.
  141. :param parents: Identifiers for which to remove information from all criteria.
  142. """
  143. if not parents:
  144. return
  145. for key, criterion in criteria.items():
  146. criteria[key] = Criterion(
  147. criterion.candidates,
  148. [
  149. information
  150. for information in criterion.information
  151. if (
  152. information.parent is None
  153. or self._p.identify(information.parent) not in parents
  154. )
  155. ],
  156. criterion.incompatibilities,
  157. )
  158. def _get_preference(self, name):
  159. return self._p.get_preference(
  160. identifier=name,
  161. resolutions=self.state.mapping,
  162. candidates=IteratorMapping(
  163. self.state.criteria,
  164. operator.attrgetter("candidates"),
  165. ),
  166. information=IteratorMapping(
  167. self.state.criteria,
  168. operator.attrgetter("information"),
  169. ),
  170. backtrack_causes=self.state.backtrack_causes,
  171. )
  172. def _is_current_pin_satisfying(self, name, criterion):
  173. try:
  174. current_pin = self.state.mapping[name]
  175. except KeyError:
  176. return False
  177. return all(
  178. self._p.is_satisfied_by(requirement=r, candidate=current_pin)
  179. for r in criterion.iter_requirement()
  180. )
  181. def _get_updated_criteria(self, candidate):
  182. criteria = self.state.criteria.copy()
  183. for requirement in self._p.get_dependencies(candidate=candidate):
  184. self._add_to_criteria(criteria, requirement, parent=candidate)
  185. return criteria
  186. def _attempt_to_pin_criterion(self, name):
  187. criterion = self.state.criteria[name]
  188. causes = []
  189. for candidate in criterion.candidates:
  190. try:
  191. criteria = self._get_updated_criteria(candidate)
  192. except RequirementsConflicted as e:
  193. self._r.rejecting_candidate(e.criterion, candidate)
  194. causes.append(e.criterion)
  195. continue
  196. # Check the newly-pinned candidate actually works. This should
  197. # always pass under normal circumstances, but in the case of a
  198. # faulty provider, we will raise an error to notify the implementer
  199. # to fix find_matches() and/or is_satisfied_by().
  200. satisfied = all(
  201. self._p.is_satisfied_by(requirement=r, candidate=candidate)
  202. for r in criterion.iter_requirement()
  203. )
  204. if not satisfied:
  205. raise InconsistentCandidate(candidate, criterion)
  206. self._r.pinning(candidate=candidate)
  207. self.state.criteria.update(criteria)
  208. # Put newly-pinned candidate at the end. This is essential because
  209. # backtracking looks at this mapping to get the last pin.
  210. self.state.mapping.pop(name, None)
  211. self.state.mapping[name] = candidate
  212. return []
  213. # All candidates tried, nothing works. This criterion is a dead
  214. # end, signal for backtracking.
  215. return causes
  216. def _backjump(self, causes):
  217. """Perform backjumping.
  218. When we enter here, the stack is like this::
  219. [ state Z ]
  220. [ state Y ]
  221. [ state X ]
  222. .... earlier states are irrelevant.
  223. 1. No pins worked for Z, so it does not have a pin.
  224. 2. We want to reset state Y to unpinned, and pin another candidate.
  225. 3. State X holds what state Y was before the pin, but does not
  226. have the incompatibility information gathered in state Y.
  227. Each iteration of the loop will:
  228. 1. Identify Z. The incompatibility is not always caused by the latest
  229. state. For example, given three requirements A, B and C, with
  230. dependencies A1, B1 and C1, where A1 and B1 are incompatible: the
  231. last state might be related to C, so we want to discard the
  232. previous state.
  233. 2. Discard Z.
  234. 3. Discard Y but remember its incompatibility information gathered
  235. previously, and the failure we're dealing with right now.
  236. 4. Push a new state Y' based on X, and apply the incompatibility
  237. information from Y to Y'.
  238. 5a. If this causes Y' to conflict, we need to backtrack again. Make Y'
  239. the new Z and go back to step 2.
  240. 5b. If the incompatibilities apply cleanly, end backtracking.
  241. """
  242. incompatible_reqs = itertools.chain(
  243. (c.parent for c in causes if c.parent is not None),
  244. (c.requirement for c in causes),
  245. )
  246. incompatible_deps = {self._p.identify(r) for r in incompatible_reqs}
  247. while len(self._states) >= 3:
  248. # Remove the state that triggered backtracking.
  249. del self._states[-1]
  250. # Ensure to backtrack to a state that caused the incompatibility
  251. incompatible_state = False
  252. while not incompatible_state:
  253. # Retrieve the last candidate pin and known incompatibilities.
  254. try:
  255. broken_state = self._states.pop()
  256. name, candidate = broken_state.mapping.popitem()
  257. except (IndexError, KeyError):
  258. raise ResolutionImpossible(causes)
  259. current_dependencies = {
  260. self._p.identify(d)
  261. for d in self._p.get_dependencies(candidate)
  262. }
  263. incompatible_state = not current_dependencies.isdisjoint(
  264. incompatible_deps
  265. )
  266. incompatibilities_from_broken = [
  267. (k, list(v.incompatibilities))
  268. for k, v in broken_state.criteria.items()
  269. ]
  270. # Also mark the newly known incompatibility.
  271. incompatibilities_from_broken.append((name, [candidate]))
  272. # Create a new state from the last known-to-work one, and apply
  273. # the previously gathered incompatibility information.
  274. def _patch_criteria():
  275. for k, incompatibilities in incompatibilities_from_broken:
  276. if not incompatibilities:
  277. continue
  278. try:
  279. criterion = self.state.criteria[k]
  280. except KeyError:
  281. continue
  282. matches = self._p.find_matches(
  283. identifier=k,
  284. requirements=IteratorMapping(
  285. self.state.criteria,
  286. operator.methodcaller("iter_requirement"),
  287. ),
  288. incompatibilities=IteratorMapping(
  289. self.state.criteria,
  290. operator.attrgetter("incompatibilities"),
  291. {k: incompatibilities},
  292. ),
  293. )
  294. candidates = build_iter_view(matches)
  295. if not candidates:
  296. return False
  297. incompatibilities.extend(criterion.incompatibilities)
  298. self.state.criteria[k] = Criterion(
  299. candidates=candidates,
  300. information=list(criterion.information),
  301. incompatibilities=incompatibilities,
  302. )
  303. return True
  304. self._push_new_state()
  305. success = _patch_criteria()
  306. # It works! Let's work on this new state.
  307. if success:
  308. return True
  309. # State does not work after applying known incompatibilities.
  310. # Try the still previous state.
  311. # No way to backtrack anymore.
  312. return False
  313. def resolve(self, requirements, max_rounds):
  314. if self._states:
  315. raise RuntimeError("already resolved")
  316. self._r.starting()
  317. # Initialize the root state.
  318. self._states = [
  319. State(
  320. mapping=collections.OrderedDict(),
  321. criteria={},
  322. backtrack_causes=[],
  323. )
  324. ]
  325. for r in requirements:
  326. try:
  327. self._add_to_criteria(self.state.criteria, r, parent=None)
  328. except RequirementsConflicted as e:
  329. raise ResolutionImpossible(e.criterion.information)
  330. # The root state is saved as a sentinel so the first ever pin can have
  331. # something to backtrack to if it fails. The root state is basically
  332. # pinning the virtual "root" package in the graph.
  333. self._push_new_state()
  334. for round_index in range(max_rounds):
  335. self._r.starting_round(index=round_index)
  336. unsatisfied_names = [
  337. key
  338. for key, criterion in self.state.criteria.items()
  339. if not self._is_current_pin_satisfying(key, criterion)
  340. ]
  341. # All criteria are accounted for. Nothing more to pin, we are done!
  342. if not unsatisfied_names:
  343. self._r.ending(state=self.state)
  344. return self.state
  345. # keep track of satisfied names to calculate diff after pinning
  346. satisfied_names = set(self.state.criteria.keys()) - set(
  347. unsatisfied_names
  348. )
  349. # Choose the most preferred unpinned criterion to try.
  350. name = min(unsatisfied_names, key=self._get_preference)
  351. failure_causes = self._attempt_to_pin_criterion(name)
  352. if failure_causes:
  353. causes = [i for c in failure_causes for i in c.information]
  354. # Backjump if pinning fails. The backjump process puts us in
  355. # an unpinned state, so we can work on it in the next round.
  356. self._r.resolving_conflicts(causes=causes)
  357. success = self._backjump(causes)
  358. self.state.backtrack_causes[:] = causes
  359. # Dead ends everywhere. Give up.
  360. if not success:
  361. raise ResolutionImpossible(self.state.backtrack_causes)
  362. else:
  363. # discard as information sources any invalidated names
  364. # (unsatisfied names that were previously satisfied)
  365. newly_unsatisfied_names = {
  366. key
  367. for key, criterion in self.state.criteria.items()
  368. if key in satisfied_names
  369. and not self._is_current_pin_satisfying(key, criterion)
  370. }
  371. self._remove_information_from_criteria(
  372. self.state.criteria, newly_unsatisfied_names
  373. )
  374. # Pinning was successful. Push a new state to do another pin.
  375. self._push_new_state()
  376. self._r.ending_round(index=round_index, state=self.state)
  377. raise ResolutionTooDeep(max_rounds)
  378. def _has_route_to_root(criteria, key, all_keys, connected):
  379. if key in connected:
  380. return True
  381. if key not in criteria:
  382. return False
  383. for p in criteria[key].iter_parent():
  384. try:
  385. pkey = all_keys[id(p)]
  386. except KeyError:
  387. continue
  388. if pkey in connected:
  389. connected.add(key)
  390. return True
  391. if _has_route_to_root(criteria, pkey, all_keys, connected):
  392. connected.add(key)
  393. return True
  394. return False
  395. Result = collections.namedtuple("Result", "mapping graph criteria")
  396. def _build_result(state):
  397. mapping = state.mapping
  398. all_keys = {id(v): k for k, v in mapping.items()}
  399. all_keys[id(None)] = None
  400. graph = DirectedGraph()
  401. graph.add(None) # Sentinel as root dependencies' parent.
  402. connected = {None}
  403. for key, criterion in state.criteria.items():
  404. if not _has_route_to_root(state.criteria, key, all_keys, connected):
  405. continue
  406. if key not in graph:
  407. graph.add(key)
  408. for p in criterion.iter_parent():
  409. try:
  410. pkey = all_keys[id(p)]
  411. except KeyError:
  412. continue
  413. if pkey not in graph:
  414. graph.add(pkey)
  415. graph.connect(pkey, key)
  416. return Result(
  417. mapping={k: v for k, v in mapping.items() if k in connected},
  418. graph=graph,
  419. criteria=state.criteria,
  420. )
  421. class Resolver(AbstractResolver):
  422. """The thing that performs the actual resolution work."""
  423. base_exception = ResolverException
  424. def resolve(self, requirements, max_rounds=100):
  425. """Take a collection of constraints, spit out the resolution result.
  426. The return value is a representation to the final resolution result. It
  427. is a tuple subclass with three public members:
  428. * `mapping`: A dict of resolved candidates. Each key is an identifier
  429. of a requirement (as returned by the provider's `identify` method),
  430. and the value is the resolved candidate.
  431. * `graph`: A `DirectedGraph` instance representing the dependency tree.
  432. The vertices are keys of `mapping`, and each edge represents *why*
  433. a particular package is included. A special vertex `None` is
  434. included to represent parents of user-supplied requirements.
  435. * `criteria`: A dict of "criteria" that hold detailed information on
  436. how edges in the graph are derived. Each key is an identifier of a
  437. requirement, and the value is a `Criterion` instance.
  438. The following exceptions may be raised if a resolution cannot be found:
  439. * `ResolutionImpossible`: A resolution cannot be found for the given
  440. combination of requirements. The `causes` attribute of the
  441. exception is a list of (requirement, parent), giving the
  442. requirements that could not be satisfied.
  443. * `ResolutionTooDeep`: The dependency tree is too deeply nested and
  444. the resolver gave up. This is usually caused by a circular
  445. dependency, but you can try to resolve this by increasing the
  446. `max_rounds` argument.
  447. """
  448. resolution = Resolution(self.provider, self.reporter)
  449. state = resolution.resolve(requirements, max_rounds=max_rounds)
  450. return _build_result(state)