Updated blobs removal script to remove all references on empty files
authorThierry Florac <tflorac@ulthar.net>
Tue, 05 Feb 2019 16:42:30 +0100
changeset 184 d410e7e21473
parent 183 7d176a46aa83
child 185 8fdec2127a9f
Updated blobs removal script to remove all references on empty files
src/pyams_file/script/__init__.py
--- a/src/pyams_file/script/__init__.py	Tue Feb 05 15:09:19 2019 +0100
+++ b/src/pyams_file/script/__init__.py	Tue Feb 05 16:42:30 2019 +0100
@@ -12,17 +12,19 @@
 
 __docformat__ = 'restructuredtext'
 
-
 from ZODB.utils import oid_repr, p64
+from zope.lifecycleevent import ObjectRemovedEvent
 
 from pyams_file.interfaces import IBlobReferenceManager
-from pyams_utils.registry import get_utility, set_local_registry
+from pyams_utils.registry import get_global_registry, get_utility, set_local_registry
 
 
 def remove_unused_blobs(root):
     """Blobs removal scripts
 
-    This file is intended to be used from *pshell* after running an *mdtools.relstorage* command like this:
+    If you used previous versions of PyAMS_file package, files and blobs references where not handled
+    correctly and this file is intended to be used from *pshell* after running an *mdtools.relstorage*
+    command like this:
 
     /var/local/env/pyams$ ./bin/zodbsearch --config etc/zodb-relstorage.conf \
         --data pyams_file.file.File pyams_file.file.ImageFile pyams_file.file.SVGImageFile \
@@ -39,6 +41,7 @@
     the blob file will be physically removed after next database pack.
     """
 
+    registry = get_global_registry()
     set_local_registry(root.getSiteManager())
     references = get_utility(IBlobReferenceManager)
 
@@ -56,5 +59,10 @@
             if blob_oid not in references.refs:
                 delattr(file, '_blob')
                 file._blob = None
+                registry.notify(ObjectRemovedEvent(file))
                 file.__parent__ = None
                 print("{!r}".format(file))
+        else:
+            registry.notify(ObjectRemovedEvent(file))
+            file.__parent__ = None
+            print("Empty file {!r}".format(file))