Salome HOME
sat #8581 : ajout d'une fonctionnalité de description fine de la version de salomeToo...
[tools/sat.git] / src / __init__.py
index e1696dbfd0f46aadee33ab685559c34258d83468..ca569a05d6a89c5f85857af4e7e7a1dfbab07376 100644 (file)
@@ -27,6 +27,7 @@ import errno
 import stat
 import fnmatch
 import pprint as PP
+from ftplib import FTP
 
 from . import pyconf
 from . import architecture
@@ -146,7 +147,7 @@ def getProductNames(cfg, wildcards, logger):
         if len(filtered) > 0:
           res.append(prod)
           ok = True
-          break
+          continue
       if not ok:
         notFound[wild] = None
     if len(res) == 0:
@@ -227,6 +228,22 @@ def get_log_path(config):
     
     return log_dir_path
 
+def get_salometool_version(config):
+   """Return the salomeTool version.
+
+   :param config Config: The global Config instance.
+   :return: the description of this version of sat in terms of tag and commit
+   """
+   # we use : 
+   # config.VARS.salometoolsway : the full path of salomeTool
+   # config.INTERNAL.sat_version : the static salomeTool version, 
+   # in case we are not in a git repo  
+   sat_version=system.git_describe(config.VARS.salometoolsway) 
+   if sat_version == False:
+       return config.INTERNAL.sat_version
+   else:
+       return sat_version
+
 def get_salome_version(config):
     import versionMinorMajorPatch as VMMP
 
@@ -403,6 +420,61 @@ 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):
+    """\
+    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
+    :return: the full path of the file or False if not found
+    :param logger Logger: The logging instance to use for the prints.
+    :rtype: str
+    """
+
+    # make sure installation_dir exists
+    if not os.path.exists(installation_dir):
+        os.makedirs(installation_dir)
+
+    destination=os.path.join(installation_dir, file_name)
+
+    # paths in ftppath may contain several paths separated by ":"
+    # we plit them, and push all paths in bigftppath
+    bigftppath=[]
+    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/...
+           ftp_archive_split=ftp_archive.split("/")
+           ftp_server=ftp_archive_split[0]
+           ftp = FTP(ftp_server)
+           logger.write("   Connect to ftp server %s\n" % ftp_server, 3)
+           ftp.login()
+           for directory in ftp_archive_split[1:]:
+               logger.write("   Change directory to %s\n" % directory, 3)
+               ftp.cwd(directory)
+       except:
+           logger.error("while connecting to ftp server %s\n" % ftp_server)
+
+       try:
+           if ftp.size(file_name) > 0:
+               # if file exists and is non empty
+               with open(destination,'wb') as dest_file:
+                   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:
+           logger.error("File not found in ftp_archive %s\n" % ftp_server)
+           pass
+
+    return False
+
 def handleRemoveReadonly(func, path, exc):
     excvalue = exc[1]
     if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES: