Salome HOME
Merge branch 'nct/may21'
[tools/sat.git] / commands / package.py
index a5b448fce18e6dc47120c0ea10f4ba99a1b01965..5cc26e6933f176bd40c44781e3ec4489b190f713 100644 (file)
@@ -25,12 +25,14 @@ import codecs
 import string
 import glob
 import pprint as PP
-
+import sys
 import src
 
 from application import get_SALOME_modules
 import src.debug as DBG
 
+old_python = sys.version_info[0] == 2 and sys.version_info[1] <= 6
+
 BINARY = "binary"
 SOURCE = "Source"
 PROJECT = "Project"
@@ -73,8 +75,8 @@ LOCAL_TEMPLATE = ("""#!/usr/bin/env python
     workdir : 'default'
     log_dir : 'default'
     archive_dir : 'default'
-    VCS : None
-    tag : None
+    VCS : 'unknown'
+    tag : 'unknown'
   }
 
 PROJECTS :
@@ -102,6 +104,8 @@ parser.add_option('', 'ftp', 'boolean', 'ftp',
     _('Optional: Do not embed archives for products in archive mode.' 
     'Sat prepare will use ftp instead to retrieve them'),
     False)
+parser.add_option('e', 'exe', 'string', 'exe',
+    _('Optional: Produce an extra launcher based upon the exe given as argument.'), "")
 parser.add_option('p', 'project', 'string', 'project',
     _('Optional: Produce an archive that contains a project.'), "")
 parser.add_option('t', 'salometools', 'boolean', 'sat',
@@ -152,7 +156,14 @@ def add_files(tar, name_archive, d_content, logger, f_exclude=None):
         try:
             key=local_path+"->"+in_archive
             if key not in already_added:
-                tar.add(local_path, arcname=in_archive, exclude=f_exclude)
+                if old_python:
+                    tar.add(local_path,
+                                 arcname=in_archive,
+                                 exclude=exclude_VCS_and_extensions_26)
+                else:
+                    tar.add(local_path,
+                                 arcname=in_archive,
+                                 filter=exclude_VCS_and_extensions)
                 already_added.add(key)
             logger.write(src.printcolors.printcSuccess(_("OK")), 3)
         except Exception as e:
@@ -162,9 +173,10 @@ def add_files(tar, name_archive, d_content, logger, f_exclude=None):
         logger.write("\n", 3)
     return success
 
-def exclude_VCS_and_extensions(filename):
+
+def exclude_VCS_and_extensions_26(filename):
     ''' The function that is used to exclude from package the link to the 
-        VCS repositories (like .git)
+        VCS repositories (like .git) (only for python 2.6)
 
     :param filename Str: The filname to exclude (or not).
     :return: True if the file has to be exclude
@@ -178,6 +190,23 @@ def exclude_VCS_and_extensions(filename):
             return True
     return False
 
+def exclude_VCS_and_extensions(tarinfo):
+    ''' The function that is used to exclude from package the link to the 
+        VCS repositories (like .git)
+
+    :param filename Str: The filname to exclude (or not).
+    :return: None if the file has to be exclude
+    :rtype: tarinfo or None
+    '''
+    filename = tarinfo.name
+    for dir_name in IGNORED_DIRS:
+        if dir_name in filename:
+            return None
+    for extension in IGNORED_EXTENSIONS:
+        if filename.endswith(extension):
+            return None
+    return tarinfo
+
 def produce_relative_launcher(config,
                               logger,
                               file_dir,
@@ -196,11 +225,18 @@ def produce_relative_launcher(config,
     :rtype: str
     '''
     
+    # set base mode to "no" for the archive - save current mode to restore it at the end
+    if "base" in config.APPLICATION:
+        base_setting=config.APPLICATION.base 
+    else:
+        base_setting="maybe"
+    config.APPLICATION.base="no"
+
     # get KERNEL installation path 
     kernel_info = src.product.get_product_config(config, "KERNEL")
     kernel_base_name=os.path.basename(kernel_info.install_dir)
-    if kernel_base_name.startswith("config"):
-        # case of kernel installed in base. We remove "config-i"
+    if kernel_info.install_mode == "base":
+        # case of kernel installed in base. the kernel install dir name is different in the archive
         kernel_base_name=os.path.basename(os.path.dirname(kernel_info.install_dir))
     
     kernel_root_dir = os.path.join(binaries_dir_name, kernel_base_name)
@@ -279,6 +315,9 @@ def produce_relative_launcher(config,
              stat.S_IXGRP |
              stat.S_IXOTH)
 
+    # restore modified setting by its initial value
+    config.APPLICATION.base=base_setting
+
     return filepath
 
 def hack_for_distene_licence(filepath, licence_file):
@@ -330,7 +369,8 @@ def hack_for_distene_licence(filepath, licence_file):
 def produce_relative_env_files(config,
                               logger,
                               file_dir,
-                              binaries_dir_name):
+                              binaries_dir_name,
+                              exe_name=None):
     '''Create some specific environment files for the binary package. These 
        files use relative paths.
     
@@ -339,9 +379,18 @@ def produce_relative_env_files(config,
     :param file_dir str: the directory where to put the files
     :param binaries_dir_name str: the name of the repository where the binaries
                                   are, in the archive.
+    :param exe_name str: if given generate a launcher executing exe_name
     :return: the list of path of the produced environment files
     :rtype: List
     '''  
+
+    # set base mode to "no" for the archive - save current mode to restore it at the end
+    if "base" in config.APPLICATION:
+        base_setting=config.APPLICATION.base 
+    else:
+        base_setting="maybe"
+    config.APPLICATION.base="no"
+
     # create an environment file writer
     writer = src.environment.FileEnvWriter(config,
                                            logger,
@@ -355,6 +404,9 @@ def produce_relative_env_files(config,
       shell = "bash"
       filename  = "env_launch.sh"
 
+    if exe_name:
+        filename=os.path.basename(exe_name)
+
     # Write
     filepath = writer.write_env_file(filename,
                           False, # for launch
@@ -365,8 +417,18 @@ def produce_relative_env_files(config,
     if src.architecture.is_windows() :
       src.replace_in_file(filepath, '"out_dir_Path', '"%out_dir_Path%' )
       src.replace_in_file(filepath, '=out_dir_Path', '=%out_dir_Path%' )
+      src.replace_in_file(filepath, ';out_dir_Path', ';%out_dir_Path%' )
     else:
       src.replace_in_file(filepath, '"out_dir_Path', '"${out_dir_Path}' )
+      src.replace_in_file(filepath, ':out_dir_Path', ':${out_dir_Path}' )
+
+    if exe_name:
+        if src.architecture.is_windows():
+            cmd="\n\nrem Launch exe with user arguments\n%s " % exe_name + "%*"
+        else:
+            cmd='\n\n# Launch exe with user arguments\n%s "$*"' % exe_name
+        with open(filepath, "a") as exe_launcher:
+            exe_launcher.write(cmd)
 
     # change the rights in order to make the file executable for everybody
     os.chmod(filepath,
@@ -378,6 +440,9 @@ def produce_relative_env_files(config,
              stat.S_IXGRP |
              stat.S_IXOTH)
     
+    # restore modified setting by its initial value
+    config.APPLICATION.base=base_setting
+
     return filepath
 
 def produce_install_bin_file(config,
@@ -521,6 +586,11 @@ def binary_package(config, logger, options, tmp_working_dir):
     l_products_name = sorted(config.APPLICATION.products.keys())
     l_product_info = src.product.get_products_infos(l_products_name,
                                                     config)
+
+    # suppress compile time products for binaries-only archives
+    if not options.sources:
+        update_config(config, logger, "compile_time", "yes")
+
     l_install_dir = []
     l_source_dir = []
     l_not_installed = []
@@ -532,6 +602,8 @@ def binary_package(config, logger, options, tmp_working_dir):
         config.APPLICATION.properties.mesa_launcher_in_package == "yes") :
             generate_mesa_launcher=True
 
+    # first loop on products : filter products, analyse properties,
+    # and store the information that will be used to create the archive in the second loop 
     for prod_name, prod_info in l_product_info:
         # skip product with property not_in_package set to yes
         if src.get_property_in_product_cfg(prod_info, "not_in_package") == "yes":
@@ -551,8 +623,13 @@ def binary_package(config, logger, options, tmp_working_dir):
                 or src.product.product_is_fixed(prod_info)
                 or not src.product.product_compiles(prod_info)):
             continue
+        # 
+        # products with single_dir property will be installed in the PRODUCTS directory of the archive
+        is_single_dir=(src.appli_test_property(config,"single_install_dir", "yes") and \
+                       src.product.product_test_property(prod_info,"single_install_dir", "yes"))
         if src.product.check_installation(config, prod_info):
-            l_install_dir.append((prod_name, prod_info.install_dir))
+            l_install_dir.append((prod_name, prod_info.name, prod_info.install_dir,
+                                  is_single_dir, prod_info.install_mode))
         else:
             l_not_installed.append(prod_name)
         
@@ -564,7 +641,7 @@ def binary_package(config, logger, options, tmp_working_dir):
                                            config.INTERNAL.config.install_dir,
                                            name_cpp) 
                 if os.path.exists(install_dir):
-                    l_install_dir.append((name_cpp, install_dir))
+                    l_install_dir.append((name_cpp, name_cpp, install_dir, False, "value"))
                 else:
                     l_not_installed.append(name_cpp)
         
@@ -627,11 +704,16 @@ WARNING: existing binaries directory from previous detar installation:
     # construct the correlation table between the product names, there 
     # actual install directories and there install directory in archive
     d_products = {}
-    for prod_name, install_dir in l_install_dir:
+    for prod_name, prod_info_name, install_dir, is_single_dir, install_mode in l_install_dir:
         prod_base_name=os.path.basename(install_dir)
-        if prod_base_name.startswith("config"):
-            # case of a products installed in base. We remove "config-i"
-            prod_base_name=os.path.basename(os.path.dirname(install_dir))
+        if install_mode == "base":
+            # case of a products installed in base. 
+            # because the archive is in base:no mode, the name of the install dir is different inside archive
+            # we set it to the product name or by PRODUCTS if single-dir
+            if is_single_dir:
+                prod_base_name=config.INTERNAL.config.single_install_dir
+            else:
+                prod_base_name=prod_info_name
         path_in_archive = os.path.join(binaries_dir_name, prod_base_name)
         d_products[prod_name + " (bin)"] = (install_dir, path_in_archive)
         
@@ -705,6 +787,22 @@ WARNING: existing binaries directory from previous detar installation:
     else:
       filename  = "env_launch.sh"
     d_products["environment file"] = (env_file, filename)      
+
+    # If option exe, produce an extra launcher based on specified exe
+    if options.exe:
+        exe_file = produce_relative_env_files(config,
+                                              logger,
+                                              tmp_working_dir,
+                                              binaries_dir_name,
+                                              options.exe)
+            
+        if src.architecture.is_windows():
+          filename  = os.path.basename(options.exe) + ".bat"
+        else:
+          filename  = os.path.basename(options.exe) + ".sh"
+        d_products["exe file"] = (exe_file, filename)      
+    
+
     return d_products
 
 def source_package(sat, config, logger, options, tmp_working_dir):
@@ -941,9 +1039,14 @@ def make_archive(prod_name, prod_info, where):
     path_targz_prod = os.path.join(where, prod_name + PACKAGE_EXT)
     tar_prod = tarfile.open(path_targz_prod, mode='w:gz')
     local_path = prod_info.source_dir
-    tar_prod.add(local_path,
-                 arcname=prod_name,
-                 exclude=exclude_VCS_and_extensions)
+    if old_python:
+        tar_prod.add(local_path,
+                     arcname=prod_name,
+                     exclude=exclude_VCS_and_extensions_26)
+    else:
+        tar_prod.add(local_path,
+                     arcname=prod_name,
+                     filter=exclude_VCS_and_extensions)
     tar_prod.close()
     return path_targz_prod       
 
@@ -1105,6 +1208,16 @@ def find_product_scripts_and_pyconf(p_name,
                     product_pyconf_cfg[section].archive_info.archive_name =\
                         p_info.name + ".tgz"
     
+    if (with_vcs) and src.product.product_is_vcs(p_info):
+        # in vcs mode we must replace explicitely the git server url
+        # (or it will not be found later because project files are not exported in archives)
+        for section in product_pyconf_cfg:
+            # replace in all sections of the product pyconf the git repo definition by its substitued value (found in p_info)
+            if "git_info" in product_pyconf_cfg[section]:
+                for repo in product_pyconf_cfg[section].git_info:
+                    if repo in p_info.git_info:
+                        product_pyconf_cfg[section].git_info[repo] =  p_info.git_info[repo]
+
     # write the pyconf file to the temporary project location
     product_tmp_pyconf_path = os.path.join(products_pyconf_tmp_dir,
                                            p_name + ".pyconf")
@@ -1130,9 +1243,10 @@ def write_application_pyconf(config, application_tmp_dir):
         f.write("#!/usr/bin/env python\n#-*- coding:utf-8 -*-\n\n")
         res = src.pyconf.Config()
         app = src.pyconf.deepCopyMapping(config.APPLICATION)
-        # no base in packages
-        if "base" in app:
-            app.base = "no" 
+
+        # set base mode to "no" for the archive
+        app.base = "no"
+
         # Change the workdir
         app.workdir = src.pyconf.Reference(
                                  app,
@@ -1322,7 +1436,7 @@ The procedure to do it is:
 
         if options.binaries or options.sources:
             d['application'] = config.VARS.application
-            d['BINARIES']    = config.INTERNAL.config.install_dir
+            d['BINARIES']    = config.INTERNAL.config.binary_dir
             d['SEPARATOR'] = config.VARS.sep
             if src.architecture.is_windows():
                 d['operatingSystem'] = 'Windows'
@@ -1363,7 +1477,7 @@ The procedure to do it is:
     
     return readme_path
 
-def update_config(config, prop, value):
+def update_config(config, logger,  prop, value):
     '''Remove from config.APPLICATION.products the products that have the property given as input.
     
     :param config Config: The global config.
@@ -1379,6 +1493,7 @@ def update_config(config, prop, value):
                 l_product_to_remove.append(product_name)
         for product_name in l_product_to_remove:
             config.APPLICATION.products.__delitem__(product_name)
+            logger.write("Remove product %s with property %s\n" % (product_name, prop), 5)
 
 def description():
     '''method that is called when salomeTools is called with --help option.
@@ -1469,15 +1584,12 @@ Please add it in file:
     
     # Remove the products that are filtered by the --without_properties option
     if options.without_properties:
-        app = runner.cfg.APPLICATION
-        logger.trace("without_properties all products:\n %s\n" % PP.pformat(sorted(app.products.keys())))
         prop, value = options.without_properties
-        update_config(runner.cfg, prop, value)
-        logger.warning("without_properties selected products:\n %s\n" % PP.pformat(sorted(app.products.keys())))
+        update_config(runner.cfg, logger, prop, value)
 
     # Remove from config the products that have the not_in_package property
-    update_config(runner.cfg, "not_in_package", "yes")
-    
+    update_config(runner.cfg, logger, "not_in_package", "yes")
+
     # get the name of the archive or build it
     if options.name:
         if os.path.basename(options.name) == options.name:
@@ -1626,7 +1738,10 @@ Please add it in file:
         tar = tarfile.open(path_targz, mode='w:gz')
         
         # get the filtering function if needed
-        filter_function = exclude_VCS_and_extensions
+        if old_python:
+            filter_function = exclude_VCS_and_extensions_26
+        else:
+            filter_function = exclude_VCS_and_extensions
 
         # Add the files to the tarfile object
         res = add_files(tar, archive_name, d_files_to_add, logger, f_exclude=filter_function)