wip: data backend ftp/ftps

This commit is contained in:
Yannik Schmidt
2024-02-26 00:11:18 +01:00
parent 7ae77b339d
commit 0de6b41a98

View File

@@ -10,7 +10,8 @@ class DataBackend:
def _create_cache_dir(self, cache_dir): def _create_cache_dir(self, cache_dir):
os.makedirs(cache_dir, exist_ok=True) os.makedirs(cache_dir, exist_ok=True)
def __init__(self, user, password, install_dir, server=None, remote_root_dir=None): def __init__(self, user, password, install_dir, server=None, remote_root_dir=None,
progress_bar=None, tkinter_root=None):
self.user = user self.user = user
self.password = password self.password = password
@@ -18,6 +19,8 @@ class DataBackend:
self.server = server self.server = server
self.install_dir = install_dir self.install_dir = install_dir
self.pro
def get(self, path, return_content=False): def get(self, path, return_content=False):
'''Return the contents of this path''' '''Return the contents of this path'''
raise NotImplementedError() raise NotImplementedError()
@@ -130,15 +133,7 @@ class FTP(DataBackend):
# run with callback # # run with callback #
ftp.retrbinary('RETR ' + fullpath, callback) ftp.retrbinary('RETR ' + fullpath, callback)
with open(fullpath, "rb") as f: return local_file
print(cache_dir, path)
target = os.path.join(cache_dir, os.path.basename(path))
with open(target, "wb") as ft:
if return_content:
return f.read()
ft.write(f.read())
return target
def list(self, path, fullpaths=False): def list(self, path, fullpaths=False):
@@ -150,19 +145,29 @@ class FTP(DataBackend):
if not os.path.isdir(fullpath): if not os.path.isdir(fullpath):
return [] return []
if fullpaths: ftp = self._connect()
return [ os.path.join(path, filename) for filename in os.listdir(fullpath)] try:
paths = ftp.nlst(fullpath)
if not fullpaths:
return paths
return [ os.path.join(path, filename) for filename in paths ]
except ftplib.error_perm as e:
if str(e) == "550 No files found":
print("No files in this directory: {}".format(fullpath))
return []
else: else:
return os.listdir(fullpath) raise e
def find_all_metadata(self): def find_all_metadata(self):
meta_info_list = [] local_meta_file_list = []
for software_dir in glob.iglob(self.remote_root_dir + "/*"):
meta_file = os.path.join(software_dir, "meta.yaml")
if not os.path.isfile(meta_file):
continue
else:
meta_info_list.append(software.Software(meta_file, self))
return meta_info_list root_elements = self.list(self.remote_root_dir)
for s in root_elements:
files = self.list(s, fullpaths=True)
for f in files:
if f.endswith("meta.yaml"):
local_meta_file = self.get(meta_file)
local_meta_file_list.append(local_meta_file)
return [ software.Software(meta_file, self) for meta_file in local_meta_file_list ]