agent remove dir
This commit is contained in:
parent
eb6577eb0f
commit
0fc4ba6b36
@ -100,7 +100,7 @@ class AdvancedSftpClientAgent(BaseAgent):
|
|||||||
if e.errno == errno.ENOENT: return False
|
if e.errno == errno.ENOENT: return False
|
||||||
return True
|
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
|
Here we assume that absolute_path on remote can be not empty
|
||||||
XXX: the ftp client can remove only empty folder
|
XXX: the ftp client can remove only empty folder
|
||||||
@ -109,10 +109,13 @@ class AdvancedSftpClientAgent(BaseAgent):
|
|||||||
sftpc = self.get_sftp_client()
|
sftpc = self.get_sftp_client()
|
||||||
|
|
||||||
def remove_dir_recursive(path):
|
def remove_dir_recursive(path):
|
||||||
|
print(f'Path: {path}')
|
||||||
items = sftpc.listdir_attr(path)
|
items = sftpc.listdir_attr(path)
|
||||||
if not items:
|
if not items:
|
||||||
# path is an empty directory you can procede to delete it
|
# 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
|
return
|
||||||
|
|
||||||
for item in items:
|
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)))
|
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):
|
if not stat.S_ISDIR(item.st_mode):
|
||||||
## remove file
|
## remove file
|
||||||
sftpc.remove(absolute_item_path)
|
print(f'Removing file {absolute_item_path}')
|
||||||
|
if not dry_run: sftpc.remove(absolute_item_path)
|
||||||
continue
|
continue
|
||||||
remove_dir_recursive(absolute_item_path)
|
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)
|
return remove_dir_recursive(absolute_path)
|
||||||
|
|
||||||
# def get_sftp_client(self):
|
# def get_sftp_client(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user