Salome HOME
#8577 extension du domaine des properties
[tools/sat.git] / commands / makeinstall.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 makeinstall command : sat makeinstall <options>
24 parser = src.options.Options()
25 parser.add_option('p', 'products', 'list2', 'products',
26     _('Optional: products to install. This option can be'
27     ' passed several time to install several products.'))
28
29
30 def log_step(logger, header, step):
31     logger.write("\r%s%s" % (header, " " * 20), 3)
32     logger.write("\r%s%s" % (header, step), 3)
33     logger.write("\n==== %s \n" % src.printcolors.printcInfo(step), 4)
34     logger.flush()
35
36 def log_res_step(logger, res):
37     if res == 0:
38         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
39         logger.flush()
40     else:
41         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
42         logger.flush()
43
44 def makeinstall_all_products(config, products_infos, logger):
45     '''Execute the proper configuration commands 
46        in each product build directory.
47
48     :param config Config: The global configuration
49     :param products_info list: List of 
50                                  (str, Config) => (product_name, product_info)
51     :param logger Logger: The logger instance to use for the display and logging
52     :return: the number of failing commands.
53     :rtype: int
54     '''
55     res = 0
56     for p_name_info in products_infos:
57         res_prod = makeinstall_product(p_name_info, config, logger)
58         if res_prod != 0:
59             res += 1 
60     return res
61
62 def makeinstall_product(p_name_info, config, logger):
63     '''Execute the proper configuration command(s) 
64        in the product build directory.
65     
66     :param p_name_info tuple: (str, Config) => (product_name, product_info)
67     :param config Config: The global configuration
68     :param logger Logger: The logger instance to use for the display 
69                           and logging
70     :return: 1 if it fails, else 0.
71     :rtype: int
72     '''
73     
74     p_name, p_info = p_name_info
75     
76     # Logging
77     logger.write("\n", 4, False)
78     logger.write("################ ", 4)
79     header = _("Make install of %s") % src.printcolors.printcLabel(p_name)
80     header += " %s " % ("." * (20 - len(p_name)))
81     logger.write(header, 3)
82     logger.write("\n", 4, False)
83     logger.flush()
84
85     # Do nothing if he product is not compilable
86     if ("properties" in p_info and "compilation" in p_info.properties and 
87                                         p_info.properties.compilation == "no"):
88         log_step(logger, header, "ignored")
89         logger.write("\n", 3, False)
90         return 0
91
92     # Instantiate the class that manages all the construction commands
93     # like cmake, make, make install, make test, environment management, etc...
94     builder = src.compilation.Builder(config, logger, p_info)
95     
96     # Prepare the environment
97     log_step(logger, header, "PREPARE ENV")
98     res_prepare = builder.prepare()
99     log_res_step(logger, res_prepare)
100     
101     # Execute buildconfigure, configure if the product is autotools
102     # Execute cmake if the product is cmake
103     res = 0
104     if not src.product.product_has_script(p_info):
105         log_step(logger, header, "MAKE INSTALL")
106         res_m = builder.install()
107         log_res_step(logger, res_m)
108         res += res_m
109     
110     # Log the result
111     if res > 0:
112         logger.write("\r%s%s" % (header, " " * 20), 3)
113         logger.write("\r" + header + src.printcolors.printcError("KO"))
114         logger.write("==== %(KO)s in make install of %(name)s \n" %
115             { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
116         logger.flush()
117     else:
118         logger.write("\r%s%s" % (header, " " * 20), 3)
119         logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
120         logger.write("==== %s \n" % src.printcolors.printcInfo("OK"), 4)
121         logger.write("==== Make install of %(name)s %(OK)s \n" %
122             { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
123         logger.flush()
124     logger.write("\n", 3, False)
125
126     return res
127
128 def description():
129     '''method that is called when salomeTools is called with --help option.
130     
131     :return: The text to display for the makeinstall command description.
132     :rtype: str
133     '''
134     return _("The makeinstall command executes the \"make install\" command in"
135              " the build directory.\nIn case of  product that is constructed "
136              "using a script (build_source :  \"script\"), then the "
137              "makeinstall command do nothing.\n\nexample:\nsat makeinstall "
138              "SALOME-master --products KERNEL,GUI")
139   
140 def run(args, runner, logger):
141     '''method that is called when salomeTools is called with makeinstall parameter.
142     '''
143     
144     # Parse the options
145     (options, args) = parser.parse_args(args)
146
147     # check that the command has been called with an application
148     src.check_config_has_application( runner.cfg )
149
150     # Get the list of products to treat
151     products_infos = src.product.get_products_list(options, runner.cfg, logger)
152
153     # Print some informations
154     logger.write(_('Executing the make install command in the build directories of the application %s\n') % 
155                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
156     
157     info = [(_("BUILD directory"),
158              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))]
159     src.print_info(logger, info)
160     
161     # Call the function that will loop over all the products and execute
162     # the right command(s)
163     res = makeinstall_all_products(runner.cfg, products_infos, logger)
164     
165     # Print the final state
166     nb_products = len(products_infos)
167     if res == 0:
168         final_status = "OK"
169     else:
170         final_status = "KO"
171    
172     logger.write(_("\nMake install: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
173         { 'status': src.printcolors.printc(final_status), 
174           'valid_result': nb_products - res,
175           'nb_products': nb_products }, 1)    
176     
177     return res