Salome HOME
Improve hat log
[tools/sat.git] / src / xmlManager.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 import os
20 import re
21
22 import src
23 from . import ElementTree as etree
24
25 class xmlLogFile(object):
26     '''Class to manage writing in salomeTools xml log file
27     '''
28     def __init__(self, filePath, rootname, attrib = {}):
29         self.logFile = filePath
30         src.ensure_path_exists(os.path.dirname(filePath))
31         self.xmlroot = etree.Element(rootname, attrib = attrib)
32     
33     def write_tree(self, stylesheet=None):
34         f = open(self.logFile, 'w')
35         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
36         if stylesheet:
37             f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)    
38         f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
39         f.close()  
40         
41     def add_simple_node(self, node_name, text=None, attrib={}):
42         n = etree.Element(node_name, attrib=attrib)
43         n.text = text
44         self.xmlroot.append(n)
45         return n
46     
47     def append_node(self, node_name, text):
48         for field in self.xmlroot:
49             if field.tag == node_name:
50                 field.text += text
51                 
52 def update_hat_xml(logDir, application=None):
53     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
54     if application:
55         xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : application})
56     else:
57         xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : "NO"})
58           
59     for fileName in os.listdir(logDir):
60         sExpr = "^[0-9]{8}_+[0-9]{6}_+[A-Za-z0-9]*.xml$"
61         oExpr = re.compile(sExpr)
62         if oExpr.search(fileName):
63             date_hour_cmd = fileName.split('_')
64             date_not_formated = date_hour_cmd[0]
65             date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
66             hour_not_formated = date_hour_cmd[1]
67             hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
68             cmd = date_hour_cmd[2][:-len('.xml')]
69         
70             xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
71     
72     xmlHat.write_tree('hat.xsl')