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
24 import src.debug as DBG
27 # Define all possible option for prepare command : sat prepare <options>
28 parser = src.options.Options()
29 parser.add_option('p', 'products', 'list2', 'products',
30 _('Optional: products to prepare. This option accepts a comma separated list.'))
31 parser.add_option('f', 'force', 'boolean', 'force',
32 _("Optional: force to prepare the products in development mode."))
33 parser.add_option('', 'force_patch', 'boolean', 'force_patch',
34 _("Optional: force to apply patch to the products in development mode."))
37 def find_products_already_getted(l_products):
38 '''function that returns the list of products that have an existing source
41 :param l_products List: The list of products to check
42 :return: The list of product configurations that have an existing source
47 for p_name_p_cfg in l_products:
48 __, prod_cfg = p_name_p_cfg
49 if os.path.exists(prod_cfg.source_dir):
50 l_res.append(p_name_p_cfg)
53 def find_products_with_patchs(l_products):
54 '''function that returns the list of products that have one or more patches.
56 :param l_products List: The list of products to check
57 :return: The list of product configurations that have one or more patches.
61 for p_name_p_cfg in l_products:
62 __, prod_cfg = p_name_p_cfg
63 l_patchs = src.get_cfg_param(prod_cfg, "patches", [])
65 l_res.append(p_name_p_cfg)
69 '''method that is called when salomeTools is called with --help option.
71 :return: The text to display for the prepare command description.
74 return _("The prepare command gets the sources of "
75 "the application products and apply the patches if there is any."
76 "\n\nexample:\nsat prepare SALOME-master --products KERNEL,GUI")
78 def run(args, runner, logger):
79 '''method that is called when salomeTools is called with prepare parameter.
83 (options, args) = parser.parse_args(args)
85 # check that the command has been called with an application
86 src.check_config_has_application( runner.cfg )
88 products_infos = src.product.get_products_list(options, runner.cfg, logger)
90 # Construct the arguments to pass to the clean, source and patch commands
91 args_appli = runner.cfg.VARS.application + " " # useful whitespace
93 listProd = list(options.products)
94 else: # no product interpeted as all products
95 listProd = [name for name, tmp in products_infos]
97 DBG.write("prepare products", sorted(listProd))
98 args_product_opt = '--products ' + ",".join(listProd)
99 do_source = (len(listProd) > 0)
102 ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
103 newList = listProd # default
104 if not options.force and len(ldev_products) > 0:
105 l_products_not_getted = find_products_already_getted(ldev_products)
106 listNot = [i for i, tmp in l_products_not_getted]
107 newList, removedList = removeInList(listProd, listNot)
108 if len(removedList) > 0:
110 Do not get the source of the following products in development mode.
111 Use the --force option to overwrite it.
113 msg += "\n%s\n" % ",".join(removedList)
114 logger.write(src.printcolors.printcWarning(msg), 1)
116 args_product_opt_clean = '--products ' + ",".join(newList)
117 do_clean = (len(newList) > 0)
119 newList = listProd # default
120 if not options.force_patch and len(ldev_products) > 0:
121 l_products_with_patchs = find_products_with_patchs(ldev_products)
122 listNot = [i for i, tmp in l_products_with_patchs]
123 newList, removedList = removeInList(listProd, listNot)
124 if len(removedList) > 0:
126 Do not patch the following products in development mode.
127 Use the --force_patch option to overwrite it.
129 msg += "\n%s\n" % ",".join(removedList)
130 logger.write(src.printcolors.printcWarning(msg), 1)
132 args_product_opt_patch = '--products ' + ",".join(newList)
133 do_patch = (len(newList) > 0)
135 # Construct the final commands arguments
136 args_clean = args_appli + args_product_opt_clean + " --sources"
137 args_source = args_appli + args_product_opt
138 args_patch = args_appli + args_product_opt_patch
140 # Initialize the results to a failing status
145 # Call the commands using the API
147 msg = _("Clean the source directories ...")
150 res_clean = runner.clean(args_clean, batch=True, verbose = 0, logger_add_link = logger)
152 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
154 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
156 msg = _("Get the sources of the products ...")
158 res_source = runner.source(args_source, logger_add_link = logger)
160 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
162 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
164 msg = _("Patch the product sources (if any) ...")
166 res_patch = runner.patch(args_patch, logger_add_link = logger)
168 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
170 logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
172 return res_clean + res_source + res_patch
175 def removeInList(aList, removeList):
176 """Removes elements of removeList list from aList
178 :param aList: (list) The list from which to remove elements
179 :param removeList: (list) The list which contains elements to remove
180 :return: (list, list) (list with elements removed, list of elements removed)
182 res1 = [i for i in aList if i not in removeList]
183 res2 = [i for i in aList if i in removeList]