How to remove old data object/collection automatically by using Python ?

47 views
Skip to first unread message

John

unread,
May 17, 2022, 5:58:12 AM5/17/22
to iRODS-Chat
Hello everyone,

I ' m new in python programming language, but I would like to use it to accomplish the following task: If a data object or a collection is older than 5 years, then it will be automatically removed/deleted from irods.

I'm using Hamster GUI https://github.com/Andries-B/Hamster, which is based on python-irodsclient https://github.com/irods/python-irodsclient.

In this file https://github.com/Andries-B/Hamster/blob/main/src/main.py,  I have the code below and I think, this could help me to accomplish the task above. Unfortunately, I don't know how. Could you please help me? Any other suggestion would be also appreciated. Thank you. 

...
def slot_delete (self):
        "comment here"
        global MY_SESSION, DICT_HAMSTER
        global FILESIZES_SCAN, NFILES_SCAN, FILESIZES_SCAN_MB
        current_collection = DICT_HAMSTER.get ("current_collection")
        selection_fullpath = DICT_HAMSTER.get ("selection_fullpath")
        del_coll = ""
        if selection_fullpath:
            if selection_fullpath.endswith ('/'):
                # collection
                del_coll = selection_fullpath[:-1]
                msg = "Delete '" + del_coll + "'\n("
                FILESIZES_SCAN = 0
                NFILES_SCAN = 0
                scan_collections_and_objects (del_coll)
                FILESIZES_SCAN_MB = FILESIZES_SCAN // 1024 // 1024
                msg += str (NFILES_SCAN) + " files, " + \
                      str (FILESIZES_SCAN_MB) + " MB, " + \
                      str (FILESIZES_SCAN) + " bytes)\n"
                # question, information, warning, critical
                button = QtWidgets.QMessageBox.information (self, \
                                   "Delete Collection", msg, buttons = \
                                        QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
                if button == QtWidgets.QMessageBox.Ok:
                    self.status_message (msg)
                    try:
                        MY_SESSION.collections.remove (del_coll)
                        if del_coll == current_collection:
                            n_2 = current_collection.rfind ('/')
                            current_collection = current_collection[0:n_2]
                            update_dict (DICT_HAMSTER,
                                'current_collection', current_collection)
                        self.update_collections_and_dataobjects_view (current_collection)
                    except CollectionDoesNotExist:
                        self.log_message ("Unable to delete collection \'" + del_coll + "\'")
            else:
                # data object
                msg = "Delete '" + selection_fullpath + "'"
                button = QtWidgets.QMessageBox.information (self, \
                                   "Delete Dataobject", \
                                   msg, \
                                   buttons=QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
                if button == QtWidgets.QMessageBox.Ok:
                    try:
                        self.status_message (msg)
                        # OLD: MY_SESSION.data_objects.unlink (selection_fullpath)
                        MY_SESSION.data_objects.unlink (selection_fullpath, force = True)
                        self.update_collections_and_dataobjects_view (current_collection)
                    except USER_FILE_DOES_NOT_EXIST:
                        self.log_message ("Unable to delete dataobject \'" \
                            + selection_fullpath + "\'")

...


def clear_form (self): 
        from datetime import datetime
        from dateutil.relativedelta import relativedelta
        creationTime = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
        expiration = datetime.now() + relativedelta(years=5)
        expirationTime = expiration.strftime('%d/%m/%Y %H:%M:%S')
        self.lineEdit_dc_creation_date.setText (creationTime)
        self.lineEdit_dc_expiration_date.setText (expirationTime)
         ...


Terrell Russell

unread,
Jun 13, 2022, 9:55:03 PM6/13/22
to irod...@googlegroups.com
Hi John,

Doing anything in iRODS 'automatically' suggests some periodic code running somewhere to pick up changes that need to be made -- in your case, removing files based on time.

This is probably best done with a recurring rule in the delay queue, which could be written in python, or the iRODS rule language...  but probably best built around a GenQuery that finds the things of a certain age and then queuing them for deletion / removal.

If you want to provide some kind of GUI interaction around this removal - then you could still populate a GUI with the same kind of GenQuery... but not make it automatic to follow through on the removal.

I hope that helps,

Terrell




--
--
The Integrated Rule-Oriented Data System (iRODS) - https://irods.org
 
iROD-Chat: http://groups.google.com/group/iROD-Chat
---
You received this message because you are subscribed to the Google Groups "iRODS-Chat" group.
To unsubscribe from this group and stop receiving emails from it, send an email to irod-chat+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/irod-chat/1ba012e0-c05b-40f4-add7-b42556bbe69fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages