contrib/import-checker.py
changeset 24491 784b278b349c
parent 24490 fb4639d5268e
child 24668 81873bb2a01d
equal deleted inserted replaced
24490:fb4639d5268e 24491:784b278b349c
   160                         ', '.join(sorted(from_stdlib[False]))))
   160                         ', '.join(sorted(from_stdlib[False]))))
   161 
   161 
   162 class CircularImport(Exception):
   162 class CircularImport(Exception):
   163     pass
   163     pass
   164 
   164 
   165 
       
   166 def cyclekey(names):
       
   167     return tuple(sorted(names))
       
   168 
       
   169 def checkmod(mod, imports):
   165 def checkmod(mod, imports):
   170     shortest = {}
   166     shortest = {}
   171     visit = [[mod]]
   167     visit = [[mod]]
   172     while visit:
   168     while visit:
   173         path = visit.pop(0)
   169         path = visit.pop(0)
   201     ...            'top.qux': ['foo']}
   197     ...            'top.qux': ['foo']}
   202     >>> print '\\n'.join(sorted(find_cycles(imports)))
   198     >>> print '\\n'.join(sorted(find_cycles(imports)))
   203     top.bar -> top.baz -> top.foo -> top.bar
   199     top.bar -> top.baz -> top.foo -> top.bar
   204     top.foo -> top.qux -> top.foo
   200     top.foo -> top.qux -> top.foo
   205     """
   201     """
   206     cycles = {}
   202     cycles = set()
   207     for mod in sorted(imports.iterkeys()):
   203     for mod in sorted(imports.iterkeys()):
   208         try:
   204         try:
   209             checkmod(mod, imports)
   205             checkmod(mod, imports)
   210         except CircularImport, e:
   206         except CircularImport, e:
   211             cycle = e.args[0]
   207             cycle = e.args[0]
   212             cycles[cyclekey(cycle)] = ' -> '.join(rotatecycle(cycle))
   208             cycles.add(" -> ".join(rotatecycle(cycle)))
   213     return cycles.values()
   209     return cycles
   214 
   210 
   215 def _cycle_sortkey(c):
   211 def _cycle_sortkey(c):
   216     return len(c), c
   212     return len(c), c
   217 
   213 
   218 def main(argv):
   214 def main(argv):