Salome HOME
bbd189b1d8954cc7723c44a5195b2ba5b8779022
[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 src
20
21 # Define all possible option for log command :  sat log <options>
22 parser = src.options.Options()
23 parser.add_option('p', 'product', 'list2', 'products',
24     _('products to prepare. This option can be'
25     ' passed several time to prepare several products.'))
26 parser.add_option('', 'no_sample', 'boolean', 'no_sample', 
27     _("do not prepare sample products."))
28 parser.add_option('f', 'force', 'boolean', 'force', 
29     _("force to prepare the products in development mode."))
30
31 def get_products_list(options, cfg, logger):
32     '''method that gives the product list with their informations from 
33        configuration regarding the passed options.
34     
35     :param options Options: The Options instance that stores the commands 
36                             arguments
37     :param config Config: The global configuration
38     :param logger Logger: The logger instance to use for the display and logging
39     :return: The list of (product name, product_informations).
40     :rtype: List
41     '''
42     # Get the products to be prepared, regarding the options
43     if options.products is None:
44         # No options, get all products sources
45         products = cfg.APPLICATION.products
46     else:
47         # if option --products, check that all products of the command line
48         # are present in the application.
49         products = options.products
50         for p in products:
51             if p not in cfg.APPLICATION.products:
52                 raise src.SatException(_("Product %(product)s "
53                             "not defined in application %(application)s") %
54                 { 'product': p, 'application': cfg.VARS.application} )
55     
56     # Construct the list of tuple containing 
57     # the products name and their definition
58     products_infos = src.product.get_products_infos(products, cfg)
59
60     # if the --no_sample option is invoked, suppress the sample products from 
61     # the list
62     if options.no_sample:
63         
64         lproducts_sample = [p for p in products_infos if src.product.product_is_sample(p[1])]
65         
66         products_infos = [p for p in products_infos if p not in lproducts_sample]
67
68         if len(lproducts_sample) > 0:
69             msg = "Ignoring the following sample products:\n"
70             logger.write(src.printcolors.printcWarning(_(msg)), 1)
71         for i, product in enumerate(lproducts_sample):
72             end_text = ', '
73             if i+1 == len(lproducts_sample):
74                 end_text = '\n'
75                 
76             logger.write(product[0] + end_text, 1)
77     
78     return products_infos
79
80 def description():
81     '''method that is called when salomeTools is called with --help option.
82     
83     :return: The text to display for the prepare command description.
84     :rtype: str
85     '''
86     return _("The prepare command apply the patches on the sources of "
87              "the application products if there is any")
88   
89 def run(args, runner, logger):
90     '''method that is called when salomeTools is called with prepare parameter.
91     '''
92     
93     # Parse the options
94     (options, args) = parser.parse_args(args)
95
96     # check that the command has been called with an application
97     src.check_config_has_application( runner.cfg )
98
99     products_infos = get_products_list(options, runner.cfg, logger)
100
101     ##################################
102     ## Source command
103
104     # Construct the option to pass to the source command
105     args_appli = runner.cfg.VARS.application + ' '
106
107     args_product_opt = '--product '
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     if args_product_opt == '--product ':
116         args_product_opt = ''
117     
118     args_sample = ''
119     if options.no_sample:
120         args_sample = ' --no_sample'
121     
122     args_source = args_appli + args_product_opt + args_sample
123         
124     if options.force:
125         args_source += ' --force'
126     
127     # Call the source command that gets the source
128     msg = src.printcolors.printcHeader(
129                                 _('Get the sources of the desired products\n'))
130     logger.write(msg)
131     res_source = runner.source(args_source)
132     
133     
134     ##################################
135     ## Patch command
136     msg = src.printcolors.printcHeader(
137                     _('\nApply the patches to the sources of the products\n'))
138     logger.write(msg)
139
140     # Construct the option to pass to the patch command    
141     if ("dev_products" in runner.cfg.APPLICATION and 
142                                 runner.cfg.APPLICATION.dev_products is not []):
143         
144         dev_products = runner.cfg.APPLICATION.dev_products
145         ldev_products = [p for p in products_infos if p[0] in dev_products]
146         
147         if len(ldev_products) > 0:
148             msg = _("Ignoring the following products "
149                     "in development mode\n")
150             logger.write(src.printcolors.printcWarning(msg), 1)
151             for i, (product_name, __) in enumerate(ldev_products):
152                 args_product_opt.replace(',' + product_name, '')
153                 end_text = ', '
154                 if i+1 == len(ldev_products):
155                     end_text = '\n'
156                     
157                 logger.write(product_name + end_text, 1)
158             
159             msg = _("Use the --force_patch option to apply the patches anyway\n")
160             logger.write(src.printcolors.printcWarning(msg), 1)
161             
162     
163     args_patch = args_appli + args_product_opt + args_sample
164     
165     # Call the source command that gets the source
166     res_patch = runner.patch(args_patch)
167     
168     return res_source + res_patch