From 5253e7340ce2c2c566f172da4564e0056a34c8dd Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Thu, 20 Jun 2013 16:04:41 +0200 Subject: [PATCH] [bcfg2-graph] comments+avoid sh module --- bcfg2/bcfg2-graph.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/bcfg2/bcfg2-graph.py b/bcfg2/bcfg2-graph.py index ce8d6fd8..febba476 100755 --- a/bcfg2/bcfg2-graph.py +++ b/bcfg2/bcfg2-graph.py @@ -1,26 +1,41 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +""" +Génère un graphe de dépendance des groups bcfg2 + +Auteurs: Daniel Stan + Valentin Samir + +""" from __future__ import print_function import xml.dom.minidom +import subprocess -tmp_file = '/tmp/graph.gv' +GRAPH_BIN = '/usr/bin/neato' +TMP_FILE = '/tmp/graph.gv' +GROUPS_FILE = '/var/lib/bcfg2/Metadata/groups.xml' +OUTPUT_FILE = '/usr/scripts/var/doc/bcfg2/groups.svg' -out = file(tmp_file,'w') +out = file(TMP_FILE, 'w') -from sh import neato -from sh import date +import datetime -groups = xml.dom.minidom.parse(file('/var/lib/bcfg2/Metadata/groups.xml','r')).documentElement +# groups est le nœud racine +groups = xml.dom.minidom.parse(file(GROUPS_FILE,'r')).documentElement print("""digraph G { edge [len=4.50, ratio=fill] """, file=out) def childGroups(parent): + """Récupère les groups enfants (dépendances) d'un nœud + Attention, cette fonction travaille sur (et renvoie) des nœuds xml + """ return [ x for x in parent.childNodes if \ x.nodeType == x.ELEMENT_NODE and x.tagName == u'Group'] -# Les clients +# Les clients (ie des serveurs) +# sont coloriés d'une autre couleur print(""" subgraph cluster_1 { node [style=filled]; @@ -44,8 +59,8 @@ for elem in childGroups(groups): print(""" label = "\\n\\nBCFG2 Groups\\nLes Nounous\\n%s"; -}""" % str(date())[0:-1], file=out) +}""" % datetime.datetime.now().strftime('%c'), file=out) out.close() -neato("-Tsvg", tmp_file, "-o", "/usr/scripts/var/doc/bcfg2/groups.svg") +subprocess.Popen([GRAPH_BIN, "-Tsvg", TMP_FILE, "-o", OUTPUT_FILE]).communicate()