Salome HOME
get_salome_version returns integer, TODO use MajorMinorPatch
[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     # Instantiate the class that manages all the construction commands
101     # like cmake, make, make install, make test, environment management, etc...
102     builder = src.compilation.Builder(config, logger, p_info)
103     
104     # Prepare the environment
105     log_step(logger, header, "PREPARE ENV")
106     res_prepare = builder.prepare()
107     log_res_step(logger, res_prepare)
108     
109     # Execute the script
110     len_end_line = 20
111     script_path_display = src.printcolors.printcLabel(p_info.compil_script)
112     log_step(logger, header, "SCRIPT " + script_path_display)
113     len_end_line += len(script_path_display)
114     res = builder.do_script_build(p_info.compil_script, number_of_proc=nb_proc)
115     log_res_step(logger, res)
116     
117     # Log the result
118     if res > 0:
119         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
120         logger.write("\r" + header + src.printcolors.printcError("KO"))
121         logger.write("==== %(KO)s in script execution of %(name)s \n" %
122             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
123         logger.flush()
124     else:
125         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
126         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
127         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
128         logger.write("==== Script execution of %(name)s %(OK)s \n" %
129             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
130         logger.flush()
131     logger.write("\n", 3, False)
132
133     return res
134
135 def description():
136     '''method that is called when salomeTools is called with --help option.
137     
138     :return: The text to display for the script command description.
139     :rtype: str
140     '''
141     return _("The script command executes the script(s) of the the given "
142              "products in the build directory.\nThis is done only for the "
143              "products that are constructed using a script (build_source "
144              ": \"script\").\nOtherwise, nothing is done."
145              "\n\nexample:\nsat script SALOME-master --products Python,numpy")
146   
147 def run(args, runner, logger):
148     '''method that is called when salomeTools is called with make parameter.
149     '''
150     
151     # Parse the options
152     (options, args) = parser.parse_args(args)
153
154     # check that the command has been called with an application
155     src.check_config_has_application( runner.cfg )
156
157     # Get the list of products to treat
158     products_infos = src.product.get_products_list(options, runner.cfg, logger)
159     
160     # Print some informations
161     logger.write(_('Executing the script in the build '
162                                 'directories of the application %s\n') % 
163                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
164     
165     info = [(_("BUILD directory"),
166              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
167     src.print_info(logger, info)
168     
169     # Call the function that will loop over all the products and execute
170     # the right command(s)
171     if options.nb_proc is None:
172         options.nb_proc = 0
173     res = run_script_all_products(runner.cfg,
174                                   products_infos,
175                                   options.nb_proc,
176                                   logger)
177     
178     # Print the final state
179     nb_products = len(products_infos)
180     if res == 0:
181         final_status = "OK"
182     else:
183         final_status = "KO"
184    
185     logger.write(_("\nScript: %(status)s "
186                    "(%(valid_result)d/%(nb_products)d)\n") % \
187         { 'status': src.printcolors.printc(final_status), 
188           'valid_result': nb_products - res,
189           'nb_products': nb_products }, 1)    
190     
191     return res