Skip to content

Commit 6af9a5b

Browse files
committed
contrib/bst-graph: Escape names used as node_ids
Colons in node_ids generate invalid graphviz, therefore escape the name and use a `label` attribute to render correct name in the graph.
1 parent 936124f commit 6af9a5b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

contrib/bst-graph

Lines changed: 21 additions & 4 deletions
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

0 commit comments

Comments
 (0)