]> SALOME platform Git repositories - tools/sat.git/blob - commands/log.py
Salome HOME
Add terminal logging
[tools/sat.git] / commands / log.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 import os
5 import shutil
6 import re
7
8 import src
9
10 # Define all possible option for log command :  sat log <options>
11 parser = src.options.Options()
12 parser.add_option('t', 'terminal', 'boolean', 'terminal', "Terminal log.")
13
14 def ask_value(nb):
15     try:
16         rep = raw_input(_("Which one (enter or 0 to quit)? "))
17         if len(rep) == 0:
18             x = 0
19         else:
20             x = int(rep)
21             if x > nb:
22                 x = -1
23     except:
24         x = -1
25     
26     return x
27
28 def show_log_command_in_terminal(filePath, logger):
29     xmlRead = src.xmlManager.readXmlFile(filePath)
30     lAttrText = xmlRead.get_attrib_text('name')
31     logger.write("\n", 1)
32     src.print_info(logger, lAttrText)
33     command_traces = xmlRead.get_node_text('traces')
34     if command_traces:
35         logger.write(_("Here are the command traces :\n"), 1)
36         logger.write(command_traces, 1)
37         logger.write("\n", 1)
38
39 def description():
40     return _("Gives access to logs of salomeTools.")    
41
42 def run(args, runner):
43     (options, args) = parser.parse_args(args)
44
45     # get the log directory. If there is an application, it is in cfg.APPLICATION.out_dir, else in user directory
46     logDir = runner.cfg.VARS.logDir
47
48     if options.terminal:
49         lLogs = []
50         for fileName in os.listdir(logDir):
51             sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
52             oExpr = re.compile(sExpr)
53             if oExpr.search(fileName):
54                 lLogs.append(fileName)
55         lLogs = sorted(lLogs)
56         nb_logs = len(lLogs)
57         index = 0
58         for t in lLogs:
59             date_hour_cmd = t.split('_')
60             date_not_formated = date_hour_cmd[0]
61             date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
62             hour_not_formated = date_hour_cmd[1]
63             hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
64             cmd = date_hour_cmd[2][:-len('.xml')]
65             
66             num = src.printcolors.printcLabel("%2d" % (nb_logs - index))
67             runner.logger.write("%s: %13s %s %s\n" % (num, cmd, date, hour), 1, False)
68             index += 1
69         
70         # ask the user
71         x = -1
72         while (x < 0):
73             x = ask_value(nb_logs)
74     
75             if x > 0:
76                 index = len(lLogs) - int(x)
77                 show_log_command_in_terminal(os.path.join(logDir, lLogs[index]), runner.logger)                
78                 x = 0
79         
80         return
81                     
82     
83     # Find the stylesheets Directory and files
84     xslDir = os.path.join(runner.cfg.VARS.srcDir, 'xsl')
85     xslCommand = os.path.join(xslDir, "command.xsl")
86     xslHat = os.path.join(xslDir, "hat.xsl")
87     imgLogo = os.path.join(xslDir, "LOGO-SAT.png")
88     
89     # copy the stylesheets in the log directory
90     shutil.copy2(xslCommand, logDir)
91     shutil.copy2(xslHat, logDir)
92     shutil.copy2(imgLogo, logDir)
93     
94     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
95     if 'APPLICATION' in runner.cfg:
96         src.xmlManager.update_hat_xml(runner.cfg.VARS.logDir, runner.cfg.VARS.application)
97     else:
98         src.xmlManager.update_hat_xml(runner.cfg.VARS.logDir)
99     
100     src.system.show_in_editor(runner.cfg.USER.browser, xmlHatFilePath)