Skip to content

Commit e99e6bb

Browse files
authored
Merge pull request #1979 from harrysarson/harry/graph
contrib/bst-graph: Escape names when using as node_id
2 parents 936124f + 655e658 commit e99e6bb

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

contrib/bst-graph

+24-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ installed.
2929
import argparse
3030
import subprocess
3131
import re
32+
import urllib.parse
3233

3334
from graphviz import Digraph
3435
from ruamel.yaml import YAML
@@ -55,6 +56,22 @@ def parse_args():
5556
return parser.parse_args()
5657

5758

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+
5875
def parse_graph(lines):
5976
'''Return nodes and edges of the parsed grpah.
6077
@@ -99,12 +116,12 @@ def generate_graph(nodes, build_deps, runtime_deps):
99116
A graphviz.Digraph object
100117
'''
101118
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)
104121
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')
106123
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')
108125
return graph
109126

110127

@@ -116,9 +133,11 @@ def main():
116133
graph_lines = subprocess.check_output(cmd, universal_newlines=True)
117134
# NOTE: We generate nodes and edges before giving them to graphviz as
118135
# 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))
120137
graph = generate_graph(nodes, build_deps, runtime_deps)
138+
121139
print(graph.source)
140+
122141
if args.format:
123142
graph.render(cleanup=True,
124143
filename='bst-graph',

0 commit comments

Comments
 (0)