Salome HOME
add sat log on http.server 8765
[tools/sat.git] / commands / log.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20 import shutil
21 import re
22 import glob
23 import datetime
24 import stat
25
26 # Compatibility python 2/3 for input function
27 # input stays input for python 3 and input = raw_input for python 2
28 try: 
29     input = raw_input
30 except NameError: 
31     pass
32
33 import src
34
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.")
49
50 def get_last_log_file(logDir, notShownCommands):
51     '''Used in case of last option. Get the last log command file path.
52     
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
56     :rtype: str
57     '''
58     last = (_, 0)
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:
69                 continue
70             if int(datehour) > last[1]:
71                 last = (fileName, int(datehour))
72     return os.path.join(logDir, last[0])
73
74 def remove_log_file(filePath, logger):
75     '''if it exists, print a warning and remove the input file
76     
77     :param filePath: the path of the file to delete
78     :param logger Logger: the logger instance to use for the print 
79     '''
80     if os.path.exists(filePath):
81         logger.write(src.printcolors.printcWarning("Removing ")
82                      + filePath + "\n", 5)
83         os.remove(filePath)
84
85 def print_log_command_in_terminal(filePath, logger):
86     '''Print the contain of filePath. It contains a command log in xml format.
87     
88     :param filePath: The command xml file from which extract the commands 
89                      context and traces
90     :param logger Logger: the logging instance to use in order to print.  
91     '''
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
98     lAttrText = []
99     for attrib in dAttrText:
100         lAttrText.append((attrib, dAttrText[attrib]))
101     logger.write("\n", 1)
102     src.print_info(logger, lAttrText)
103     # Get the traces
104     command_traces = xmlRead.get_node_text('Log')
105     # Print it if there is any
106     if command_traces:
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)
111
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)
116     # list the logs
117     nb = len(log_dirs)
118     nb_cols = 4
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
123             if k < nb:
124                 l = sorted_log_dirs[k]
125                 str_indice = src.printcolors.printcLabel("%2d" % (k+1))
126                 log_name = l
127                 logger.write("%s: %-30s" % (str_indice, log_name), 1, False)
128         logger.write("\n", 1, False)
129
130     # loop till exit
131     x = -1
132     while (x < 0):
133         x = ask_value(nb)
134         if x > 0:
135             product_log_dir = os.path.join(log_dir, sorted_log_dirs[x-1])
136             show_product_last_logs(logger, config, product_log_dir)
137
138 def show_product_last_logs(logger, config, product_log_dir):
139     """Show last compilation logs of a product"""
140     # sort the files chronologically
141     l_time_file = []
142     for file_n in os.listdir(product_log_dir):
143         my_stat = os.stat(os.path.join(product_log_dir, file_n))
144         l_time_file.append(
145               (datetime.datetime.fromtimestamp(my_stat[stat.ST_MTIME]), file_n))
146     
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))
150         opt = []
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])))
153         
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)
157         
158     # loop till exit
159     x = -1
160     while (x < 0):
161         x = ask_value(len(l_time_file))
162         if x > 0:
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)
166         
167 def ask_value(nb):
168     '''Ask for an int n. 0<n<nb
169     
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
172     :rtype: int
173     '''
174     try:
175         # ask for a value
176         rep = input(_("Which one (enter or 0 to quit)? "))
177         # Verify it is on the right range
178         if len(rep) == 0:
179             x = 0
180         else:
181             x = int(rep)
182             if x > nb:
183                 x = -1
184     except:
185         x = -1
186     
187     return x
188
189 def description():
190     '''method that is called when salomeTools is called with --help option.
191     
192     :return: The text to display for the log command description.
193     :rtype: str
194     '''
195     return _("""\
196 The log command gives access to the logs produced by the salomeTools commands.
197
198 example:
199 >> sat log
200 """)
201
202 def run(args, runner, logger):
203     '''method that is called when salomeTools is called with log parameter.
204     '''
205     # Parse the options
206     (options, args) = parser.parse_args(args)
207
208     # get the log directory.
209     logDir = src.get_log_path(runner.cfg)
210
211     # Print a header
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)
216     
217     # If the clean options is invoked, 
218     # do nothing but deleting the concerned files.
219     if options.clean:
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:
227             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), 
235                             'OUT', 
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), 
241                             'OUT', 
242                             os.path.basename(filePath)[:-len('.xml')] + '.pyconf')
243             remove_log_file(pyconfFilePath, logger)
244
245         
246         logger.write(src.printcolors.printcSuccess("OK\n"))
247         logger.write("%i logs deleted.\n" % nbClean)
248         return 0 
249
250     # determine the commands to show in the hat log
251     notShownCommands = list(runner.cfg.INTERNAL.log.not_shown_commands)
252     if options.full:
253         notShownCommands = []
254
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")
261     
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
265     try:
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)
272     except:
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)
276
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)
282         return 0
283
284     # If the last option is invoked, just, show the last log file
285     if options.last:
286         lastLogFilePath = get_last_log_file(logDir,
287                                             notShownCommands + ["config"])
288         if options.terminal:
289             # Show the log corresponding to the selected command call
290             print_log_command_in_terminal(lastLogFilePath, logger)
291         else:
292             # open the log xml file in the user editor
293             src.system.show_in_editor(runner.cfg.USER.browser, 
294                                       lastLogFilePath, logger)
295         return 0
296
297     # If the user asks for a terminal display
298     if options.terminal:
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)
303         lLogsFiltered = []
304         for filePath, __, date, __, hour, cmd, __ in lLogs:
305             showLog, cmdAppli, __ = src.logger.show_command_log(filePath, cmd, 
306                                 runner.cfg.VARS.application, notShownCommands)
307             if showLog:
308                 lLogsFiltered.append((filePath, date, hour, cmd, cmdAppli))
309             
310         lLogsFiltered = sorted(lLogsFiltered)
311         nb_logs = len(lLogsFiltered)
312         index = 0
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)
318             index += 1
319         
320         # ask the user what for what command he wants to be displayed
321         x = -1
322         while (x < 0):
323             x = ask_value(nb_logs)
324             if x > 0:
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)                
328                 x = 0
329         
330         return 0
331                     
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')
335     try:
336       src.logger.update_hat_xml(logDir,
337                               application = runner.cfg.VARS.application, 
338                               notShownCommands = notShownCommands)
339
340       logger.write(src.printcolors.printc("OK"), 3)
341     except:
342       logger.write(src.printcolors.printc("KO"), 3)
343       logger.write(" problem update hat.xml", 3)
344
345     logger.write("\n", 3)
346     
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)
351     else:
352         logger.write("\nHat log File is %s\n" % xmlHatFilePath, 3)
353     return 0