Salome HOME
Add log command and update logging mecanism
authorSerge Rehbinder <serge.rehbinder@cea.fr>
Tue, 9 Feb 2016 10:48:06 +0000 (11:48 +0100)
committerSerge Rehbinder <serge.rehbinder@cea.fr>
Tue, 9 Feb 2016 10:48:06 +0000 (11:48 +0100)
commands/config.py
commands/log.py [new file with mode: 0644]
src/logger.py
src/xmlManager.py
src/xsl/LOGO-SAT.png [new file with mode: 0644]
src/xsl/hat.xsl [new file with mode: 0644]

index e8701dd30cff7a8891fb872967a3af5af3f6d259..f294e10ac316fc39e6cbac67cafe82ab039df28b 100644 (file)
@@ -272,6 +272,15 @@ class ConfigManager:
         for rule in self.get_command_line_overrides(options, ["USER"]):
             exec('cfg.' + rule) # this cannot be factorize because of the exec
 
+        # =======================================================================================
+        # Add log directory in the config
+        # get the log directory regarding the fact the command uses an application or not
+        if 'APPLICATION' in cfg:
+            logDir = os.path.join(cfg.APPLICATION.out_dir, 'LOGS')
+        else:
+            logDir = os.path.join(cfg.VARS.personalDir, 'LOGS')
+        cfg.VARS.logDir = logDir
+
         return cfg
 
     def setUserConfigFile(self, config):
@@ -459,7 +468,7 @@ def run(args, runner):
             
             # perform the copy
             shutil.copyfile(source_full_path, dest_file)
-            runner.logger.write(_("%s has been created.") % dest_file)
+            runner.logger.write(_("%s has been created.\n") % dest_file)
     
     # case : display all the available pyconf applications
     elif options.list:
diff --git a/commands/log.py b/commands/log.py
new file mode 100644 (file)
index 0000000..fe1031e
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+#-*- coding:utf-8 -*-
+
+import os
+import shutil
+
+import src
+
+# Define all possible option for log command :  sat log <options>
+parser = src.options.Options()
+parser.add_option('t', 'terminal', 'boolean', 'terminal', "Terminal log.")
+
+
+
+def description():
+    return _("Gives access to logs of salomeTools.")    
+
+def run(args, runner):
+    (options, args) = parser.parse_args(args)
+    if options.terminal:
+        print('terminal')
+    
+    # Find stylesheet Directory and files
+    xslDir = os.path.join(runner.cfg.VARS.srcDir, 'xsl')
+    xslCommand = os.path.join(xslDir, "command.xsl")
+    xslHat = os.path.join(xslDir, "hat.xsl")
+    imgLogo = os.path.join(xslDir, "LOGO-SAT.png")
+
+    # get the log direcory. If there is an application, it is in cfg.APPLICATION.out_dir, else in user directory
+    logDir = runner.cfg.VARS.logDir
+    
+    # copy the stylesheet in the log directory
+    shutil.copy2(xslCommand, logDir)
+    shutil.copy2(xslHat, logDir)
+    shutil.copy2(imgLogo, logDir)
+    
+    xmlHatFilePath = os.path.join(logDir, 'hat.xml')   
+    src.xmlManager.update_hat_xml(logDir)
+    
+    src.system.show_in_editor(runner.cfg.USER.browser, xmlHatFilePath)
\ No newline at end of file
index 9b0135d3ca72a18d2d27d15bc16bcc6ba8623646..a624600ccbc1737e0f332938d87e1e4b1c788412 100644 (file)
@@ -48,7 +48,7 @@ class Logger(object):
         self.logFileName = logFileName
         self.logFilePath = logFilePath   
         
-        self.xmlFile = xmlManager.xmlLogFile(logFilePath, config.VARS.command)
+        self.xmlFile = xmlManager.xmlLogFile(logFilePath, "SATcommand", attrib = {"command" : config.VARS.command})
         self.putInitialXMLFields()
         
     def putInitialXMLFields(self):
@@ -118,6 +118,7 @@ class Logger(object):
         self.xmlFile.add_simple_node("field", text=endtime , attrib={"name" : "endTime"})
         self.xmlFile.add_simple_node("field", text="%ih%im%is" % (hours, minutes, seconds) , attrib={"name" : "Total Time"})
         self.xmlFile.write_tree(stylesheet = "command.xsl")
+        src.xmlManager.update_hat_xml(self.config.VARS.logDir)
 
 def date_to_datetime(date):
     Y = int(date[:4])
index 26e980c5194c35d25ac8ce7893b7879f32b4e022..87cdeb43e287386201feceb008173af7ab910e0e 100644 (file)
@@ -17,6 +17,7 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
 import os
+import re
 
 import src
 from . import ElementTree as etree
@@ -24,10 +25,10 @@ from . import ElementTree as etree
 class xmlLogFile(object):
     '''Class to manage writing in salomeTools xml log file
     '''
-    def __init__(self, filePath, command):
+    def __init__(self, filePath, rootname, attrib = {}):
         self.logFile = filePath
-        src.ensure_path_exists(os.path.dirname(filePath))           
-        self.xmlroot = etree.Element("SATcommand", attrib = {"command" : command})
+        src.ensure_path_exists(os.path.dirname(filePath))
+        self.xmlroot = etree.Element(rootname, attrib = attrib)
     
     def write_tree(self, stylesheet=None):
         f = open(self.logFile, 'w')
@@ -37,7 +38,6 @@ class xmlLogFile(object):
         f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
         f.close()  
         
-    
     def add_simple_node(self, node_name, text=None, attrib={}):
         n = etree.Element(node_name, attrib=attrib)
         n.text = text
@@ -47,4 +47,23 @@ class xmlLogFile(object):
     def append_node(self, node_name, text):
         for field in self.xmlroot:
             if field.tag == node_name:
-                field.text += text
\ No newline at end of file
+                field.text += text
+                
+def update_hat_xml(logDir):
+    xmlHatFilePath = os.path.join(logDir, 'hat.xml')
+    xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist")
+          
+    for fileName in os.listdir(logDir):
+        sExpr = "^[0-9]{8}_+[0-9]{6}_+[A-Za-z0-9]*.xml$"
+        oExpr = re.compile(sExpr)
+        if oExpr.search(fileName):
+            date_hour_cmd = fileName.split('_')
+            date_not_formated = date_hour_cmd[0]
+            date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
+            hour_not_formated = date_hour_cmd[1]
+            hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
+            cmd = date_hour_cmd[2][:-len('.xml')]
+        
+            xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
+    
+    xmlHat.write_tree('hat.xsl')
\ No newline at end of file
diff --git a/src/xsl/LOGO-SAT.png b/src/xsl/LOGO-SAT.png
new file mode 100644 (file)
index 0000000..44f2ee6
Binary files /dev/null and b/src/xsl/LOGO-SAT.png differ
diff --git a/src/xsl/hat.xsl b/src/xsl/hat.xsl
new file mode 100644 (file)
index 0000000..c532309
--- /dev/null
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:output method="html" />
+<xsl:template match="/">
+       
+<head>  
+    <title>SAlomeTools log</title>
+    <style type="text/css">
+        table       { 
+                      margin:1px;
+                      padding:1px;
+                      border-collapse:collapse;
+                      empty-cells : show;
+                    }
+        td          { vertical-align : center;}
+        h1          { text-align : center; }
+        .legend     { font-weight : bold;
+                      text-align : center;
+                    } 
+        .def        { font-family: Arial, Verdana, "Times New Roman", Times, serif;}
+        hr.note     { color: #BFBFBF; }
+        .note       { text-align : right; font-style: italic; font-size: small; }
+        div.release { -moz-column-count: 2;
+                      overflow: auto;
+                      max-height: 250px;
+                    }
+    </style>
+</head>
+       <body class="def" bgcolor="aliceblue">
+               <h1><img src="LOGO-SAT.png"/></h1>
+                       <table border="0">
+                               <tr>
+                               <td width="100px">Command</td><td width="100px">date</td><td>time</td>
+                               </tr>
+                               <xsl:for-each select="LOGlist/LogCommand">
+                                       <xsl:sort select="." order="descending" />
+                                       <tr bgcolor="aliceblue" width="2">
+                                               <td>
+                                                       <a title="log" target="_blank">
+                                                               <xsl:attribute name="href"><xsl:value-of select="."/></xsl:attribute>
+                                                               <xsl:value-of select="@cmd"/>
+                                                       </a>
+                                               </td>
+                                               <td><xsl:value-of select="@date"/></td>
+                                               <td><xsl:value-of select="@hour"/></td>
+                                       </tr>
+                               </xsl:for-each>
+                       </table>
+       </body>
+</xsl:template>
+</xsl:stylesheet>