OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Prints paths between gyp targets. | 6 """Prints paths between gyp targets. |
7 """ | 7 """ |
8 | 8 |
9 import json | 9 import json |
10 import os | 10 import os |
11 import sys | 11 import sys |
12 import time | 12 import time |
13 | 13 |
14 from collections import deque | 14 from collections import deque |
15 | 15 |
16 def usage(): | 16 def usage(): |
17 print """\ | 17 print """\ |
18 Usage: | 18 Usage: |
19 tools/gyp-explain.py chrome_dll# gtest# | 19 tools/gyp-explain.py [--dot] chrome_dll# gtest# |
20 """ | 20 """ |
21 | 21 |
22 | 22 |
23 def GetPath(graph, fro, to): | 23 def GetPath(graph, fro, to): |
24 """Given a graph in (node -> list of successor nodes) dictionary format, | 24 """Given a graph in (node -> list of successor nodes) dictionary format, |
25 yields all paths from |fro| to |to|, starting with the shortest.""" | 25 yields all paths from |fro| to |to|, starting with the shortest.""" |
26 # Storing full paths in the queue is a bit wasteful, but good enough for this. | 26 # Storing full paths in the queue is a bit wasteful, but good enough for this. |
27 q = deque([(fro, [])]) | 27 q = deque([(fro, [])]) |
28 while q: | 28 while q: |
29 t, path = q.popleft() | 29 t, path = q.popleft() |
(...skipping 14 matching lines...) Expand all Loading... | |
44 if not candidates: | 44 if not candidates: |
45 print 'No targets match "%s"' % substring | 45 print 'No targets match "%s"' % substring |
46 sys.exit(1) | 46 sys.exit(1) |
47 if len(candidates) > 1: | 47 if len(candidates) > 1: |
48 print 'More than one target matches "%s": %s' % ( | 48 print 'More than one target matches "%s": %s' % ( |
49 substring, ' '.join(candidates)) | 49 substring, ' '.join(candidates)) |
50 sys.exit(1) | 50 sys.exit(1) |
51 return candidates[0] | 51 return candidates[0] |
52 | 52 |
53 | 53 |
54 def CommonPrefix(seq): | |
55 """Given a list of strings, return the longest common prefix.""" | |
56 prefix = seq[0] | |
57 for item in seq: | |
58 for i in range(len(prefix)): | |
59 if prefix[:i+1] != item[:i+1]: | |
60 prefix = prefix[:i] | |
61 if i == 0: | |
62 return '' | |
63 break | |
64 return prefix | |
Nico
2013/05/07 21:17:54
os.path.commonprefix?
| |
65 | |
66 | |
67 def EscapeForDot(string): | |
68 suffix = '#target' | |
69 if string.endswith(suffix): | |
70 string = string[:-len(suffix)] | |
71 string = string.replace('\\', '\\\\') | |
72 return '"' + string + '"' | |
73 | |
74 | |
75 def GenerateDot(fro, to, paths): | |
76 prefixes = [CommonPrefix(path) for path in paths] | |
77 prefix = CommonPrefix(prefixes) | |
78 # "strict" collapses common paths. | |
79 print '// Build with "dot -Tpng -ooutput.png this_file.dot"' | |
80 print 'strict digraph {' | |
81 for path in paths: | |
82 print (' -> '.join(EscapeForDot(item[len(prefix):]) for item in path)), ';' | |
83 print '}' | |
84 | |
85 | |
54 def Main(argv): | 86 def Main(argv): |
55 # Check that dump.json exists and that it's not too old. | 87 # Check that dump.json exists and that it's not too old. |
56 dump_json_dirty = False | 88 dump_json_dirty = False |
57 try: | 89 try: |
58 st = os.stat('dump.json') | 90 st = os.stat('dump.json') |
59 file_age_s = time.time() - st.st_mtime | 91 file_age_s = time.time() - st.st_mtime |
60 if file_age_s > 2 * 60 * 60: | 92 if file_age_s > 2 * 60 * 60: |
61 print 'dump.json is more than 2 hours old.' | 93 print 'dump.json is more than 2 hours old.' |
62 dump_json_dirty = True | 94 dump_json_dirty = True |
63 except OSError: | 95 except OSError: |
64 print 'dump.json not found.' | 96 print 'dump.json not found.' |
65 dump_json_dirty = True | 97 dump_json_dirty = True |
66 | 98 |
67 if dump_json_dirty: | 99 if dump_json_dirty: |
68 print 'Run' | 100 print 'Run' |
69 print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' | 101 print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' |
70 print 'first, then try again.' | 102 print 'first, then try again.' |
71 sys.exit(1) | 103 sys.exit(1) |
72 | 104 |
73 g = json.load(open('dump.json')) | 105 g = json.load(open('dump.json')) |
74 | 106 |
75 if len(argv) != 3: | 107 if len(argv) not in (3, 4): |
76 usage() | 108 usage() |
77 sys.exit(1) | 109 sys.exit(1) |
78 | 110 |
111 generate_dot = argv[1] == '--dot' | |
112 if generate_dot: | |
113 argv.pop(1) | |
114 | |
79 fro = MatchNode(g, argv[1]) | 115 fro = MatchNode(g, argv[1]) |
80 to = MatchNode(g, argv[2]) | 116 to = MatchNode(g, argv[2]) |
81 | 117 |
82 paths = list(GetPath(g, fro, to)) | 118 paths = list(GetPath(g, fro, to)) |
83 if len(paths) > 0: | 119 if len(paths) > 0: |
84 print 'These paths lead from %s to %s:' % (fro, to) | 120 if generate_dot: |
85 for path in paths: | 121 GenerateDot(fro, to, paths) |
86 print ' -> '.join(path) | 122 else: |
123 print 'These paths lead from %s to %s:' % (fro, to) | |
124 for path in paths: | |
125 print ' -> '.join(path) | |
87 else: | 126 else: |
88 print 'No paths found from %s to %s.' % (fro, to) | 127 print 'No paths found from %s to %s.' % (fro, to) |
89 | 128 |
90 | 129 |
91 if __name__ == '__main__': | 130 if __name__ == '__main__': |
92 Main(sys.argv) | 131 Main(sys.argv) |
OLD | NEW |