This commit is contained in:
Amber 2024-10-08 10:47:54 +02:00
parent 4a019c55b5
commit 6b6cdf08b8

View File

@ -69,30 +69,6 @@ class AdvancedSftpClientAgent(BaseAgent):
continue continue
print('name: %s is regular FILE' % (attr.filename)) print('name: %s is regular FILE' % (attr.filename))
# def put(self, localpath, remotepath, callback=None):
# '''
# wrap the method put of paramiko sftp client
# Copy a local file (localpath) to the SFTP server as remotepath.
# Any exception raised by operations will be passed through. This method is primarily provided as a convenience.
# '''
# sftpc = self.get_sftp_client()
# sftpc.put(localpath, remotepath, callback=callback)
#
# def get(self, localpath, remotepath, callback=None):
# sftpc = self.get_sftp_client()
# sftpc.get(localpath, remotepath, callback=callback)
# def open(self, remotepath):
# sftpc = self.get_sftp_client()
# buf = ''
# with sftpc.open(remotepath,mode='r') as rfile:
# buf = rfile.read()
# return buf
# def mkdir(self, path):
# sftpc = self.get_sftp_client()
# sftpc.mkdir(path)
def rpath_exits(self, path): def rpath_exits(self, path):
sftpc = self.get_sftp_client() sftpc = self.get_sftp_client()
try: try:
@ -101,6 +77,14 @@ class AdvancedSftpClientAgent(BaseAgent):
if e.errno == errno.ENOENT: return False if e.errno == errno.ENOENT: return False
return True return True
def remove_file(self, remote_file_path):
'''
@param remote_file_path absolute path of file to remove, must exists
'''
sftpc = self.get_sftp_client()
print(f'Remove: {remote_file_path}')
return sftpc.remove(remote_file_path)
def remove_dir(self, absolute_path, dry_run=False): def remove_dir(self, absolute_path, dry_run=False):
''' '''
Here we assume that absolute_path on remote can be not empty Here we assume that absolute_path on remote can be not empty
@ -137,11 +121,26 @@ class AdvancedSftpClientAgent(BaseAgent):
return remove_dir_recursive(absolute_path) return remove_dir_recursive(absolute_path)
## move in SftpClientAgent
def copy_file(self, localpath, remotepath, callback=None, confirm=False):
'''
put(localpath, remotepath, callback=None, confirm=True)
remotepath must contains the name of file you want to copy
'''
def def_cback(size, filesize):
print(f'{localpath} copied to {remotepath}, success, ({filesize})')
callback = callback if callback else def_cback
client = self.get_sftp_client()
client.put(localpath, remotepath, callback=callback, confirm=confirm)
return
def copy_dir(self,absolute_local_path, remote_mount_point, dry_run=False): def copy_dir(self,absolute_local_path, remote_mount_point, dry_run=False):
''' '''
copy recursivly a directory and its content on remote copy recursivly a directory and its content on remote
XXX IF FOLDER ALREADY EXISTS IT FAILS WITH IOError
@param absolute_local_path root path on local file system @param absolute_local_path root path on local file system
@param remote_root_path is the starting point on remote @param remote_mount_point is a folder on the remote, the mount point (must exists)
''' '''
sftpc = self.get_sftp_client() sftpc = self.get_sftp_client()
@ -150,19 +149,17 @@ class AdvancedSftpClientAgent(BaseAgent):
local_path = _path_utils.normalize_path(local_path) local_path = _path_utils.normalize_path(local_path)
remote_path = _path_utils.normalize_path(remote_path) remote_path = _path_utils.normalize_path(remote_path)
print(f'Creating Path and its content on remote: {remote_path}') # if os.path.isdir(local_path):
if os.path.isdir(local_path):
print(f'Creating empty remote folder: {remote_path}')
if not dry_run:
sftpc.mkdir(remote_path)
# items = sftpc.listdir_attr(path) # items = sftpc.listdir_attr(path)
items = os.listdir(local_path) items = os.listdir(local_path)
# if not items: if not items:
# print(f'Creating empty remote folder: {remote_path}') print(f'Creating empty remote folder: {remote_path}')
# if not dry_run: sftpc.mkdir(remote_path) try:
# return if not dry_run: sftpc.mkdir(remote_path)
except IOError as e:
print(f'Mount point already exists {remote_path}: detail {e}')
return
# first you must create the folder so order # first you must create the folder so order
@ -185,59 +182,15 @@ class AdvancedSftpClientAgent(BaseAgent):
local_path_split = [s for s in absolute_local_path.split(os.sep) if s] local_path_split = [s for s in absolute_local_path.split(os.sep) if s]
node_name = local_path_split[-1] node_name = local_path_split[-1]
remote_root_path = _path_utils.normalize_path(remote_mount_point) + node_name remote_root_path = _path_utils.normalize_path(remote_mount_point) + node_name
print(f'Creating mount point in remote : {remote_root_path}')
if not dry_run:
try:
sftpc.mkdir(remote_root_path)
except IOError as e:
print(f'Failure creating {remote_root_path}: detail {e}')
return copy_dir_recursive(absolute_local_path, remote_root_path, dry_run=dry_run) return copy_dir_recursive(absolute_local_path, remote_root_path, dry_run=dry_run)
def check_rfile_status(self, file_path:str):
try:
iobuf, dig = self.generate_rfile_hash(file_path, return_also_buffer=True)
return {
'exists' : True,
'iobuffer' : iobuf,
'hash': dig,
}
except IOError as e:
if e.errno == errno.ENOENT: return {
'exists' : False,
'iobuffer' : None,
'hash': None
}
raise Exception(f'Something went wrong and strange {file_path}')
def check_rfolder_status(self, path:str):
try:
dig = self.generate_tree_hash_oversftp(path)
return {
'exists' : True,
'iobuffer' : None,
'hash': dig,
}
except IOError as e:
if e.errno == errno.ENOENT: return {
'exists' : False,
'iobuffer' : None,
'hash': None
}
raise Exception(f'Something went wrong and strange {path}')
## move in SftpClientAgent
def copy_file_to_remote(self, localpath, remotepath, callback=None, confirm=False):
'''
put(localpath, remotepath, callback=None, confirm=True)
remotepath must contains the name of file you want to copy
'''
def def_cback(size, filesize):
print(f'{localpath} copied to {remotepath}, success, ({filesize})')
callback = callback if callback else def_cback
client = self.get_sftp_client()
client.put(localpath, remotepath, callback=callback, confirm=confirm)
return
def generate_rfile_hash(self, file_path, hexdigest=True, return_also_buffer=False): def generate_rfile_hash(self, file_path, hexdigest=True, return_also_buffer=False):
# a = self.agent # a = self.agent
client = self.get_sftp_client() client = self.get_sftp_client()
@ -344,6 +297,38 @@ class AdvancedSftpClientAgent(BaseAgent):
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
return rtreemap return rtreemap
def check_rfile_status(self, file_path:str):
try:
iobuf, dig = self.generate_rfile_hash(file_path, return_also_buffer=True)
return {
'exists' : True,
'iobuffer' : iobuf,
'hash': dig,
}
except IOError as e:
if e.errno == errno.ENOENT: return {
'exists' : False,
'iobuffer' : None,
'hash': None
}
raise Exception(f'Something went wrong and strange {file_path}')
def check_rfolder_status(self, path:str):
try:
dig = self.generate_tree_hash_oversftp(path)
return {
'exists' : True,
'iobuffer' : None,
'hash': dig,
}
except IOError as e:
if e.errno == errno.ENOENT: return {
'exists' : False,
'iobuffer' : None,
'hash': None
}
raise Exception(f'Something went wrong and strange {path}')
def test_sync_rtree(path='/home/notanamber/notes_dev/'): def test_sync_rtree(path='/home/notanamber/notes_dev/'):
start = time.time() start = time.time()
print('Start task') print('Start task')