agent remove dir

This commit is contained in:
Amber 2024-10-02 21:28:38 +02:00
parent eb6577eb0f
commit 0fc4ba6b36

View File

@ -100,7 +100,7 @@ class AdvancedSftpClientAgent(BaseAgent):
if e.errno == errno.ENOENT: return False
return True
def remove_dir(self, absolute_path):
def remove_dir(self, absolute_path, dry_run=False):
'''
Here we assume that absolute_path on remote can be not empty
XXX: the ftp client can remove only empty folder
@ -109,10 +109,13 @@ class AdvancedSftpClientAgent(BaseAgent):
sftpc = self.get_sftp_client()
def remove_dir_recursive(path):
print(f'Path: {path}')
items = sftpc.listdir_attr(path)
if not items:
# path is an empty directory you can procede to delete it
sftpc.rmdir(path)
print(f'Path: {path} empty removing')
if not dry_run:
sftpc.rmdir(path)
return
for item in items:
@ -120,10 +123,15 @@ class AdvancedSftpClientAgent(BaseAgent):
print('absolute_item_path: %s, item %s, isdir: %s' % (absolute_item_path, item.filename, stat.S_ISDIR(item.st_mode)))
if not stat.S_ISDIR(item.st_mode):
## remove file
sftpc.remove(absolute_item_path)
print(f'Removing file {absolute_item_path}')
if not dry_run: sftpc.remove(absolute_item_path)
continue
remove_dir_recursive(absolute_item_path)
print(f'Path: {path} empty removing')
if not dry_run:
sftpc.rmdir(path)
return remove_dir_recursive(absolute_path)
# def get_sftp_client(self):