Comments
Patch
@@ -137,6 +137,15 @@
continue
check_one_mod(i, imports, path=path, ignore=ignore)
+def rotatecycle(cycle):
+ """arrange a cycle so that the lexicographically first module listed first
+
+ >>> rotatecycle(['foo', 'bar', 'foo'])
+ ['bar', 'foo', 'bar']
+ """
+ lowest = min(cycle)
+ idx = cycle.index(lowest)
+ return cycle[idx:] + cycle[1:idx] + [lowest]
def find_cycles(imports):
"""Find cycles in an already-loaded import graph.
@@ -146,8 +155,8 @@
... 'top.baz': ['foo'],
... 'top.qux': ['foo']}
>>> print '\\n'.join(sorted(find_cycles(imports)))
- top.bar -> top.baz -> top.foo -> top.bar
- top.foo -> top.qux -> top.foo
+ top.bar -> top.baz -> top.foo -> top.bar -> top.bar
+ top.foo -> top.qux -> top.foo -> top.foo
"""
cycles = {}
seen_in_cycle = set()
@@ -156,7 +165,7 @@
check_one_mod(mod, imports, ignore=cycles)
except CircularImport, e:
cycle = e.args[0]
- cycles[cyclekey(cycle)] = ' -> '.join(cycle)
+ cycles[cyclekey(cycle)] = ' -> '.join(rotatecycle(cycle))
seen_in_cycle |= set(cycle)
return cycles.values()