+ (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
+
+def show_command_log(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, None
+
+ # Get the application of the log file
+ try:
+ logFileXml = src.xmlManager.ReadXmlFile(logFilePath)
+ except Exception as e:
+ msg = _("WARNING: the log file %s cannot be read:" % logFilePath)
+ sys.stdout.write(printcolors.printcWarning("%s\n%s\n" % (msg, e)))
+ return False, None, None
+
+ try:
+ if 'application' in logFileXml.xmlroot.keys():
+ appliLog = logFileXml.xmlroot.get('application')
+ launched_cmd = logFileXml.xmlroot.find('Site').attrib['launchedCommand']
+ # if it corresponds, then the log has to be shown
+ if appliLog == application:
+ return True, appliLog, launched_cmd
+ elif application != 'None':
+ return False, appliLog, launched_cmd
+
+ return True, appliLog, launched_cmd
+ except Exception as e:
+ msg = _("WARNING: the log file %s cannot be parsed:" % logFilePath)
+ sys.stdout.write(printcolors.printcWarning("%s\n%s\n" % (msg, e)))
+ return False, None, None
+
+ if application == 'None':
+ return True, None, None
+
+ return False, None, None
+
+def list_log_file(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):
+ file_name = fileName
+ if fileName.startswith("micro_"):
+ file_name = fileName[len("micro_"):]
+ # get date and hour and format it
+ date_hour_cmd_host = file_name.split('_')
+ date_not_formated = date_hour_cmd_host[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_host[1]
+ hour = "%s:%s:%s" % (hour_not_formated[0:2],
+ hour_not_formated[2:4],
+ hour_not_formated[4:6])
+ if len(date_hour_cmd_host) < 4:
+ cmd = date_hour_cmd_host[2][:-len('.xml')]
+ host = ""
+ else:
+ cmd = date_hour_cmd_host[2]
+ host = date_hour_cmd_host[3][:-len('.xml')]
+ lRes.append((os.path.join(dirPath, fileName),
+ date_not_formated,
+ date,
+ hour_not_formated,
+ hour,
+ cmd,
+ host))
+ 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 = list_log_file(logDir, log_macro_command_file_expression)
+ for filePath, __, date, __, hour, cmd, __ in lLogFile:
+ showLog, cmdAppli, full_cmd = show_command_log(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,
+ "full_command" : full_cmd})
+
+ # Write the file on the hard drive
+ xmlHat.write_tree('hat.xsl')
+ # Sometimes other users will make 'sat log' and update this file
+ os.chmod(xmlHatFilePath,
+ stat.S_IRUSR |
+ stat.S_IRGRP |
+ stat.S_IROTH |
+ stat.S_IWUSR |
+ stat.S_IWGRP |
+ stat.S_IWOTH )
+
+
+
+# TODO for future
+# prepare skip to logging logger sat5.1
+# suppose only one logger in sat5.1
+_currentLogger = []
+
+def getCurrentLogger():
+ """get current logging logger, set as DefaultLogger if not set yet"""
+ if len(_currentLogger) == 0:
+ import src.loggingSimple as LOGSI
+ logger = LOGSI.getDefaultLogger()
+ _currentLogger.append(logger)
+ logger.warning("set by default current logger as %s" % logger.name)
+ return _currentLogger[0]
+
+def getDefaultLogger():
+ """get simple logging logger DefaultLogger, set it as current"""
+ import src.loggingSimple as LOGSI
+ logger = LOGSI.getDefaultLogger()
+ setCurrentLogger(logger) # set it as current
+ return logger
+
+def getUnittestLogger():
+ """get simple logging logger UnittestLogger, set it as current"""
+ import src.loggingSimple as LOGSI
+ logger = LOGSI.getUnittestLogger()
+ setCurrentLogger(logger) # set it as current
+ return logger
+
+def setCurrentLogger(logger):
+ """temporary send all in stdout as simple logging logger"""
+ if len(_currentLogger) == 0:
+ _currentLogger.append(logger)
+ logger.debug("set current logger as %s" % logger.name)
+ else:
+ if _currentLogger[0].name != logger.name:
+ # logger.debug("quit current logger as default %s" % _currentLogger[0].name)
+ _currentLogger[0] = logger
+ logger.warning("change current logger as %s" % logger.name)
+ return _currentLogger[0]
+
+def isCurrentLoggerUnittest():
+ logger = getCurrentLogger()
+ if "Unittest" in logger.name:
+ res = True
+ else:
+ res = False
+ #DBG.write("isCurrentLoggerUnittest %s" % logger.name, res)
+ return res
+
+def sendMessageToCurrentLogger(message, level):
+ """
+ assume relay from obsolescent
+ logger.write(msg, 1/2/3...) to future
+ logging.critical/warning/info...(msg) (as logging package tips)
+ """
+ logger = getCurrentLogger()
+ if level is None:
+ lev = 2
+ else:
+ lev = level
+ if lev <= 1:
+ logger.critical(message)
+ return
+ if lev == 2:
+ logger.warning(message)
+ return
+ if lev == 3:
+ logger.info(message)
+ return
+ if lev == 4:
+ logger.step(message)
+ return
+ if lev == 5:
+ logger.trace(message)
+ return
+ if lev >= 6:
+ logger.debug(message)
+ return
+ msg = "What is this level: '%s' for message:\n%s" % (level, message)
+ logger.warning(msg)
+ return