OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
M-A Ruel
2011/11/23 20:06:30
Copyright
Nico
2011/11/23 20:12:07
Done.
| |
3 """Prints paths between gyp targets. | |
4 """ | |
5 | |
6 import json | |
7 import os | |
8 import sys | |
9 import time | |
10 | |
11 from collections import deque | |
12 | |
13 def usage(): | |
14 print """\ | |
15 Usage: | |
16 tools/gyp-explain.sh chrome_dll gtest# | |
viettrungluu
2011/11/23 20:19:51
Drive-by: ".sh"?
It might be more helpful to put:
Nico
2011/11/23 20:22:58
Done.
| |
17 """ | |
18 | |
19 def GetPath(graph, fro, to, get_all=False): | |
viettrungluu
2011/11/23 20:19:51
Is get_all even necessary, given that you're retur
Nico
2011/11/23 20:22:58
Oh, good point. Removed.
| |
20 """Given a graph in (node -> list of successor nodes) dictionary format, | |
21 yields either the shortest path from |fro| to |to|, or if |get_all| is True, | |
22 all paths from |fro| to |to|.""" | |
23 # Storing full paths in the queue is a bit wasteful, but good enough for this. | |
24 q = deque([(fro, [])]) | |
25 while len(q) > 0: | |
26 t, path = q.popleft() | |
27 if t == to: | |
28 yield path + [t] | |
29 if not get_all: | |
30 break | |
31 for d in g[t]: | |
32 q.append((d, path + [t])) | |
33 | |
34 | |
35 def MatchNode(graph, substring): | |
36 """Given a dictionary, returns the key that matches |substring| best. Exits | |
37 if there's not one single best match.""" | |
38 candidates = [] | |
39 for target in g: | |
40 if substring in target: | |
41 candidates.append(target) | |
42 | |
43 if not candidates: | |
44 print 'No targets match "%s"' % substring | |
45 sys.exit(1) | |
46 if len(candidates) > 1: | |
47 print 'More than one target matches "%s": %s' % ( | |
48 substring, ' '.join(candidates)) | |
49 sys.exit(1) | |
50 return candidates[0] | |
51 | |
52 | |
53 if __name__ == '__main__': | |
M-A Ruel
2011/11/23 20:06:30
Use a main()
Nico
2011/11/23 20:12:07
Done.
| |
54 # Check that dump.json exists and that it's not too old. | |
55 dump_json_dirty = False | |
56 try: | |
57 st = os.stat('dump.json') | |
58 file_age_s = time.time() - st.st_mtime | |
59 if file_age_s > 2 * 60 * 60: | |
60 print 'dump.json is more than 2 hours old.' | |
61 dump_json_dirty = True | |
62 except IOError: | |
63 print 'dump.json not found.' | |
64 dump_json_dirty = True | |
65 | |
66 if dump_json_dirty: | |
67 print 'Run' | |
68 print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' | |
69 print 'first, then try again.' | |
70 sys.exit(1) | |
71 | |
72 g = json.load(open('dump.json')) | |
73 | |
74 if len(sys.argv) != 3: | |
75 usage() | |
76 sys.exit(1) | |
77 | |
78 fro = MatchNode(g, sys.argv[1]) | |
79 to = MatchNode(g, sys.argv[2]) | |
80 | |
81 paths = list(GetPath(g, fro, to, get_all=True)) | |
82 if len(paths) > 0: | |
83 print 'These paths lead from %s to %s:' % (fro, to) | |
84 for path in paths: | |
85 print ' -> '.join(path) | |
86 else: | |
87 print 'No paths found from %s to %s.' % (fro, to) | |
OLD | NEW |