Added generations adapter to remove all thumbnails and their blobs, and remove references to unused blobs
authorThierry Florac <tflorac@ulthar.net>
Tue, 05 Feb 2019 09:41:53 +0100
changeset 178 8361af8c753f
parent 177 d160b2d18797
child 179 27d5fe5112dc
Added generations adapter to remove all thumbnails and their blobs, and remove references to unused blobs
src/pyams_file/generations/__init__.py
src/pyams_file/generations/evolve1.py
src/pyams_file/generations/evolve2.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_file/generations/__init__.py	Tue Feb 05 09:41:53 2019 +0100
@@ -0,0 +1,42 @@
+#
+# Copyright (c) 2008-2019 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+from importlib import import_module
+
+from pyams_file.file import BlobReferencesManager
+from pyams_file.interfaces import IBlobReferenceManager
+from pyams_utils.interfaces.site import ISiteGenerations
+from pyams_utils.registry import utility_config
+from pyams_utils.site import check_required_utilities
+
+
+REQUIRED_UTILITIES = ((IBlobReferenceManager, '', BlobReferencesManager, 'Blobs references manager'),)
+
+
+@utility_config(name='PyAMS file', provides=ISiteGenerations)
+class WebsiteGenerationsChecker(object):
+    """PyAMS file package generations checker"""
+
+    order = 15
+    generation = 3
+
+    def evolve(self, site, current=None):
+        """Check for required utilities, tables and tools"""
+        check_required_utilities(site, REQUIRED_UTILITIES)
+        if not current:
+            current = 1
+        for generation in range(current, self.generation):
+            module_name = 'pyams_file.generations.evolve{}'.format(generation)
+            module = import_module(module_name)
+            module.evolve(site)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_file/generations/evolve1.py	Tue Feb 05 09:41:53 2019 +0100
@@ -0,0 +1,46 @@
+#
+# Copyright (c) 2008-2019 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+import logging
+
+from zope.intid import IIntIds
+
+from pyams_file.interfaces import IMediaFile, IThumbnails
+from pyams_utils.registry import get_local_registry, get_utility, set_local_registry
+
+
+logger = logging.getLogger('PyAMS (file)')
+
+
+def evolve(site):
+    """Evolve 1: remove all images thumbnails to free blobs"""
+    registry = get_local_registry()
+    try:
+        medias = set()
+        set_local_registry(site.getSiteManager())
+        logger.warning("Removing all thumbnails from database to free unused blobs...")
+        intids = get_utility(IIntIds)
+        for ref in list(intids.refs.keys()):
+            obj = intids.queryObject(ref)
+            if IMediaFile.providedBy(obj):
+                logger.debug(">>> removing thumbnails for image {!r}".format(obj))
+                thumbnails = IThumbnails(obj, None)
+                if thumbnails is not None:
+                    medias.add(obj)
+                    thumbnails.clear_thumbnails()
+        logger.warning("Thumbnails cleanup is finished. Launch *zodbpack* command if you are using RelStorage "
+                       "to remove all unused blobs.")
+        logger.warning("{} images updated".format(len(medias)))
+    finally:
+        set_local_registry(registry)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_file/generations/evolve2.py	Tue Feb 05 09:41:53 2019 +0100
@@ -0,0 +1,44 @@
+#
+# Copyright (c) 2008-2019 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+import logging
+
+from zope.intid import IIntIds
+
+from pyams_file.interfaces import IBlobReferenceManager, IFile
+from pyams_utils.registry import get_local_registry, get_utility, set_local_registry
+
+
+logger = logging.getLogger('PyAMS (file)')
+
+
+def evolve(site):
+    """Evolve 2: create reference for all files blobs"""
+    registry = get_local_registry()
+    try:
+        files = set()
+        set_local_registry(site.getSiteManager())
+        logger.warning("Creating references to all blobs...")
+        intids = get_utility(IIntIds)
+        references = get_utility(IBlobReferenceManager)
+        for ref in list(intids.refs.keys()):
+            obj = intids.queryObject(ref)
+            if IFile.providedBy(obj):
+                blob = getattr(obj, '_blob', None)
+                if blob is not None:
+                    references.add_reference(blob, obj)
+                logger.debug(">>> updated blob reference for file {!r}".format(obj))
+        logger.warning("{} files updated".format(len(files)))
+    finally:
+        set_local_registry(registry)