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