3 # Copyright (C) 2010-2012 CEA/DEN
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.
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.
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
23 import src.debug as DBG
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."))
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.
39 :param options Options: The Options instance that stores the commands
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).
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
51 # if option --products, check that all products of the command line
52 # are present in the application.
53 products = options.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} )
60 # Construct the list of tuple containing
61 # the products name and their definition
62 products_infos = src.product.get_products_infos(products, cfg)
66 def remove_products(arguments, l_products_info, logger):
67 '''function that removes the products in l_products_info from arguments list.
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.
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)
83 for name in largs[1:]: # skip largs[0] as "--products "
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)
94 def find_products_already_getted(l_products):
95 '''function that returns the list of products that have an existing source
98 :param l_products List: The list of products to check
99 :return: The list of product configurations that have an existing source
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)
110 def find_products_with_patchs(l_products):
111 '''function that returns the list of products that have one or more patches.
113 :param l_products List: The list of products to check
114 :return: The list of product configurations that have one or more patches.
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", [])
122 l_res.append(p_name_p_cfg)
126 '''method that is called when salomeTools is called with --help option.
128 :return: The text to display for the prepare command description.
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")
135 def run(args, runner, logger):
136 '''method that is called when salomeTools is called with prepare parameter.
140 (options, args) = parser.parse_args(args)
142 # check that the command has been called with an application
143 src.check_config_has_application( runner.cfg )
145 products_infos = get_products_list(options, runner.cfg, logger)
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 '
151 for p_name in options.products:
152 args_product_opt += ',' + p_name
154 for p_name, __ in products_infos:
155 args_product_opt += ',' + p_name
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:
163 Do not get the source of the following products in development mode
164 Use the --force option to overwrite it.
166 logger.write(src.printcolors.printcWarning(msg), 1)
167 args_product_opt_clean = remove_products(args_product_opt_clean,
168 l_products_not_getted,
170 logger.write("\n", 1)
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:
178 do not patch the following products in development mode
179 Use the --force_patch option to overwrite it.
181 logger.write(src.printcolors.printcWarning(msg), 1)
182 args_product_opt_patch = remove_products(args_product_opt_patch,
183 l_products_with_patchs,
185 logger.write("\n", 1)
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
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))
200 # Initialize the results to a failing status
205 # Call the commands using the API
207 msg = _("Clean the source directories ...")
210 res_clean = runner.clean(args_clean, batch=True, verbose = 0,
211 logger_add_link = logger)
213 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
215 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
217 msg = _("Get the sources of the products ...")
219 res_source = runner.source(args_source,
220 logger_add_link = logger)
222 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
224 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
226 msg = _("Patch the product sources (if any) ...")
228 res_patch = runner.patch(args_patch,
229 logger_add_link = logger)
231 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
233 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
235 return res_clean + res_source + res_patch