def get_deps_in_order(filename): def link_graph(depgraph): """Link graph so it really can be traversed the way I'm traversing it""" for key, depdict in depgraph.items(): for depkey in depdict.keys(): if depkey in depgraph: depdict[depkey] = depgraph[depkey] mf = py2depgraph.mymf(debug=0, excludes=['unittest']) mf.run_script(filename) depgraph = mf._depgraph link_graph(depgraph) def mod_we_need(mod): """Returns try if module is something we need to concatenate""" if mod.__name__ in sys.builtin_module_names: return False if mod.__file__[-3:] == '.so': return False if 'site-packages' in mod.__file__: return True if '/lib/python' in mod.__file__: return False return True def leafgen(tree): """Walk the tree of dependencies depth-first, returning only leaves""" for leaf in tree: mod = mf.modules[leaf] if not mod_we_need(mod) and not mod.__name__ == '__main__': continue # we don't need __main__, but still want its dependecies if tree[leaf] == 1: yield mod else: for innerleaf in leafgen(tree[leaf]): yield innerleaf if mod_we_need(mod) and mod.__name__ != '__main__': yield mod def no_repeats(iterable): """yields a value only the first time it occurs in iterable (by == equality)""" imported = [] for k in iterable: if k in imported: pass else: imported.append(k) yield k return no_repeats(leafgen(depgraph))
def genDepGraph(inScript, options): path = sys.path[:] debug = 0 mf = mymf(path, debug, options.exclude) mf.run_script(inScript) # find all modules in standard lib and set them aside for later; # assume that modules that don't have a filename are from stdlib ignore = set() for moduleName, module in mf.modules.iteritems(): ign = False if options.ignoreNoFile and (not module.__file__): ign = True if options.ignoreStdlib and module.__file__: path1 = os.path.abspath(os.path.dirname(module.__file__)).lower() path2 = os.path.abspath('c:\python24\lib').lower() if path1 == path2: ign = True if ign: ignore.add(moduleName) return dict(depgraph=mf._depgraph, types=mf._types), ignore
def main(): # Create module finder path = sys.path[:] debug = 0 exclude = [] mf = mymf(path,debug,exclude) # Parse command line options parser = OptionParser() parser.add_option('-m', '--mono', action='store_false', dest='colored', default=True, help='Turns graph output black and white') parser.add_option('-o', '--option', dest='output_file', default='stdout', help='Filename to store output in') (options, args) = parser.parse_args() # Find all the modules mf.run_script(args[0]) # Run the dot file builder dep_graph = DepGraph(mf._depgraph, mf._types) dep_graph.main(options)