Salome HOME
fix(commands/sources): missing newline in logger
[tools/sat.git] / commands / source.py
index 9e086e8c705d60fe800b6849a204089072f7a41d..832e484f0f032653e3522caa83bbfe2e58b87ef7 100644 (file)
 
 import os
 import shutil
+import re
+import subprocess
 
 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'
-    ' passed several time to get the sources of several products.'))
+parser.add_option('p', 'products', 'list2', 'products',
+    _('Optional: products from which to get the sources. This option accepts a comma separated list.'))
 
 def get_source_for_dev(config, product_info, source_dir, logger, pad):
     '''The method called if the product is in development mode
@@ -60,7 +62,13 @@ 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(config,
+                        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,20 +78,32 @@ 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
     '''
     # The str to display
     coflag = 'git'
 
-    # Get the repository address. (from repo_dev key if the product is 
-    # in dev mode.
-    if is_dev and 'repo_dev' in product_info.git_info:
+    use_repo_dev=False
+    if ("APPLICATION" in config  and
+            "properties"  in config.APPLICATION  and
+            "repo_dev"    in config.APPLICATION.properties  and
+            config.APPLICATION.properties.repo_dev == "yes") :
+                use_repo_dev=True
+
+    # Get the repository address.
+    # If the application has the repo_dev property
+    # Or if the product is in dev mode
+    # Then we use repo_dev if the key exists
+    if (is_dev or use_repo_dev) and 'repo_dev' in product_info.git_info:
         coflag = src.printcolors.printcHighlight(coflag.upper())
         repo_git = product_info.git_info.repo_dev    
     else:
         repo_git = product_info.git_info.repo    
-        
+
+
     # Display informations
     logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3, 
                  False)
@@ -96,15 +116,39 @@ def get_source_from_git(product_info, source_dir, logger, pad, is_dev=False):
                  False)
     logger.flush()
     logger.write('\n', 5, 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)
+
+    git_options= ''
+    if "git_options" in product_info.git_info:
+        git_options = product_info.git_info.git_options
+
+    sub_dir = None
+    # what do we do with git tree structure and history
+    if is_dev and "sub_dir" in product_info.git_info:
+        logger.error("dev mode for product is incompatible with 'sub_dir' option")
+        return False
+
+    if not is_dev and "sub_dir" in product_info.git_info:
+        sub_dir = product_info.git_info.sub_dir
+
+    if sub_dir is None:
+      # Call the system function that do the extraction in git mode
+      retcode = src.system.git_extract(repo_git,
+                                   product_info.git_info.tag, git_options,
+                                   source_dir, logger, environ)
+    else:
+      # Call the system function that do the extraction of a sub_dir in git mode
+      logger.write("sub_dir:%s " % sub_dir, 3)
+      retcode = src.system.git_extract_sub_dir(repo_git,
+                                   product_info.git_info.tag,git_options,
+                                   source_dir, sub_dir, logger, environ)
+
+
     return retcode
 
-def get_source_from_archive(product_info, source_dir, logger):
+def get_source_from_archive(config, product_info, source_dir, logger):
     '''The method called if the product is to be get in archive mode
     
+    :param config Config: The global configuration
     :param product_info Config: The configuration specific to 
                                the product to be prepared
     :param source_dir Path: The Path instance corresponding to the 
@@ -113,10 +157,27 @@ def get_source_from_archive(product_info, source_dir, logger):
     :return: True if it succeed, else False
     :rtype: boolean
     '''
+
+    # check if pip should be used : pip mode id activated if the application and product have pip property
+    if (src.appli_test_property(config,"pip", "yes") and 
+       src.product.product_test_property(product_info,"pip", "yes")):
+        pip_msg = "PIP : do nothing, product will be downloaded later at compile time "
+        logger.write(pip_msg, 3) 
+        return True
+
     # check archive exists
     if not os.path.exists(product_info.archive_info.archive_name):
-        raise src.SatException(_("Archive not found: '%s'") % 
-                               product_info.archive_info.archive_name)
+        # The archive is not found on local file system (ARCHIVEPATH)
+        # We try ftp!
+        logger.write("\n   The archive is not found on local file system, we try ftp\n", 3)
+        ret=src.find_file_in_ftppath(product_info.archive_info.archive_name, 
+                                     config.PATHS.ARCHIVEFTP, config.LOCAL.archive_dir, logger)
+        if ret:
+            # archive was found on ftp and stored in ret
+            product_info.archive_info.archive_name=ret
+        else:
+            raise src.SatException(_("Archive not found in ARCHIVEPATH, nor on ARCHIVEFTP: '%s'") % 
+                                   product_info.archive_info.archive_name)
 
     logger.write('arc:%s ... ' % 
                  src.printcolors.printcInfo(product_info.archive_info.archive_name),
@@ -138,7 +199,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 +246,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 +295,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 +313,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 +334,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 +359,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, 
@@ -260,45 +375,55 @@ def get_product_sources(config,
                                    pad)
 
     if product_info.get_source == "git":
-        return get_source_from_git(product_info, source_dir, logger, pad, 
-                                    is_dev)
+        return get_source_from_git(config, product_info, source_dir, logger, pad, 
+                                    is_dev, env_appli)
 
     if product_info.get_source == "archive":
-        return get_source_from_archive(product_info, source_dir, logger)
+        return get_source_from_archive(config, 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
-        return get_source_from_cvs(cvs_user, 
-                                    product_info, 
-                                    source_dir, 
-                                    checkout, 
-                                    logger,
-                                    pad)
+        return get_source_from_cvs(cvs_user, product_info, source_dir, 
+                                    checkout, logger, 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)
+                                    checkout, logger, env_appli)
 
     if product_info.get_source == "native":
-        # skip
-        logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
-                     3,
-                     False)
-        msg = _('INFORMATION : Not doing anything because the product'
-                ' is of type "native".\n')
-        logger.write(msg, 3)
-        return True        
+        # for native products we check the corresponding system packages are installed
+        logger.write("Native : Checking system packages are installed\n" , 3)
+        check_cmd=src.system.get_pkg_check_cmd(config.VARS.dist_name) # (either rmp or apt)
+        run_pkg,build_pkg=src.product.check_system_dep(config.VARS.dist, check_cmd, product_info)
+        result=True
+        for pkg in run_pkg:
+            logger.write(" - " + pkg + " : " + run_pkg[pkg] + '\n', 1)
+            if "KO" in run_pkg[pkg]:
+                result=False
+        for pkg in build_pkg:
+            logger.write(" - " + pkg + " : " + build_pkg[pkg] + '\n', 1)
+            if "KO" in build_pkg[pkg]:
+                result=False
+        if result==False:
+            logger.error("some system dependencies are missing, please install them with "+\
+                         check_cmd[0])
+        return result        
 
     if product_info.get_source == "fixed":
         # skip
         logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
                      3,
                      False)
-        msg = _('INFORMATION : Not doing anything because the product'
-                ' is of type "fixed".\n')
-        logger.write(msg, 3)
+        msg = "FIXED : %s\n" % product_info.install_dir
+
+        if not os.path.isdir(product_info.install_dir):
+            logger.warning("The fixed path do not exixts!! Please check it : %s\n" % product_info.install_dir)
+        else:
+            logger.write(msg, 3)
         return True  
 
     # if the get_source is not in [git, archive, cvs, svn, fixed, native]
@@ -328,6 +453,7 @@ 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:
         # get product name, product informations and the directory where to put
         # the sources
@@ -346,23 +472,16 @@ 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
             continue
 
         # Call to the function that get the sources for one product
-        retcode = get_product_sources(config, 
-                                     product_info, 
-                                     is_dev, 
-                                     source_dir,
-                                     logger, 
-                                     max_product_name_len, 
+        retcode = get_product_sources(config, product_info, is_dev, 
+                                     source_dir, logger, max_product_name_len, 
                                      checkout=False)
         
         '''
@@ -385,8 +504,6 @@ def get_all_product_sources(config, products, logger):
         results[product_name] = retcode
         if retcode:
             # The case where it succeed
-            
-            
             res = src.OK_STATUS
             good_result = good_result + 1
         else:
@@ -431,11 +548,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)
     # Parse the options
     (options, args) = parser.parse_args(args)
     
@@ -450,12 +569,10 @@ def run(args, runner, logger):
     logger.write("\n", 2, False)
        
     # Get the products list with products informations regarding the options
-    products_infos = prepare.get_products_list(options, runner.cfg, logger)
+    products_infos = src.product.get_products_list(options, runner.cfg, logger)
     
     # Call to the function that gets all the sources
-    good_result, results = get_all_product_sources(runner.cfg, 
-                                                  products_infos,
-                                                  logger)
+    good_result, results = get_all_product_sources(runner.cfg, products_infos, logger)
 
     # Display the results (how much passed, how much failed, etc...)
     status = src.OK_STATUS