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 script command : sat script <options>
24 parser = src.options.Options()
25 parser.add_option('p', 'products', 'list2', 'products',
26 _('Optional: products to configure. This option can be'
27 ' passed several time to configure several products.'))
28 parser.add_option('', 'nb_proc', 'int', 'nb_proc',
29 _('Optional: The number of processors to use in the script if the make '
30 'command is used in it.\n\tWarning: the script has to be correctly written '
31 'if you want this option to work.\n\tThe $MAKE_OPTIONS has to be '
35 def log_step(logger, header, step):
36 logger.write("\r%s%s" % (header, " " * 20), 3)
37 logger.write("\r%s%s" % (header, step), 3)
38 logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
41 def log_res_step(logger, res):
43 logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
46 logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
49 def run_script_all_products(config, products_infos, nb_proc, logger):
50 '''Execute the script in each product build directory.
52 :param config Config: The global configuration
53 :param products_info list: List of
54 (str, Config) => (product_name, product_info)
55 :param nb_proc int: The number of processors to use
56 :param logger Logger: The logger instance to use for the display and logging
57 :return: the number of failing commands.
61 for p_name_info in products_infos:
62 res_prod = run_script_of_product(p_name_info,
70 def run_script_of_product(p_name_info, nb_proc, config, logger):
71 '''Execute the proper configuration command(s)
72 in the product build directory.
74 :param p_name_info tuple: (str, Config) => (product_name, product_info)
75 :param nb_proc int: The number of processors to use
76 :param config Config: The global configuration
77 :param logger Logger: The logger instance to use for the display
79 :return: 1 if it fails, else 0.
83 p_name, p_info = p_name_info
86 logger.write("\n", 4, False)
87 logger.write("################ ", 4)
88 header = _("Running script of %s") % src.printcolors.printcLabel(p_name)
89 header += " %s " % ("." * (20 - len(p_name)))
90 logger.write(header, 3)
91 logger.write("\n", 4, False)
94 # Do nothing if he product is not compilable or has no compilation script
95 if (("properties" in p_info and "compilation" in p_info.properties and
96 p_info.properties.compilation == "no") or
97 (not src.product.product_has_script(p_info))):
98 log_step(logger, header, "ignored")
99 logger.write("\n", 3, False)
102 # Instantiate the class that manages all the construction commands
103 # like cmake, make, make install, make test, environment management, etc...
104 builder = src.compilation.Builder(config, logger, p_info)
106 # Prepare the environment
107 log_step(logger, header, "PREPARE ENV")
108 res_prepare = builder.prepare()
109 log_res_step(logger, res_prepare)
113 script_path_display = src.printcolors.printcLabel(p_info.compil_script)
114 log_step(logger, header, "SCRIPT " + script_path_display)
115 len_end_line += len(script_path_display)
116 res = builder.do_script_build(p_info.compil_script, number_of_proc=nb_proc)
117 log_res_step(logger, res)
121 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
122 logger.write("\r" + header + src.printcolors.printcError("KO"))
123 logger.write("==== %(KO)s in script execution of %(name)s \n" %
124 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
127 logger.write("\r%s%s" % (header, " " * len_end_line), 3)
128 logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
129 logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
130 logger.write("==== Script execution of %(name)s %(OK)s \n" %
131 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
133 logger.write("\n", 3, False)
138 '''method that is called when salomeTools is called with --help option.
140 :return: The text to display for the script command description.
143 return _("The script command executes the script(s) of the the given "
144 "products in the build directory.\nThis is done only for the "
145 "products that are constructed using a script (build_source "
146 ": \"script\").\nOtherwise, nothing is done."
147 "\n\nexample:\nsat script SALOME-master --products Python,numpy")
149 def run(args, runner, logger):
150 '''method that is called when salomeTools is called with make parameter.
154 (options, args) = parser.parse_args(args)
156 # check that the command has been called with an application
157 src.check_config_has_application( runner.cfg )
159 # Get the list of products to treat
160 products_infos = src.product.get_products_list(options, runner.cfg, logger)
162 # Print some informations
163 logger.write(_('Executing the script in the build '
164 'directories of the application %s\n') %
165 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
167 info = [(_("BUILD directory"),
168 os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
169 src.print_info(logger, info)
171 # Call the function that will loop over all the products and execute
172 # the right command(s)
173 if options.nb_proc is None:
175 res = run_script_all_products(runner.cfg,
180 # Print the final state
181 nb_products = len(products_infos)
187 logger.write(_("\nScript: %(status)s "
188 "(%(valid_result)d/%(nb_products)d)\n") % \
189 { 'status': src.printcolors.printc(final_status),
190 'valid_result': nb_products - res,
191 'nb_products': nb_products }, 1)