Salome HOME
Finalise the preparation commands
[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 prepare command :  sat prepare <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('f', 'force', 'boolean', 'force', 
27     _("force to prepare the products in development mode."))
28 parser.add_option('f', 'force_patch', 'boolean', 'force_patch', 
29     _("force to apply patch to 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     return products_infos
61
62 def remove_products(arguments, l_products_info, logger):
63     '''method that removes the products in l_products_info from arguments list.
64     
65     :param arguments str: The arguments from which to remove products
66     :param l_products_info list: List of 
67                                  (str, Config) => (product_name, product_info)
68     :param logger Logger: The logger instance to use for the display and logging
69     :return: The updated arguments.
70     :rtype: str
71     '''
72     args = arguments
73     for i, (product_name, __) in enumerate(l_products_info):
74         args = args.replace(',' + product_name, '')
75         end_text = ', '
76         if i+1 == len(l_products_info):
77             end_text = '\n'            
78         logger.write(product_name + end_text, 1)
79     return args
80
81 def description():
82     '''method that is called when salomeTools is called with --help option.
83     
84     :return: The text to display for the prepare command description.
85     :rtype: str
86     '''
87     return _("The prepare command gets the sources of "
88              "the application products and apply the patches if there is any.")
89   
90 def run(args, runner, logger):
91     '''method that is called when salomeTools is called with prepare parameter.
92     '''
93     
94     # Parse the options
95     (options, args) = parser.parse_args(args)
96
97     # check that the command has been called with an application
98     src.check_config_has_application( runner.cfg )
99
100     products_infos = get_products_list(options, runner.cfg, logger)
101
102     # Construct the arguments to pass to the clean, source and patch commands
103     args_appli = runner.cfg.VARS.application + ' '
104     args_product_opt = '--product '
105     if options.products:
106         for p_name in options.products:
107             args_product_opt += ',' + p_name
108     else:
109         for p_name, __ in products_infos:
110             args_product_opt += ',' + p_name
111
112     ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
113     args_product_opt_clean = args_product_opt
114     if not options.force and len(ldev_products) > 0:
115         msg = _("Do not get the source of the following products "
116                 "in development mode\nUse the --force option to"
117                 " overwrite it.\n")
118         logger.write(src.printcolors.printcWarning(msg), 1)
119         args_product_opt_clean = remove_products(args_product_opt_clean,
120                                                  ldev_products,
121                                                  logger)
122         logger.write("\n", 1)
123
124     
125     args_product_opt_patch = args_product_opt
126     if not options.force_patch and len(ldev_products) > 0:
127         msg = _("do not patch the following products "
128                 "in development mode\nUse the --force_patch option to"
129                 " overwrite it.\n")
130         logger.write(src.printcolors.printcWarning(msg), 1)
131         args_product_opt_patch = remove_products(args_product_opt_patch,
132                                                  ldev_products,
133                                                  logger)
134         logger.write("\n", 1)
135     
136     # Construct the final commands arguments
137     args_clean = args_appli + args_product_opt_clean + " --source"
138     args_source = args_appli + args_product_opt  
139     args_patch = args_appli + args_product_opt_patch
140     
141     # Call the commands using the API
142     msg = _("Clean the source directories ...")
143     logger.write(msg, 3)
144     res_clean = runner.clean(args_clean, batch=True, verbose = 0)
145     if res_clean == 0:
146         logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
147     res_source = runner.source(args_source)
148     res_patch = runner.patch(args_patch)
149     
150     return res_clean + res_source + res_patch