solver: improve apply_order to simplify more code

Signed-off-by: Niels Thykier <niels@thykier.net>
ubuntu/rebased
Niels Thykier 6 years ago
parent b306700376
commit 69d42ffb36
No known key found for this signature in database
GPG Key ID: A65B78DBE67C7AAC

@ -136,23 +136,23 @@ def compute_scc(graph):
return result return result
def apply_order(key, relation, ptable, order, negative_relation, debug_solver, logger): def apply_order(key, other, order, logger, order_cause, invert=False, order_sub_cause=''):
other = ptable[relation]
if other == key: if other == key:
# "Self-relation" => ignore # "Self-relation" => ignore
return return
order_key = order[key] order_key = order[key]
if negative_relation: if invert:
order_key.before.add(other)
order[other].after.add(key) order[other].after.add(key)
debug_check = order_key.before order_set = order_key.before
else: else:
order_key.after.add(other)
order[other].before.add(key) order[other].before.add(key)
debug_check = order_key.after order_set = order_key.after
if debug_solver and other not in debug_check: # pragma: no cover if logger.isEnabledFor(logging.DEBUG) and other not in order_set: # pragma: no cover
cause = 'Conflict' if negative_relation else 'Conflict' if order_sub_cause:
logger.debug("%s induced order: %s before %s", cause, key, other) order_sub_cause = ' (%s)' % order_sub_cause
logger.debug("%s induced order%s: %s before %s", order_cause, order_sub_cause, key, other)
# Defer adding until the end to ensure we only log the first time a dependency order is introduced.
order_set.add(other)
class InstallabilitySolver(object): class InstallabilitySolver(object):
@ -171,7 +171,6 @@ class InstallabilitySolver(object):
sat_in_testing = self._inst_tester.any_of_these_are_in_the_suite sat_in_testing = self._inst_tester.any_of_these_are_in_the_suite
universe = self._universe universe = self._universe
logger = self.logger logger = self.logger
debug_solver = logger.isEnabledFor(logging.DEBUG)
for rdep in chain.from_iterable(universe.reverse_dependencies_of(r) for r in rms): for rdep in chain.from_iterable(universe.reverse_dependencies_of(r) for r in rms):
# The binaries have reverse dependencies in testing; # The binaries have reverse dependencies in testing;
# check if we can/should migrate them first. # check if we can/should migrate them first.
@ -181,9 +180,9 @@ class InstallabilitySolver(object):
# (partly) satisfied by testing, assume it is okay # (partly) satisfied by testing, assume it is okay
continue continue
if rdep in ptable: if rdep in ptable:
apply_order(key, rdep, ptable, order, False, debug_solver, logger) apply_order(key, ptable[rdep], order, logger, 'Removal')
def _compute_order_for_dependency(self, key, depgroup, ptable, order, going_in, debug_solver): def _compute_order_for_dependency(self, key, depgroup, ptable, order, going_in):
# We got three cases: # We got three cases:
# - "swap" (replace existing binary with a newer version) # - "swap" (replace existing binary with a newer version)
# - "addition" (add new binary without removing any) # - "addition" (add new binary without removing any)
@ -194,6 +193,7 @@ class InstallabilitySolver(object):
# affect us. # affect us.
other_adds = set() other_adds = set()
other_rms = set() other_rms = set()
logger = self.logger
for d in ifilter_only(ptable, depgroup): for d in ifilter_only(ptable, depgroup):
other = ptable[d] other = ptable[d]
if d in going_in: if d in going_in:
@ -205,20 +205,12 @@ class InstallabilitySolver(object):
# schedule accordingly. # schedule accordingly.
other_rms.add(other) other_rms.add(other)
for other in (other_adds - other_rms): for other in other_adds - other_rms:
if debug_solver and other != key and other not in order[key].after: # pragma: no cover apply_order(key, other, order, logger, 'Dependency', order_sub_cause='add')
self.logger.debug("Dependency induced order (add): %s before %s", key, other) for other in other_rms - other_adds:
order[key].after.add(other) apply_order(key, other, order, logger, 'Dependency', order_sub_cause='remove', invert=True)
order[other].before.add(key)
for other in (other_rms - other_adds):
if debug_solver and other != key and other not in order[key].before: # pragma: no cover
self.logger.debug("Dependency induced order (remove): %s before %s", key, other)
order[key].before.add(other)
order[other].after.add(key)
def _compute_group_order_adds(self, adds, order, key, ptable, going_out, going_in): def _compute_group_order_adds(self, adds, order, key, ptable, going_out, going_in):
debug_solver = self.logger.isEnabledFor(logging.DEBUG)
sat_in_testing = self._inst_tester.any_of_these_are_in_the_suite sat_in_testing = self._inst_tester.any_of_these_are_in_the_suite
universe = self._universe universe = self._universe
for depgroup in chain.from_iterable(universe.dependencies_of(a) for a in adds): for depgroup in chain.from_iterable(universe.dependencies_of(a) for a in adds):
@ -229,7 +221,7 @@ class InstallabilitySolver(object):
if sat_in_testing(rigid): if sat_in_testing(rigid):
# (partly) satisfied by testing, assume it is okay # (partly) satisfied by testing, assume it is okay
continue continue
self._compute_order_for_dependency(key, depgroup, ptable, order, going_in, debug_solver) self._compute_order_for_dependency(key, depgroup, ptable, order, going_in)
def _compute_group_order(self, groups, key2item): def _compute_group_order(self, groups, key2item):
universe = self._universe universe = self._universe
@ -266,7 +258,7 @@ class InstallabilitySolver(object):
# "key" removes a conflict with one of # "key" removes a conflict with one of
# "other"'s binaries, so it is probably a good # "other"'s binaries, so it is probably a good
# idea to migrate "key" before "other" # idea to migrate "key" before "other"
apply_order(key, o, ptable, order, True, debug_solver, logger) apply_order(key, ptable[o], order, logger, 'Conflict', invert=True)
self._compute_group_order_rms(rms, order, key, ptable, going_out) self._compute_group_order_rms(rms, order, key, ptable, going_out)
self._compute_group_order_adds(adds, order, key, ptable, going_out, going_in) self._compute_group_order_adds(adds, order, key, ptable, going_out, going_in)

Loading…
Cancel
Save