Salome HOME
#8577 extension du domaine des properties
[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 check. This option can be'
27     ' passed several time to check several products.'))
28
29 CHECK_PROPERTY = "has_unit_tests"
30
31
32 def log_step(logger, header, step):
33     logger.write("\r%s%s" % (header, " " * 20), 3)
34     logger.write("\r%s%s" % (header, step), 3)
35     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
36     logger.flush()
37
38 def log_res_step(logger, res):
39     if res == 0:
40         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
41         logger.flush()
42     else:
43         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
44         logger.flush()
45
46 def check_all_products(config, products_infos, logger):
47     '''Execute the proper configuration commands 
48        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 logger Logger: The logger instance to use for the display and logging
54     :return: the number of failing commands.
55     :rtype: int
56     '''
57     res = 0
58     for p_name_info in products_infos:
59         res_prod = check_product(p_name_info, config, logger)
60         if res_prod != 0:
61             res += 1 
62     return res
63
64 def check_product(p_name_info, config, logger):
65     '''Execute the proper configuration command(s) 
66        in the product build directory.
67     
68     :param p_name_info tuple: (str, Config) => (product_name, product_info)
69     :param config Config: The global configuration
70     :param logger Logger: The logger instance to use for the display 
71                           and logging
72     :return: 1 if it fails, else 0.
73     :rtype: int
74     '''
75     
76     p_name, p_info = p_name_info
77     
78     # Logging
79     logger.write("\n", 4, False)
80     logger.write("################ ", 4)
81     header = _("Check of %s") % src.printcolors.printcLabel(p_name)
82     header += " %s " % ("." * (20 - len(p_name)))
83     logger.write(header, 3)
84     logger.write("\n", 4, False)
85     logger.flush()
86
87     # Verify if the command has to be launched or not
88     ignored = False
89     if not src.get_property_in_product_cfg(p_info, CHECK_PROPERTY):
90         msg = _("The product %s is defined as not having tests. "
91                 "product ignored." % p_name)
92         logger.write("%s\n" % msg, 4)
93         ignored = True
94     if "build_dir" not in p_info:
95         msg = _("No build_dir key defined in "
96                 "the config file of %s: product ignored." % p_name)
97         logger.write("%s\n" % msg, 4)
98         ignored = True
99     if not src.product.product_compiles(p_info):
100         msg = _("The product %s is defined as not compiling. "
101                 "product ignored." % p_name)
102         logger.write("%s\n" % msg, 4)
103         ignored = True
104
105     # Get the command to execute for script products
106     cmd_found = True
107     command = ""
108     if src.product.product_has_script(p_info) and not ignored:
109         command = src.get_cfg_param(p_info, "test_build", "Not found")
110         if command == "Not found":
111             cmd_found = False
112             msg = _('WARNING: The product %s is defined as having tests. But it'
113                     ' is compiled using a script and the key "test_build" is '
114                     'not defined in the definition of %s' % (p_name, p_name))
115             logger.write("%s\n" % msg, 4)
116                 
117     if ignored or not cmd_found:
118         log_step(logger, header, "ignored")
119         logger.write("==== %(name)s %(IGNORED)s\n" %
120             { "name" : p_name ,
121              "IGNORED" : src.printcolors.printcInfo("IGNORED")},
122             4)
123         logger.write("\n", 3, False)
124         logger.flush()
125         if not cmd_found:
126             return 1
127         return 0
128     
129     # Instantiate the class that manages all the construction commands
130     # like cmake, check, make install, make test, environment management, etc...
131     builder = src.compilation.Builder(config, logger, p_info)
132     
133     # Prepare the environment
134     log_step(logger, header, "PREPARE ENV")
135     res_prepare = builder.prepare()
136     log_res_step(logger, res_prepare)
137     
138     len_end_line = 20
139
140     # Launch the check    
141     log_step(logger, header, "CHECK")
142     res = builder.check(command=command)
143     log_res_step(logger, res)
144     
145     # Log the result
146     if res > 0:
147         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
148         logger.write("\r" + header + src.printcolors.printcError("KO"))
149         logger.write("==== %(KO)s in check of %(name)s \n" %
150             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
151         logger.flush()
152     else:
153         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
154         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
155         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
156         logger.write("==== Check of %(name)s %(OK)s \n" %
157             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
158         logger.flush()
159     logger.write("\n", 3, False)
160
161     return res
162
163 def description():
164     '''method that is called when salomeTools is called with --help option.
165     
166     :return: The text to display for the check command description.
167     :rtype: str
168     '''
169     return _("The check command executes the \"check\" command in"
170              " the build directory of all the products of the application."
171              "\nIt is possible to reduce the list of products to check by using"
172              " the --products option\n\nexample\nsat check SALOME-master "
173              "--products KERNEL,GUI,GEOM")
174   
175 def run(args, runner, logger):
176     '''method that is called when salomeTools is called with check parameter.
177     '''
178     
179     # Parse the options
180     (options, args) = parser.parse_args(args)
181
182     # check that the command has been called with an application
183     src.check_config_has_application( runner.cfg )
184
185     # Get the list of products to treat
186     products_infos = src.product.get_products_list(options, runner.cfg, logger)
187     
188     # Print some informations
189     logger.write(_('Executing the check command 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     res = check_all_products(runner.cfg, products_infos, logger)
200     
201     # Print the final state
202     nb_products = len(products_infos)
203     if res == 0:
204         final_status = "OK"
205     else:
206         final_status = "KO"
207    
208     logger.write(_("\nCheck: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
209         { 'status': src.printcolors.printc(final_status), 
210           'valid_result': nb_products - res,
211           'nb_products': nb_products }, 1)    
212     
213     return res