Salome HOME
add the check 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     _('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         res_prod = check_product(p_name_info, config, logger)
93         if res_prod != 0:
94             res += 1 
95     return res
96
97 def check_product(p_name_info, config, logger):
98     '''Execute the proper configuration command(s) 
99        in the product build directory.
100     
101     :param p_name_info tuple: (str, Config) => (product_name, product_info)
102     :param config Config: The global configuration
103     :param logger Logger: The logger instance to use for the display 
104                           and logging
105     :return: 1 if it fails, else 0.
106     :rtype: int
107     '''
108     
109     p_name, p_info = p_name_info
110     
111     # Logging
112     logger.write("\n", 4, False)
113     logger.write("################ ", 4)
114     header = _("Check of %s") % src.printcolors.printcLabel(p_name)
115     header += " %s " % ("." * (20 - len(p_name)))
116     logger.write(header, 3)
117     logger.write("\n", 4, False)
118     logger.flush()
119     
120     # Instantiate the class that manages all the construction commands
121     # like cmake, check, make install, make test, environment management, etc...
122     builder = src.compilation.Builder(config, logger, p_info)
123     
124     # Prepare the environment
125     log_step(logger, header, "PREPARE ENV")
126     res_prepare = builder.prepare()
127     log_res_step(logger, res_prepare)
128     
129     # Execute buildconfigure, configure if the product is autotools
130     # Execute ccheck if the product is ccheck
131     len_end_line = 20
132
133     log_step(logger, header, "CHECK")
134     res = builder.check()
135     log_res_step(logger, res)
136     
137     # Log the result
138     if res > 0:
139         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
140         logger.write("\r" + header + src.printcolors.printcError("KO"))
141         logger.write("==== %(KO)s in check of %(name)s \n" %
142             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
143         logger.flush()
144     else:
145         logger.write("\r%s%s" % (header, " " * len_end_line), 3)
146         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
147         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
148         logger.write("==== Check of %(name)s %(OK)s \n" %
149             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
150         logger.flush()
151     logger.write("\n", 3, False)
152
153     return res
154
155 def description():
156     '''method that is called when salomeTools is called with --help option.
157     
158     :return: The text to display for the check command description.
159     :rtype: str
160     '''
161     return _("The check command executes the \"check\" command in"
162              " the build directory")
163   
164 def run(args, runner, logger):
165     '''method that is called when salomeTools is called with check parameter.
166     '''
167     
168     # Parse the options
169     (options, args) = parser.parse_args(args)
170
171     # check that the command has been called with an application
172     src.check_config_has_application( runner.cfg )
173
174     # Get the list of products to treat
175     products_infos = get_products_list(options, runner.cfg, logger)
176     
177     # Print some informations
178     logger.write(_('Executing the check command in the build '
179                                 'directories of the application %s\n') % 
180                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
181     
182     info = [(_("BUILD directory"),
183              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
184     src.print_info(logger, info)
185     
186     # Call the function that will loop over all the products and execute
187     # the right command(s)
188     res = check_all_products(runner.cfg, products_infos, logger)
189     
190     # Print the final state
191     nb_products = len(products_infos)
192     if res == 0:
193         final_status = "OK"
194     else:
195         final_status = "KO"
196    
197     logger.write(_("\nCheck: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
198         { 'status': src.printcolors.printc(final_status), 
199           'valid_result': nb_products - res,
200           'nb_products': nb_products }, 1)    
201     
202     return res