Salome HOME
sat jobs: use the --logs_paths_in_file option to get the remote log files
[tools/sat.git] / commands / job.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
21 import src
22
23 # Define all possible option for the make command :  sat make <options>
24 parser = src.options.Options()
25 parser.add_option('j', 'jobs_config', 'string', 'jobs_cfg', 
26                   _('The name of the config file that contains'
27                   ' the jobs configuration'))
28 parser.add_option('', 'job', 'string', 'job',
29     _('The job name from which to execute commands.'), "")
30
31 def description():
32     '''method that is called when salomeTools is called with --help option.
33     
34     :return: The text to display for the job command description.
35     :rtype: str
36     '''
37     return _("Executes the commands of the job defined"
38              " in the jobs configuration file")
39   
40 def run(args, runner, logger):
41     '''method that is called when salomeTools is called with job parameter.
42     '''
43     
44     # Parse the options
45     (options, args) = parser.parse_args(args)
46       
47     jobs_cfg_files_dir = runner.cfg.SITE.jobs.config_path
48     
49     l_cfg_dir = [jobs_cfg_files_dir, os.path.join(runner.cfg.VARS.datadir, "jobs")]
50     
51     # Make sure the path to the jobs config files directory exists 
52     src.ensure_path_exists(jobs_cfg_files_dir)   
53     
54     # Make sure the jobs_config option has been called
55     if not options.jobs_cfg:
56         message = _("The option --jobs_config is required\n")      
57         raise src.SatException( message )
58     
59     # Find the file in the directories
60     found = False
61     for cfg_dir in l_cfg_dir:
62         file_jobs_cfg = os.path.join(cfg_dir, options.jobs_cfg)
63         if not file_jobs_cfg.endswith('.pyconf'):
64             file_jobs_cfg += '.pyconf'
65         
66         if not os.path.exists(file_jobs_cfg):
67             continue
68         else:
69             found = True
70             break
71     
72     if not found:
73         msg = _("The file configuration %(name_file)s was not found."
74                 "\nUse the --list option to get the possible files.")
75         src.printcolors.printcError(msg)
76         return 1
77     
78     info = [
79     (_("Platform"), runner.cfg.VARS.dist),
80     (_("File containing the jobs configuration"), file_jobs_cfg)
81     ]
82     src.print_info(logger, info)
83     
84     # Read the config that is in the file
85     config_jobs = src.read_config_from_a_file(file_jobs_cfg)
86     
87     # Find the job and its commands
88     found = False
89     for job in config_jobs.jobs:
90         if job.name == options.job:
91             commands = job.commands
92             found = True
93             break
94     if not found:
95         msg = _("Impossible to find the job \"%(job_name)s\" in "
96                 "%(jobs_config_file)s" % {"job_name" : options.job,
97                                           "jobs_config_file" : file_jobs_cfg})
98         logger.write(src.printcolors.printcError(msg) + "\n")
99         return 1
100     
101     # Find the maximum length of the commands in order to format the display
102     len_max_command = max([len(cmd) for cmd in commands])
103     
104     # Loop over the commands and execute it
105     res = 0
106     nb_pass = 0
107     for command in commands:
108         # Determine if it is a sat command or a shell command
109         cmd_exe = command.split(" ")[0] # first part
110         if cmd_exe == "sat":
111             sat_command_name = command.split(" ")[1]
112             end_cmd = command.replace(cmd_exe + " " + sat_command_name, "")
113         else:
114             sat_command_name = "shell"
115             end_cmd = "--command " + command
116         
117         # Get dynamically the command function to call 
118         sat_command = runner.__getattr__(sat_command_name)
119         logger.write("Executing " + 
120                      src.printcolors.printcLabel(command) + " ", 3)
121         logger.write("." * (len_max_command - len(command)) + " ", 3)
122         logger.flush()
123         # Execute the command
124         code = sat_command(end_cmd,
125                                      batch = True,
126                                      verbose = 0,
127                                      logger_add_link = logger)
128         # Print the status of the command
129         if code == 0:
130             nb_pass += 1
131             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
132         else:
133             res = 1
134             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
135     
136     # Print the final state
137     if res == 0:
138         final_status = "OK"
139     else:
140         final_status = "KO"
141    
142     logger.write(_("\nCommands: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
143         { 'status': src.printcolors.printc(final_status), 
144           'valid_result': nb_pass,
145           'nb_products': len(commands) }, 3)
146     
147     return res