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