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