19 lines
978 B
Python
19 lines
978 B
Python
#!/usr/bin/env python3
|
|
import sys,yaml,subprocess,shutil
|
|
inp,dotf,svgf=sys.argv[1],sys.argv[2],sys.argv[3]
|
|
with open(inp) as f:c=yaml.safe_load(f) or {}
|
|
svcs=c.get('services') or {}
|
|
lines=["digraph Compose {"," rankdir=LR;"," node [fontname=Helvetica];"]
|
|
for s in svcs: lines.append(f' "svc:{s}" [label="{s}", shape=box, style=filled, fillcolor="#dfefff"];')
|
|
for n in (c.get('networks') or {}).keys(): lines.append(f' "net:{n}" [label="{n}", shape=ellipse, style=filled, fillcolor="#f4f4f4"];')
|
|
for s,sv in svcs.items():
|
|
ns=sv.get('networks') or []
|
|
if isinstance(ns,dict): ns=ns.keys()
|
|
for n in ns: lines.append(f' "svc:{s}" -> "net:{n}";')
|
|
lines.append("}")
|
|
open(dotf,'w').write('\n'.join(lines)+'\n')
|
|
if shutil.which('dot'):
|
|
subprocess.run(['dot','-Tsvg',dotf,'-o',svgf],check=True)
|
|
else:
|
|
open(svgf,'w').write('<svg xmlns="http://www.w3.org/2000/svg" width="640" height="80"><text x="10" y="40">Graphviz dot not found in environment.</text></svg>\n')
|