From 1096bfff407f27bb63b772a88b6a14c5e89c14bd Mon Sep 17 00:00:00 2001
From: Blake Fitch <blake.fitch@tuebingen.mpg.de>
Date: Sat, 22 Jun 2024 14:55:06 +0200
Subject: [PATCH] add replace_existing flag and function to confirmed_put()

---
 pymods/irods_utils/irods_utils.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/pymods/irods_utils/irods_utils.py b/pymods/irods_utils/irods_utils.py
index 51da26d..9fc32f8 100644
--- a/pymods/irods_utils/irods_utils.py
+++ b/pymods/irods_utils/irods_utils.py
@@ -350,11 +350,12 @@ def streaming_transfer_file_to_object( args ):
     return rc, hash_digest256, hash_digest512, transfer_size
 
 # Returns None or the data_object irods ref.
-def confirmed_put( irods_sesh, file_pathname, new_obj_ipath, metadata_dict=None, datatype=None, block_size=(2**28) ):
+def confirmed_put( irods_sesh, file_pathname, new_obj_ipath, metadata_dict=None, datatype=None, block_size=(2**28), replace_existing=None ):
     logging.debug( "file_pathname: "  + file_pathname
                  + " new_obj_ipath: " + new_obj_ipath
                  + " datatype: "      + datatype
-                 + " block_size: "    + str( block_size ) )
+                 + " block_size: "    + str( block_size )
+                 + " replace_existing " + str( replace_existing) )
 
     # NOTE: this routine uploades the file as a temporary iRODS object.
     # Common sense suggests checking if the objct already exsists, a rare condition first.
@@ -504,6 +505,14 @@ def confirmed_put( irods_sesh, file_pathname, new_obj_ipath, metadata_dict=None,
 
     logging.debug( "file " + file_pathname + " in place at ipath " + new_obj_ipath )
 
+    if existing_obj != None and replace_existing:
+        logging.warning( f"replace_existing flag set. Removing old data object. ipath: {dup_ipath}" )
+        try:
+            dup_obj = irods_sesh.data_objects.get( dup_ipath )
+            dup_obj.unlink(force=True)
+        except Exception as ex:
+            logging.warning( "Failed remove dup when remove_existing flag set. ipath: {dup_ipath}  ex: {ex} " )
+          
     new_obj = None
     try:
         new_obj = irods_sesh.data_objects.get( new_obj_ipath )
-- 
GitLab