Salome HOME
Default archive name for products that are in archive mode
[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     _('products to prepare. This option can be'
27     ' passed several time to prepare several products.'))
28 parser.add_option('f', 'force', 'boolean', 'force', 
29     _("force to prepare the products in development mode."))
30 parser.add_option('f', 'force_patch', 'boolean', 'force_patch', 
31     _("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   
92 def run(args, runner, logger):
93     '''method that is called when salomeTools is called with prepare parameter.
94     '''
95     
96     # Parse the options
97     (options, args) = parser.parse_args(args)
98
99     # check that the command has been called with an application
100     src.check_config_has_application( runner.cfg )
101
102     products_infos = get_products_list(options, runner.cfg, logger)
103
104     # Construct the arguments to pass to the clean, source and patch commands
105     args_appli = runner.cfg.VARS.application + ' '
106     args_product_opt = '--products '
107     if options.products:
108         for p_name in options.products:
109             args_product_opt += ',' + p_name
110     else:
111         for p_name, __ in products_infos:
112             args_product_opt += ',' + p_name
113
114     ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
115     args_product_opt_clean = args_product_opt
116     if not options.force and len(ldev_products) > 0:
117         msg = _("Do not get the source of the following products "
118                 "in development mode\nUse the --force option to"
119                 " overwrite it.\n")
120         logger.write(src.printcolors.printcWarning(msg), 1)
121         args_product_opt_clean = remove_products(args_product_opt_clean,
122                                                  ldev_products,
123                                                  logger)
124         logger.write("\n", 1)
125
126     
127     args_product_opt_patch = args_product_opt
128     if not options.force_patch and len(ldev_products) > 0:
129         msg = _("do not patch the following products "
130                 "in development mode\nUse the --force_patch option to"
131                 " overwrite it.\n")
132         logger.write(src.printcolors.printcWarning(msg), 1)
133         args_product_opt_patch = remove_products(args_product_opt_patch,
134                                                  ldev_products,
135                                                  logger)
136         logger.write("\n", 1)
137
138     # Construct the final commands arguments
139     args_clean = args_appli + args_product_opt_clean + " --sources"
140     args_source = args_appli + args_product_opt  
141     args_patch = args_appli + args_product_opt_patch
142
143     # If there is no more any product in the command arguments,
144     # do not call the concerned command 
145     oExpr = re.compile("^--products *$")
146     do_clean = not(oExpr.search(args_product_opt_clean))
147     do_source = not(oExpr.search(args_product_opt))
148     do_patch = not(oExpr.search(args_product_opt_patch))
149     
150     
151     # Initialize the results to a failing status
152     res_clean = 1
153     res_source = 1
154     res_patch = 1
155     
156     # Call the commands using the API
157     if do_clean:
158         msg = _("Clean the source directories ...")
159         logger.write(msg, 3)
160         logger.flush()
161         res_clean, __ = runner.clean(args_clean, batch=True, verbose = 0,
162                                     logger_add_link = logger)
163         if res_clean == 0:
164             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
165         else:
166             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
167     if do_source:
168         msg = _("Get the sources of the products ...")
169         logger.write(msg, 5)
170         res_source, __ = runner.source(args_source,
171                                     logger_add_link = logger)
172         if res_source == 0:
173             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
174         else:
175             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
176     if do_patch:
177         msg = _("Patch the product sources (if any) ...")
178         logger.write(msg, 5)
179         res_patch, __ = runner.patch(args_patch,
180                                     logger_add_link = logger)
181         if res_patch == 0:
182             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
183         else:
184             logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
185     
186     return res_clean + res_source + res_patch