Chromium Code Reviews| Index: tools/gyp-explain.py |
| diff --git a/tools/gyp-explain.py b/tools/gyp-explain.py |
| index 35b4667816eff7c4349e61fbe8c6762722154bec..097c1ed11e1d239d6f73fbf426b767f2a64c3210 100755 |
| --- a/tools/gyp-explain.py |
| +++ b/tools/gyp-explain.py |
| @@ -16,7 +16,7 @@ from collections import deque |
| def usage(): |
| print """\ |
| Usage: |
| - tools/gyp-explain.py chrome_dll# gtest# |
| + tools/gyp-explain.py [--dot] chrome_dll# gtest# |
| """ |
| @@ -51,6 +51,38 @@ def MatchNode(graph, substring): |
| return candidates[0] |
| +def CommonPrefix(seq): |
| + """Given a list of strings, return the longest common prefix.""" |
| + prefix = seq[0] |
| + for item in seq: |
| + for i in range(len(prefix)): |
| + if prefix[:i+1] != item[:i+1]: |
| + prefix = prefix[:i] |
| + if i == 0: |
| + return '' |
| + break |
| + return prefix |
|
Nico
2013/05/07 21:17:54
os.path.commonprefix?
|
| + |
| + |
| +def EscapeForDot(string): |
| + suffix = '#target' |
| + if string.endswith(suffix): |
| + string = string[:-len(suffix)] |
| + string = string.replace('\\', '\\\\') |
| + return '"' + string + '"' |
| + |
| + |
| +def GenerateDot(fro, to, paths): |
| + prefixes = [CommonPrefix(path) for path in paths] |
| + prefix = CommonPrefix(prefixes) |
| + # "strict" collapses common paths. |
| + print '// Build with "dot -Tpng -ooutput.png this_file.dot"' |
| + print 'strict digraph {' |
| + for path in paths: |
| + print (' -> '.join(EscapeForDot(item[len(prefix):]) for item in path)), ';' |
| + print '}' |
| + |
| + |
| def Main(argv): |
| # Check that dump.json exists and that it's not too old. |
| dump_json_dirty = False |
| @@ -72,18 +104,25 @@ def Main(argv): |
| g = json.load(open('dump.json')) |
| - if len(argv) != 3: |
| + if len(argv) not in (3, 4): |
| usage() |
| sys.exit(1) |
| + generate_dot = argv[1] == '--dot' |
| + if generate_dot: |
| + argv.pop(1) |
| + |
| fro = MatchNode(g, argv[1]) |
| to = MatchNode(g, argv[2]) |
| paths = list(GetPath(g, fro, to)) |
| if len(paths) > 0: |
| - print 'These paths lead from %s to %s:' % (fro, to) |
| - for path in paths: |
| - print ' -> '.join(path) |
| + if generate_dot: |
| + GenerateDot(fro, to, paths) |
| + else: |
| + print 'These paths lead from %s to %s:' % (fro, to) |
| + for path in paths: |
| + print ' -> '.join(path) |
| else: |
| print 'No paths found from %s to %s.' % (fro, to) |