Salome HOME
spns #42205 [SAT][Windows] support different values for CMAKE_BUILD_TYPE - define...
[tools/sat.git] / src / __init__.py
index ad0997b4f816e1c0aa5ecc563f02250cc5bd816c..7cc648691267fed9fa48964145300194eab774f8 100644 (file)
@@ -53,22 +53,26 @@ KO_STATUS = "KO"
 NA_STATUS = "NA"
 KNOWNFAILURE_STATUS = "KF"
 TIMEOUT_STATUS = "TIMEOUT"
-
 class SatException(Exception):
-    """rename Exception Class"""
-    pass
+    """sat exception class"""
+    def message(self, arg):
+        if sys.version_info[0] >= 3:
+            # message method is not available for python 3.8+
+            return super().msg(arg)
+        else:
+            return super(SatException,self).message(arg)
 
 def ensure_path_exists(p):
     """Create a path if not existing
-    
+
     :param p str: The path.
     """
     if not os.path.exists(p):
         os.makedirs(p)
-        
+
 def check_config_has_application( config, details = None ):
     """check that the config has the key APPLICATION. Else raise an exception.
-    
+
     :param config class 'common.pyconf.Config': The config.
     """
     if 'APPLICATION' not in config:
@@ -77,11 +81,22 @@ def check_config_has_application( config, details = None ):
             details.append(message)
         raise SatException( message )
 
+def check_platform_is_supported( config, logger ):
+    """check that the platform is supported, write warning if not.
+
+    :param config class 'common.pyconf.Config': The config.
+    """
+    if 'platform' in config.APPLICATION and config.VARS.dist not in config.APPLICATION.platform:
+        msg = "WARNING: Your application configuration is not supported on this platform (%s)\n"\
+              "         Please consider using the native application!" % config.VARS.dist
+        logger.write("\n%s\n\n" % printcolors.printcWarning(msg), 1)
+    return
+
 def check_config_has_profile( config, details = None ):
     """\
     check that the config has the key APPLICATION.profile.
     else, raise an exception.
-    
+
     :param config class 'common.pyconf.Config': The config.
     """
     check_config_has_application(config)
@@ -91,6 +106,20 @@ def check_config_has_profile( config, details = None ):
             details.append(message)
         raise SatException( message )
 
+def check_application_syntax_deprecated(config, logger):
+    """\
+    check that the application has the key git_server.
+    else, raise a warning
+
+    :param config class 'common.pyconf.Config': The config.
+    :param logger Logger: The logging instance to use for the prints.
+    """
+    if 'APPLICATION' in config and 'properties' in config.APPLICATION and not 'git_server' in config.APPLICATION.properties :
+        msg = 'WARNING: Your application is using repo_dev key which is deprecated and will be removed from future SAT releases!\n'
+        msg+= '         Please upgrade your application configuration file and add a valid git_server key.\n'
+        msg+= '         git_server key values need to be defined in the project file (e.g. salome.pyconf)!'
+        logger.write("\n%s\n\n" % printcolors.printcWarning(msg), 1)
+
 def appli_test_property(config,property_name, property_value):
     """Generic function to test if an application has a property set to a value
     :param config class 'common.pyconf.Config': The config.
@@ -110,7 +139,6 @@ def appli_test_property(config,property_name, property_value):
                       (property_name,property_value)
     result = eval(eval_expression)
     return result
-    
 
 def config_has_application( config ):
     return 'APPLICATION' in config
@@ -118,10 +146,10 @@ def config_has_application( config ):
 def get_cfg_param(config, param_name, default):
     """\
     eearch for param_name value in config.
-    if param_name is not in config 
+    if param_name is not in config
     then return default,
     else return the found value
-       
+
     :param config class 'common.pyconf.Config': The config.
     :param param_name str: the name of the parameter to get the value
     :param default str: The value to return if param_name is not in config
@@ -183,7 +211,7 @@ def getProductNames(cfg, wildcards, logger):
 def print_info(logger, info):
     """\
     Prints the tuples that are in info variable in a formatted way.
-    
+
     :param logger Logger: The logging instance to use for the prints.
     :param info list: The list of tuples to display
     """
@@ -198,7 +226,7 @@ def print_info(logger, info):
 def get_base_path(config):
     """\
     Returns the path of the products base.
-    
+
     :param config Config: The global Config instance.
     :return: The path of the products base.
     :rtype: str
@@ -209,15 +237,15 @@ def get_base_path(config):
                                       "local.pyconf")
         msg = _("Please define a base path in the file %s" % local_file_path)
         raise SatException(msg)
-        
+
     base_path = os.path.abspath(config.LOCAL.base)
-    
+
     return base_path
 
 def get_launcher_name(config):
     """\
     Returns the name of salome launcher.
-    
+
     :param config Config: The global Config instance.
     :return: The name of salome launcher.
     :rtype: str
@@ -230,10 +258,27 @@ def get_launcher_name(config):
 
     return launcher_name
 
+def get_launcher_exe(config):
+    """\
+    Returns the name of exe defined in profile section.
+
+    :param config Config: The global Config instance.
+    :return: The name of the exe to use in launcher.
+    :rtype: str
+    """
+    check_config_has_application(config)
+    if 'profile' in config.APPLICATION and 'exe' in config.APPLICATION.profile:
+        exe_name = config.APPLICATION.profile.exe
+    else:
+        exe_name = None
+
+    return exe_name
+
+
 def get_log_path(config):
     """\
     Returns the path of the logs.
-    
+
     :param config Config: The global Config instance.
     :return: The path of the logs.
     :rtype: str
@@ -244,9 +289,9 @@ def get_log_path(config):
                                       "local.pyconf")
         msg = _("Please define a log_dir in the file %s" % local_file_path)
         raise SatException(msg)
-      
+
     log_dir_path = os.path.abspath(config.LOCAL.log_dir)
-    
+
     return log_dir_path
 
 def get_salometool_version(config):
@@ -276,9 +321,8 @@ def get_salome_version(config):
           line = f.readline()  # example: '[SALOME KERNEL] : 8.4.0'
         version = VMMP.MinorMajorPatch(line.split(":")[1])
 
-    res = version.strCompact()
-    # print("get_salome_version %s -> %s" % (version, res))
-    return int(res) # TODO may be future avoid test(s) on integer, use MajorMinorPatch
+    # from nov. 2023 and SALOME 9.10.0 forbid test(s) on integer, use MajorMinorPatch class tests
+    return version
 
 def read_config_from_a_file(filePath):
         try:
@@ -334,14 +378,14 @@ class Path:
         return Path(os.path.basename(self.path))
 
     def make(self, mode=None):
-        os.makedirs(self.path)        
+        os.makedirs(self.path)
         if mode:
             os.chmod(self.path, mode)
-        
+
     def chmod(self, mode):
         os.chmod(self.path, mode)
 
-    def rm(self):    
+    def rm(self):
         if self.islink():
             os.remove(self.path)
         else:
@@ -371,14 +415,14 @@ class Path:
         try:
             os.symlink(str(path), self.path)
             return True
-        except:
+        except Exception:
             return False
 
     def copylink(self, path):
         try:
             os.symlink(os.readlink(self.path), str(path))
             return True
-        except:
+        except Exception:
             return False
 
     def copydir(self, dst, smart=False):
@@ -397,27 +441,27 @@ class Path:
                 dstname = dst + name
                 srcname.copy(dstname, smart)
             return True
-        except:
+        except Exception:
             return False
 
     def copyfile(self, path):
         try:
             shutil.copy2(self.path, str(path))
             return True
-        except:
+        except Exception:
             return False
 
 def find_file_in_lpath(file_name, lpath, additional_dir = ""):
     """\
     Find in all the directories in lpath list the file that has the same name
-    as file_name. 
-    If it is found 
+    as file_name.
+    If it is found
     then return the full path of the file
     else return False.
-    The additional_dir (optional) is the name of the directory to add to all 
+
+    The additional_dir (optional) is the name of the directory to add to all
     paths in lpath.
-    
+
     :param file_name str: The file name to search
     :param lpath List: The list of directories where to search
     :param additional_dir str: The name of the additional directory
@@ -434,13 +478,13 @@ def find_file_in_lpath(file_name, lpath, additional_dir = ""):
                 return os.path.join(dir_complete, file_name)
     return False
 
-def find_file_in_ftppath(file_name, ftppath, installation_dir, logger):
+def find_file_in_ftppath(file_name, ftppath, installation_dir, logger, additional_dir = ""):
     """\
     Find in all ftp servers in ftppath the file called file_name
     If it is found then return the destination path of the file
     (the place where the file was downloaded"
     else return False.
-    
+
     :param file_name str: The file name to search
     :param ftppath, List: The list of ftp servers where to search
     :param installation_dir str: The name of the installation directory
@@ -461,7 +505,7 @@ def find_file_in_ftppath(file_name, ftppath, installation_dir, logger):
     for ipath in ftppath:
         splpath=ipath.split(":")
         bigftppath+=splpath
-        
+
     for ftp_archive in bigftppath:
        try:
            # ftp_archive has the form ftp.xxx.yyy/dir1/dir2/...
@@ -473,8 +517,20 @@ def find_file_in_ftppath(file_name, ftppath, installation_dir, logger):
            for directory in ftp_archive_split[1:]:
                logger.write("   Change directory to %s\n" % directory, 3)
                ftp.cwd(directory)
-       except:
+           if additional_dir:
+               ftp.cwd(additional_dir)
+       except Exception:
            logger.error("while connecting to ftp server %s\n" % ftp_server)
+           continue
+
+       try:  # get md5 file if it exists
+           file_name_md5=file_name + ".md5"
+           destination_md5=destination + ".md5"
+           if ftp.size(file_name_md5) > 0:
+               with open(destination_md5,'wb') as dest_file_md5:
+                   ftp.retrbinary("RETR "+file_name_md5, dest_file_md5.write)
+       except Exception:
+           pass
 
        try:
            if ftp.size(file_name) > 0:
@@ -483,9 +539,8 @@ def find_file_in_ftppath(file_name, ftppath, installation_dir, logger):
                    ftp.retrbinary("RETR "+file_name, dest_file.write)
                logger.write("   Archive %s was retrieved and stored in %s\n" % (file_name, destination), 3)
                return destination
-       except:
+       except Exception:
            logger.error("File not found in ftp_archive %s\n" % ftp_server)
-           pass
 
     return False
 
@@ -500,7 +555,7 @@ def handleRemoveReadonly(func, path, exc):
 def deepcopy_list(input_list):
     """\
     Do a deep copy of a list
-    
+
     :param input_list List: The list to copy
     :return: The copy of the list
     :rtype: List
@@ -513,7 +568,7 @@ def deepcopy_list(input_list):
 def remove_item_from_list(input_list, item):
     """\
     Remove all occurences of item from input_list
-    
+
     :param input_list List: The list to modify
     :return: The without any item
     :rtype: List
@@ -528,7 +583,7 @@ def remove_item_from_list(input_list, item):
 def parse_date(date):
     """\
     Transform YYYYMMDD_hhmmss into YYYY-MM-DD hh:mm:ss.
-    
+
     :param date str: The date to transform
     :return: The date in the new format
     :rtype: str
@@ -572,7 +627,7 @@ def get_property_in_product_cfg(product_cfg, pprty):
 
 def activate_mesa_property(config):
     """Add mesa property into application properties
-    
+
     :param config Config: The global configuration. It must have an application!
     """
     # Verify the existence of the file
@@ -580,3 +635,26 @@ def activate_mesa_property(config):
         config.APPLICATION.addMapping( 'properties', pyconf.Mapping(), None )
     config.APPLICATION.properties.use_mesa="yes"
 
+def git_server_has_all_repositories( config, the_git_server):
+    """check that the git  server contains all repositories (closed and open)
+    :param config class 'common.pyconf.Config': The config.
+    :param logger Logger: The logging instance to use for the prints.
+    """
+    if 'opensource_git_servers' in config.VARS:
+        for git_server in config.VARS['opensource_git_servers']:
+            if git_server == the_git_server:
+                return False
+    return True
+
+def get_git_server(config, logger):
+    the_git_server= None
+    has_properties =  'properties' in config.APPLICATION
+    if has_properties and "git_server" in config.APPLICATION.properties:
+        the_git_server =  config.APPLICATION.properties.git_server
+    elif has_properties and "repo_dev" in config.APPLICATION.properties:
+        # Fall back to deprecated approach but issue a warning that this approach is deprecated
+        if config.APPLICATION.properties.repo_dev == 'yes':
+            the_git_server = [ git_server for git_server in config.VARS.git_servers if git_server not in config.VARS.opensource_git_servers][0]
+        else:
+            the_git_server =  [ git_server for git_server in config.VARS.git_servers if git_server in config.VARS.opensource_git_servers][0]
+    return the_git_server