OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
M-A Ruel
2011/11/23 20:28:05
Most script don't put an empty line here, in fact
Nico
2011/11/23 20:33:01
Done.
Mark Mentovai
2011/11/23 21:01:23
I actually prefer the empty line because it lets t
| |
3 # Copyright (c) 2011 Google Inc. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Prints paths between gyp targets. | |
8 """ | |
9 | |
10 import json | |
11 import os | |
12 import sys | |
13 import time | |
14 | |
15 from collections import deque | |
16 | |
17 def usage(): | |
18 print """\ | |
19 Usage: | |
20 tools/gyp-explain.sh chrome_dll gtest# | |
21 """ | |
22 | |
23 def GetPath(graph, fro, to, get_all=False): | |
24 """Given a graph in (node -> list of successor nodes) dictionary format, | |
25 yields either the shortest path from |fro| to |to|, or if |get_all| is True, | |
26 all paths from |fro| to |to|.""" | |
27 # Storing full paths in the queue is a bit wasteful, but good enough for this. | |
28 q = deque([(fro, [])]) | |
29 while len(q) > 0: | |
30 t, path = q.popleft() | |
31 if t == to: | |
32 yield path + [t] | |
33 if not get_all: | |
34 break | |
35 for d in graph[t]: | |
36 q.append((d, path + [t])) | |
37 | |
38 | |
39 def MatchNode(graph, substring): | |
40 """Given a dictionary, returns the key that matches |substring| best. Exits | |
41 if there's not one single best match.""" | |
42 candidates = [] | |
43 for target in graph: | |
44 if substring in target: | |
45 candidates.append(target) | |
46 | |
47 if not candidates: | |
48 print 'No targets match "%s"' % substring | |
49 sys.exit(1) | |
50 if len(candidates) > 1: | |
51 print 'More than one target matches "%s": %s' % ( | |
52 substring, ' '.join(candidates)) | |
53 sys.exit(1) | |
54 return candidates[0] | |
55 | |
56 | |
57 def Main(argv): | |
58 # Check that dump.json exists and that it's not too old. | |
59 dump_json_dirty = False | |
60 try: | |
61 st = os.stat('dump.json') | |
62 file_age_s = time.time() - st.st_mtime | |
63 if file_age_s > 2 * 60 * 60: | |
64 print 'dump.json is more than 2 hours old.' | |
65 dump_json_dirty = True | |
66 except IOError: | |
67 print 'dump.json not found.' | |
68 dump_json_dirty = True | |
69 | |
70 if dump_json_dirty: | |
71 print 'Run' | |
72 print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' | |
M-A Ruel
2011/11/23 20:28:05
Why not run it automatically? It'd be more user fr
Nico
2011/11/23 20:33:01
I disagree.
M-A Ruel
2011/11/23 20:35:57
Ah, why?
Nico
2011/11/23 20:37:30
No particular reason. I'll roll it now.
| |
73 print 'first, then try again.' | |
74 sys.exit(1) | |
M-A Ruel
2011/11/23 20:28:05
I highly prefer to return 1 and have
sys.exit(Ma
Nico
2011/11/23 20:33:01
Yeah, me too, but I already sys.exit from MatchNod
| |
75 | |
76 g = json.load(open('dump.json')) | |
77 | |
78 if len(argv) != 3: | |
79 usage() | |
80 sys.exit(1) | |
81 | |
82 fro = MatchNode(g, argv[1]) | |
83 to = MatchNode(g, argv[2]) | |
84 | |
85 paths = list(GetPath(g, fro, to, get_all=True)) | |
86 if len(paths) > 0: | |
87 print 'These paths lead from %s to %s:' % (fro, to) | |
88 for path in paths: | |
89 print ' -> '.join(path) | |
90 else: | |
91 print 'No paths found from %s to %s.' % (fro, to) | |
M-A Ruel
2011/11/23 20:28:05
return 1 so this can be used as a test?
Nico
2011/11/23 20:33:01
Can be added once something needs it :-)
| |
92 | |
93 | |
94 if __name__ == '__main__': | |
95 Main(sys.argv) | |
OLD | NEW |