Salome HOME
New command sait init. USER.workdir is replaced by LOCAL.workdir. Idem for USER.base...
[tools/sat.git] / commands / check.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 check command :  sat check <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
29 CHECK_PROPERTY = "has_unit_tests"
30
31 def get_products_list(options, cfg, logger):
32     '''method that gives the product list with their informations from 
33        configuration regarding the passed options.
34     
35     :param options Options: The Options instance that stores the commands 
36                             arguments
37     :param cfg Config: The global configuration
38     :param logger Logger: The logger instance to use for the display and 
39                           logging
40     :return: The list of (product name, product_informations).
41     :rtype: List
42     '''
43     # Get the products to be prepared, regarding the options
44     if options.products is None:
45         # No options, get all products sources
46         products = cfg.APPLICATION.products
47     else:
48         # if option --products, check that all products of the command line
49         # are present in the application.
50         products = options.products
51         for p in products:
52             if p not in cfg.APPLICATION.products:
53                 raise src.SatException(_("Product %(product)s "
54                             "not defined in application %(application)s") %
55                         { 'product': p, 'application': cfg.VARS.application} )
56     
57     # Construct the list of tuple containing 
58     # the products name and their definition
59     products_infos = src.product.get_products_infos(products, cfg)
60     
61     products_infos = [pi for pi in products_infos if not(
62                                      src.product.product_is_native(pi[1]) or 
63                                      src.product.product_is_fixed(pi[1]))]
64     
65     return products_infos
66
67 def log_step(logger, header, step):
68     logger.write("\r%s%s" % (header, " " * 20), 3)
69     logger.write("\r%s%s" % (header, step), 3)
70     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
71     logger.flush()
72
73 def log_res_step(logger, res):
74     if res == 0:
75         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
76         logger.flush()
77     else:
78         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
79         logger.flush()
80
81 def check_all_products(config, products_infos, logger):
82     '''Execute the proper configuration commands 
83        in each product build directory.
84
85     :param config Config: The global configuration
86     :param products_info list: List of 
87                                  (str, Config) => (product_name, product_info)
88     :param logger Logger: The logger instance to use for the display and logging
89     :return: the number of failing commands.
90     :rtype: int
91     '''
92     res = 0
93     for p_name_info in products_infos:
94         res_prod = check_product(p_name_info, config, logger)
95         if res_prod != 0:
96             res += 1 
97     return res
98
99 def check_product(p_name_info, config, logger):
100     '''Execute the proper configuration command(s) 
101        in the product build directory.
102     
103     :param p_name_info tuple: (str, Config) => (product_name, product_info)
104     :param config Config: The global configuration
105     :param logger Logger: The logger instance to use for the display 
106                           and logging
107     :return: 1 if it fails, else 0.
108     :rtype: int
109     '''
110     
111     p_name, p_info = p_name_info
112     
113     # Logging
114     logger.write("\n", 4, False)
115     logger.write("################ ", 4)
116     header = _("Check of %s") % src.printcolors.printcLabel(p_name)
117     header += " %s " % ("." * (20 - len(p_name)))
118     logger.write(header, 3)
119     logger.write("\n", 4, False)
120     logger.flush()
121
122     # Verify if the command has to be launched or not
123     ignored = False
124     if not src.get_property_in_product_cfg(p_info, CHECK_PROPERTY):
125         msg = _("The product %s is defined as not having tests. "
126                 "product ignored." % p_name)
127         logger.write("%s\n" % msg, 4)
128         ignored = True
129     if "build_dir" not in p_info:
130         msg = _("No build_dir key defined in "
131                 "the config file of %s: product ignored." % p_name)
132         logger.write("%s\n" % msg, 4)
133         ignored = True
134     if not src.product.product_compiles(p_info):
135         msg = _("The product %s is defined as not compiling. "
136                 "product ignored." % p_name)
137         logger.write("%s\n" % msg, 4)
138         ignored = True
139
140     # Get the command to execute for script products
141     cmd_found = True
142     command = ""
143     if src.product.product_has_script(p_info) and not ignored:
144         command = src.get_cfg_param(p_info, "test_build", "Not found")
145         if command == "Not found":
146             cmd_found = False
147             msg = _('WARNING: The product %s is defined as having tests. But it'
148                     ' is compiled using a script and the key "test_build" is '
149                     'not defined in the definition of %s' % (p_name, p_name))
150             logger.write("%s\n" % msg, 4)
151                 
152     if ignored or not cmd_found:
153         log_step(logger, header, "ignored")
154         logger.write("==== %(name)s %(IGNORED)s\n" %
155             { "name" : p_name ,
156              "IGNORED" : src.printcolors.printcInfo("IGNORED")},
157             4)
158         logger.write("\n", 3, False)
159         logger.flush()
160         if not cmd_found:
161             return 1
162         return 0
163     
164     # Instantiate the class that manages all the construction commands
165     # like cmake, check, make install, make test, environment management, etc...
166     builder = src.compilation.Builder(config, logger, p_info)
167     
168     # Prepare the environment
169     log_step(logger, header, "PREPARE ENV")
170     res_prepare = builder.prepare()
171     log_res_step(logger, res_prepare)
172     
173     len_end_line = 20
174
175     # Launch the check    
176     log_step(logger, header, "CHECK")
177     res = builder.check(command=command)
178     log_res_step(logger, res)
179     
180     # Log the result
181     if res > 0:
182         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
183         logger.write("\r" + header + src.printcolors.printcError("KO"))
184         logger.write("==== %(KO)s in check of %(name)s \n" %
185             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
186         logger.flush()
187     else:
188         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
189         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
190         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
191         logger.write("==== Check of %(name)s %(OK)s \n" %
192             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
193         logger.flush()
194     logger.write("\n", 3, False)
195
196     return res
197
198 def description():
199     '''method that is called when salomeTools is called with --help option.
200     
201     :return: The text to display for the check command description.
202     :rtype: str
203     '''
204     return _("The check command executes the \"check\" command in"
205              " the build directory of all the products of the application."
206              "\nIt is possible to reduce the list of products to check by using"
207              " the --products option\n\nexample\nsat check SALOME-master "
208              "--products KERNEL,GUI,GEOM")
209   
210 def run(args, runner, logger):
211     '''method that is called when salomeTools is called with check parameter.
212     '''
213     
214     # Parse the options
215     (options, args) = parser.parse_args(args)
216
217     # check that the command has been called with an application
218     src.check_config_has_application( runner.cfg )
219
220     # Get the list of products to treat
221     products_infos = get_products_list(options, runner.cfg, logger)
222     
223     # Print some informations
224     logger.write(_('Executing the check command in the build '
225                                 'directories of the application %s\n') % 
226                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
227     
228     info = [(_("BUILD directory"),
229              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
230     src.print_info(logger, info)
231     
232     # Call the function that will loop over all the products and execute
233     # the right command(s)
234     res = check_all_products(runner.cfg, products_infos, logger)
235     
236     # Print the final state
237     nb_products = len(products_infos)
238     if res == 0:
239         final_status = "OK"
240     else:
241         final_status = "KO"
242    
243     logger.write(_("\nCheck: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
244         { 'status': src.printcolors.printc(final_status), 
245           'valid_result': nb_products - res,
246           'nb_products': nb_products }, 1)    
247     
248     return res