Salome HOME
fix #10569
[tools/sat.git] / commands / prepare.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 re
20 import os
21
22 import src
23 import src.debug as DBG
24
25 # Define all possible option for prepare command :  sat prepare <options>
26 parser = src.options.Options()
27 parser.add_option('p', 'products', 'list2', 'products',
28     _('Optional: products to prepare. This option can be'
29     ' passed several time to prepare several products.'))
30 parser.add_option('f', 'force', 'boolean', 'force', 
31     _("Optional: force to prepare the products in development mode."))
32 parser.add_option('', 'force_patch', 'boolean', 'force_patch', 
33     _("Optional: force to apply patch to the products in development mode."))
34
35 def get_products_list(options, cfg, logger):
36     '''method that gives the product list with their informations from 
37        configuration regarding the passed options.
38     
39     :param options Options: The Options instance that stores the commands 
40                             arguments
41     :param config Config: The global configuration
42     :param logger Logger: The logger instance to use for the display and logging
43     :return: The list of (product name, product_informations).
44     :rtype: List
45     '''
46     # Get the products to be prepared, regarding the options
47     if options.products is None:
48         # No options, get all products sources
49         products = cfg.APPLICATION.products
50     else:
51         # if option --products, check that all products of the command line
52         # are present in the application.
53         products = options.products
54         for p in products:
55             if p not in cfg.APPLICATION.products:
56                 raise src.SatException(_("Product %(product)s "
57                             "not defined in application %(application)s") %
58                         { 'product': p, 'application': cfg.VARS.application} )
59     
60     # Construct the list of tuple containing 
61     # the products name and their definition
62     products_infos = src.product.get_products_infos(products, cfg)
63     
64     return products_infos
65
66 def remove_products(arguments, l_products_info, logger):
67     '''function that removes the products in l_products_info from arguments list.
68     
69     :param arguments str: The arguments from which to remove products
70     :param l_products_info list: List of 
71                                  (str, Config) => (product_name, product_info)
72     :param logger Logger: The logger instance to use for the display and logging
73     :return: The updated arguments.
74     :rtype: str
75     '''
76     args = str(arguments) #copy of "--products ,XDATA,TESSCODE,cmake" for example
77     largs = args.split(',')
78     DBG.write("largs", largs)
79     toRemove = [name for name,_ in l_products_info]
80     DBG.write("remove_products", toRemove)
81     removed = []
82     notRemoved = []
83     for name in largs[1:]: # skip largs[0] as "--products "
84       if name in toRemove:
85         removed.append(name)
86       else:
87         notRemoved.append(name)
88     # DBG.write(removed, removed, True)
89     logger.write("  %s\n" % ",".join(removed), 1)
90     DBG.write("notRemoved", notRemoved)
91     res = largs[0] + ",".join(notRemoved)
92     return res
93
94 def find_products_already_getted(l_products):
95     '''function that returns the list of products that have an existing source 
96        directory.
97     
98     :param l_products List: The list of products to check
99     :return: The list of product configurations that have an existing source 
100              directory.
101     :rtype: List
102     '''
103     l_res = []
104     for p_name_p_cfg in l_products:
105         __, prod_cfg = p_name_p_cfg
106         if os.path.exists(prod_cfg.source_dir):
107             l_res.append(p_name_p_cfg)
108     return l_res
109
110 def find_products_with_patchs(l_products):
111     '''function that returns the list of products that have one or more patches.
112     
113     :param l_products List: The list of products to check
114     :return: The list of product configurations that have one or more patches.
115     :rtype: List
116     '''
117     l_res = []
118     for p_name_p_cfg in l_products:
119         __, prod_cfg = p_name_p_cfg
120         l_patchs = src.get_cfg_param(prod_cfg, "patches", [])
121         if len(l_patchs)>0:
122             l_res.append(p_name_p_cfg)
123     return l_res
124
125 def description():
126     '''method that is called when salomeTools is called with --help option.
127     
128     :return: The text to display for the prepare command description.
129     :rtype: str
130     '''
131     return _("The prepare command gets the sources of "
132              "the application products and apply the patches if there is any."
133              "\n\nexample:\nsat prepare SALOME-master --products KERNEL,GUI")
134   
135 def run(args, runner, logger):
136     '''method that is called when salomeTools is called with prepare parameter.
137     '''
138     
139     # Parse the options
140     (options, args) = parser.parse_args(args)
141
142     # check that the command has been called with an application
143     src.check_config_has_application( runner.cfg )
144
145     products_infos = get_products_list(options, runner.cfg, logger)
146
147     # Construct the arguments to pass to the clean, source and patch commands
148     args_appli = runner.cfg.VARS.application + ' '
149     args_product_opt = '--products '
150     if options.products:
151         for p_name in options.products:
152             args_product_opt += ',' + p_name
153     else:
154         for p_name, __ in products_infos:
155             args_product_opt += ',' + p_name
156
157     ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
158     args_product_opt_clean = args_product_opt
159     if not options.force and len(ldev_products) > 0:
160         l_products_not_getted = find_products_already_getted(ldev_products)
161         if len(l_products_not_getted) > 0:
162             msg = _("""\
163 Do not get the source of the following products in development mode
164 Use the --force option to overwrite it.
165 """)
166             logger.write(src.printcolors.printcWarning(msg), 1)
167             args_product_opt_clean = remove_products(args_product_opt_clean,
168                                                      l_products_not_getted,
169                                                      logger)
170             logger.write("\n", 1)
171
172     
173     args_product_opt_patch = args_product_opt
174     if not options.force_patch and len(ldev_products) > 0:
175         l_products_with_patchs = find_products_with_patchs(ldev_products)
176         if len(l_products_with_patchs) > 0:
177             msg = _("""\
178 do not patch the following products in development mode
179 Use the --force_patch option to overwrite it.
180 """)
181             logger.write(src.printcolors.printcWarning(msg), 1)
182             args_product_opt_patch = remove_products(args_product_opt_patch,
183                                                      l_products_with_patchs,
184                                                      logger)
185             logger.write("\n", 1)
186
187     # Construct the final commands arguments
188     args_clean = args_appli + args_product_opt_clean + " --sources"
189     args_source = args_appli + args_product_opt  
190     args_patch = args_appli + args_product_opt_patch
191
192     # If there is no more any product in the command arguments,
193     # do not call the concerned command 
194     oExpr = re.compile("^--products *$")
195     do_clean = not(oExpr.search(args_product_opt_clean))
196     do_source = not(oExpr.search(args_product_opt))
197     do_patch = not(oExpr.search(args_product_opt_patch))
198     
199     
200     # Initialize the results to a failing status
201     res_clean = 1
202     res_source = 1
203     res_patch = 1
204     
205     # Call the commands using the API
206     if do_clean:
207         msg = _("Clean the source directories ...")
208         logger.write(msg, 3)
209         logger.flush()
210         res_clean = runner.clean(args_clean, batch=True, verbose = 0,
211                                     logger_add_link = logger)
212         if res_clean == 0:
213             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
214         else:
215             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
216     if do_source:
217         msg = _("Get the sources of the products ...")
218         logger.write(msg, 5)
219         res_source = runner.source(args_source,
220                                     logger_add_link = logger)
221         if res_source == 0:
222             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
223         else:
224             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
225     if do_patch:
226         msg = _("Patch the product sources (if any) ...")
227         logger.write(msg, 5)
228         res_patch = runner.patch(args_patch,
229                                     logger_add_link = logger)
230         if res_patch == 0:
231             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
232         else:
233             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
234     
235     return res_clean + res_source + res_patch