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