Salome HOME
begin to fix 8596, 8908, 8605, 8638, 10458, 8646, 8576
[tools/sat.git] / commands / source.py
index b452900bc4a552eb0545e41dbfafe8cd150bd311..a616c7768272e66bd2d773a508d8cbd3e4d05138 100644 (file)
@@ -21,11 +21,12 @@ import shutil
 
 import src
 import prepare
+import src.debug as DBG
 
 # Define all possible option for patch command :  sat patch <options>
 parser = src.options.Options()
-parser.add_option('p', 'product', 'list2', 'products',
-    _('products from which to get the sources. This option can be'
+parser.add_option('p', 'products', 'list2', 'products',
+    _('Optional: products from which to get the sources. This option can be'
     ' passed several time to get the sources of several products.'))
 
 def get_source_for_dev(config, product_info, source_dir, logger, pad):
@@ -60,7 +61,12 @@ def get_source_for_dev(config, product_info, source_dir, logger, pad):
     
     return retcode
 
-def get_source_from_git(product_info, source_dir, logger, pad, is_dev=False):
+def get_source_from_git(product_info,
+                        source_dir,
+                        logger,
+                        pad,
+                        is_dev=False,
+                        environ = None):
     '''The method called if the product is to be get in git mode
     
     :param product_info Config: The configuration specific to 
@@ -70,6 +76,8 @@ def get_source_from_git(product_info, source_dir, logger, pad, is_dev=False):
     :param logger Logger: The logger instance to use for the display and logging
     :param pad int: The gap to apply for the terminal display
     :param is_dev boolean: True if the product is in development mode
+    :param environ src.environment.Environ: The environment to source when
+                                                extracting.
     :return: True if it succeed, else False
     :rtype: boolean
     '''
@@ -99,7 +107,7 @@ def get_source_from_git(product_info, source_dir, logger, pad, is_dev=False):
     # Call the system function that do the extraction in git mode
     retcode = src.system.git_extract(repo_git,
                                  product_info.git_info.tag,
-                                 source_dir, logger)
+                                 source_dir, logger, environ)
     return retcode
 
 def get_source_from_archive(product_info, source_dir, logger):
@@ -138,7 +146,43 @@ def get_source_from_archive(product_info, source_dir, logger):
     
     return retcode
 
-def get_source_from_cvs(user, product_info, source_dir, checkout, logger, pad):
+def get_source_from_dir(product_info, source_dir, logger):
+    
+    if "dir_info" not in product_info:
+        msg = _("Error: you must put a dir_info section"
+                " in the file %s.pyconf" % product_info.name)
+        logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
+        return False
+
+    if "dir" not in product_info.dir_info:
+        msg = _("Error: you must put a dir in the dir_info section"
+                " in the file %s.pyconf" % product_info.name)
+        logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
+        return False
+
+    # check that source exists
+    if not os.path.exists(product_info.dir_info.dir):
+        msg = _("Error: the dir %s defined in the file"
+                " %s.pyconf does not exists" % (product_info.dir_info.dir,
+                                                product_info.name))
+        logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
+        return False
+    
+    logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
+                                           product_info.dir_info.dir), 3)
+    logger.flush()
+
+    retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
+    
+    return retcode
+    
+def get_source_from_cvs(user,
+                        product_info,
+                        source_dir,
+                        checkout,
+                        logger,
+                        pad,
+                        environ = None):
     '''The method called if the product is to be get in cvs mode
     
     :param user str: The user to use in for the cvs command
@@ -149,6 +193,8 @@ def get_source_from_cvs(user, product_info, source_dir, checkout, logger, pad):
     :param checkout boolean: If True, get the source in checkout mode
     :param logger Logger: The logger instance to use for the display and logging
     :param pad int: The gap to apply for the terminal display
+    :param environ src.environment.Environ: The environment to source when
+                                                extracting.
     :return: True if it succeed, else False
     :rtype: boolean
     '''
@@ -196,10 +242,15 @@ def get_source_from_cvs(user, product_info, source_dir, checkout, logger, pad):
                                  product_info.cvs_info.product_base,
                                  product_info.cvs_info.tag,
                                  product_info.cvs_info.source,
-                                 source_dir, logger, checkout)
+                                 source_dir, logger, checkout, environ)
     return retcode
 
-def get_source_from_svn(user, product_info, source_dir, checkout, logger):
+def get_source_from_svn(user,
+                        product_info,
+                        source_dir,
+                        checkout,
+                        logger,
+                        environ = None):
     '''The method called if the product is to be get in svn mode
     
     :param user str: The user to use in for the svn command
@@ -209,6 +260,8 @@ def get_source_from_svn(user, product_info, source_dir, checkout, logger):
                             directory where to put the sources
     :param checkout boolean: If True, get the source in checkout mode
     :param logger Logger: The logger instance to use for the display and logging
+    :param environ src.environment.Environ: The environment to source when
+                                                extracting.
     :return: True if it succeed, else False
     :rtype: boolean
     '''
@@ -228,7 +281,8 @@ def get_source_from_svn(user, product_info, source_dir, checkout, logger):
                                      product_info.svn_info.tag,
                                      source_dir, 
                                      logger, 
-                                     checkout)
+                                     checkout,
+                                     environ)
     return retcode
 
 def get_product_sources(config, 
@@ -252,6 +306,14 @@ def get_product_sources(config,
     :return: True if it succeed, else False
     :rtype: boolean
     '''
+    
+    # Get the application environment
+    logger.write(_("Set the application environment\n"), 5)
+    env_appli = src.environment.SalomeEnviron(config,
+                                      src.environment.Environ(dict(os.environ)))
+    env_appli.set_application_env(logger)
+    
+    # Call the right function to get sources regarding the product settings
     if not checkout and is_dev:
         return get_source_for_dev(config, 
                                    product_info, 
@@ -261,10 +323,13 @@ def get_product_sources(config,
 
     if product_info.get_source == "git":
         return get_source_from_git(product_info, source_dir, logger, pad, 
-                                    is_dev)
+                                    is_dev,env_appli)
 
     if product_info.get_source == "archive":
         return get_source_from_archive(product_info, source_dir, logger)
+
+    if product_info.get_source == "dir":
+        return get_source_from_dir(product_info, source_dir, logger)
     
     if product_info.get_source == "cvs":
         cvs_user = config.USER.cvs_user
@@ -273,13 +338,15 @@ def get_product_sources(config,
                                     source_dir, 
                                     checkout, 
                                     logger,
-                                    pad)
+                                    pad,
+                                    env_appli)
 
     if product_info.get_source == "svn":
         svn_user = config.USER.svn_user
         return get_source_from_svn(svn_user, product_info, source_dir, 
                                     checkout,
-                                    logger)
+                                    logger,
+                                    env_appli)
 
     if product_info.get_source == "native":
         # skip
@@ -328,7 +395,9 @@ def get_all_product_sources(config, products, logger):
         max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
     
     # The loop on all the products from which to get the sources
+    # DBG.write("source.get_all_product_sources config id", id(config), True)
     for product_name, product_info in products:
+        print "get_all_product_sources", product_name #, product_info
         # get product name, product informations and the directory where to put
         # the sources
         if (not (src.product.product_is_fixed(product_info) or 
@@ -346,11 +415,8 @@ def get_all_product_sources(config, products, logger):
         # the product is not in development mode
         is_dev = src.product.product_is_dev(product_info)
         if source_dir.exists():
-            logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
-                         3,
-                         False)
-            msg = _("INFORMATION : Not doing anything because the source"
-                    " directory already exists.\n")
+            logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS), 3, False)
+            msg = _("INFO : Not doing anything because the source directory already exists:\n    %s\n") % source_dir
             logger.write(msg, 3)
             good_result = good_result + 1
             # Do not get the sources and go to next product
@@ -370,17 +436,20 @@ def get_all_product_sources(config, products, logger):
             if product_info.no_rpath:
                 hack_no_rpath(config, product_info, logger)
         '''
+        
+        # Check that the sources are correctly get using the files to be tested
+        # in product information
+        if retcode:
+            check_OK, wrong_path = check_sources(product_info, logger)
+            if not check_OK:
+                # Print the missing file path
+                msg = _("The required file %s does not exists. " % wrong_path)
+                logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
+                retcode = False
 
         # show results
         results[product_name] = retcode
-        if retcode == 'N\A':
-            # The case where the product was not prepared because it is 
-            # in development mode
-            res =(src.printcolors.printc(src.OK_STATUS) + 
-                    src.printcolors.printcWarning(_(
-                                    ' source directory already exists')))
-            good_result = good_result + 1
-        elif retcode:
+        if retcode:
             # The case where it succeed
             res = src.OK_STATUS
             good_result = good_result + 1
@@ -395,6 +464,30 @@ def get_all_product_sources(config, products, logger):
 
     return good_result, results
 
+def check_sources(product_info, logger):
+    '''Check that the sources are correctly get, using the files to be tested
+       in product information
+    
+    :param product_info Config: The configuration specific to 
+                                the product to be prepared
+    :return: True if the files exists (or no files to test is provided).
+    :rtype: boolean
+    '''
+    # Get the files to test if there is any
+    if ("present_files" in product_info and 
+        "source" in product_info.present_files):
+        l_files_to_be_tested = product_info.present_files.source
+        for file_path in l_files_to_be_tested:
+            # The path to test is the source directory 
+            # of the product joined the file path provided
+            path_to_test = os.path.join(product_info.source_dir, file_path)
+            logger.write(_("\nTesting existence of file: \n"), 5)
+            logger.write(path_to_test, 5)
+            if not os.path.exists(path_to_test):
+                return False, path_to_test
+            logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
+    return True, ""
+
 def description():
     '''method that is called when salomeTools is called with --help option.
     
@@ -402,11 +495,13 @@ def description():
     :rtype: str
     '''
     return _("The source command gets the sources of the application products "
-             "from cvs, git, an archive or a directory..")
+             "from cvs, git or an archive.\n\nexample:"
+             "\nsat source SALOME-master --products KERNEL,GUI")
   
 def run(args, runner, logger):
     '''method that is called when salomeTools is called with source parameter.
     '''
+    DBG.write("source.run()", args, True)
     # Parse the options
     (options, args) = parser.parse_args(args)