[email protected] | 23696a4 | 2011-11-23 22:28:37 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 2f1fe298 | 2012-01-12 18:16:29 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 23696a4 | 2011-11-23 22:28:37 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Prints paths between gyp targets. |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | import os |
| 11 | import sys |
| 12 | import time |
| 13 | |
| 14 | from collections import deque |
| 15 | |
| 16 | def usage(): |
| 17 | print """\ |
| 18 | Usage: |
| 19 | tools/gyp-explain.py chrome_dll gtest# |
| 20 | """ |
| 21 | |
| 22 | |
| 23 | def GetPath(graph, fro, to): |
| 24 | """Given a graph in (node -> list of successor nodes) dictionary format, |
| 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. |
| 27 | q = deque([(fro, [])]) |
| 28 | while q: |
| 29 | t, path = q.popleft() |
| 30 | if t == to: |
| 31 | yield path + [t] |
| 32 | for d in graph[t]: |
| 33 | q.append((d, path + [t])) |
| 34 | |
| 35 | |
| 36 | def MatchNode(graph, substring): |
| 37 | """Given a dictionary, returns the key that matches |substring| best. Exits |
| 38 | if there's not one single best match.""" |
| 39 | candidates = [] |
| 40 | for target in graph: |
| 41 | if substring in target: |
| 42 | candidates.append(target) |
| 43 | |
| 44 | if not candidates: |
| 45 | print 'No targets match "%s"' % substring |
| 46 | sys.exit(1) |
| 47 | if len(candidates) > 1: |
| 48 | print 'More than one target matches "%s": %s' % ( |
| 49 | substring, ' '.join(candidates)) |
| 50 | sys.exit(1) |
| 51 | return candidates[0] |
| 52 | |
| 53 | |
| 54 | def Main(argv): |
[email protected] | 2f1fe298 | 2012-01-12 18:16:29 | [diff] [blame] | 55 | if sys.platform in ['win32', 'cygwin']: |
| 56 | print 'The dump_dependency_json gyp generator required for gyp-explain.py' |
| 57 | print 'does not support Windows.' |
| 58 | sys.exit(1) |
| 59 | |
[email protected] | 23696a4 | 2011-11-23 22:28:37 | [diff] [blame] | 60 | # Check that dump.json exists and that it's not too old. |
| 61 | dump_json_dirty = False |
| 62 | try: |
| 63 | st = os.stat('dump.json') |
| 64 | file_age_s = time.time() - st.st_mtime |
| 65 | if file_age_s > 2 * 60 * 60: |
| 66 | print 'dump.json is more than 2 hours old.' |
| 67 | dump_json_dirty = True |
[email protected] | 68a2062 | 2011-12-02 19:19:09 | [diff] [blame] | 68 | except OSError: |
[email protected] | 23696a4 | 2011-11-23 22:28:37 | [diff] [blame] | 69 | print 'dump.json not found.' |
| 70 | dump_json_dirty = True |
| 71 | |
| 72 | if dump_json_dirty: |
| 73 | print 'Run' |
| 74 | print ' GYP_GENERATORS=dump_dependency_json build/gyp_chromium' |
| 75 | print 'first, then try again.' |
| 76 | sys.exit(1) |
| 77 | |
| 78 | g = json.load(open('dump.json')) |
| 79 | |
| 80 | if len(argv) != 3: |
| 81 | usage() |
| 82 | sys.exit(1) |
| 83 | |
| 84 | fro = MatchNode(g, argv[1]) |
| 85 | to = MatchNode(g, argv[2]) |
| 86 | |
| 87 | paths = list(GetPath(g, fro, to)) |
| 88 | if len(paths) > 0: |
| 89 | print 'These paths lead from %s to %s:' % (fro, to) |
| 90 | for path in paths: |
| 91 | print ' -> '.join(path) |
| 92 | else: |
| 93 | print 'No paths found from %s to %s.' % (fro, to) |
| 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | Main(sys.argv) |