@@ -29,6 +29,7 @@ installed.
29
29
import argparse
30
30
import subprocess
31
31
import re
32
+ import urllib .parse
32
33
33
34
from graphviz import Digraph
34
35
from ruamel .yaml import YAML
@@ -55,6 +56,22 @@ def parse_args():
55
56
return parser .parse_args ()
56
57
57
58
59
+ def unique_node_name (s ):
60
+ '''Generate unique node name for `s`.
61
+
62
+ Graphviz node names cannot contain colons or backslashes so we use
63
+ url-encoding to generate the unique node name. (A cryptographic hash could
64
+ be used instead but that would make the graphviz file less readable.)
65
+
66
+ Args:
67
+ s: element name
68
+
69
+ Returns:
70
+ A string containing the unique node name
71
+ '''
72
+ return urllib .parse .quote_plus (s )
73
+
74
+
58
75
def parse_graph (lines ):
59
76
'''Return nodes and edges of the parsed grpah.
60
77
@@ -99,12 +116,12 @@ def generate_graph(nodes, build_deps, runtime_deps):
99
116
A graphviz.Digraph object
100
117
'''
101
118
graph = Digraph ()
102
- for node in nodes :
103
- graph .node (node )
119
+ for name in nodes :
120
+ graph .node (unique_node_name ( name ), label = name )
104
121
for source , target in build_deps :
105
- graph .edge (source , target , label = 'build-dep' )
122
+ graph .edge (unique_node_name ( source ), unique_node_name ( target ) , label = 'build-dep' )
106
123
for source , target in runtime_deps :
107
- graph .edge (source , target , label = 'runtime-dep' )
124
+ graph .edge (unique_node_name ( source ), unique_node_name ( target ) , label = 'runtime-dep' )
108
125
return graph
109
126
110
127
@@ -116,9 +133,11 @@ def main():
116
133
graph_lines = subprocess .check_output (cmd , universal_newlines = True )
117
134
# NOTE: We generate nodes and edges before giving them to graphviz as
118
135
# the library does not de-deuplicate them.
119
- nodes , build_deps , runtime_deps = parse_graph (re .split ("\|\|" , graph_lines ))
136
+ nodes , build_deps , runtime_deps = parse_graph (re .split (r "\|\|" , graph_lines ))
120
137
graph = generate_graph (nodes , build_deps , runtime_deps )
138
+
121
139
print (graph .source )
140
+
122
141
if args .format :
123
142
graph .render (cleanup = True ,
124
143
filename = 'bst-graph' ,
0 commit comments