Salome HOME
sat #32302 pip option --build obsolète : integration du patch fourni par Nabil
[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 import pprint as PP
22
23 import src
24 import src.debug as DBG
25
26
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."))
35 parser.add_option('c', 'complete', 'boolean', 'complete',
36     _("Optional: completion mode, only prepare products not present in SOURCES dir."),
37     False)
38
39
40 def find_products_already_prepared(l_products):
41     '''function that returns the list of products that have an existing source 
42        directory.
43     
44     :param l_products List: The list of products to check
45     :return: The list of product configurations that have an existing source 
46              directory.
47     :rtype: List
48     '''
49     l_res = []
50     for p_name_p_cfg in l_products:
51         __, prod_cfg = p_name_p_cfg
52         if "source_dir" in prod_cfg and os.path.exists(prod_cfg.source_dir):
53             l_res.append(p_name_p_cfg)
54     return l_res
55
56 def find_products_with_patchs(l_products):
57     '''function that returns the list of products that have one or more patches.
58     
59     :param l_products List: The list of products to check
60     :return: The list of product configurations that have one or more patches.
61     :rtype: List
62     '''
63     l_res = []
64     for p_name_p_cfg in l_products:
65         __, prod_cfg = p_name_p_cfg
66         l_patchs = src.get_cfg_param(prod_cfg, "patches", [])
67         if len(l_patchs)>0:
68             l_res.append(p_name_p_cfg)
69     return l_res
70
71 def description():
72     '''method that is called when salomeTools is called with --help option.
73     
74     :return: The text to display for the prepare command description.
75     :rtype: str
76     '''
77     return _("The prepare command gets the sources of "
78              "the application products and apply the patches if there is any."
79              "\n\nexample:\nsat prepare SALOME-master --products KERNEL,GUI")
80   
81 def run(args, runner, logger):
82     '''method that is called when salomeTools is called with prepare parameter.
83     '''
84     
85     # Parse the options
86     (options, args) = parser.parse_args(args)
87
88     # check that the command has been called with an application
89     src.check_config_has_application( runner.cfg )
90
91     # write warning if platform is not declared as supported
92     src.check_platform_is_supported( runner.cfg, logger )
93
94     products_infos = src.product.get_products_list(options, runner.cfg, logger)
95
96     # Construct the arguments to pass to the clean, source and patch commands
97     args_appli = runner.cfg.VARS.application + " "  # useful whitespace
98     if options.products:
99         listProd = list(options.products)
100     else: # no product interpeted as all products
101         listProd = [name for name, tmp in products_infos]
102
103     if options.complete:
104         # remove products that are already prepared 'completion mode)
105         pi_already_prepared=find_products_already_prepared(products_infos)
106         l_already_prepared = [i for i, tmp in pi_already_prepared]
107         newList, removedList = removeInList(listProd, l_already_prepared)
108         listProd = newList
109         if len(newList) == 0 and len(removedList) > 0 :
110             msg = "\nAll the products are already installed, do nothing!\n"
111             logger.write(src.printcolors.printcWarning(msg), 1)
112             return 0
113         if len(removedList) > 0 :
114             msg = "\nList of already prepared products that are skipped : %s\n" % ",".join(removedList)
115             logger.write(msg, 3)
116         
117
118     args_product_opt = '--products ' + ",".join(listProd)
119     do_source = (len(listProd) > 0)
120
121
122     ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
123     newList = listProd # default
124     if not options.force and len(ldev_products) > 0:
125         l_products_not_getted = find_products_already_prepared(ldev_products)
126         listNot = [i for i, tmp in l_products_not_getted]
127         newList, removedList = removeInList(listProd, listNot)
128         if len(removedList) > 0:
129             msg = _("""\
130 Do not get the source of the following products in development mode.
131 Use the --force option to overwrite it.
132 """)
133             msg += "\n%s\n" % ",".join(removedList)
134             logger.write(src.printcolors.printcWarning(msg), 1)
135
136     args_product_opt_clean = '--products ' + ",".join(newList)
137     do_clean = (len(newList) > 0)
138     
139     newList = listProd # default
140     if not options.force_patch and len(ldev_products) > 0:
141         l_products_with_patchs = find_products_with_patchs(ldev_products)
142         listNot = [i for i, tmp in l_products_with_patchs]
143         newList, removedList = removeInList(listProd, listNot)
144         if len(removedList) > 0:
145             msg = _("""\
146 Do not patch the following products in development mode.
147 Use the --force_patch option to overwrite it.
148 """)
149             msg += "\n%s\n" % ",".join(removedList)
150             logger.write(src.printcolors.printcWarning(msg), 1)
151                                                      
152     args_product_opt_patch = '--products ' + ",".join(newList)
153     do_patch = (len(newList) > 0)
154       
155     # Construct the final commands arguments
156     args_clean = args_appli + args_product_opt_clean + " --sources"
157     args_source = args_appli + args_product_opt  
158     args_patch = args_appli + args_product_opt_patch
159       
160     # Initialize the results to a running status
161     res_clean = 0
162     res_source = 0
163     res_patch = 0
164     
165     # Call the commands using the API
166     if do_clean:
167         msg = _("Clean the source directories ...")
168         logger.write(msg, 3)
169         logger.flush()
170         res_clean = runner.clean(args_clean, batch=True, verbose = 0, logger_add_link = logger)
171         if res_clean == 0:
172             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
173         else:
174             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
175     if do_source:
176         msg = _("Get the sources of the products ...")
177         logger.write(msg, 5)
178         res_source = runner.source(args_source, logger_add_link = logger)
179         if res_source == 0:
180             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
181         else:
182             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
183     if do_patch:
184         msg = _("Patch the product sources (if any) ...")
185         logger.write(msg, 5)
186         res_patch = runner.patch(args_patch, logger_add_link = logger)
187         if res_patch == 0:
188             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
189         else:
190             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
191     
192     return res_clean + res_source + res_patch
193
194
195 def removeInList(aList, removeList):
196     """Removes elements of removeList list from aList
197     
198     :param aList: (list) The list from which to remove elements
199     :param removeList: (list) The list which contains elements to remove
200     :return: (list, list) (list with elements removed, list of elements removed) 
201     """
202     res1 = [i for i in aList if i not in removeList]
203     res2 = [i for i in aList if i in removeList]
204     return (res1, res2)
205
206