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.
7 """Using the JSON dumped by the dump-dependency-json generator,
8 generate input suitable for graphviz to render a dependency graph of
16 def ParseTarget(target):
17 target, _, suffix = target.partition('#')
18 filename, _, target = target.partition(':')
19 return filename, target, suffix
22 def LoadEdges(filename, targets):
23 """Load the edges map from the dump file, and filter it to only
24 show targets in |targets| and their depedendents."""
26 file = open('dump.json')
27 edges = json.load(file)
30 # Copy out only the edges we're interested in from the full edge list.
35 if src in target_edges:
37 target_edges[src] = edges[src]
38 to_visit.extend(edges[src])
43 def WriteGraph(edges):
44 """Print a graphviz graph to stdout.
45 |edges| is a map of target to a list of other targets it depends on."""
47 # Bucket targets by file.
48 files = collections.defaultdict(list)
49 for src, dst in edges.items():
50 build_file, target_name, toolset = ParseTarget(src)
51 files[build_file].append(src)
54 print ' fontsize=8' # Used by subgraphs.
55 print ' node [fontsize=8]'
57 # 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 for filename, targets in files.items():
62 # If there's only one node for this file, simplify
63 # the display by making it a box without an internal node.
65 build_file, target_name, toolset = ParseTarget(target)
66 print ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename,
69 # Group multiple nodes together in a subgraph.
70 print ' subgraph "cluster_%s" {' % filename
71 print ' label = "%s"' % filename
72 for target in targets:
73 build_file, target_name, toolset = ParseTarget(target)
74 print ' "%s" [label="%s"]' % (target, target_name)
77 # Now that we've placed all the nodes within subgraphs, output all
78 # the edges between nodes.
79 for src, dsts in edges.items():
81 print ' "%s" -> "%s"' % (src, dst)
88 print >>sys.stderr, __doc__
90 print >>sys.stderr, 'usage: %s target1 target2...' % (sys.argv[0])
93 edges = LoadEdges('dump.json', sys.argv[1:])
99 if __name__ == '__main__':