3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 # Compatibility python 2/3 for input function
24 # input stays input for python 3 and input = raw_input for python 2
32 # Define all possible option for log command : sat log <options>
33 parser = src.options.Options()
34 parser.add_option('t', 'terminal', 'boolean', 'terminal', "Terminal log.")
35 parser.add_option('l', 'last', 'boolean', 'last', "Show the log of the last "
37 parser.add_option('f', 'full', 'boolean', 'full', "Show the logs of ALL "
39 parser.add_option('c', 'clean', 'int', 'clean', "Erase the n most ancient "
42 def get_last_log_file(logDir, notShownCommands):
43 '''Used in case of last option. Get the last log command file path.
45 :param logDir str: The directory where to search the log files
46 :param notShownCommands list: the list of commands to ignore
47 :return: the path to the last log file
51 for fileName in os.listdir(logDir):
52 # YYYYMMDD_HHMMSS_namecmd.xml
53 sExpr = src.logger.logCommandFileExpression
54 oExpr = re.compile(sExpr)
55 if oExpr.search(fileName):
56 # get date and hour and format it
57 date_hour_cmd = fileName.split('_')
58 datehour = date_hour_cmd[0] + date_hour_cmd[1]
59 cmd = date_hour_cmd[2][:-len('.xml')]
60 if cmd in notShownCommands:
62 if int(datehour) > last[1]:
63 last = (fileName, int(datehour))
64 return os.path.join(logDir, last[0])
66 def remove_log_file(filePath, logger):
67 '''if it exists, print a warning and remove the input file
69 :param filePath: the path of the file to delete
70 :param logger Logger: the logger instance to use for the print
72 if os.path.exists(filePath):
73 logger.write(src.printcolors.printcWarning("Removing ")
77 def print_log_command_in_terminal(filePath, logger):
78 '''Print the contain of filePath. It contains a command log in xml format.
80 :param filePath: The command xml file from which extract the commands
82 :param logger Logger: the logging instance to use in order to print.
84 logger.write(_("Reading ") + src.printcolors.printcHeader(filePath) + "\n", 5)
85 # Instantiate the ReadXmlFile class that reads xml files
86 xmlRead = src.xmlManager.ReadXmlFile(filePath)
87 # Get the attributes containing the context (user, OS, time, etc..)
88 dAttrText = xmlRead.get_attrib('Site')
89 # format dAttrText and print the context
91 for attrib in dAttrText:
92 lAttrText.append((attrib, dAttrText[attrib]))
94 src.print_info(logger, lAttrText)
96 command_traces = xmlRead.get_node_text('Log')
97 # Print it if there is any
99 logger.write(src.printcolors.printcHeader(
100 _("Here are the command traces :\n")), 1)
101 logger.write(command_traces, 1)
102 logger.write("\n", 1)
105 '''Ask for an int n. 0<n<nb
107 :param nb int: The maximum value of the value to be returned by the user.
108 :return: the value entered by the user. Return -1 if it is not as expected
113 rep = input(_("Which one (enter or 0 to quit)? "))
114 # Verify it is on the right range
127 '''method that is called when salomeTools is called with --help option.
129 :return: The text to display for the log command description.
132 return _("Gives access to the logs produced by the salomeTools commands.")
134 def run(args, runner, logger):
135 '''method that is called when salomeTools is called with log parameter.
138 (options, args) = parser.parse_args(args)
140 # get the log directory.
141 # If there is an application, it is in cfg.APPLICATION.out_dir,
142 # else in user directory
143 logDir = runner.cfg.SITE.log.log_dir
145 # If the clean options is invoked,
146 # do nothing but deleting the concerned files.
148 nbClean = options.clean
149 # get the list of files to remove
150 lLogs = src.logger.list_log_file(logDir,
151 src.logger.logCommandFileExpression)
152 nbLogFiles = len(lLogs)
153 # Delete all if the invoked number is bigger than the number of log files
154 if nbClean > nbLogFiles:
156 # Get the list to delete and do the removing
157 lLogsToDelete = sorted(lLogs)[:nbClean]
158 for filePath, _, _, _, _, _ in lLogsToDelete:
159 # remove the xml log file
160 remove_log_file(filePath, logger)
161 # remove also the corresponding txt file in OUT directory
162 txtFilePath = os.path.join(os.path.dirname(filePath),
164 os.path.basename(filePath)[:-len('.xml')] + '.txt')
165 remove_log_file(txtFilePath, logger)
166 # remove also the corresponding pyconf file in OUT directory
167 pyconfFilePath = os.path.join(os.path.dirname(filePath),
169 os.path.basename(filePath)[:-len('.xml')] + '.pyconf')
170 remove_log_file(pyconfFilePath, logger)
173 logger.write(src.printcolors.printcSuccess("OK\n"))
174 logger.write("%i logs deleted.\n" % nbClean)
177 # determine the commands to show in the hat log
178 notShownCommands = runner.cfg.INTERNAL.log.not_shown_commands
180 notShownCommands = []
182 # Find the stylesheets Directory and files
183 xslDir = os.path.join(runner.cfg.VARS.srcDir, 'xsl')
184 xslCommand = os.path.join(xslDir, "command.xsl")
185 xslHat = os.path.join(xslDir, "hat.xsl")
186 imgLogo = os.path.join(xslDir, "LOGO-SAT.png")
188 # copy the stylesheets in the log directory
189 shutil.copy2(xslCommand, logDir)
190 shutil.copy2(xslHat, logDir)
191 shutil.copy2(imgLogo, logDir)
193 # If the last option is invoked, just, show the last log file
195 lastLogFilePath = get_last_log_file(logDir, notShownCommands)
197 # Show the log corresponding to the selected command call
198 print_log_command_in_terminal(lastLogFilePath, logger)
200 # open the log xml file in the user editor
201 src.system.show_in_editor(runner.cfg.USER.browser,
202 lastLogFilePath, logger)
205 # If the user asks for a terminal display
207 # Parse the log directory in order to find
208 # all the files corresponding to the commands
209 lLogs = src.logger.list_log_file(logDir,
210 src.logger.logCommandFileExpression)
212 for filePath, _, date, _, hour, cmd in lLogs:
213 showLog, cmdAppli = src.logger.show_command_log(filePath, cmd,
214 runner.cfg.VARS.application, notShownCommands)
216 lLogsFiltered.append((filePath, date, hour, cmd, cmdAppli))
218 lLogsFiltered = sorted(lLogsFiltered)
219 nb_logs = len(lLogsFiltered)
221 # loop on all files and print it with date, time and command name
222 for _, date, hour, cmd, cmdAppli in lLogsFiltered:
223 num = src.printcolors.printcLabel("%2d" % (nb_logs - index))
224 logger.write("%s: %13s %s %s %s\n" %
225 (num, cmd, date, hour, cmdAppli), 1, False)
228 # ask the user what for what command he wants to be displayed
231 x = ask_value(nb_logs)
233 index = len(lLogsFiltered) - int(x)
234 # Show the log corresponding to the selected command call
235 print_log_command_in_terminal(lLogsFiltered[index][0], logger)
240 # Create or update the hat xml that gives access to all the commands log files
241 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
242 src.logger.update_hat_xml(runner.cfg.SITE.log.log_dir,
243 application = runner.cfg.VARS.application,
244 notShownCommands = notShownCommands)
246 # open the hat xml in the user editor
247 src.system.show_in_editor(runner.cfg.USER.browser, xmlHatFilePath, logger)