| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2011 Google Inc. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Using the JSON dumped by the dump-dependency-json generator, | 7 """Using the JSON dumped by the dump-dependency-json generator, |
| 8 generate input suitable for graphviz to render a dependency graph of | 8 generate input suitable for graphviz to render a dependency graph of |
| 9 targets.""" | 9 targets.""" |
| 10 | 10 |
| 11 from __future__ import print_function |
| 12 |
| 11 import collections | 13 import collections |
| 12 import json | 14 import json |
| 13 import sys | 15 import sys |
| 14 | 16 |
| 15 | 17 |
| 16 def ParseTarget(target): | 18 def ParseTarget(target): |
| 17 target, _, suffix = target.partition('#') | 19 target, _, suffix = target.partition('#') |
| 18 filename, _, target = target.partition(':') | 20 filename, _, target = target.partition(':') |
| 19 return filename, target, suffix | 21 return filename, target, suffix |
| 20 | 22 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 43 def WriteGraph(edges): | 45 def WriteGraph(edges): |
| 44 """Print a graphviz graph to stdout. | 46 """Print a graphviz graph to stdout. |
| 45 |edges| is a map of target to a list of other targets it depends on.""" | 47 |edges| is a map of target to a list of other targets it depends on.""" |
| 46 | 48 |
| 47 # Bucket targets by file. | 49 # Bucket targets by file. |
| 48 files = collections.defaultdict(list) | 50 files = collections.defaultdict(list) |
| 49 for src, dst in edges.items(): | 51 for src, dst in edges.items(): |
| 50 build_file, target_name, toolset = ParseTarget(src) | 52 build_file, target_name, toolset = ParseTarget(src) |
| 51 files[build_file].append(src) | 53 files[build_file].append(src) |
| 52 | 54 |
| 53 print 'digraph D {' | 55 print('digraph D {') |
| 54 print ' fontsize=8' # Used by subgraphs. | 56 print(' fontsize=8') # Used by subgraphs. |
| 55 print ' node [fontsize=8]' | 57 print(' node [fontsize=8]') |
| 56 | 58 |
| 57 # Output nodes by file. We must first write out each node within | 59 # Output nodes by file. We must first write out each node within |
| 58 # its file grouping before writing out any edges that may refer | 60 # its file grouping before writing out any edges that may refer |
| 59 # to those nodes. | 61 # to those nodes. |
| 60 for filename, targets in files.items(): | 62 for filename, targets in files.items(): |
| 61 if len(targets) == 1: | 63 if len(targets) == 1: |
| 62 # If there's only one node for this file, simplify | 64 # If there's only one node for this file, simplify |
| 63 # the display by making it a box without an internal node. | 65 # the display by making it a box without an internal node. |
| 64 target = targets[0] | 66 target = targets[0] |
| 65 build_file, target_name, toolset = ParseTarget(target) | 67 build_file, target_name, toolset = ParseTarget(target) |
| 66 print ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, | 68 print(' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, |
| 67 target_name) | 69 target_name)) |
| 68 else: | 70 else: |
| 69 # Group multiple nodes together in a subgraph. | 71 # Group multiple nodes together in a subgraph. |
| 70 print ' subgraph "cluster_%s" {' % filename | 72 print(' subgraph "cluster_%s" {' % filename) |
| 71 print ' label = "%s"' % filename | 73 print(' label = "%s"' % filename) |
| 72 for target in targets: | 74 for target in targets: |
| 73 build_file, target_name, toolset = ParseTarget(target) | 75 build_file, target_name, toolset = ParseTarget(target) |
| 74 print ' "%s" [label="%s"]' % (target, target_name) | 76 print(' "%s" [label="%s"]' % (target, target_name)) |
| 75 print ' }' | 77 print(' }') |
| 76 | 78 |
| 77 # Now that we've placed all the nodes within subgraphs, output all | 79 # Now that we've placed all the nodes within subgraphs, output all |
| 78 # the edges between nodes. | 80 # the edges between nodes. |
| 79 for src, dsts in edges.items(): | 81 for src, dsts in edges.items(): |
| 80 for dst in dsts: | 82 for dst in dsts: |
| 81 print ' "%s" -> "%s"' % (src, dst) | 83 print(' "%s" -> "%s"' % (src, dst)) |
| 82 | 84 |
| 83 print '}' | 85 print('}') |
| 84 | 86 |
| 85 | 87 |
| 86 def main(): | 88 def main(): |
| 87 if len(sys.argv) < 2: | 89 if len(sys.argv) < 2: |
| 88 print >>sys.stderr, __doc__ | 90 print(__doc__, file=sys.stderr) |
| 89 print >>sys.stderr | 91 print(file=sys.stderr) |
| 90 print >>sys.stderr, 'usage: %s target1 target2...' % (sys.argv[0]) | 92 print('usage: %s target1 target2...' % (sys.argv[0]), file=sys.stderr) |
| 91 return 1 | 93 return 1 |
| 92 | 94 |
| 93 edges = LoadEdges('dump.json', sys.argv[1:]) | 95 edges = LoadEdges('dump.json', sys.argv[1:]) |
| 94 | 96 |
| 95 WriteGraph(edges) | 97 WriteGraph(edges) |
| 96 return 0 | 98 return 0 |
| 97 | 99 |
| 98 | 100 |
| 99 if __name__ == '__main__': | 101 if __name__ == '__main__': |
| 100 sys.exit(main()) | 102 sys.exit(main()) |
| OLD | NEW |