source: trip-planner-front/node_modules/node-gyp/gyp/tools/graphviz.py@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#!/usr/bin/env python
2
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.
6
7"""Using the JSON dumped by the dump-dependency-json generator,
8generate input suitable for graphviz to render a dependency graph of
9targets."""
10
11from __future__ import print_function
12
13import collections
14import json
15import sys
16
17
18def ParseTarget(target):
19 target, _, suffix = target.partition("#")
20 filename, _, target = target.partition(":")
21 return filename, target, suffix
22
23
24def LoadEdges(filename, targets):
25 """Load the edges map from the dump file, and filter it to only
26 show targets in |targets| and their depedendents."""
27
28 file = open("dump.json")
29 edges = json.load(file)
30 file.close()
31
32 # Copy out only the edges we're interested in from the full edge list.
33 target_edges = {}
34 to_visit = targets[:]
35 while to_visit:
36 src = to_visit.pop()
37 if src in target_edges:
38 continue
39 target_edges[src] = edges[src]
40 to_visit.extend(edges[src])
41
42 return target_edges
43
44
45def WriteGraph(edges):
46 """Print a graphviz graph to stdout.
47 |edges| is a map of target to a list of other targets it depends on."""
48
49 # Bucket targets by file.
50 files = collections.defaultdict(list)
51 for src, dst in edges.items():
52 build_file, target_name, toolset = ParseTarget(src)
53 files[build_file].append(src)
54
55 print("digraph D {")
56 print(" fontsize=8") # Used by subgraphs.
57 print(" node [fontsize=8]")
58
59 # Output nodes by file. We must first write out each node within
60 # its file grouping before writing out any edges that may refer
61 # to those nodes.
62 for filename, targets in files.items():
63 if len(targets) == 1:
64 # If there's only one node for this file, simplify
65 # the display by making it a box without an internal node.
66 target = targets[0]
67 build_file, target_name, toolset = ParseTarget(target)
68 print(
69 ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, target_name)
70 )
71 else:
72 # Group multiple nodes together in a subgraph.
73 print(' subgraph "cluster_%s" {' % filename)
74 print(' label = "%s"' % filename)
75 for target in targets:
76 build_file, target_name, toolset = ParseTarget(target)
77 print(' "%s" [label="%s"]' % (target, target_name))
78 print(" }")
79
80 # Now that we've placed all the nodes within subgraphs, output all
81 # the edges between nodes.
82 for src, dsts in edges.items():
83 for dst in dsts:
84 print(' "%s" -> "%s"' % (src, dst))
85
86 print("}")
87
88
89def main():
90 if len(sys.argv) < 2:
91 print(__doc__, file=sys.stderr)
92 print(file=sys.stderr)
93 print("usage: %s target1 target2..." % (sys.argv[0]), file=sys.stderr)
94 return 1
95
96 edges = LoadEdges("dump.json", sys.argv[1:])
97
98 WriteGraph(edges)
99 return 0
100
101
102if __name__ == "__main__":
103 sys.exit(main())
Note: See TracBrowser for help on using the repository browser.