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