Salome HOME
add test for logging functionnality
[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 show_log_command_in_terminal(filePath, logger):
15     '''Print the contain of filePath. It contains a command log in xml format.
16     
17     :param filePath: The command xml file from which extract the commands context and traces
18     :logger Logger: the logging instance to use in order to print.  
19     '''
20     # Instantiate the readXmlFile class that reads xml files
21     xmlRead = src.xmlManager.readXmlFile(filePath)
22     # Get the attributes containing the context (user, OS, time, etc..)
23     lAttrText = xmlRead.get_attrib_text('name')
24     logger.write("\n", 1)
25     # Print the context
26     src.print_info(logger, lAttrText)
27     # Get the traces
28     command_traces = xmlRead.get_node_text('traces')
29     # Print it if there is any
30     if command_traces:
31         logger.write(_("Here are the command traces :\n"), 1)
32         logger.write(command_traces, 1)
33         logger.write("\n", 1)
34
35 def ask_value(nb):
36     '''Ask for an int n. 0<n<nb
37     
38     :param nb int: The maximum value of the value to be returned by the user.
39     :return: the value entered by the user. Return -1 if it is not as expected
40     :rtype: int
41     '''
42     try:
43         # ask for a value
44         rep = raw_input(_("Which one (enter or 0 to quit)? "))
45         # Verify it is on the right range
46         if len(rep) == 0:
47             x = 0
48         else:
49             x = int(rep)
50             if x > nb:
51                 x = -1
52     except:
53         x = -1
54     
55     return x
56
57 def description():
58     '''method that is called when salomeTools is called with --help option.
59     
60     :return: The text to display for the log command description.
61     :rtype: str
62     '''
63     return _("Gives access to logs of salomeTools.")    
64
65 def run(args, runner, logger):
66     '''method that is called when salomeTools is called with log parameter.
67     '''
68     # Parse the options
69     (options, args) = parser.parse_args(args)
70
71     # get the log directory. If there is an application, it is in cfg.APPLICATION.out_dir, else in user directory
72     logDir = runner.cfg.VARS.logDir
73
74     # If the user asks for a terminal display
75     if options.terminal:
76         # Parse the log directory in order to find all the files corresponding to the commands
77         lLogs = []
78         for fileName in os.listdir(logDir):
79             sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
80             oExpr = re.compile(sExpr)
81             if oExpr.search(fileName):
82                 lLogs.append(fileName)
83         lLogs = sorted(lLogs)
84         nb_logs = len(lLogs)
85         index = 0
86         # loop on all files and print it with date, time and command name 
87         for t in lLogs:
88             date_hour_cmd = t.split('_')
89             date_not_formated = date_hour_cmd[0]
90             date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
91             hour_not_formated = date_hour_cmd[1]
92             hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
93             cmd = date_hour_cmd[2][:-len('.xml')]
94             
95             num = src.printcolors.printcLabel("%2d" % (nb_logs - index))
96             logger.write("%s: %13s %s %s\n" % (num, cmd, date, hour), 1, False)
97             index += 1
98         
99         # ask the user what for what command he wants to be displayed
100         x = -1
101         while (x < 0):
102             x = ask_value(nb_logs)
103             if x > 0:
104                 index = len(lLogs) - int(x)
105                 # Show the log corresponding to the selected command call
106                 show_log_command_in_terminal(os.path.join(logDir, lLogs[index]), logger)                
107                 x = 0
108         
109         return 0
110                     
111     
112     # Find the stylesheets Directory and files
113     xslDir = os.path.join(runner.cfg.VARS.srcDir, 'xsl')
114     xslCommand = os.path.join(xslDir, "command.xsl")
115     xslHat = os.path.join(xslDir, "hat.xsl")
116     imgLogo = os.path.join(xslDir, "LOGO-SAT.png")
117     
118     # copy the stylesheets in the log directory
119     shutil.copy2(xslCommand, logDir)
120     shutil.copy2(xslHat, logDir)
121     shutil.copy2(imgLogo, logDir)
122     
123     # Create or update the hat xml that gives access to all the commands log files
124     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
125     if 'APPLICATION' in runner.cfg:
126         src.xmlManager.update_hat_xml(runner.cfg.VARS.logDir, runner.cfg.VARS.application)
127     else:
128         src.xmlManager.update_hat_xml(runner.cfg.VARS.logDir)
129     
130     # open the hat xml in the user editor
131     src.system.show_in_editor(runner.cfg.USER.browser, xmlHatFilePath)