]> SALOME platform Git repositories - tools/sat.git/commitdiff
Salome HOME
LOG : Add full option, logs correspond to the application
authorSerge Rehbinder <serge.rehbinder@cea.fr>
Tue, 16 Feb 2016 10:42:13 +0000 (11:42 +0100)
committerSerge Rehbinder <serge.rehbinder@cea.fr>
Tue, 16 Feb 2016 10:42:13 +0000 (11:42 +0100)
commands/log.py
src/ElementTree.py
src/internal_config/salomeTools.pyconf
src/logger.py
src/xmlManager.py
src/xsl/hat.xsl

index a6ba33188643d81f54e6df768f15d03ba093f61f..e05f247f7f93fa022b8c9dfa5784ed42084ef83d 100644 (file)
@@ -10,6 +10,10 @@ 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.")
+parser.add_option('l', 'last', 'boolean', 'last', "Show the log of the last launched command.")
+parser.add_option('f', 'full', 'boolean', 'full', "Show the logs of ALL launched commands.")
+parser.add_option('c', 'clean', 'int', 'clean', "Erase the n most ancient log files.")
+
 
 def show_log_command_in_terminal(filePath, logger):
     '''Print the contain of filePath. It contains a command log in xml format.
@@ -123,10 +127,15 @@ def run(args, runner, logger):
     shutil.copy2(xslCommand, logDir)
     shutil.copy2(xslHat, logDir)
     shutil.copy2(imgLogo, logDir)
-    
+
+    # determine the commands to show in the hat log
+    notShownCommands = runner.cfg.INTERNAL.log.notShownCommands
+    if options.full:
+        notShownCommands = []
+
     # Create or update the hat xml that gives access to all the commands log files
     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
-    src.xmlManager.update_hat_xml(runner.cfg.SITE.log.logDir, runner.cfg.VARS.application)
+    src.xmlManager.update_hat_xml(runner.cfg.SITE.log.logDir, application = runner.cfg.VARS.application, notShownCommands = notShownCommands)
     
     # open the hat xml in the user editor
     src.system.show_in_editor(runner.cfg.USER.browser, xmlHatFilePath, logger)
\ No newline at end of file
index d11ad7fe2df0838170b29281b9ba457ae134fb6a..49a06430d47c6cba58c34434f0f2d154e23ce2ba 100644 (file)
@@ -370,7 +370,13 @@ class _ElementInterface:
     # @defreturn string or None
 
     def get(self, key, default=None):
-        return self.attrib.get(key, default)
+        res = self.attrib.get(key, default)
+        if not res:
+            res = self.attrib.get(key.encode(), default)
+        if isinstance(res, bytes):
+            return res.decode()
+        else:
+            return res
 
     ##
     # Sets an element attribute.
@@ -389,8 +395,14 @@ class _ElementInterface:
     # @defreturn list of strings
 
     def keys(self):
-        return self.attrib.keys()
-
+        res = []
+        for key in self.attrib.keys():
+            if isinstance(key, bytes):
+                res.append(key.decode())
+            else:
+                res.append(key)
+        return res
+                
     ##
     # Gets element attributes, as a sequence.  The attributes are
     # returned in an arbitrary order.
@@ -1057,7 +1069,12 @@ class TreeBuilder:
     def _flush(self):
         if self._data:
             if self._last is not None:
-                text = string.join(self._data, "")
+                text = ""
+                for item in self._data:
+                    try:
+                        text += item
+                    except:
+                        text += item.decode()
                 if self._tail:
                     assert self._last.tail is None, "internal error (tail)"
                     self._last.tail = text
@@ -1150,9 +1167,9 @@ class XMLTreeBuilder:
             parser.StartElementHandler = self._start_list
         except AttributeError:
             pass
-        encoding = None
-        if not parser.returns_unicode:
-            encoding = "utf-8"
+        #encoding = None
+        #if not parser.returns_unicode:
+        #    encoding = "utf-8"
         # target.xml(encoding, None)
         self._doctype = None
         self.entity = {}
index 9aa08bfe138c9af011aea7ebacd71cbb230e51bb..6cf2a2991869d2199d303f277db139293f60f9a1 100644 (file)
@@ -4,5 +4,9 @@
 INTERNAL :
 {   
     sat_version : "5.0.0dev"
+    log :
+    {
+       notShownCommands : ["log"]
+    }
 }
 
index 24b234ab86fc0a72721e48bd25ebd4bfb583a1c0..42e328d57ff62c65ec31819ad022f063eb4bbf21 100644 (file)
@@ -45,7 +45,7 @@ class Logger(object):
         
         self.logFileName = logFileName
         self.logFilePath = logFilePath   
-        self.xmlFile = xmlManager.xmlLogFile(logFilePath, "SATcommand", attrib = {"command" : config.VARS.command})
+        self.xmlFile = xmlManager.xmlLogFile(logFilePath, "SATcommand", attrib = {"application" : config.VARS.application})
         self.putInitialXMLFields()
         
     def putInitialXMLFields(self):
@@ -65,6 +65,9 @@ class Logger(object):
         Y, m, dd, H, M, S = date_to_datetime(self.config.VARS.datehour)
         date_hour = "%2s/%2s/%4s %2sh%2sm%2ss" % (dd, m, Y, H, M, S)
         self.xmlFile.append_node_attrib("Site", attrib={"beginTime" : date_hour})
+        # The application if any
+        if "APPLICATION" in self.config:
+            self.xmlFile.append_node_attrib("Site", attrib={"application" : self.config.VARS.application})
         # The initialization of the trace node
         self.xmlFile.add_simple_node("Log",text="")
 
@@ -141,8 +144,6 @@ class Logger(object):
         # Call the method to write the xml file on the hard drive
         self.xmlFile.write_tree(stylesheet = "command.xsl")
         
-        # Update the hat xml (that shows all logs) in order to make the new log visible on the main log page)
-        src.xmlManager.update_hat_xml(self.config.SITE.log.logDir)
 
 def date_to_datetime(date):
     '''Little method that gets year, mon, day, hour , minutes and seconds from a str in format YYYYMMDD_HHMMSS
index 9c84cc18f0fedf5f7749340ae19781536dbb9dba..7b383c7cf5c870b9670e313f0097edd572d7cd4c 100644 (file)
@@ -93,11 +93,21 @@ class readXmlFile(object):
         self.filePath = filePath
         etree_inst = etree.parse(filePath)
         self.xmlroot = etree_inst.parse(filePath)
+
+    def getRootAttrib(self):
+        '''Get the attibutes of the self.xmlroot
+        
+        :return: The attributes of the root node
+        :rtype: dict
+        '''
+        return self.xmlroot.attrib
     
     def get_attrib(self, node_name):
         '''Get the attibutes of the node node_name in self.xmlroot
         
         :param node_name str: the name of the node
+        :return: the attibutes of the node node_name in self.xmlroot
+        :rtype: dict
         '''
         return self.xmlroot.find(node_name).attrib
     
@@ -105,6 +115,8 @@ class readXmlFile(object):
         '''Get the text of the first node that has name that corresponds to the parameter node
         
         :param node str: the name of the node from which get the text
+        :return: the text of the first node that has name that corresponds to the parameter node
+        :rtype: str
         '''
         # Loop on all root nodes
         for field in self.xmlroot:
@@ -112,8 +124,40 @@ class readXmlFile(object):
                 return field.text
         return ''
 
+def showcommandLog(logFilePath, cmd, application, notShownCommands):
+    '''Used in updateHatXml. Determine if the log xml file logFilePath has to be shown or not in the hat log.
+    
+    :param logFilePath str: the path to the command xml log file
+    :param cmd str: the command of the log file
+    :param application str: the application passed as parameter to the salomeTools command
+    :param notShownCommands list: the list of commands that are not shown by default
+    
+    :return: True if cmd is not in notShownCommands and the application in the log file corresponds to application
+    :rtype: boolean
+    '''
+    # When the command is not in notShownCommands, no need to go further. Do not show
+    if cmd in notShownCommands:
+        return False, None
+    # Get the application of the log file
+    logFileXml = readXmlFile(logFilePath)
+    if 'application' in logFileXml.xmlroot.keys():
+        appliLog = logFileXml.xmlroot.get('application')
+        # if it corresponds, then the log has to be shown
+        if appliLog == application:
+            return True, appliLog
+        elif application != 'None':
+            return False, appliLog
+        
+        return True, appliLog
+    
+    if application == 'None':
+            return True, None    
+        
+    return False, None
+    
 
-def update_hat_xml(logDir, application=None):
+def update_hat_xml(logDir, application=None, notShownCommands = []):
     '''Create the xml file in logDir that contain all the xml file and have a name like YYYYMMDD_HHMMSS_namecmd.xml
     
     :param logDir str: the directory to parse
@@ -121,16 +165,11 @@ def update_hat_xml(logDir, application=None):
     '''
     # Create an instance of xmlLogFile class to create hat.xml file
     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
-    # If there is an application, add the attribute to the root node
-    if application:
-        xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : application})
-    else:
-        xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : "NO"})
-    
+    xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : application})
     # parse the log directory to find all the command logs, then add it to the xml file
     for fileName in os.listdir(logDir):
         # YYYYMMDD_HHMMSS_namecmd.xml
-        sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
+        sExpr = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$"
         oExpr = re.compile(sExpr)
         if oExpr.search(fileName):
             # get date and hour and format it
@@ -140,8 +179,11 @@ def update_hat_xml(logDir, application=None):
             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')]
-            # add a node to the hat.xml file
-            xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
+            showLog, cmdAppli = showcommandLog(os.path.join(logDir, fileName), cmd, application, notShownCommands)
+            #if cmd not in notShownCommands:
+            if showLog:
+                # add a node to the hat.xml file
+                xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd, "application" : cmdAppli})
     
     # Write the file on the hard drive
     xmlHat.write_tree('hat.xsl')
\ No newline at end of file
index 7fea5507bc49bdfacc2f565ed388bbae69435dc9..2e33907ef50c1fc452986529f16cb25e7b8b7a1c 100644 (file)
        <body class="def" bgcolor="aliceblue">
                <h1><img src="LOGO-SAT.png"/></h1>
                        <xsl:choose>
-                         <xsl:when test="LOGlist/@application='NO'">
-                                 <h1>Logs of commands without application</h1>
+                         <xsl:when test="not(LOGlist/@application='None')">
+                                 <h1>Logs of application <xsl:value-of select="LOGlist/@application"/></h1>
                          </xsl:when>
                          <xsl:otherwise>
                                  <h1>
-                                         Logs of application <xsl:value-of select="LOGlist/@application"/>
+                                         General Logs
                                  </h1>
                          </xsl:otherwise>
                        </xsl:choose>
                        <table border="0">
                                <tr>
-                               <td width="100px">Command</td><td width="100px">date</td><td>time</td>
+                               <td width="100px">Command</td><td width="100px">date</td><td width="100px">time</td><td>application</td>
                                </tr>
                                <xsl:for-each select="LOGlist/LogCommand">
                                        <xsl:sort select="." order="descending" />
@@ -53,6 +53,7 @@
                                                </td>
                                                <td><xsl:value-of select="@date"/></td>
                                                <td><xsl:value-of select="@hour"/></td>
+                                               <td><xsl:value-of select="@application"/></td>
                                        </tr>
                                </xsl:for-each>
                        </table>