From 0fc4ba6b3665441d183a178e6dcdd56e2714d174 Mon Sep 17 00:00:00 2001 From: Amber Date: Wed, 2 Oct 2024 21:28:38 +0200 Subject: [PATCH] agent remove dir --- src/lib/sclient/agent.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/sclient/agent.py b/src/lib/sclient/agent.py index 700230d..e616dba 100644 --- a/src/lib/sclient/agent.py +++ b/src/lib/sclient/agent.py @@ -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):