+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
+ :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)
+ if additional_dir:
+ ftp.cwd(additional_dir)
+ except:
+ 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:
+ pass
+
+ 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)
+
+ return False
+