# HG changeset patch # User Thierry Florac # Date 1549356113 -3600 # Node ID 8361af8c753febded7d376904705daa8e9cff294 # Parent d160b2d1879779b5188739c30c877b62a5b7fe59 Added generations adapter to remove all thumbnails and their blobs, and remove references to unused blobs diff -r d160b2d18797 -r 8361af8c753f src/pyams_file/generations/__init__.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 +# 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) diff -r d160b2d18797 -r 8361af8c753f src/pyams_file/generations/evolve1.py --- /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 +# 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) diff -r d160b2d18797 -r 8361af8c753f src/pyams_file/generations/evolve2.py --- /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 +# 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)