Salome HOME
bug fix in generate command
[tools/sat.git] / commands / prepare.py
index 12e39e3aa31f51f82d4c8809ebac1363e199da08..eef6968f8b26c7b541946c49bb62b0a865b7005d 100644 (file)
 #  License along with this library; if not, write to the Free Software
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
+import re
+
 import src
 
-# Define all possible option for log command :  sat log <options>
+# Define all possible option for prepare command :  sat prepare <options>
 parser = src.options.Options()
-parser.add_option('m', 'module', 'list2', 'modules',
-    _('modules to prepare. This option can be'
-    ' passed several time to prepare several modules.'))
-parser.add_option('', 'no_sample', 'boolean', 'no_sample', 
-    _("do not prepare sample modules."))
+parser.add_option('p', 'products', 'list2', 'products',
+    _('products to prepare. This option can be'
+    ' passed several time to prepare several products.'))
 parser.add_option('f', 'force', 'boolean', 'force', 
-    _("force to prepare the modules in development mode."))
+    _("force to prepare the products in development mode."))
+parser.add_option('', 'force_patch', 'boolean', 'force_patch', 
+    _("force to apply patch to the products in development mode."))
 
-def get_modules_list(options, cfg, logger):
-    '''method that gives the module list with their informations from 
+def get_products_list(options, cfg, logger):
+    '''method that gives the product list with their informations from 
        configuration regarding the passed options.
     
     :param options Options: The Options instance that stores the commands 
                             arguments
     :param config Config: The global configuration
     :param logger Logger: The logger instance to use for the display and logging
-    :return: The list of (module name, module_infomrmations).
+    :return: The list of (product name, product_informations).
     :rtype: List
     '''
-    # Get the modules to be prepared, regarding the options
-    if options.modules is None:
-        # No options, get all modules sources
-        modules = cfg.APPLICATION.modules
+    # Get the products to be prepared, regarding the options
+    if options.products is None:
+        # No options, get all products sources
+        products = cfg.APPLICATION.products
     else:
-        # if option --modules, check that all modules of the command line
+        # if option --products, check that all products of the command line
         # are present in the application.
-        modules = options.modules
-        for m in modules:
-            if m not in cfg.APPLICATION.modules:
-                raise src.SatException(_("Module %(module)s "
+        products = options.products
+        for p in products:
+            if p not in cfg.APPLICATION.products:
+                raise src.SatException(_("Product %(product)s "
                             "not defined in application %(application)s") %
-                { 'module': m, 'application': cfg.VARS.application} )
+                        { 'product': p, 'application': cfg.VARS.application} )
     
     # Construct the list of tuple containing 
-    # the modules name and their definition
-    modules_infos = src.module.get_modules_infos(modules, cfg)
-
-    # if the --no_sample option is invoked, suppress the sample modules from 
-    # the list
-    if options.no_sample:
-        
-        lmodules_sample = [m for m in modules_infos if src.module.module_is_sample(m[1])]
-        
-        modules_infos = [m for m in modules_infos if m not in lmodules_sample]
+    # the products name and their definition
+    products_infos = src.product.get_products_infos(products, cfg)
+    
+    return products_infos
 
-        if len(lmodules_sample) > 0:
-            logger.write(src.printcolors.printcWarning(_("Ignoring the following sample modules:\n")), 1)
-        for i, module in enumerate(lmodules_sample):
-            end_text = ', '
-            if i+1 == len(lmodules_sample):
-                end_text = '\n'
-                
-            logger.write(module[0] + end_text, 1)
+def remove_products(arguments, l_products_info, logger):
+    '''method that removes the products in l_products_info from arguments list.
     
-    return modules_infos
+    :param arguments str: The arguments from which to remove products
+    :param l_products_info list: List of 
+                                 (str, Config) => (product_name, product_info)
+    :param logger Logger: The logger instance to use for the display and logging
+    :return: The updated arguments.
+    :rtype: str
+    '''
+    args = arguments
+    for i, (product_name, __) in enumerate(l_products_info):
+        args = args.replace(',' + product_name, '')
+        end_text = ', '
+        if i+1 == len(l_products_info):
+            end_text = '\n'            
+        logger.write(product_name + end_text, 1)
+    return args
 
 def description():
     '''method that is called when salomeTools is called with --help option.
@@ -82,8 +86,8 @@ def description():
     :return: The text to display for the prepare command description.
     :rtype: str
     '''
-    return _("The prepare command apply the patches on the sources of "
-             "the application modules if there is any")
+    return _("The prepare command gets the sources of "
+             "the application products and apply the patches if there is any.")
   
 def run(args, runner, logger):
     '''method that is called when salomeTools is called with prepare parameter.
@@ -95,47 +99,88 @@ def run(args, runner, logger):
     # check that the command has been called with an application
     src.check_config_has_application( runner.cfg )
 
-    modules_infos = get_modules_list(options, runner.cfg, logger)
+    products_infos = get_products_list(options, runner.cfg, logger)
+
+    # Construct the arguments to pass to the clean, source and patch commands
+    args_appli = runner.cfg.VARS.application + ' '
+    args_product_opt = '--products '
+    if options.products:
+        for p_name in options.products:
+            args_product_opt += ',' + p_name
+    else:
+        for p_name, __ in products_infos:
+            args_product_opt += ',' + p_name
+
+    ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
+    args_product_opt_clean = args_product_opt
+    if not options.force and len(ldev_products) > 0:
+        msg = _("Do not get the source of the following products "
+                "in development mode\nUse the --force option to"
+                " overwrite it.\n")
+        logger.write(src.printcolors.printcWarning(msg), 1)
+        args_product_opt_clean = remove_products(args_product_opt_clean,
+                                                 ldev_products,
+                                                 logger)
+        logger.write("\n", 1)
 
-    # Construct the option to pass to the source command
-    args_source = runner.cfg.VARS.application + ' '
-    
-    if options.modules:
-        args_source += '--module ' + ','.join(options.modules)
-    
-    if options.no_sample:
-        args_source += ' --no_sample'
-        
-    if options.force:
-        args_source += ' --force'
     
-    # Call the source command that gets the source
-    msg = src.printcolors.printcHeader(
-                                _('Get the sources of the desired modules\n'))
-    logger.write(msg)
-    res_source = runner.source(args_source)
+    args_product_opt_patch = args_product_opt
+    if not options.force_patch and len(ldev_products) > 0:
+        msg = _("do not patch the following products "
+                "in development mode\nUse the --force_patch option to"
+                " overwrite it.\n")
+        logger.write(src.printcolors.printcWarning(msg), 1)
+        args_product_opt_patch = remove_products(args_product_opt_patch,
+                                                 ldev_products,
+                                                 logger)
+        logger.write("\n", 1)
+
+    # Construct the final commands arguments
+    args_clean = args_appli + args_product_opt_clean + " --sources"
+    args_source = args_appli + args_product_opt  
+    args_patch = args_appli + args_product_opt_patch
+
+    # If there is no more any product in the command arguments,
+    # do not call the concerned command 
+    oExpr = re.compile("^--products *$")
+    do_clean = not(oExpr.search(args_product_opt_clean))
+    do_source = not(oExpr.search(args_product_opt))
+    do_patch = not(oExpr.search(args_product_opt_patch))
     
-    # Construct the option to pass to the patch command
-    args_patch = args_source.replace(' --force', '')
     
-    if ("dev_modules" in runner.cfg.APPLICATION and 
-                                runner.cfg.APPLICATION.dev_modules is not []):
-        
-        dev_modules = runner.cfg.APPLICATION.dev_modules
-        ldev_modules = [m for m in modules_infos if m[0] in dev_modules]
-        
-        if len(ldev_modules) > 0:
-            msg = _("The patches are not applied on "
-                    "the module in development mode\n")
-            
-            logger.write()
-            
-            modules_infos = [m for m in modules_infos if m[0] not in ldev_modules]
+    # Initialize the results to a failing status
+    res_clean = 1
+    res_source = 1
+    res_patch = 1
     
-    # Call the source command that gets the source
-    msg = src.printcolors.printcHeader(
-                    _('\nApply the patches to the sources of the modules\n'))
-    logger.write(msg)
-    res_patch = runner.patch(args_patch)
+    # Call the commands using the API
+    if do_clean:
+        msg = _("Clean the source directories ...")
+        logger.write(msg, 3)
+        logger.flush()
+        res_clean = runner.clean(args_clean, batch=True, verbose = 0,
+                                    logger_add_link = logger)
+        if res_clean == 0:
+            logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
+        else:
+            logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 3)
+    if do_source:
+        msg = _("Get the sources of the products ...")
+        logger.write(msg, 5)
+        res_source = runner.source(args_source,
+                                    logger_add_link = logger)
+        if res_source == 0:
+            logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
+        else:
+            logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
+    if do_patch:
+        msg = _("Patch the product sources (if any) ...")
+        logger.write(msg, 5)
+        res_patch = runner.patch(args_patch,
+                                    logger_add_link = logger)
+        if res_patch == 0:
+            logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 5)
+        else:
+            logger.write('%s\n' % src.printcolors.printc(src.KO_STATUS), 5)
     
-    return res_source + res_patch
\ No newline at end of file
+    return res_clean + res_source + res_patch
\ No newline at end of file