Salome HOME
Get an absolute path for env files in packages.
[tools/sat.git] / commands / prepare.py
index ece313e64882ca14bcbf0c978ea8f9af2e1c48ec..bdacf4fe486d0396c8b2fc5a702f3cff62b23e71 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 os
+
 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('p', 'product', 'list2', 'products',
-    _('products to prepare. This option can be'
+parser.add_option('p', 'products', 'list2', 'products',
+    _('Optional: products to prepare. This option can be'
     ' passed several time to prepare several products.'))
-parser.add_option('', 'no_sample', 'boolean', 'no_sample', 
-    _("do not prepare sample products."))
 parser.add_option('f', 'force', 'boolean', 'force', 
-    _("force to prepare the products in development mode."))
-parser.add_option('f', 'force_patch', 'boolean', 'force_patch', 
-    _("force to apply patch to the products in development mode."))
+    _("Optional: force to prepare the products in development mode."))
+parser.add_option('', 'force_patch', 'boolean', 'force_patch', 
+    _("Optional: force to apply patch to the products in development mode."))
 
 def get_products_list(options, cfg, logger):
     '''method that gives the product list with their informations from 
@@ -53,31 +54,63 @@ def get_products_list(options, cfg, logger):
             if p not in cfg.APPLICATION.products:
                 raise src.SatException(_("Product %(product)s "
                             "not defined in application %(application)s") %
-                { 'product': p, 'application': cfg.VARS.application} )
+                        { 'product': p, 'application': cfg.VARS.application} )
     
     # Construct the list of tuple containing 
     # the products name and their definition
     products_infos = src.product.get_products_infos(products, cfg)
+    
+    return products_infos
 
-    # if the --no_sample option is invoked, suppress the sample products from 
-    # the list
-    if options.no_sample:
-        
-        lproducts_sample = [p for p in products_infos if src.product.product_is_sample(p[1])]
-        
-        products_infos = [p for p in products_infos if p not in lproducts_sample]
+def remove_products(arguments, l_products_info, logger):
+    '''function that removes the products in l_products_info from arguments list.
+    
+    :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
 
-        if len(lproducts_sample) > 0:
-            msg = "Ignoring the following sample products:\n"
-            logger.write(src.printcolors.printcWarning(_(msg)), 1)
-        for i, product in enumerate(lproducts_sample):
-            end_text = ', '
-            if i+1 == len(lproducts_sample):
-                end_text = '\n'
-                
-            logger.write(product[0] + end_text, 1)
+def find_products_already_getted(l_products):
+    '''function that returns the list of products that have an existing source 
+       directory.
     
-    return products_infos
+    :param l_products List: The list of products to check
+    :return: The list of product configurations that have an existing source 
+             directory.
+    :rtype: List
+    '''
+    l_res = []
+    for p_name_p_cfg in l_products:
+        __, prod_cfg = p_name_p_cfg
+        if os.path.exists(prod_cfg.source_dir):
+            l_res.append(p_name_p_cfg)
+    return l_res
+
+def find_products_with_patchs(l_products):
+    '''function that returns the list of products that have one or more patches.
+    
+    :param l_products List: The list of products to check
+    :return: The list of product configurations that have one or more patches.
+    :rtype: List
+    '''
+    l_res = []
+    for p_name_p_cfg in l_products:
+        __, prod_cfg = p_name_p_cfg
+        l_patchs = src.get_cfg_param(prod_cfg, "patches", [])
+        if len(l_patchs)>0:
+            l_res.append(p_name_p_cfg)
+    return l_res
 
 def description():
     '''method that is called when salomeTools is called with --help option.
@@ -86,7 +119,8 @@ def description():
     :rtype: str
     '''
     return _("The prepare command gets the sources of "
-             "the application products and apply the patches if there is any.")
+             "the application products and apply the patches if there is any."
+             "\n\nexample:\nsat prepare SALOME-master --products KERNEL,GUI")
   
 def run(args, runner, logger):
     '''method that is called when salomeTools is called with prepare parameter.
@@ -100,67 +134,90 @@ def run(args, runner, logger):
 
     products_infos = get_products_list(options, runner.cfg, logger)
 
-    ##################################
-    ## Source command
-
-    # Construct the option to pass to the source command
+    # Construct the arguments to pass to the clean, source and patch commands
     args_appli = runner.cfg.VARS.application + ' '
-
-    args_product_opt = '--product '
+    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:
+        l_products_not_getted = find_products_already_getted(ldev_products)
+        if len(l_products_not_getted) > 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,
+                                                     l_products_not_getted,
+                                                     logger)
+            logger.write("\n", 1)
+
     
-    args_sample = ''
-    if options.no_sample:
-        args_sample = ' --no_sample'
-    
-    args_source = args_appli + args_product_opt + args_sample
-        
-    if options.force:
-        args_source += ' --force'
+    args_product_opt_patch = args_product_opt
+    if not options.force_patch and len(ldev_products) > 0:
+        l_products_with_patchs = find_products_with_patchs(ldev_products)
+        if len(l_products_with_patchs) > 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,
+                                                     l_products_with_patchs,
+                                                     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))
     
-    # Call the source command that gets the source
-    msg = src.printcolors.printcHeader(
-                                _('Get the sources of the desired products\n'))
-    logger.write(msg)
-    res_source = runner.source(args_source)
     
+    # Initialize the results to a failing status
+    res_clean = 1
+    res_source = 1
+    res_patch = 1
     
-    ##################################
-    ## Patch command
-    msg = src.printcolors.printcHeader(
-                    _('\nApply the patches to the sources of the products\n'))
-    logger.write(msg)
-
-    # Construct the option to pass to the patch command
-    ldev_products = [p for p in products_infos if src.product.product_is_dev(p[1])]
-    if len(ldev_products) > 0 and not options.force_patch:
-        msg = _("Ignoring the following products "
-                "in development mode\n")
-        logger.write(src.printcolors.printcWarning(msg), 1)
-        for i, (product_name, __) in enumerate(ldev_products):
-            args_product_opt = args_product_opt.replace(',' + product_name, '')
-            end_text = ', '
-            if i+1 == len(ldev_products):
-                end_text = '\n'
-                
-            logger.write(product_name + end_text, 1)
-        
-        msg = _("Use the --force_patch option to apply the patches anyway\n\n")
-        logger.write(src.printcolors.printcWarning(msg), 1)
-            
-    if args_product_opt == '--product ':
-        msg = _("Nothing to patch\n")
-        logger.write(msg)
-        res_patch = 0
-    else:
-        args_patch = args_appli + args_product_opt + args_sample
-        
-        # Call the source command that gets the source
-        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