Salome HOME
ajout option d'impression des graphes de dépendance
[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.flush()
131     else:
132         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
133         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
134         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
135         logger.write("==== Script execution of %(name)s %(OK)s \n" %
136             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
137         logger.flush()
138     logger.write("\n", 3, False)
139
140     return res
141
142 def description():
143     '''method that is called when salomeTools is called with --help option.
144     
145     :return: The text to display for the script command description.
146     :rtype: str
147     '''
148     return _("The script command executes the script(s) of the the given "
149              "products in the build directory.\nThis is done only for the "
150              "products that are constructed using a script (build_source "
151              ": \"script\").\nOtherwise, nothing is done."
152              "\n\nexample:\nsat script SALOME-master --products Python,numpy")
153   
154 def run(args, runner, logger):
155     '''method that is called when salomeTools is called with make parameter.
156     '''
157     
158     # Parse the options
159     (options, args) = parser.parse_args(args)
160
161     # check that the command has been called with an application
162     src.check_config_has_application( runner.cfg )
163
164     # Get the list of products to treat
165     products_infos = src.product.get_products_list(options, runner.cfg, logger)
166     products_infos = [pi for pi in products_infos if not(
167                                      src.product.product_is_native(pi[1]) or 
168                                      src.product.product_is_fixed(pi[1]))]
169     
170     
171     # Print some informations
172     logger.write(_('Executing the script in the build '
173                                 'directories of the application %s\n') % 
174                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
175     
176     info = [(_("BUILD directory"),
177              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
178     src.print_info(logger, info)
179     
180     # Call the function that will loop over all the products and execute
181     # the right command(s)
182     if options.nb_proc is None:
183         options.nb_proc = 0
184     res = run_script_all_products(runner.cfg,
185                                   products_infos,
186                                   options.nb_proc,
187                                   logger)
188     
189     # Print the final state
190     nb_products = len(products_infos)
191     if res == 0:
192         final_status = "OK"
193     else:
194         final_status = "KO"
195    
196     logger.write(_("\nScript: %(status)s "
197                    "(%(valid_result)d/%(nb_products)d)\n") % \
198         { 'status': src.printcolors.printc(final_status), 
199           'valid_result': nb_products - res,
200           'nb_products': nb_products }, 1)    
201     
202     return res