3 # Copyright (C) 2010-2012 CEA/DEN
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.
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.
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
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 _('Mandatory: The name of the config file that contains'
27 ' the jobs configuration'))
28 parser.add_option('', 'name', 'string', 'job',
29 _('Mandatory: The job name from which to execute commands.'), "")
32 '''method that is called when salomeTools is called with --help option.
34 :return: The text to display for the job command description.
37 return _("Executes the commands of the job defined"
38 " in the jobs configuration file\n\nexample:\nsat job "
39 "--jobs_config my_jobs --name my_job")
41 def run(args, runner, logger):
42 '''method that is called when salomeTools is called with job parameter.
46 (options, args) = parser.parse_args(args)
48 l_cfg_dir = runner.cfg.PATHS.JOBPATH
50 # Make sure the jobs_config option has been called
51 if not options.jobs_cfg:
52 message = _("The option --jobs_config is required\n")
53 logger.write(src.printcolors.printcError(message))
56 # Make sure the name option has been called
58 message = _("The option --name is required\n")
59 logger.write(src.printcolors.printcError(message))
62 # Find the file in the directories
64 for cfg_dir in l_cfg_dir:
65 file_jobs_cfg = os.path.join(cfg_dir, options.jobs_cfg)
66 if not file_jobs_cfg.endswith('.pyconf'):
67 file_jobs_cfg += '.pyconf'
69 if not os.path.exists(file_jobs_cfg):
76 msg = _("The file configuration %(name_file)s was not found."
77 "\nUse the --list option to get the possible files.")
78 src.printcolors.printcError(msg)
82 (_("Platform"), runner.cfg.VARS.dist),
83 (_("File containing the jobs configuration"), file_jobs_cfg)
85 src.print_info(logger, info)
87 # Read the config that is in the file
88 config_jobs = src.read_config_from_a_file(file_jobs_cfg)
90 # Find the job and its commands
92 for job in config_jobs.jobs:
93 if job.name == options.job:
94 commands = job.commands
98 msg = _("Impossible to find the job \"%(job_name)s\" in "
99 "%(jobs_config_file)s" % {"job_name" : options.job,
100 "jobs_config_file" : file_jobs_cfg})
101 logger.write(src.printcolors.printcError(msg) + "\n")
104 # Find the maximum length of the commands in order to format the display
105 len_max_command = max([len(cmd) for cmd in commands])
107 # Loop over the commands and execute it
110 for command in commands:
111 # Determine if it is a sat command or a shell command
112 cmd_exe = command.split(" ")[0] # first part
114 sat_command_name = command.split(" ")[1]
115 end_cmd = command.replace(cmd_exe + " " + sat_command_name, "")
117 sat_command_name = "shell"
118 end_cmd = "--command " + command
120 # Get dynamically the command function to call
121 sat_command = runner.__getattr__(sat_command_name)
122 logger.write("Executing " +
123 src.printcolors.printcLabel(command) + " ", 3)
124 logger.write("." * (len_max_command - len(command)) + " ", 3)
129 # Execute the command
130 code = sat_command(end_cmd,
133 logger_add_link = logger)
134 except Exception as e:
138 # Print the status of the command
141 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
144 logger.write('%s %s\n' % (src.printcolors.printc(src.KO_STATUS),
147 # Print the final state
153 logger.write(_("\nCommands: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
154 { 'status': src.printcolors.printc(final_status),
155 'valid_result': nb_pass,
156 'nb_products': len(commands) }, 3)