From 75c387abdb9d9551aae852414ef189f761cc7a64 Mon Sep 17 00:00:00 2001 From: Serge Rehbinder Date: Wed, 10 Feb 2016 10:50:26 +0100 Subject: [PATCH] Add terminal logging --- commands/config.py | 3 - commands/log.py | 69 ++++++++++++++++++-- commands/{test_command.py => testcommand.py} | 2 +- salomeTools.py | 30 +++++---- src/__init__.py | 9 ++- src/logger.py | 5 +- src/xmlManager.py | 26 +++++++- 7 files changed, 117 insertions(+), 27 deletions(-) rename commands/{test_command.py => testcommand.py} (93%) diff --git a/commands/config.py b/commands/config.py index f294e10..6b0f2cb 100644 --- a/commands/config.py +++ b/commands/config.py @@ -496,7 +496,4 @@ def run(args, runner): runner.logger.write("\n") - runner.logger.endWrite() - - \ No newline at end of file diff --git a/commands/log.py b/commands/log.py index 96fc057..d09dbb6 100644 --- a/commands/log.py +++ b/commands/log.py @@ -3,6 +3,7 @@ import os import shutil +import re import src @@ -10,26 +11,82 @@ import src parser = src.options.Options() parser.add_option('t', 'terminal', 'boolean', 'terminal', "Terminal log.") +def ask_value(nb): + try: + rep = raw_input(_("Which one (enter or 0 to quit)? ")) + if len(rep) == 0: + x = 0 + else: + x = int(rep) + if x > nb: + x = -1 + except: + x = -1 + + return x +def show_log_command_in_terminal(filePath, logger): + xmlRead = src.xmlManager.readXmlFile(filePath) + lAttrText = xmlRead.get_attrib_text('name') + logger.write("\n", 1) + src.print_info(logger, lAttrText) + command_traces = xmlRead.get_node_text('traces') + if command_traces: + logger.write(_("Here are the command traces :\n"), 1) + logger.write(command_traces, 1) + logger.write("\n", 1) def description(): return _("Gives access to logs of salomeTools.") def run(args, runner): (options, args) = parser.parse_args(args) + + # get the log directory. If there is an application, it is in cfg.APPLICATION.out_dir, else in user directory + logDir = runner.cfg.VARS.logDir + if options.terminal: - print('terminal') + 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) + index = 0 + 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')] + + num = src.printcolors.printcLabel("%2d" % (nb_logs - index)) + runner.logger.write("%s: %13s %s %s\n" % (num, cmd, date, hour), 1, False) + index += 1 + + # ask the user + x = -1 + while (x < 0): + x = ask_value(nb_logs) - # Find stylesheet Directory and files + if x > 0: + index = len(lLogs) - int(x) + show_log_command_in_terminal(os.path.join(logDir, lLogs[index]), runner.logger) + x = 0 + + return + + + # Find the stylesheets 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 + # copy the stylesheets in the log directory shutil.copy2(xslCommand, logDir) shutil.copy2(xslHat, logDir) shutil.copy2(imgLogo, logDir) diff --git a/commands/test_command.py b/commands/testcommand.py similarity index 93% rename from commands/test_command.py rename to commands/testcommand.py index 560cd45..f2b0f80 100644 --- a/commands/test_command.py +++ b/commands/testcommand.py @@ -15,7 +15,7 @@ def description(): def run(args, runner): (options, args) = parser.parse_args(args) if options.unique: - print('unique') + runner.logger.write('unique\n') elif options.value: runner.cfg.VARS.user = 'TEST' runner.config('-v ' + options.value) diff --git a/salomeTools.py b/salomeTools.py index 742bc70..711ee28 100755 --- a/salomeTools.py +++ b/salomeTools.py @@ -138,22 +138,26 @@ class Sat(object): appliToLoad = argv[0].rstrip('*') argv = argv[1:] - # Read the config if it is not already done - if not self.cfg: - # read the configuration from all the pyconf files - cfgManager = config.ConfigManager() - self.cfg = cfgManager.getConfig(dataDir=self.dataDir, application=appliToLoad, options=self.options, command=__nameCmd__) + # read the configuration from all the pyconf files + cfgManager = config.ConfigManager() + self.cfg = cfgManager.getConfig(dataDir=self.dataDir, application=appliToLoad, options=self.options, command=__nameCmd__) - # set output level - if self.options.output_level: - self.cfg.USER.output_level = self.options.output_level - if self.cfg.USER.output_level < 1: - self.cfg.USER.output_level = 1 + # set output level + if self.options.output_level: + self.cfg.USER.output_level = self.options.output_level + if self.cfg.USER.output_level < 1: + self.cfg.USER.output_level = 1 - # create log file - self.logger = src.logger.Logger(self.cfg, silent_sysstd=self.options.silent) + # create log file + self.logger = src.logger.Logger(self.cfg, silent_sysstd=self.options.silent) - return __module__.run(argv, self) + # Execute the run method of the command + res = __module__.run(argv, self) + + # put final attributes in xml log file (end time, total time, ...) and write it + self.logger.endWrite() + + return res # Make sure that run_command will be redefined at each iteration of the loop globals_up = {} diff --git a/src/__init__.py b/src/__init__.py index f8a8dc7..66e341a 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -48,4 +48,11 @@ def check_config_has_application( config, details = None ): message = _("An APPLICATION is required. Use 'config --list' to get the list of available applications.\n") if details : details.append(message) - raise SatException( message ) \ No newline at end of file + raise SatException( message ) + +def print_info(logger, info): + smax = max(map(lambda l: len(l[0]), info)) + for i in info: + sp = " " * (smax - len(i[0])) + printcolors.print_value(logger, sp + i[0], i[1], 2) + logger.write("\n", 2) \ No newline at end of file diff --git a/src/logger.py b/src/logger.py index 5c821e6..50c349e 100644 --- a/src/logger.py +++ b/src/logger.py @@ -47,7 +47,6 @@ class Logger(object): self.logFileName = logFileName self.logFilePath = logFilePath - self.xmlFile = xmlManager.xmlLogFile(logFilePath, "SATcommand", attrib = {"command" : config.VARS.command}) self.putInitialXMLFields() @@ -106,6 +105,10 @@ class Logger(object): sys.stdout.flush() def endWrite(self): + + self.write(_('\nTap the following command to get the log :\n'), screenOnly=True) + self.write('%s/sat log\n' % self.config.VARS.salometoolsway, screenOnly=True) + dt = datetime.datetime.now() endtime = dt.strftime('%Y%m%d_%H%M%S') t0 = date_to_datetime(self.config.VARS.datehour) diff --git a/src/xmlManager.py b/src/xmlManager.py index f5c3b09..ad0d815 100644 --- a/src/xmlManager.py +++ b/src/xmlManager.py @@ -48,7 +48,29 @@ class xmlLogFile(object): for field in self.xmlroot: if field.tag == node_name: field.text += text - + +class readXmlFile(object): + '''Class to manage reading of an xml log file + ''' + def __init__(self, filePath): + self.filePath = filePath + etree_inst = etree.parse(filePath) + self.xmlroot = etree_inst.parse(filePath) + + def get_attrib_text(self, attribname): + lres = [] + for field in self.xmlroot: + if attribname in field.attrib: + lres.append((field.attrib[attribname], field.text)) + return lres + + def get_node_text(self, node): + for field in self.xmlroot: + if field.tag == node: + return field.text + return '' + + def update_hat_xml(logDir, application=None): xmlHatFilePath = os.path.join(logDir, 'hat.xml') if application: @@ -57,7 +79,7 @@ def update_hat_xml(logDir, application=None): xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : "NO"}) for fileName in os.listdir(logDir): - sExpr = "^[0-9]{8}_+[0-9]{6}_+[A-Za-z0-9]*.xml$" + sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$" oExpr = re.compile(sExpr) if oExpr.search(fileName): date_hour_cmd = fileName.split('_') -- 2.39.2