Salome HOME
#8577 extension du domaine des properties
[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 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 '
32       'used.'), 0)
33
34
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)
39     logger.flush()
40
41 def log_res_step(logger, res):
42     if res == 0:
43         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
44         logger.flush()
45     else:
46         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
47         logger.flush()
48
49 def run_script_all_products(config, products_infos, nb_proc, logger):
50     '''Execute the script in each product build directory.
51
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.
58     :rtype: int
59     '''
60     res = 0
61     for p_name_info in products_infos:
62         res_prod = run_script_of_product(p_name_info,
63                                       nb_proc,
64                                       config,
65                                       logger)
66         if res_prod != 0:
67             res += 1 
68     return res
69
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.
73     
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 
78                           and logging
79     :return: 1 if it fails, else 0.
80     :rtype: int
81     '''
82     
83     p_name, p_info = p_name_info
84     
85     # Logging
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)
92     logger.flush()
93
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)
100         return 0
101
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)
105     
106     # Prepare the environment
107     log_step(logger, header, "PREPARE ENV")
108     res_prepare = builder.prepare()
109     log_res_step(logger, res_prepare)
110     
111     # Execute the script
112     len_end_line = 20
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)
118     
119     # Log the result
120     if res > 0:
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)
125         logger.flush()
126     else:
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)
132         logger.flush()
133     logger.write("\n", 3, False)
134
135     return res
136
137 def description():
138     '''method that is called when salomeTools is called with --help option.
139     
140     :return: The text to display for the script command description.
141     :rtype: str
142     '''
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")
148   
149 def run(args, runner, logger):
150     '''method that is called when salomeTools is called with make parameter.
151     '''
152     
153     # Parse the options
154     (options, args) = parser.parse_args(args)
155
156     # check that the command has been called with an application
157     src.check_config_has_application( runner.cfg )
158
159     # Get the list of products to treat
160     products_infos = src.product.get_products_list(options, runner.cfg, logger)
161     
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)
166     
167     info = [(_("BUILD directory"),
168              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
169     src.print_info(logger, info)
170     
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:
174         options.nb_proc = 0
175     res = run_script_all_products(runner.cfg,
176                                   products_infos,
177                                   options.nb_proc,
178                                   logger)
179     
180     # Print the final state
181     nb_products = len(products_infos)
182     if res == 0:
183         final_status = "OK"
184     else:
185         final_status = "KO"
186    
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)    
192     
193     return res