]> SALOME platform Git repositories - tools/sat.git/blob - commands/job.py
Salome HOME
the legend is now collapsible
[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('', 'name', '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     l_cfg_dir = runner.cfg.PATHS.JOBPATH
48     
49     # Make sure the jobs_config option has been called
50     if not options.jobs_cfg:
51         message = _("The option --jobs_config is required\n")      
52         logger.write(src.printcolors.printcError(message))
53         return 1
54     
55     # Find the file in the directories
56     found = False
57     for cfg_dir in l_cfg_dir:
58         file_jobs_cfg = os.path.join(cfg_dir, options.jobs_cfg)
59         if not file_jobs_cfg.endswith('.pyconf'):
60             file_jobs_cfg += '.pyconf'
61         
62         if not os.path.exists(file_jobs_cfg):
63             continue
64         else:
65             found = True
66             break
67     
68     if not found:
69         msg = _("The file configuration %(name_file)s was not found."
70                 "\nUse the --list option to get the possible files.")
71         src.printcolors.printcError(msg)
72         return 1
73     
74     info = [
75     (_("Platform"), runner.cfg.VARS.dist),
76     (_("File containing the jobs configuration"), file_jobs_cfg)
77     ]
78     src.print_info(logger, info)
79     
80     # Read the config that is in the file
81     config_jobs = src.read_config_from_a_file(file_jobs_cfg)
82     
83     # Find the job and its commands
84     found = False
85     for job in config_jobs.jobs:
86         if job.name == options.job:
87             commands = job.commands
88             found = True
89             break
90     if not found:
91         msg = _("Impossible to find the job \"%(job_name)s\" in "
92                 "%(jobs_config_file)s" % {"job_name" : options.job,
93                                           "jobs_config_file" : file_jobs_cfg})
94         logger.write(src.printcolors.printcError(msg) + "\n")
95         return 1
96     
97     # Find the maximum length of the commands in order to format the display
98     len_max_command = max([len(cmd) for cmd in commands])
99     
100     # Loop over the commands and execute it
101     res = 0
102     nb_pass = 0
103     for command in commands:
104         # Determine if it is a sat command or a shell command
105         cmd_exe = command.split(" ")[0] # first part
106         if cmd_exe == "sat":
107             sat_command_name = command.split(" ")[1]
108             end_cmd = command.replace(cmd_exe + " " + sat_command_name, "")
109         else:
110             sat_command_name = "shell"
111             end_cmd = "--command " + command
112         
113         # Get dynamically the command function to call 
114         sat_command = runner.__getattr__(sat_command_name)
115         logger.write("Executing " + 
116                      src.printcolors.printcLabel(command) + " ", 3)
117         logger.write("." * (len_max_command - len(command)) + " ", 3)
118         logger.flush()
119         # Execute the command
120         code = sat_command(end_cmd,
121                            batch = True,
122                            verbose = 0,
123                            logger_add_link = logger)
124         # Print the status of the command
125         if code == 0:
126             nb_pass += 1
127             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
128         else:
129             res = 1
130             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
131     
132     # Print the final state
133     if res == 0:
134         final_status = "OK"
135     else:
136         final_status = "KO"
137    
138     logger.write(_("\nCommands: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
139         { 'status': src.printcolors.printc(final_status), 
140           'valid_result': nb_pass,
141           'nb_products': len(commands) }, 3)
142     
143     return res