annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 def unroll_dependencies(dependencies):
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """unroll dependencies"""
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 order = []
151
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
6
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
7 # unroll the dependencies
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 for package, deps in dependencies.items():
150
ef01512b2212 fixes; doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 149
diff changeset
9 print package, order
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 try:
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 index = order.index(package)
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 except ValueError:
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 order.append(package)
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 index = len(order) - 1
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 for dep in deps:
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 try:
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 dep_index = order.index(dep)
151
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
18 if dep_index > index:
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
19 order.insert(dep_index+1, package)
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
20 order.pop(index)
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
21 index = dep_index
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 except ValueError:
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 order.insert(index, dep)
151
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
24 except AssertionError:
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
25 print reverse_deps
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
26 raise
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
27
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
28 # ensure no cyclic dependencies
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
29 # XXX should do this first
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
30 order_dict = dict([(j, i) for i, j in enumerate(order)])
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
31 for package, deps in dependencies.items():
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
32 index = order_dict[package]
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
33 for d in deps:
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
34 assert index > order_dict[d], "Cyclic dependencies detected"
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
35
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 return order
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 if __name__ == '__main__':
151
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
39 deps = {'packageA': set(['packageB', 'packageC', 'packageF']),
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
40 'packageB': set(['packageC', 'packageD', 'packageE', 'packageG']),
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
41 'packageC': set(['packageE']),
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
42 'packageE': set(['packageF', 'packageG']),
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
43 'packageF': set(['packageG']),
f89c3615b414 this is how its done
Jeff Hammel <jhammel@mozilla.com>
parents: 150
diff changeset
44 'packageX': set()}
149
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 unrolled = unroll_dependencies(deps)
05e461e4b409 add an unroll-deps example prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 print unrolled