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
26 # Compatibility python 2/3 for input function
27 # input stays input for python 3 and input = raw_input for python 2
35 # Define all possible option for log command : sat log <options>
36 parser = src.options.Options()
37 parser.add_option('t', 'terminal', 'boolean', 'terminal',
38 "Optional: Show the log (in terminal) of a command, with user choice.")
39 parser.add_option('l', 'last', 'boolean', 'last',
40 "Optional: Show the log (in browser) of the last launched command.")
41 parser.add_option('', 'last_compile', 'boolean', 'last_compile',
42 "Optional: Show the log (in terminal) of the last compilation for all products.")
43 parser.add_option('f', 'full', 'boolean', 'full',
44 "Optional: Show the logs of ALL the launched commands.")
45 parser.add_option('c', 'clean', 'int', 'clean',
46 "Erase the n most ancient log files.")
47 parser.add_option('n', 'no_browser', 'boolean', 'no_browser',
48 "Optional: Do not launch the browser at the end of the command. Only update the hat file.")
50 def get_last_log_file(logDir, notShownCommands):
51 '''Used in case of last option. Get the last log command file path.
53 :param logDir str: The directory where to search the log files
54 :param notShownCommands list: the list of commands to ignore
55 :return: the path to the last log file
59 for fileName in os.listdir(logDir):
60 # YYYYMMDD_HHMMSS_namecmd.xml
61 sExpr = src.logger.log_macro_command_file_expression
62 oExpr = re.compile(sExpr)
63 if oExpr.search(fileName):
64 # get date and hour and format it
65 date_hour_cmd = fileName.split('_')
66 datehour = date_hour_cmd[0] + date_hour_cmd[1]
67 cmd = date_hour_cmd[2]
68 if cmd in notShownCommands:
70 if int(datehour) > last[1]:
71 last = (fileName, int(datehour))
72 return os.path.join(logDir, last[0])
74 def remove_log_file(filePath, logger):
75 '''if it exists, print a warning and remove the input file
77 :param filePath: the path of the file to delete
78 :param logger Logger: the logger instance to use for the print
80 if os.path.exists(filePath):
81 logger.write(src.printcolors.printcWarning("Removing ")
85 def print_log_command_in_terminal(filePath, logger):
86 '''Print the contain of filePath. It contains a command log in xml format.
88 :param filePath: The command xml file from which extract the commands
90 :param logger Logger: the logging instance to use in order to print.
92 logger.write(_("Reading ") + src.printcolors.printcHeader(filePath) + "\n", 5)
93 # Instantiate the ReadXmlFile class that reads xml files
94 xmlRead = src.xmlManager.ReadXmlFile(filePath)
95 # Get the attributes containing the context (user, OS, time, etc..)
96 dAttrText = xmlRead.get_attrib('Site')
97 # format dAttrText and print the context
99 for attrib in dAttrText:
100 lAttrText.append((attrib, dAttrText[attrib]))
101 logger.write("\n", 1)
102 src.print_info(logger, lAttrText)
104 command_traces = xmlRead.get_node_text('Log')
105 # Print it if there is any
107 logger.write(src.printcolors.printcHeader(
108 _("Here are the command traces :\n")), 1)
109 logger.write(command_traces, 1)
110 logger.write("\n", 1)
112 def show_last_logs(logger, config, log_dirs):
113 """Show last compilation logs"""
114 log_dir = os.path.join(config.APPLICATION.workdir, 'LOGS')
115 sorted_log_dirs = sorted(log_dirs)
119 col_size = (nb / nb_cols) + 1
120 for index in range(0, col_size):
121 for i in range(0, nb_cols):
122 k = index + i * col_size
124 l = sorted_log_dirs[k]
125 str_indice = src.printcolors.printcLabel("%2d" % (k+1))
127 logger.write("%s: %-30s" % (str_indice, log_name), 1, False)
128 logger.write("\n", 1, False)
135 product_log_dir = os.path.join(log_dir, sorted_log_dirs[x-1])
136 show_product_last_logs(logger, config, product_log_dir)
138 def show_product_last_logs(logger, config, product_log_dir):
139 """Show last compilation logs of a product"""
140 # sort the files chronologically
142 for file_n in os.listdir(product_log_dir):
143 my_stat = os.stat(os.path.join(product_log_dir, file_n))
145 (datetime.datetime.fromtimestamp(my_stat[stat.ST_MTIME]), file_n))
147 # display the available logs
148 for i, (__, file_name) in enumerate(sorted(l_time_file)):
149 str_indice = src.printcolors.printcLabel("%2d" % (i+1))
151 my_stat = os.stat(os.path.join(product_log_dir, file_name))
152 opt.append(str(datetime.datetime.fromtimestamp(my_stat[stat.ST_MTIME])))
154 opt.append("(%8.2f)" % (my_stat[stat.ST_SIZE] / 1024.0))
155 logger.write(" %-35s" % " ".join(opt), 1, False)
156 logger.write("%s: %-30s\n" % (str_indice, file_name), 1, False)
161 x = ask_value(len(l_time_file))
163 (__, file_name) = sorted(l_time_file)[x-1]
164 log_file_path = os.path.join(product_log_dir, file_name)
165 src.system.show_in_editor(config.USER.editor, log_file_path, logger)
168 '''Ask for an int n. 0<n<nb
170 :param nb int: The maximum value of the value to be returned by the user.
171 :return: the value entered by the user. Return -1 if it is not as expected
176 rep = input(_("Which one (enter or 0 to quit)? "))
177 # Verify it is on the right range
190 '''method that is called when salomeTools is called with --help option.
192 :return: The text to display for the log command description.
196 The log command gives access to the logs produced by the salomeTools commands.
202 def run(args, runner, logger):
203 '''method that is called when salomeTools is called with log parameter.
206 (options, args) = parser.parse_args(args)
208 # get the log directory.
209 logDir = src.get_log_path(runner.cfg)
212 nb_files_log_dir = len(glob.glob(os.path.join(logDir, "*")))
213 info = [("log directory", logDir),
214 ("number of log files", nb_files_log_dir)]
215 src.print_info(logger, info)
217 # If the clean options is invoked,
218 # do nothing but deleting the concerned files.
220 nbClean = options.clean
221 # get the list of files to remove
222 lLogs = src.logger.list_log_file(logDir,
223 src.logger.log_all_command_file_expression)
224 nbLogFiles = len(lLogs)
225 # Delete all if the invoked number is bigger than the number of log files
226 if nbClean > nbLogFiles:
228 # Get the list to delete and do the removing
229 lLogsToDelete = sorted(lLogs)[:nbClean]
230 for filePath, __, __, __, __, __, __ in lLogsToDelete:
231 # remove the xml log file
232 remove_log_file(filePath, logger)
233 # remove also the corresponding txt file in OUT directory
234 txtFilePath = os.path.join(os.path.dirname(filePath),
236 os.path.basename(filePath)[:-len('.xml')] + '.txt')
237 remove_log_file(txtFilePath, logger)
238 # remove also the corresponding pyconf (do not exist 2016-06)
239 # file in OUT directory
240 pyconfFilePath = os.path.join(os.path.dirname(filePath),
242 os.path.basename(filePath)[:-len('.xml')] + '.pyconf')
243 remove_log_file(pyconfFilePath, logger)
246 logger.write(src.printcolors.printcSuccess("OK\n"))
247 logger.write("%i logs deleted.\n" % nbClean)
250 # determine the commands to show in the hat log
251 notShownCommands = list(runner.cfg.INTERNAL.log.not_shown_commands)
253 notShownCommands = []
255 # Find the stylesheets Directory and files
256 xslDir = os.path.join(runner.cfg.VARS.srcDir, 'xsl')
257 xslCommand = os.path.join(xslDir, "command.xsl")
258 xslHat = os.path.join(xslDir, "hat.xsl")
259 xsltest = os.path.join(xslDir, "test.xsl")
260 imgLogo = os.path.join(xslDir, "LOGO-SAT.png")
262 # copy the stylesheets in the log directory
263 # OP We use copy instead of copy2 to update the creation date
264 # So we can clean the LOGS directories easily
266 src.ensure_path_exists(logDir)
267 shutil.copy(xslCommand, logDir)
268 shutil.copy(xslHat, logDir)
269 src.ensure_path_exists(os.path.join(logDir, "TEST"))
270 shutil.copy(xsltest, os.path.join(logDir, "TEST"))
271 shutil.copy(imgLogo, logDir)
273 # we are here if an user make sat log in jenkins LOGS without write rights
274 # Make a warning and do nothing
275 logger.warning("problem for writing in directory '%s', may be not owner." % logDir)
277 # If the last option is invoked, just, show the last log file
278 if options.last_compile:
279 src.check_config_has_application(runner.cfg)
280 log_dirs = os.listdir(os.path.join(runner.cfg.APPLICATION.workdir, 'LOGS'))
281 show_last_logs(logger, runner.cfg, log_dirs)
284 # If the last option is invoked, just, show the last log file
286 lastLogFilePath = get_last_log_file(logDir,
287 notShownCommands + ["config"])
289 # Show the log corresponding to the selected command call
290 print_log_command_in_terminal(lastLogFilePath, logger)
292 # open the log xml file in the user editor
293 src.system.show_in_editor(runner.cfg.USER.browser,
294 lastLogFilePath, logger)
297 # If the user asks for a terminal display
299 # Parse the log directory in order to find
300 # all the files corresponding to the commands
301 lLogs = src.logger.list_log_file(logDir,
302 src.logger.log_macro_command_file_expression)
304 for filePath, __, date, __, hour, cmd, __ in lLogs:
305 showLog, cmdAppli, __ = src.logger.show_command_log(filePath, cmd,
306 runner.cfg.VARS.application, notShownCommands)
308 lLogsFiltered.append((filePath, date, hour, cmd, cmdAppli))
310 lLogsFiltered = sorted(lLogsFiltered)
311 nb_logs = len(lLogsFiltered)
313 # loop on all files and print it with date, time and command name
314 for __, date, hour, cmd, cmdAppli in lLogsFiltered:
315 num = src.printcolors.printcLabel("%2d" % (nb_logs - index))
316 logger.write("%s: %13s %s %s %s\n" %
317 (num, cmd, date, hour, cmdAppli), 1, False)
320 # ask the user what for what command he wants to be displayed
323 x = ask_value(nb_logs)
325 index = len(lLogsFiltered) - int(x)
326 # Show the log corresponding to the selected command call
327 print_log_command_in_terminal(lLogsFiltered[index][0], logger)
332 # Create or update the hat xml that gives access to all the commands log files
333 logger.write(_("Generating the hat log file (can be long) ... "), 3)
334 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
336 src.logger.update_hat_xml(logDir,
337 application = runner.cfg.VARS.application,
338 notShownCommands = notShownCommands)
340 logger.write(src.printcolors.printc("OK"), 3)
342 logger.write(src.printcolors.printc("KO"), 3)
343 logger.write(" problem update hat.xml", 3)
345 logger.write("\n", 3)
347 # open the hat xml in the user editor
348 if not options.no_browser:
349 logger.write(_("\nOpening the hat log file %s\n" % xmlHatFilePath), 3)
350 src.system.show_in_webbrowser(runner.cfg.USER.browser, xmlHatFilePath, logger)
352 logger.write("\nHat log File is %s\n" % xmlHatFilePath, 3)