]> SALOME platform Git repositories - tools/sat.git/blob - commands/prepare.py
Salome HOME
fix sat compile from detar problem file sat-product.pyconf evaluation
[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
36
37 def find_products_already_getted(l_products):
38     '''function that returns the list of products that have an existing source 
39        directory.
40     
41     :param l_products List: The list of products to check
42     :return: The list of product configurations that have an existing source 
43              directory.
44     :rtype: List
45     '''
46     l_res = []
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)
51     return l_res
52
53 def find_products_with_patchs(l_products):
54     '''function that returns the list of products that have one or more patches.
55     
56     :param l_products List: The list of products to check
57     :return: The list of product configurations that have one or more patches.
58     :rtype: List
59     '''
60     l_res = []
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", [])
64         if len(l_patchs)>0:
65             l_res.append(p_name_p_cfg)
66     return l_res
67
68 def description():
69     '''method that is called when salomeTools is called with --help option.
70     
71     :return: The text to display for the prepare command description.
72     :rtype: str
73     '''
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")
77   
78 def run(args, runner, logger):
79     '''method that is called when salomeTools is called with prepare parameter.
80     '''
81     
82     # Parse the options
83     (options, args) = parser.parse_args(args)
84
85     # check that the command has been called with an application
86     src.check_config_has_application( runner.cfg )
87
88     products_infos = src.product.get_products_list(options, runner.cfg, logger)
89
90     # Construct the arguments to pass to the clean, source and patch commands
91     args_appli = runner.cfg.VARS.application + " "  # useful whitespace
92     if options.products:
93         listProd = list(options.products)
94     else: # no product interpeted as all products
95         listProd = [name for name, tmp in products_infos]
96
97     DBG.write("prepare products", sorted(listProd))
98     args_product_opt = '--products ' + ",".join(listProd)
99     do_source = (len(listProd) > 0)
100
101
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:
109             msg = _("""\
110 Do not get the source of the following products in development mode.
111 Use the --force option to overwrite it.
112 """)
113             msg += "\n%s\n" % ",".join(removedList)
114             logger.write(src.printcolors.printcWarning(msg), 1)
115
116     args_product_opt_clean = '--products ' + ",".join(newList)
117     do_clean = (len(newList) > 0)
118     
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:
125             msg = _("""\
126 Do not patch the following products in development mode.
127 Use the --force_patch option to overwrite it.
128 """)
129             msg += "\n%s\n" % ",".join(removedList)
130             logger.write(src.printcolors.printcWarning(msg), 1)
131                                                      
132     args_product_opt_patch = '--products ' + ",".join(newList)
133     do_patch = (len(newList) > 0)
134       
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
139       
140     # Initialize the results to a failing status
141     res_clean = 1
142     res_source = 1
143     res_patch = 1
144     
145     # Call the commands using the API
146     if do_clean:
147         msg = _("Clean the source directories ...")
148         logger.write(msg, 3)
149         logger.flush()
150         res_clean = runner.clean(args_clean, batch=True, verbose = 0, logger_add_link = logger)
151         if res_clean == 0:
152             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
153         else:
154             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
155     if do_source:
156         msg = _("Get the sources of the products ...")
157         logger.write(msg, 5)
158         res_source = runner.source(args_source, logger_add_link = logger)
159         if res_source == 0:
160             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
161         else:
162             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
163     if do_patch:
164         msg = _("Patch the product sources (if any) ...")
165         logger.write(msg, 5)
166         res_patch = runner.patch(args_patch, logger_add_link = logger)
167         if res_patch == 0:
168             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
169         else:
170             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
171     
172     return res_clean + res_source + res_patch
173
174
175 def removeInList(aList, removeList):
176     """Removes elements of removeList list from aList
177     
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) 
181     """
182     res1 = [i for i in aList if i not in removeList]
183     res2 = [i for i in aList if i in removeList]
184     return (res1, res2)
185
186