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