def sort_by_dependency(tp_modules): paths = [] for mod in tp_modules: paths.append(os.path.dirname(mod)) paths = sorted(list(set(paths))) tp_modules_with_main = [] lt = level_tree.Tree() for mod in tp_modules: finder = modulefinder.ModuleFinder(path=paths) finder.run_script(mod) for name, m in finder.modules.iteritems(): if name is "__main__": if "main" in m.globalnames.keys(): tp_modules_with_main.append(mod) continue if (m.__file__): lt.add_edge((mod, m.__file__)) else: lt.add_edge((mod, name)) lt.levelize() lt_levels_result = lt.get_level() #Sort the module by their dependency levels. lt_levels_result_sorted = sorted(lt_levels_result.iteritems(), key=operator.itemgetter(1)) lt_levels_result_sorted = [(f[0], f[0] in tp_modules_with_main) for f in lt_levels_result_sorted] return lt_levels_result_sorted
def testLevelizeMoreThanOnce(self): lt_eg = [('t_1a', 't_3'), ('t_1b', 't_2'), ('t_3', 't_4'), ('t_2', 't_3')] lt = level_tree.Tree() for e in lt_eg: lt.add_edge(e) lt.levelize() self.assertRaises(ValueError, lt.levelize)
def testRepeatedEdge(self): # [4] [3] [2] [1] # t_1a ----------> t_3 --> t_4 # t_1b --> t_2------^ lt_eg = [('t_1a', 't_3'), ('t_1b', 't_2'), ('t_3', 't_4'), ('t_2', 't_3'), ('t_2', 't_3')] lt_levels_expected = { 't_1a': 4, 't_1b': 4, 't_2': 3, 't_3': 2, 't_4': 1 } lt = level_tree.Tree() for e in lt_eg: lt.add_edge(e) lt.levelize() lt_levels_result = lt.get_level() for k in lt_levels_result.keys(): self.assertEqual(lt_levels_expected[k], lt_levels_result[k])
def testNonTupleEdge(self): lt = level_tree.Tree() self.assertRaises(TypeError, lt.add_edge, 1) self.assertRaises(TypeError, lt.add_edge, [('a', 'b')]) self.assertRaises(TypeError, lt.add_edge, {})