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