Commit 77fa3f64 authored by Valentin Samir's avatar Valentin Samir

[sip/asterisk-graph] Supression du fichier temporaire

On écrit directement dans PIPE avec subprocess
parent c8cee16a
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function # -*- coding: utf-8 -*-
from sh import neato """
from sh import date Génère un graphe de dépendance des extensions
from sh import dot du plan de numérotation d'asterisk
tmp_file = '/tmp/graph.gv' Auteur: Valentin Samir
exten_file = '/etc/asterisk/extensions.conf'
start_nodes = ["crans-sip", "crans-from-external", "ovh-sip", "crans-sms" ]
out = file(tmp_file,'w') """
arrow={}
iarrow={}
print("""digraph G { from subprocess import Popen, PIPE, STDOUT
rankdir=LR import time
edge [len=2.50, ratio=fill]
""", file=out)
for node in start_nodes:
print('"%s" [style=filled];' % node, file=out)
file=open(exten_file).read() def gen_extentions_graph(exten_file, start_nodes, svg_file):
file=file.split('\n[')
del(file[0])
for context in file:
context=context.split('\n')
context_name = context[0].strip()[:-1]
arrow[context_name]= arrow.get(context_name, set())
iarrow[context_name]= iarrow.get(context_name, set())
del(context[0])
print('"%s";' % context_name, file=out)
for line in context:
if line.startswith('include'):
dest=line.split('=>', 1)[1].strip()
iarrow[context_name].add(dest)
line=line.split(',')
if len(line)>2 and line[2].strip().startswith('Goto('):
dest=line[2].strip()[5:]
arrow[context_name].add(dest)
out=""
arrow={}
iarrow={}
for (context_name, dests) in arrow.items(): out+="""digraph G {
for dest in dests: rankdir=LR
print('"%s" -> "%s" [arrowhead=normal];' % (context_name, dest), file=out) edge [len=2.50, ratio=fill]
for (context_name, dests) in iarrow.items(): """
for dest in dests: for node in start_nodes:
print('"%s" -> "%s" [arrowhead=crow, style=dashed];' % (context_name, dest), file=out) out+='"%s" [style=filled];' % node
print(""" file=open(exten_file).read()
node [shape=plaintext] file=file.split('\n[')
subgraph cluster_01 { del(file[0])
key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> for context in file:
<tr><td align="right" port="i1">B inclus dans A : A</td></tr> context=context.split('\n')
<tr><td align="right" port="i2">Saut de A vers B : A</td></tr> context_name = context[0].strip()[:-1]
</table>>]; arrow[context_name]= arrow.get(context_name, set())
key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> iarrow[context_name]= iarrow.get(context_name, set())
<tr><td port="j1">B</td></tr> del(context[0])
<tr><td port="j2">B</td></tr> out+='"%s";' % context_name
</table>>]; for line in context:
key:i1:e -> key2:j1:e [style=dashed, arrowhead=crow] if line.startswith('include'):
key:i2:e -> key2:j2:w [] dest=line.split('=>', 1)[1].strip()
} iarrow[context_name].add(dest)
""", file=out) line=line.split(',')
if len(line)>2 and line[2].strip().startswith('Goto('):
dest=line[2].strip()[5:]
arrow[context_name].add(dest)
print("""
label = "\\n\\nAsterisk extensions\\nLes Nounous\\n%s";
}""" % str(date())[0:-1], file=out)
out.close() for (context_name, dests) in arrow.items():
for dest in dests:
out+='"%s" -> "%s" [arrowhead=normal];' % (context_name, dest)
for (context_name, dests) in iarrow.items():
for dest in dests:
out+='"%s" -> "%s" [arrowhead=crow, style=dashed];' % (context_name, dest)
dot("-Tsvg", tmp_file, "-o", "/usr/scripts/var/doc/asterisk/extensions.svg") out+="""
#neato("-Tsvg", tmp_file, "-o", "/usr/scripts/var/doc/asterisk/extensions.svg") node [shape=plaintext]
subgraph cluster_01 {
key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0">
<tr><td align="right" port="i1">B inclus dans A : A</td></tr>
<tr><td align="right" port="i2">Saut de A vers B : A</td></tr>
</table>>];
key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0">
<tr><td port="j1">B</td></tr>
<tr><td port="j2">B</td></tr>
</table>>];
key:i1:e -> key2:j1:e [style=dashed, arrowhead=crow]
key:i2:e -> key2:j2:w []
}
"""
out+="""
label = "\\n\\nAsterisk extensions\\nLes Nounous\\n%s";
}""" % time.strftime('%A %d %B %Y, %H:%M:%S')
p = Popen(['dot', '-Tsvg', '-o', svg_file], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.communicate(input=out)
if __name__ == '__main__' :
import sys
if len(sys.argv)>1:
import locale
locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8')
gen_extentions_graph('/etc/asterisk/extensions.conf', ["crans-sip", "crans-from-external", "ovh-sip", "crans-sms"], sys.argv[1])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment