comparison python/unroll_deps.py @ 151:f89c3615b414

this is how its done
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 12 Jul 2011 18:21:48 -0700
parents ef01512b2212
children 8d04ad79ef79
comparison
equal deleted inserted replaced
150:ef01512b2212 151:f89c3615b414
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 def unroll_dependencies(dependencies): 3 def unroll_dependencies(dependencies):
4 """unroll dependencies""" 4 """unroll dependencies"""
5 order = [] 5 order = []
6
7 # unroll the dependencies
6 for package, deps in dependencies.items(): 8 for package, deps in dependencies.items():
7 print package, order 9 print package, order
8 try: 10 try:
9 index = order.index(package) 11 index = order.index(package)
10 except ValueError: 12 except ValueError:
11 order.append(package) 13 order.append(package)
12 index = len(order) - 1 14 index = len(order) - 1
13 for dep in deps: 15 for dep in deps:
14 try: 16 try:
15 dep_index = order.index(dep) 17 dep_index = order.index(dep)
16 assert dep_index < index, "Cyclic dependencies detected: %s, %s" % (package, dep) 18 if dep_index > index:
19 order.insert(dep_index+1, package)
20 order.pop(index)
21 index = dep_index
17 except ValueError: 22 except ValueError:
18 order.insert(index, dep) 23 order.insert(index, dep)
24 except AssertionError:
25 print reverse_deps
26 raise
27
28 # ensure no cyclic dependencies
29 # XXX should do this first
30 order_dict = dict([(j, i) for i, j in enumerate(order)])
31 for package, deps in dependencies.items():
32 index = order_dict[package]
33 for d in deps:
34 assert index > order_dict[d], "Cyclic dependencies detected"
35
19 return order 36 return order
20 37
21 if __name__ == '__main__': 38 if __name__ == '__main__':
22 deps = {'packageA': set(['packageB', 'packageC']), 39 deps = {'packageA': set(['packageB', 'packageC', 'packageF']),
23 'packageB': set(['packageC', 'packageD', 'packageE']), 40 'packageB': set(['packageC', 'packageD', 'packageE', 'packageG']),
24 'packageC': set(['packageE'])} 41 'packageC': set(['packageE']),
42 'packageE': set(['packageF', 'packageG']),
43 'packageF': set(['packageG']),
44 'packageX': set()}
25 unrolled = unroll_dependencies(deps) 45 unrolled = unroll_dependencies(deps)
26 print unrolled 46 print unrolled