Salome HOME
decode pip version
[tools/sat.git] / commands / script.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 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 accepts a comma separated list.'))
27 parser.add_option('', 'nb_proc', 'int', 'nb_proc',
28     _("""Optional: The number of processors to use in the script if the make command is used in it.
29       Warning: the script has to be correctly written if you want this option to work.
30       The $MAKE_OPTIONS has to be used."""), 0)
31
32
33 def log_step(logger, header, step):
34     logger.write("\r%s%s" % (header, " " * 20), 3)
35     logger.write("\r%s%s" % (header, step), 3)
36     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
37     logger.flush()
38
39 def log_res_step(logger, res):
40     if res == 0:
41         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
42         logger.flush()
43     else:
44         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
45         logger.flush()
46
47 def run_script_all_products(config, products_infos, nb_proc, logger):
48     '''Execute the script in each product build directory.
49
50     :param config Config: The global configuration
51     :param products_info list: List of 
52                                  (str, Config) => (product_name, product_info)
53     :param nb_proc int: The number of processors to use
54     :param logger Logger: The logger instance to use for the display and logging
55     :return: the number of failing commands.
56     :rtype: int
57     '''
58     res = 0
59     for p_name_info in products_infos:
60         res_prod = run_script_of_product(p_name_info,
61                                       nb_proc,
62                                       config,
63                                       logger)
64         if res_prod != 0:
65             res += 1 
66     return res
67
68 def run_script_of_product(p_name_info, nb_proc, config, logger):
69     '''Execute the proper configuration command(s) 
70        in the product build directory.
71     
72     :param p_name_info tuple: (str, Config) => (product_name, product_info)
73     :param nb_proc int: The number of processors to use
74     :param config Config: The global configuration
75     :param logger Logger: The logger instance to use for the display 
76                           and logging
77     :return: 1 if it fails, else 0.
78     :rtype: int
79     '''
80     
81     p_name, p_info = p_name_info
82     
83     # Logging
84     logger.write("\n", 4, False)
85     logger.write("################ ", 4)
86     header = _("Running script of %s") % src.printcolors.printcLabel(p_name)
87     header += " %s " % ("." * (20 - len(p_name)))
88     logger.write(header, 3)
89     logger.write("\n", 4, False)
90     logger.flush()
91
92     # Do nothing if he product is not compilable or has no compilation script
93     if (("properties" in p_info and "compilation" in p_info.properties and 
94                                   p_info.properties.compilation == "no") or
95                                   (not src.product.product_has_script(p_info))):
96         log_step(logger, header, "ignored")
97         logger.write("\n", 3, False)
98         return 0
99
100     if not os.path.isfile(p_info.compil_script):
101         msg_err="\n\nError : The compilation script file do not exists!"+\
102                 "\n        It was not found by sat!"+\
103                 "\n        Please check your salomeTool configuration\n"
104         logger.error(msg_err)
105         return 1
106
107     # Instantiate the class that manages all the construction commands
108     # like cmake, make, make install, make test, environment management, etc...
109     builder = src.compilation.Builder(config, logger, p_name, p_info)
110     
111     # Prepare the environment
112     log_step(logger, header, "PREPARE ENV")
113     res_prepare = builder.prepare()
114     log_res_step(logger, res_prepare)
115     
116     # Execute the script
117     len_end_line = 20
118     script_path_display = src.printcolors.printcLabel(p_info.compil_script)
119     log_step(logger, header, "SCRIPT " + script_path_display)
120     len_end_line += len(script_path_display)
121     res = builder.do_script_build(p_info.compil_script, number_of_proc=nb_proc)
122     log_res_step(logger, res)
123     
124     # Log the result
125     if res > 0:
126         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
127         logger.write("\r" + header + src.printcolors.printcError("KO"))
128         logger.write("==== %(KO)s in script execution of %(name)s \n" %
129             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
130         logger.write("\n", 3, False)
131         logger.flush()
132         return res
133
134     if src.product.product_has_post_script(p_info):
135         # the product has a post install script we run
136         #script_path_display = src.printcolors.printcLabel(p_info.p_info.post_script)
137         #log_step(logger, header, "POST SCRIPT " + script_path_display)
138         res = builder.do_script_build(p_info.post_script)
139         #log_res_step(logger, res)
140         if res > 0:
141             logger.write("\r%s%s" % (header, " " * len_end_line), 3)
142             logger.write("\r" + header + src.printcolors.printcError("KO"))
143             logger.write("==== %(KO)s in post script execution of %(name)s \n" %
144                 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
145             logger.write("\n", 3, False)
146             logger.flush()
147             return res
148     
149     logger.write("\r%s%s" % (header, " " * len_end_line), 3)
150     logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
151     logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
152     logger.write("==== Script execution of %(name)s %(OK)s \n" %
153         { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
154     logger.write("\n", 3, False)
155     logger.flush()
156
157     return res
158
159 def description():
160     '''method that is called when salomeTools is called with --help option.
161     
162     :return: The text to display for the script command description.
163     :rtype: str
164     '''
165     return _("The script command executes the script(s) of the the given "
166              "products in the build directory.\nThis is done only for the "
167              "products that are constructed using a script (build_source "
168              ": \"script\").\nOtherwise, nothing is done."
169              "\n\nexample:\nsat script SALOME-master --products Python,numpy")
170   
171 def run(args, runner, logger):
172     '''method that is called when salomeTools is called with make parameter.
173     '''
174     
175     # Parse the options
176     (options, args) = parser.parse_args(args)
177
178     # check that the command has been called with an application
179     src.check_config_has_application( runner.cfg )
180
181     # Get the list of products to treat
182     products_infos = src.product.get_products_list(options, runner.cfg, logger)
183     products_infos = [pi for pi in products_infos if not(
184                                      src.product.product_is_native(pi[1]) or 
185                                      src.product.product_is_fixed(pi[1]))]
186     
187     
188     # Print some informations
189     logger.write(_('Executing the script in the build '
190                                 'directories of the application %s\n') % 
191                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
192     
193     info = [(_("BUILD directory"),
194              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
195     src.print_info(logger, info)
196     
197     # Call the function that will loop over all the products and execute
198     # the right command(s)
199     if options.nb_proc is None:
200         options.nb_proc = 0
201     res = run_script_all_products(runner.cfg,
202                                   products_infos,
203                                   options.nb_proc,
204                                   logger)
205     
206     # Print the final state
207     nb_products = len(products_infos)
208     if res == 0:
209         final_status = "OK"
210     else:
211         final_status = "KO"
212    
213     logger.write(_("\nScript: %(status)s "
214                    "(%(valid_result)d/%(nb_products)d)\n") % \
215         { 'status': src.printcolors.printc(final_status), 
216           'valid_result': nb_products - res,
217           'nb_products': nb_products }, 1)    
218     
219     return res