# HG changeset patch # User Thierry Florac # Date 1549356054 -3600 # Node ID d160b2d1879779b5188739c30c877b62a5b7fe59 # Parent 4eb49f19314aa6ffd623241faf43208630b7403c Added unused blobs removal script diff -r 4eb49f19314a -r d160b2d18797 src/pyams_file/script/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_file/script/__init__.py Tue Feb 05 09:40:54 2019 +0100 @@ -0,0 +1,60 @@ +# +# 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 ZODB.utils import oid_repr, p64 + +from pyams_file.interfaces import IBlobReferenceManager +from pyams_utils.registry import 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: + + /var/local/env/pyams$ ./bin/zodbsearch --config etc/zodb-relstorage.conf \ + --data pyams_file.file.File pyams_file.file.ImageFile pyams_file.file.SVGImageFile \ + pyams_file.file.VideoFile pyams_file.file.AudioFile 2>&1 \ + | grep found | awk '{print $7}' | sort -u | cut -d \" -f 2 > var/FILES.oids + + P.S.: this command can be used with any database storage (ZEO, Relstorage or Newt)!!! + + This command will generate a FILES.oids file in "var" directory, containing the OIDs of all + files stored in the ZODB. + + The script will then read this file and get access to all stored files; if the file blob is not referenced + into blobs references manager, it can be removed. Removal is done by removing all references to it, so that + the blob file will be physically removed after next database pack. + """ + + set_local_registry(root.getSiteManager()) + references = get_utility(IBlobReferenceManager) + + db = getattr(root, '_p_jar') + files = open('var/FILES.oids', 'r') + + for foid in files.readlines(): + if not foid: + continue + file_oid = p64(int(foid, 16)) + file = db.get(file_oid) + blob = getattr(file, '_blob', None) + if blob is not None: + blob_oid = oid_repr(getattr(blob, '_p_oid')) + if blob_oid not in references.refs: + delattr(file, '_blob') + file._blob = None + file.__parent__ = None + print("{!r}".format(file))