Salome HOME
Revert "Add generated modules in EDF appli for SALOME 6.6.0"
[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
21 import src
22
23 # Define all possible option for prepare command :  sat prepare <options>
24 parser = src.options.Options()
25 parser.add_option('p', 'products', 'list2', 'products',
26     _('Optional: products to prepare. This option can be'
27     ' passed several time to prepare several products.'))
28 parser.add_option('f', 'force', 'boolean', 'force', 
29     _("Optional: force to prepare the products in development mode."))
30 parser.add_option('', 'force_patch', 'boolean', 'force_patch', 
31     _("Optional: force to apply patch to the products in development mode."))
32
33 def get_products_list(options, cfg, logger):
34     '''method that gives the product list with their informations from 
35        configuration regarding the passed options.
36     
37     :param options Options: The Options instance that stores the commands 
38                             arguments
39     :param config Config: The global configuration
40     :param logger Logger: The logger instance to use for the display and logging
41     :return: The list of (product name, product_informations).
42     :rtype: List
43     '''
44     # Get the products to be prepared, regarding the options
45     if options.products is None:
46         # No options, get all products sources
47         products = cfg.APPLICATION.products
48     else:
49         # if option --products, check that all products of the command line
50         # are present in the application.
51         products = options.products
52         for p in products:
53             if p not in cfg.APPLICATION.products:
54                 raise src.SatException(_("Product %(product)s "
55                             "not defined in application %(application)s") %
56                         { 'product': p, 'application': cfg.VARS.application} )
57     
58     # Construct the list of tuple containing 
59     # the products name and their definition
60     products_infos = src.product.get_products_infos(products, cfg)
61     
62     return products_infos
63
64 def remove_products(arguments, l_products_info, logger):
65     '''method that removes the products in l_products_info from arguments list.
66     
67     :param arguments str: The arguments from which to remove products
68     :param l_products_info list: List of 
69                                  (str, Config) => (product_name, product_info)
70     :param logger Logger: The logger instance to use for the display and logging
71     :return: The updated arguments.
72     :rtype: str
73     '''
74     args = arguments
75     for i, (product_name, __) in enumerate(l_products_info):
76         args = args.replace(',' + product_name, '')
77         end_text = ', '
78         if i+1 == len(l_products_info):
79             end_text = '\n'            
80         logger.write(product_name + end_text, 1)
81     return args
82
83 def description():
84     '''method that is called when salomeTools is called with --help option.
85     
86     :return: The text to display for the prepare command description.
87     :rtype: str
88     '''
89     return _("The prepare command gets the sources of "
90              "the application products and apply the patches if there is any."
91              "\n\nexample:\nsat prepare SALOME-master --products KERNEL,GUI")
92   
93 def run(args, runner, logger):
94     '''method that is called when salomeTools is called with prepare parameter.
95     '''
96     
97     # Parse the options
98     (options, args) = parser.parse_args(args)
99
100     # check that the command has been called with an application
101     src.check_config_has_application( runner.cfg )
102
103     products_infos = get_products_list(options, runner.cfg, logger)
104
105     # Construct the arguments to pass to the clean, source and patch commands
106     args_appli = runner.cfg.VARS.application + ' '
107     args_product_opt = '--products '
108     if options.products:
109         for p_name in options.products:
110             args_product_opt += ',' + p_name
111     else:
112         for p_name, __ in products_infos:
113             args_product_opt += ',' + p_name
114
115     ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
116     args_product_opt_clean = args_product_opt
117     if not options.force and len(ldev_products) > 0:
118         msg = _("Do not get the source of the following products "
119                 "in development mode\nUse the --force option to"
120                 " overwrite it.\n")
121         logger.write(src.printcolors.printcWarning(msg), 1)
122         args_product_opt_clean = remove_products(args_product_opt_clean,
123                                                  ldev_products,
124                                                  logger)
125         logger.write("\n", 1)
126
127     
128     args_product_opt_patch = args_product_opt
129     if not options.force_patch and len(ldev_products) > 0:
130         msg = _("do not patch the following products "
131                 "in development mode\nUse the --force_patch option to"
132                 " overwrite it.\n")
133         logger.write(src.printcolors.printcWarning(msg), 1)
134         args_product_opt_patch = remove_products(args_product_opt_patch,
135                                                  ldev_products,
136                                                  logger)
137         logger.write("\n", 1)
138
139     # Construct the final commands arguments
140     args_clean = args_appli + args_product_opt_clean + " --sources"
141     args_source = args_appli + args_product_opt  
142     args_patch = args_appli + args_product_opt_patch
143
144     # If there is no more any product in the command arguments,
145     # do not call the concerned command 
146     oExpr = re.compile("^--products *$")
147     do_clean = not(oExpr.search(args_product_opt_clean))
148     do_source = not(oExpr.search(args_product_opt))
149     do_patch = not(oExpr.search(args_product_opt_patch))
150     
151     
152     # Initialize the results to a failing status
153     res_clean = 1
154     res_source = 1
155     res_patch = 1
156     
157     # Call the commands using the API
158     if do_clean:
159         msg = _("Clean the source directories ...")
160         logger.write(msg, 3)
161         logger.flush()
162         res_clean = runner.clean(args_clean, batch=True, verbose = 0,
163                                     logger_add_link = logger)
164         if res_clean == 0:
165             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
166         else:
167             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
168     if do_source:
169         msg = _("Get the sources of the products ...")
170         logger.write(msg, 5)
171         res_source = runner.source(args_source,
172                                     logger_add_link = logger)
173         if res_source == 0:
174             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
175         else:
176             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
177     if do_patch:
178         msg = _("Patch the product sources (if any) ...")
179         logger.write(msg, 5)
180         res_patch = runner.patch(args_patch,
181                                     logger_add_link = logger)
182         if res_patch == 0:
183             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
184         else:
185             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
186     
187     return res_clean + res_source + res_patch