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
26 # Define all possible option for the make command : sat make <options>
27 parser = src.options.Options()
28 parser.add_option('j', 'jobs_config', 'string', 'jobs_cfg',
29 _('Mandatory: The name of the config file that contains'
30 ' the jobs configuration'))
31 parser.add_option('', 'name', 'string', 'job',
32 _('Mandatory: The job name from which to execute commands.'), "")
35 '''method that is called when salomeTools is called with --help option.
37 :return: The text to display for the job command description.
40 return _("Executes the commands of the job defined"
41 " in the jobs configuration file\n\nexample:\nsat job "
42 "--jobs_config my_jobs --name my_job")
44 def run(args, runner, logger):
45 '''method that is called when salomeTools is called with job parameter.
49 (options, args) = parser.parse_args(args)
51 l_cfg_dir = runner.cfg.PATHS.JOBPATH
53 # Make sure the jobs_config option has been called
54 if not options.jobs_cfg:
55 message = _("The option --jobs_config is required\n")
56 logger.write(src.printcolors.printcError(message))
59 # Make sure the name option has been called
61 message = _("The option --name is required\n")
62 logger.write(src.printcolors.printcError(message))
65 # Find the file in the directories
67 for cfg_dir in l_cfg_dir:
68 file_jobs_cfg = os.path.join(cfg_dir, options.jobs_cfg)
69 if not file_jobs_cfg.endswith('.pyconf'):
70 file_jobs_cfg += '.pyconf'
72 if not os.path.exists(file_jobs_cfg):
79 msg = _("The file configuration %(name_file)s was not found."
80 "\nUse the --list option to get the possible files.")
81 src.printcolors.printcError(msg)
85 (_("Platform"), runner.cfg.VARS.dist),
86 (_("File containing the jobs configuration"), file_jobs_cfg)
88 src.print_info(logger, info)
90 # Read the config that is in the file
91 config_jobs = src.read_config_from_a_file(file_jobs_cfg)
93 # Find the job and its commands
95 for job in config_jobs.jobs:
96 if job.name == options.job:
97 commands = job.commands
101 msg = _("Impossible to find the job \"%(job_name)s\" in "
102 "%(jobs_config_file)s" % {"job_name" : options.job,
103 "jobs_config_file" : file_jobs_cfg})
104 logger.write(src.printcolors.printcError(msg) + "\n")
107 # Find the maximum length of the commands in order to format the display
108 len_max_command = max([len(cmd) for cmd in commands])
110 # Loop over the commands and execute it
113 for command in commands:
114 # Determine if it is a sat command or a shell command
115 cmd_exe = command.split(" ")[0] # first part
117 sat_command_name = command.split(" ")[1]
118 end_cmd = command.replace(cmd_exe + " " + sat_command_name, "")
120 sat_command_name = "shell"
121 end_cmd = "--command " + command
123 # Get dynamically the command function to call
124 sat_command = runner.__getattr__(sat_command_name)
125 logger.write("Executing " +
126 src.printcolors.printcLabel(command) + " ", 3)
127 logger.write("." * (len_max_command - len(command)) + " ", 3)
133 # Execute the command
134 code = sat_command(end_cmd,
137 logger_add_link = logger)
138 except Exception as e:
143 __, __, exc_traceback = sys.exc_info()
144 fp = tempfile.TemporaryFile()
145 traceback.print_tb(exc_traceback, file=fp)
148 logger.add_link(_("Dead Link"),
151 "ERROR: %s TRACEBACK: %s" % (error,
152 stack.replace('"',"'")))
154 # Print the status of the command
157 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
160 logger.write('%s %s\n' % (src.printcolors.printc(src.KO_STATUS),
163 logger.write('stack: %s\n' % stack, 3)
165 # Print the final state
171 logger.write(_("\nCommands: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
172 { 'status': src.printcolors.printc(final_status),
173 'valid_result': nb_pass,
174 'nb_products': len(commands) }, 3)