Commit 4251bcdb authored by Sergio Bruni's avatar Sergio Bruni

commmit

parent e402f751
......@@ -701,7 +701,7 @@ class DaemonGPS(daemon.Daemon):
try:
o = urlparse(self.conf_rete["org"])
if re.search('ftp', o.scheme):
if re.search('^ftp$', o.scheme):
user = self.conf_rete["user"] if 'user' in self.conf_rete else self.conf_global["download"][
"ftp_anonymous_user"]
password = self.conf_rete["password"] if 'password' in self.conf_rete else \
......@@ -715,6 +715,18 @@ class DaemonGPS(daemon.Daemon):
else:
return ftp.FTP_GENERIC(self.conf_rete["org"], self.conf_rete["ftp_params"], self.logger, user, password, self.conf_global["test"])
elif re.search('^sftp$', o.scheme):
user = self.conf_rete["user"]
password = self.conf_rete["password"]
ftpClass = 'SFTP_' + self.conf_rete["name"]
if ftpClass in ftp.get_classes():
MyClass = getattr(importlib.import_module('ingv.ftp'), ftpClass)
return MyClass(self.conf_rete["org"], self.conf_rete["ftp_params"], self.logger, user, password, self.conf_global["test"])
else:
return ftp.SFTP_GENERIC(self.conf_rete["org"], self.conf_rete["ftp_params"], self.logger, user, password, self.conf_global["test"])
elif re.search('http', o.scheme):
user = self.conf_rete["user"] if 'user' in self.conf_rete else ''
password = self.conf_rete["password"] if 'password' in self.conf_rete else ''
......
......@@ -10,6 +10,7 @@ import time
import shutil
from urllib.parse import urlparse
import sys
import pysftp
#from signal import signal, SIGPIPE, SIG_DFL
#signal(SIGPIPE,SIG_DFL)
......@@ -236,3 +237,109 @@ class ImplicitFTP_TLS(ftplib.FTP_TLS, object):
value = self.context.wrap_socket(value)
self._sock = value
class SFTP_GENERIC:
def __init__(self, org, params, logger, user, password, test):
self.logger = logger
self.test = test
self.org = org
self.params = params
self.user = user
self.password = password
self.ftp = None
# -------------
def connect(self):
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
o = urlparse(self.org)
self.ftp = pysftp.Connection(host=o.hostname,
username=self.user,
password=self.password,
cnopts=cnopts)
return True
# if self.ftp:
# if self.is_connected(): return True
# self.ftp = None
# retry_counter = 0
# while retry_counter < 3:
# time.sleep((retry_counter + 1) * 10)
# try:
# o = urlparse(self.org)
# if o.scheme == "ftp":
# self.ftp = FTP(o.hostname, self.params, self.user, self.password)
# elif o.scheme == "ftps":
# self.ftp = ImplicitFTP_TLS(o.hostname, self.params, self.user, self.password)
# else:
# self.logger.error("[%s] Protocol [%s] is not managed from class [%s]" % (__name__, o.scheme, self.__class__))
# return False
# return True
# except Exception as e:
# retry_counter += 1
# self.logger.error(
# "[%s] connection failed! attempt number: [%d] Error: [%s]" % (__name__, retry_counter, str(e)))
# self.logger.error(
# "[%s] connection definitively failed! made [%d] attempts" % (__name__, retry_counter))
# return False
# def reconnect(self):
# try:
# self.ftp.quit()
# except Exception as e:
# self.logger.error("[%s] reconnect ftp.quit failed!!! error: %s" % (__name__, str(e)))
# finally:
# self.ftp = None
# time.sleep(10)
# return self.connect()
def get_file(self, remote_full_name, dest_full_name):
self.ftp.get(remote_full_name, dest_full_name)
return True
# if not self.connect(): return False
#
# retry_count = 0
#
# while retry_count <= 1:
#
# if retry_count == 1:
# if not self.reconnect(): return False
#
# remote_dir_name = os.path.dirname(remote_full_name)
# remote_file_name = os.path.basename(remote_full_name)
#
# try:
# if self.test:
# f = open(dest_full_name, 'w')
# f.write('test\n')
# f.close()
# return True
#
# remote_file_full_name = os.path.join(remote_dir_name, remote_file_name)
# with open(dest_full_name, "wb") as f:
# self.ftp.retrbinary("RETR " + remote_file_full_name, f.write)
# return True
#
# except ftplib.all_errors as e:
# if os.path.exists(dest_full_name):
# os.remove(dest_full_name)
# self.logger.error("[%s] get_file remote file: [%s] retry_count=%d Error Message [%s]" % (__name__, remote_full_name, retry_count, str(e)))
# retry_count += 1
#
# return False
def list_remote_files(self, remote_dir, wildcard = '*'):
filelist = []
try:
self.ftp.cwd(remote_dir)
except Exception as e:
self.logger.error("[%s] list_remote_files Error: [%s] remote path [%s] does not exist" % (__name__, str(e), remote_dir))
return filelist
directory_structure = self.ftp.listdir_attr()
for attr in directory_structure:
if fnmatch.fnmatch(attr.filename, wildcard):
filelist.append(attr.filename)
return filelist
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment