Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 | |
| 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.py chrome_dll gtest# | |
| 21 """ | |
| 22 | |
| 23 | |
| 24 def GetPath(graph, fro, to): | |
| 25 """Given a graph in (node -> list of successor nodes) dictionary format, | |
| 26 yields all paths from |fro| to |to|, starting with the shortest.""" | |
| 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: | |
|
viettrungluu
2011/11/23 20:30:40
nit: |while q:|
Nico
2011/11/23 20:33:01
Done.
| |
| 30 t, path = q.popleft() | |
| 31 if t == to: | |
| 32 yield path + [t] | |
| 33 for d in graph[t]: | |
| 34 q.append((d, path + [t])) | |
| 35 | |
| 36 | |
| 37 def MatchNode(graph, substring): | |
| 38 """Given a dictionary, returns the key that matches |substring| best. Exits | |
| 39 if there's not one single best match.""" | |
| 40 candidates = [] | |
| 41 for target in graph: | |
| 42 if substring in target: | |
| 43 candidates.append(target) | |
| 44 | |
| 45 if not candidates: | |
| 46 print 'No targets match "%s"' % substring | |
| 47 sys.exit(1) | |
| 48 if len(candidates) > 1: | |
| 49 print 'More than one target matches "%s": %s' % ( | |
| 50 substring, ' '.join(candidates)) | |
| 51 sys.exit(1) | |
|
M-A Ruel
2011/11/23 20:35:57
I didn't mean to remove the calls in this function
| |
| 52 return candidates[0] | |
| 53 | |
| 54 | |
| 55 def Main(argv): | |
| 56 # Check that dump.json exists and that it's not too old. | |
| 57 dump_json_dirty = False | |
| 58 try: | |
| 59 st = os.stat('dump.json') | |
| 60 file_age_s = time.time() - st.st_mtime | |
| 61 if file_age_s > 2 * 60 * 60: | |
| 62 print 'dump.json is more than 2 hours old.' | |
| 63 dump_json_dirty = True | |
| 64 except IOError: | |
| 65 print 'dump.json not found.' | |
| 66 dump_json_dirty = True | |
| 67 | |
| 68 if dump_json_dirty: | |
| 69 print 'Run' | |
| 70 print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' | |
| 71 print 'first, then try again.' | |
| 72 sys.exit(1) | |
| 73 | |
| 74 g = json.load(open('dump.json')) | |
| 75 | |
| 76 if len(argv) != 3: | |
| 77 usage() | |
| 78 sys.exit(1) | |
| 79 | |
| 80 fro = MatchNode(g, argv[1]) | |
| 81 to = MatchNode(g, argv[2]) | |
| 82 | |
| 83 paths = list(GetPath(g, fro, to)) | |
| 84 if len(paths) > 0: | |
| 85 print 'These paths lead from %s to %s:' % (fro, to) | |
| 86 for path in paths: | |
| 87 print ' -> '.join(path) | |
| 88 else: | |
| 89 print 'No paths found from %s to %s.' % (fro, to) | |
| 90 | |
| 91 | |
| 92 if __name__ == '__main__': | |
| 93 Main(sys.argv) | |
| OLD | NEW |