Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/circular-dependencies.py
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | for module in sorted(deps.keys()): | ||||
old_size = len(closure) | old_size = len(closure) | ||||
old_closure_keys = sorted(closure.keys()) | old_closure_keys = sorted(closure.keys()) | ||||
for src in old_closure_keys: | for src in old_closure_keys: | ||||
for dep in deps[src]: | for dep in deps[src]: | ||||
if dep not in closure: | if dep not in closure: | ||||
closure[dep] = closure[src] + [src] | closure[dep] = closure[src] + [src] | ||||
if len(closure) == old_size: | if len(closure) == old_size: | ||||
break | break | ||||
# If module is in its own transitive closure, it's a circular dependency; check if it is the shortest | # If module is in its own transitive closure, it's a circular | ||||
if module in closure and (shortest_cycle is None or len(closure[module]) + 1 < len(shortest_cycle)): | # dependency; check if it is the shortest | ||||
if module in closure and (shortest_cycle is None or len( | |||||
closure[module]) + 1 < len(shortest_cycle)): | |||||
shortest_cycle = [module] + closure[module] | shortest_cycle = [module] + closure[module] | ||||
if shortest_cycle is None: | if shortest_cycle is None: | ||||
break | break | ||||
# We have the shortest circular dependency; report it | # We have the shortest circular dependency; report it | ||||
module = shortest_cycle[0] | module = shortest_cycle[0] | ||||
print("Circular dependency: {}".format( | print("Circular dependency: {}".format( | ||||
" -> ".join(shortest_cycle + [module]))) | " -> ".join(shortest_cycle + [module]))) | ||||
# And then break the dependency to avoid repeating in other cycles | # And then break the dependency to avoid repeating in other cycles | ||||
deps[shortest_cycle[-1]] = deps[shortest_cycle[-1]] - set([module]) | deps[shortest_cycle[-1]] = deps[shortest_cycle[-1]] - set([module]) | ||||
have_cycle = True | have_cycle = True | ||||
sys.exit(1 if have_cycle else 0) | sys.exit(1 if have_cycle else 0) |