Salome HOME
sat job: add a red broken link when a command crashs
[tools/sat.git] / commands / job.py
index ca56a0195218cb9d4ebbb4a65d90a5c5dadf62fb..b3aa9533ec5dae5738b96c57a5f77a323aae531e 100644 (file)
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
 import os
+import sys
+import traceback
+import tempfile
 
 import src
 
 # Define all possible option for the make command :  sat make <options>
 parser = src.options.Options()
 parser.add_option('j', 'jobs_config', 'string', 'jobs_cfg', 
-                  _('The name of the config file that contains'
+                  _('Mandatory: The name of the config file that contains'
                   ' the jobs configuration'))
 parser.add_option('', 'name', 'string', 'job',
-    _('The job name from which to execute commands.'), "")
+    _('Mandatory: The job name from which to execute commands.'), "")
 
 def description():
     '''method that is called when salomeTools is called with --help option.
@@ -35,7 +38,8 @@ def description():
     :rtype: str
     '''
     return _("Executes the commands of the job defined"
-             " in the jobs configuration file")
+             " in the jobs configuration file\n\nexample:\nsat job "
+             "--jobs_config my_jobs --name my_job")
   
 def run(args, runner, logger):
     '''method that is called when salomeTools is called with job parameter.
@@ -43,13 +47,8 @@ def run(args, runner, logger):
     
     # Parse the options
     (options, args) = parser.parse_args(args)
-      
-    jobs_cfg_files_dir = runner.cfg.SITE.jobs.config_path
-    
-    l_cfg_dir = [jobs_cfg_files_dir, os.path.join(runner.cfg.VARS.datadir, "jobs")]
-    
-    # Make sure the path to the jobs config files directory exists 
-    src.ensure_path_exists(jobs_cfg_files_dir)   
+         
+    l_cfg_dir = runner.cfg.PATHS.JOBPATH
     
     # Make sure the jobs_config option has been called
     if not options.jobs_cfg:
@@ -57,6 +56,12 @@ def run(args, runner, logger):
         logger.write(src.printcolors.printcError(message))
         return 1
     
+    # Make sure the name option has been called
+    if not options.job:
+        message = _("The option --name is required\n")      
+        logger.write(src.printcolors.printcError(message))
+        return 1
+    
     # Find the file in the directories
     found = False
     for cfg_dir in l_cfg_dir:
@@ -121,18 +126,41 @@ def run(args, runner, logger):
                      src.printcolors.printcLabel(command) + " ", 3)
         logger.write("." * (len_max_command - len(command)) + " ", 3)
         logger.flush()
-        # Execute the command
-        code = sat_command(end_cmd,
-                                     batch = True,
-                                     verbose = 0,
-                                     logger_add_link = logger)
+        
+        error = ""
+        stack = ""
+        try:
+            # Execute the command
+            code = sat_command(end_cmd,
+                               batch = True,
+                               verbose = 0,
+                               logger_add_link = logger)
+        except Exception as e:
+            code = 1
+            # Get error
+            error = str(e)
+            # get stack
+            __, __, exc_traceback = sys.exc_info()
+            fp = tempfile.TemporaryFile()
+            traceback.print_tb(exc_traceback, file=fp)
+            fp.seek(0)
+            stack = fp.read()
+            logger.add_link(_("Dead Link"),
+                            sat_command_name,
+                            code,
+                            "ERROR: %s TRACEBACK: %s" % (error,
+                                                    stack.replace('"',"'")))
+            
         # Print the status of the command
         if code == 0:
             nb_pass += 1
             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
         else:
             res = 1
-            logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
+            logger.write('%s %s\n' % (src.printcolors.printc(src.KO_STATUS),
+                                      error), 3)
+            if len(stack) > 0:
+                logger.write('stack: %s\n' % stack, 3)
     
     # Print the final state
     if res == 0: