From 5a9bd27a40377b0cadfc51fd576ae98c9ee06668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20K=C3=B6ster?= Date: Fri, 16 Aug 2024 15:03:45 +0200 Subject: [PATCH] fix: clean up target path upon storage in order to avoid merges of directories (#25) ## Summary by CodeRabbit - **New Features** - Enhanced the storage process with a cleanup procedure, ensuring a clean state before storing new data. - **Improvements** - Improved robustness by effectively managing both directory and file scenarios during cleanup, preventing conflicts from residual data. --- snakemake_storage_plugin_fs/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/snakemake_storage_plugin_fs/__init__.py b/snakemake_storage_plugin_fs/__init__.py index 45a8a7e..00cdd60 100644 --- a/snakemake_storage_plugin_fs/__init__.py +++ b/snakemake_storage_plugin_fs/__init__.py @@ -206,8 +206,15 @@ def retrieve_object(self): def store_object(self): # Ensure that the object is stored at the location specified by - # self.local_path(). - self.query_path.parent.mkdir(exist_ok=True, parents=True) + # self.query_path. + if self.query_path.exists(): + # Clean up the target path + if self.query_path.is_dir(): + shutil.rmtree(self.query_path, ignore_errors=True) + else: + self.query_path.unlink(missing_ok=True) + else: + self.query_path.parent.mkdir(exist_ok=True, parents=True) # We want to respect the permissions in the target folder, in particular the # setgid bit. Hence, we use --no-p to avoid preserving of permissions from the # source to the target.