Patchwork [7,of,8,v2] import-checker: try a little harder to show fewer cycles

login
register
mail settings
Submitter Augie Fackler
Date Nov. 17, 2013, 9:37 p.m.
Message ID <37732daba197e51ad201.1384724245@hit-nxdomain.opendns.com>
Download mbox | patch
Permalink /patch/3043/
State Accepted
Commit c65a6937b828c0a111584805751e2120b4bce902
Headers show

Comments

Augie Fackler - Nov. 17, 2013, 9:37 p.m.
# HG changeset patch
# User Augie Fackler <raf@durin42.com>
# Date 1384713200 18000
#      Sun Nov 17 13:33:20 2013 -0500
# Node ID 37732daba197e51ad201cb6b07796a9c1e7cd878
# Parent  595f370199e52637504a25be843397c3fdd7a324
import-checker: try a little harder to show fewer cycles

This makes sure that all cycles begin with the lexicographically first
module, so that we're less likely to show overlapping cycles in the
final analysis.

Patch

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -145,6 +145,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.
@@ -154,8 +163,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 = {}
     for mod in sorted(imports.iterkeys()):