5from .providers
import AbstractResolver
6from .structs
import DirectedGraph, IteratorMapping, build_iter_view
9 "RequirementInformation", [
"requirement",
"parent"]
14 """A base class for all exceptions raised by this module.
16 Exceptions derived by this class should all be handled in this module. Any
17 bubbling pass the resolver should be treated as a bug.
27 return "Requirements conflict: {}".format(
28 ", ".join(repr(r)
for r
in self.
criterion.iter_requirement()),
34 super(InconsistentCandidate, self).
__init__(candidate, criterion)
39 return "Provided candidate {!r} does not satisfy {}".format(
41 ", ".join(repr(r)
for r
in self.
criterion.iter_requirement()),
46 """Representation of possible resolution results of a package.
48 This holds three attributes:
50 * `information` is a collection of `RequirementInformation` pairs.
51 Each pair is a requirement contributing to this criterion, and the
52 candidate that provides the requirement.
53 * `incompatibilities` is a collection of all known not-to-work candidates
54 to exclude from consideration.
55 * `candidates` is a collection containing all possible candidates deducted
56 from the union of contributing requirements and known incompatibilities.
57 It should never be empty, except when the criterion is an attribute of a
58 raised `RequirementsConflicted` (in which case it is always empty).
61 This class is intended to be externally immutable. **Do not** mutate
62 any of its attribute containers.
65 def __init__(self, candidates, information, incompatibilities):
71 requirements =
", ".join(
72 "({!r}, via={!r})".format(req, parent)
75 return "Criterion({})".format(requirements)
88class ResolutionImpossible(ResolutionError):
106 """Stateful resolution object.
108 This is designed as a one-off object that holds information to kick start
109 the resolution process, and holds the results afterwards.
125 """Push a new state into history.
127 This new state will be used to hold resolution results of the next
139 self.
_r.adding_requirement(requirement=requirement, parent=parent)
141 identifier = self.
_p.identify(requirement_or_candidate=requirement)
146 incompatibilities = []
148 matches = self.
_p.find_matches(
149 identifier=identifier,
153 {identifier: [requirement]},
158 {identifier: incompatibilities},
169 candidates=build_iter_view(matches),
170 information=information,
171 incompatibilities=incompatibilities,
175 criteria[identifier] = criterion
178 """Remove information from parents of criteria.
180 Concretely, removes all values from each criterion's ``information``
181 field that have one of ``parents`` as provider of the requirement.
183 :param criteria: The criteria to update.
184 :param parents: Identifiers for which to remove information from all criteria.
203 return self.
_p.get_preference(
205 resolutions=self.
state.mapping,
214 backtrack_causes=self.
state.backtrack_causes,
219 current_pin = self.
state.mapping[name]
223 self.
_p.is_satisfied_by(requirement=r, candidate=current_pin)
229 for requirement
in self.
_p.get_dependencies(candidate=candidate):
234 criterion = self.
state.criteria[name]
240 except RequirementsConflicted
as e:
250 self.
_p.is_satisfied_by(requirement=r, candidate=candidate)
256 self.
_r.pinning(candidate=candidate)
262 self.
state.mapping[name] = candidate
271 """Perform backjumping.
273 When we enter here, the stack is like this::
278 .... earlier states are irrelevant.
280 1. No pins worked for Z, so it does not have a pin.
281 2. We want to reset state Y to unpinned, and pin another candidate.
282 3. State X holds what state Y was before the pin, but does not
283 have the incompatibility information gathered in state Y.
285 Each iteration of the loop will:
287 1. Identify Z. The incompatibility is not always caused by the latest
288 state. For example, given three requirements A, B and C, with
289 dependencies A1, B1 and C1, where A1 and B1 are incompatible: the
290 last state might be related to C, so we want to discard the
293 3. Discard Y but remember its incompatibility information gathered
294 previously, and the failure we're dealing with right now.
295 4. Push a new state Y' based on X, and apply the incompatibility
296 information from Y to Y'.
297 5a. If this causes Y' to conflict, we need to backtrack again. Make Y'
298 the new Z and go back to step 2.
299 5b. If the incompatibilities apply cleanly, end backtracking.
305 incompatible_deps = {self.
_p.identify(r)
for r
in incompatible_reqs}
311 incompatible_state =
False
312 while not incompatible_state:
315 broken_state = self.
_states.pop()
317 except (IndexError, KeyError):
319 current_dependencies = {
321 for d
in self.
_p.get_dependencies(candidate)
327 incompatibilities_from_broken = [
338 for k, incompatibilities
in incompatibilities_from_broken:
339 if not incompatibilities:
342 criterion = self.
state.criteria[k]
345 matches = self.
_p.find_matches(
354 {k: incompatibilities},
357 candidates = build_iter_view(matches)
362 candidates=candidates,
364 incompatibilities=incompatibilities,
383 raise RuntimeError(
"already resolved")
395 for r
in requirements:
398 except RequirementsConflicted
as e:
406 for round_index
in range(max_rounds):
407 self.
_r.starting_round(index=round_index)
409 unsatisfied_names = [
416 if not unsatisfied_names:
417 self.
_r.ending(state=self.
state)
433 self.
_r.resolving_conflicts(causes=causes)
435 self.
state.backtrack_causes[:] = causes
443 newly_unsatisfied_names = {
446 if key
in satisfied_names
450 self.
state.criteria, newly_unsatisfied_names
455 self.
_r.ending_round(index=round_index, state=self.
state)
463 if key
not in criteria:
465 for p
in criteria[key].iter_parent():
467 pkey = all_keys[id(p)]
470 if pkey
in connected:
485 all_keys[id(
None)] =
None
498 pkey = all_keys[id(p)]
501 if pkey
not in graph:
506 mapping={k: v
for k, v
in mapping.items()
if k
in connected},
513 """The thing that performs the actual resolution work."""
515 base_exception = ResolverException
517 def resolve(self, requirements, max_rounds=100):
518 """Take a collection of constraints, spit out the resolution result.
520 The return value is a representation to the final resolution result. It
521 is a tuple subclass with three public members:
523 * `mapping`: A dict of resolved candidates. Each key is an identifier
524 of a requirement (as returned by the provider's `identify` method),
525 and the value is the resolved candidate.
526 * `graph`: A `DirectedGraph` instance representing the dependency tree.
527 The vertices are keys of `mapping`, and each edge represents *why*
528 a particular package is included. A special vertex `None` is
529 included to represent parents of user-supplied requirements.
530 * `criteria`: A dict of "criteria" that hold detailed information on
531 how edges in the graph are derived. Each key is an identifier of a
532 requirement, and the value is a `Criterion` instance.
534 The following exceptions may be raised if a resolution cannot be found:
536 * `ResolutionImpossible`: A resolution cannot be found for the given
537 combination of requirements. The `causes` attribute of the
538 exception is a list of (requirement, parent), giving the
539 requirements that could not be satisfied.
540 * `ResolutionTooDeep`: The dependency tree is too deeply nested and
541 the resolver gave up. This is usually caused by a circular
542 dependency, but you can try to resolve this by increasing the
543 `max_rounds` argument.
__init__(self, candidates, information, incompatibilities)
__init__(self, candidate, criterion)
__init__(self, criterion)
__init__(self, round_count)
_remove_information_from_criteria(self, criteria, parents)
__init__(self, provider, reporter)
resolve(self, requirements, max_rounds)
_get_updated_criteria(self, candidate)
_get_preference(self, name)
_attempt_to_pin_criterion(self, name)
_add_to_criteria(self, criteria, requirement, parent)
_is_current_pin_satisfying(self, name, criterion)
resolve(self, requirements, max_rounds=100)
_has_route_to_root(criteria, key, all_keys, connected)