-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.py
93 lines (76 loc) · 1.94 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
CMake dependency graph.
Requires graphviz installed.
Requires user to first invoke "cmake -B build --graphviz=gfx/block.dot"
"""
import shutil
import subprocess
import argparse
from pathlib import Path
import webbrowser
p = argparse.ArgumentParser(description="convert .dot graph to SVG or PNG")
p.add_argument(
"path",
help="Project gfx/ directory from cmake -B build --graphviz=gfx/block.dot",
)
p.add_argument(
"format", help="output format", choices=["svg", "png"],
default="svg", nargs="?"
)
P = p.parse_args()
fmt = P.format
if not (dot := shutil.which("dot")):
raise FileNotFoundError("GraphViz Dot program not available.")
path = Path(P.path).expanduser().resolve(strict=True)
if not path.is_dir():
raise NotADirectoryError(path)
try:
name_file = next(path.glob("*.dot"))
except StopIteration:
raise FileNotFoundError(f"No .dot files in {path}")
name_pat = name_file.name
# %% write HTML file to display all graphs
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>{path} CMake Graphs</title>
<style>
img {{
display: block;
margin-left: auto;
margin-right: auto;
width: 100%;
}}
</style>
</head>
<body>
<figure>
<img src="{name_file.name}.{fmt}" alt="graph legend and top-level diagram">
<figcaption>Graph legend and top-level project diagram</figcaption>
</figure>
"""
for file in path.glob(name_pat + "*"):
if file.suffix in (".png", ".svg"):
continue
out_name = file.name
if out_name != name_pat:
# remove vestigial name from front
out_name = out_name[len(name_pat) + 1:]
out_file = f"{out_name}.{fmt}"
cmd = ["dot", f"-T{fmt}", f"-o{out_file}", str(file.name)]
print(" ".join(cmd))
subprocess.run(cmd, cwd=path)
html += f"""
<figure>
<img src="{out_file}" alt="{out_name}">
<figcaption>{out_name}</figcaption>
</figure>
"""
html += """
</body>
</html>
"""
html_path = path / "index.html"
html_path.write_text(html)
webbrowser.open(html_path.as_uri())