From: Serge Rehbinder Date: Wed, 17 Feb 2016 09:27:09 +0000 (+0100) Subject: 'sat log' : add --clean option X-Git-Tag: sprint-02bis~6 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=ddbfaafc467805dc1f1063a016797a2973d19b7c;p=tools%2Fsat.git 'sat log' : add --clean option --- diff --git a/commands/log.py b/commands/log.py index c4addf6..dab5d09 100644 --- a/commands/log.py +++ b/commands/log.py @@ -4,6 +4,14 @@ import os import shutil import re +import gettext + +# Compatibility python 2/3 for input function +# input stays input for python 3 and input = raw_input for python 2 +try: + input = raw_input +except NameError: + pass import src @@ -15,10 +23,17 @@ parser.add_option('f', 'full', 'boolean', 'full', "Show the logs of ALL launched parser.add_option('c', 'clean', 'int', 'clean', "Erase the n most ancient log files.") def getLastLogFile(logDir, notShownCommands): + '''Used in case of last option. Get the last log command file path. + + :param logDir str: The directory where to search the log files + :param notShownCommands list: the list of commands to ignore + :return: the path to the last log file + :rtype: str + ''' last = (_, 0) for fileName in os.listdir(logDir): # YYYYMMDD_HHMMSS_namecmd.xml - sExpr = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$" + sExpr = src.logger.logCommandFileExpression oExpr = re.compile(sExpr) if oExpr.search(fileName): # get date and hour and format it @@ -31,17 +46,17 @@ def getLastLogFile(logDir, notShownCommands): last = (fileName, int(datehour)) return os.path.join(logDir, last[0]) -def show_log_command_in_terminal(filePath, logger): +def print_log_command_in_terminal(filePath, logger): '''Print the contain of filePath. It contains a command log in xml format. :param filePath: The command xml file from which extract the commands context and traces - :logger Logger: the logging instance to use in order to print. + :param logger Logger: the logging instance to use in order to print. ''' + logger.write(_("Reading ") + src.printcolors.printcHeader(filePath) + "\n", 5) # Instantiate the readXmlFile class that reads xml files xmlRead = src.xmlManager.readXmlFile(filePath) # Get the attributes containing the context (user, OS, time, etc..) dAttrText = xmlRead.get_attrib('Site') - # format dAttrText and print the context lAttrText = [] for attrib in dAttrText: @@ -52,7 +67,7 @@ def show_log_command_in_terminal(filePath, logger): command_traces = xmlRead.get_node_text('Log') # Print it if there is any if command_traces: - logger.write(_("Here are the command traces :\n"), 1) + logger.write(src.printcolors.printcHeader(_("Here are the command traces :\n")), 1) logger.write(command_traces, 1) logger.write("\n", 1) @@ -65,7 +80,7 @@ def ask_value(nb): ''' try: # ask for a value - rep = raw_input(_("Which one (enter or 0 to quit)? ")) + rep = input(_("Which one (enter or 0 to quit)? ")) # Verify it is on the right range if len(rep) == 0: x = 0 @@ -84,7 +99,7 @@ def description(): :return: The text to display for the log command description. :rtype: str ''' - return _("Gives access to logs of salomeTools.") + return _("Gives access to the logs produced by the salomeTools commands.") def run(args, runner, logger): '''method that is called when salomeTools is called with log parameter. @@ -95,29 +110,47 @@ def run(args, runner, logger): # get the log directory. If there is an application, it is in cfg.APPLICATION.out_dir, else in user directory logDir = runner.cfg.SITE.log.logDir + # If the clean options is invoked, do nothing but deleting the concerned files. + if options.clean: + nbClean = options.clean + # get the list of files to remove + lLogs = src.logger.listLogFile(logDir, src.logger.logCommandFileExpression) + nbLogFiles = len(lLogs) + # Delete all if the invoked number is bigger than the number of log files + if nbClean > nbLogFiles: + nbClean = nbLogFiles + # Get the list to delete and do the removing + lLogsToDelete = sorted(lLogs)[:nbClean] + for filePath, _, _, _, _, _ in lLogsToDelete: + logger.write(src.printcolors.printcWarning("Removing ") + filePath + "\n", 5) + os.remove(filePath) + + logger.write(src.printcolors.printcSuccess("OK\n")) + logger.write("%i files deleted.\n" % nbClean) + return 0 + + # determine the commands to show in the hat log + notShownCommands = runner.cfg.INTERNAL.log.notShownCommands + if options.full: + notShownCommands = [] + # If the user asks for a terminal display if options.terminal: # Parse the log directory in order to find all the files corresponding to the commands - lLogs = [] - for fileName in os.listdir(logDir): - sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$" - oExpr = re.compile(sExpr) - if oExpr.search(fileName): - lLogs.append(fileName) - lLogs = sorted(lLogs) - nb_logs = len(lLogs) + lLogs = src.logger.listLogFile(logDir, src.logger.logCommandFileExpression) + lLogsFiltered = [] + for filePath, _, date, _, hour, cmd in lLogs: + showLog, cmdAppli = src.logger.showcommandLog(filePath, cmd, runner.cfg.VARS.application, notShownCommands) + if showLog: + lLogsFiltered.append((filePath, date, hour, cmd, cmdAppli)) + + lLogsFiltered = sorted(lLogsFiltered) + nb_logs = len(lLogsFiltered) index = 0 # loop on all files and print it with date, time and command name - for t in lLogs: - date_hour_cmd = t.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')] - + for _, date, hour, cmd, cmdAppli in lLogsFiltered: num = src.printcolors.printcLabel("%2d" % (nb_logs - index)) - logger.write("%s: %13s %s %s\n" % (num, cmd, date, hour), 1, False) + logger.write("%s: %13s %s %s %s\n" % (num, cmd, date, hour, cmdAppli), 1, False) index += 1 # ask the user what for what command he wants to be displayed @@ -125,9 +158,9 @@ def run(args, runner, logger): while (x < 0): x = ask_value(nb_logs) if x > 0: - index = len(lLogs) - int(x) + index = len(lLogsFiltered) - int(x) # Show the log corresponding to the selected command call - show_log_command_in_terminal(os.path.join(logDir, lLogs[index]), logger) + print_log_command_in_terminal(lLogsFiltered[index][0], logger) x = 0 return 0 @@ -144,11 +177,6 @@ def run(args, runner, logger): 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 = [] - # If the last option is invoked, just, show the last log file if options.last: lastLogFilePath = getLastLogFile(logDir, notShownCommands) @@ -158,7 +186,7 @@ def run(args, runner, logger): # 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, application = runner.cfg.VARS.application, notShownCommands = notShownCommands) + src.logger.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) diff --git a/src/ElementTree.py b/src/ElementTree.py index 49a0643..37ed467 100644 --- a/src/ElementTree.py +++ b/src/ElementTree.py @@ -324,6 +324,8 @@ class _ElementInterface: # @defreturn Element or None def find(self, path): + if ElementPath.find(self, path) == None: + return ElementPath.find(self, path.encode()) return ElementPath.find(self, path) ## diff --git a/src/logger.py b/src/logger.py index 42e328d..a34ebfe 100644 --- a/src/logger.py +++ b/src/logger.py @@ -19,11 +19,14 @@ import sys import os import datetime +import re import src from . import printcolors from . import xmlManager +logCommandFileExpression = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$" + class Logger(object): '''Class to handle log mechanism ''' @@ -169,4 +172,81 @@ def timedelta_total_seconds(timedelta): ''' return ( timedelta.microseconds + 0.0 + - (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 \ No newline at end of file + (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 + +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 = src.xmlManager.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 listLogFile(dirPath, expression): + '''Find all files corresponding to expression in dirPath + + :param dirPath str: the directory where to search the files + :param expression str: the regular expression of files to find + :return: the list of files path and informations about it + :rtype: list + ''' + lRes = [] + for fileName in os.listdir(dirPath): + # YYYYMMDD_HHMMSS_namecmd.xml + sExpr = expression + oExpr = re.compile(sExpr) + if oExpr.search(fileName): + # get date and hour and format it + 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')] + lRes.append((os.path.join(dirPath, fileName), date_not_formated, date, hour_not_formated, hour, cmd)) + return lRes + +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 + :param application str: the name of the application if there is any + ''' + # Create an instance of xmlLogFile class to create hat.xml file + xmlHatFilePath = os.path.join(logDir, 'hat.xml') + xmlHat = src.xmlManager.xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : application}) + # parse the log directory to find all the command logs, then add it to the xml file + lLogFile = listLogFile(logDir, logCommandFileExpression) + for filePath, _, date, _, hour, cmd in lLogFile: + showLog, cmdAppli = showcommandLog(filePath, cmd, application, notShownCommands) + #if cmd not in notShownCommands: + if showLog: + # add a node to the hat.xml file + xmlHat.add_simple_node("LogCommand", text=os.path.basename(filePath), 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 diff --git a/src/xmlManager.py b/src/xmlManager.py index 7b383c7..0572b74 100644 --- a/src/xmlManager.py +++ b/src/xmlManager.py @@ -17,7 +17,6 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os -import re import src from . import ElementTree as etree @@ -109,7 +108,20 @@ class readXmlFile(object): :return: the attibutes of the node node_name in self.xmlroot :rtype: dict ''' - return self.xmlroot.find(node_name).attrib + attrib = self.xmlroot.find(node_name).attrib + # To be python 3 compatible, convert bytes to str if there are any + fixedAttrib = {} + for k in attrib.keys(): + if isinstance(k, bytes): + key = k.decode() + else: + key = k + if isinstance(attrib[k], bytes): + value = attrib[k].decode() + else: + value = attrib[k] + fixedAttrib[key] = value + return fixedAttrib def get_node_text(self, node): '''Get the text of the first node that has name that corresponds to the parameter node @@ -118,72 +130,5 @@ class readXmlFile(object): :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: - if field.tag == node: - 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 - + return self.xmlroot.find(node).text -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 - :param application str: the name of the application if there is any - ''' - # Create an instance of xmlLogFile class to create hat.xml file - xmlHatFilePath = os.path.join(logDir, 'hat.xml') - 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$" - oExpr = re.compile(sExpr) - if oExpr.search(fileName): - # get date and hour and format it - 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')] - 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