[Zope-Checkins] SVN: Zope/trunk/ - added utilities/reindex_catalog.py to perform ZCatalog maintenance

Andreas Jung andreas at andreas-jung.com
Tue Aug 3 04:18:19 EDT 2004


Log message for revision 26870:
       - added utilities/reindex_catalog.py to perform ZCatalog maintenance
         operations from the command line (through zopectl)
  


Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/setup.py
  A   Zope/trunk/utilities/reindex_catalog.py


-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2004-08-03 02:51:30 UTC (rev 26869)
+++ Zope/trunk/doc/CHANGES.txt	2004-08-03 08:18:19 UTC (rev 26870)
@@ -26,6 +26,9 @@
 
     Features added
 
+     - added utilities/reindex_catalog.py to perform ZCatalog maintenance
+       operations from the command line (through zopectl)
+
      - RESPONSE.setBody and RESPONSE.setStatus now accept lock
        parameters in the same way as RESPONSE.redirect. These prevent
        further calls to the methods from overwriting the previous value.

Modified: Zope/trunk/setup.py
===================================================================
--- Zope/trunk/setup.py	2004-08-03 02:51:30 UTC (rev 26869)
+++ Zope/trunk/setup.py	2004-08-03 08:18:19 UTC (rev 26870)
@@ -1131,7 +1131,7 @@
     scripts=["utilities/mkzeoinstance.py", "utilities/mkzopeinstance.py",
              "utilities/check_catalog.py", "utilities/load_site.py",
              "utilities/requestprofiler.py", "utilities/zpasswd.py",
-             "utilities/copyzopeskel.py",
+             "utilities/copyzopeskel.py", "utilities/reindex_catalog.py",
              "utilities/compilezpy.py", "utilities/decompilezpy.py",
              "test.py"],
     distclass=ZopeDistribution,

Added: Zope/trunk/utilities/reindex_catalog.py
===================================================================
--- Zope/trunk/utilities/reindex_catalog.py	2004-08-03 02:51:30 UTC (rev 26869)
+++ Zope/trunk/utilities/reindex_catalog.py	2004-08-03 08:18:19 UTC (rev 26870)
@@ -0,0 +1,124 @@
+##############################################################################
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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
+#
+##############################################################################
+
+""" 
+A utility to perform external reindex operations for ZCatalogs
+
+Usage:
+
+   zopectl run reindex_catalog.py [options]
+
+   Use --help to get a list of all options.
+
+Author: Andreas Jung (andreas at andreas-jung.com)
+
+$Id$
+"""
+
+import sys
+from optparse import OptionParser
+
+from Products.ZCatalog.ProgressHandler import StdoutHandler
+
+
+def path2catalog(path):
+    """ lookup catalog by path """
+
+    catalog = app.restrictedTraverse(path, None)
+    if not catalog:
+        raise ValueError('No catalog found at %s' % path)
+    return catalog
+
+
+def getHandler(options):
+    """ return a progress handler """
+    
+    if options.silent:
+        return None
+    else:
+        return StdoutHandler(options.steps)
+
+
+def listIndexes(options, args):
+    """ print a list of all indexes to stdout """
+
+    catalog = path2catalog(options.catalog)
+    indexes = catalog._catalog.indexes    
+
+    print 'Listing of all indexes at %s' % options.catalog
+    print
+
+    for id, idx in indexes.items():
+        print '%-20s %-50s %d' % (id, idx.meta_type, idx.numObjects())
+
+
+def reindexIndexes(optioons, args):
+    """ reindex given list of indexes """
+
+    catalog = path2catalog(options.catalog)
+    handler = getHandler(options)
+
+    for id in args:
+        print 'Reindexing index %s at %s' % (id, options.catalog)
+        catalog.reindexIndex(id, None, handler)
+    get_transaction().commit()
+
+
+def refreshMetadata(options, args):
+    """ reindex metadata """
+
+    catalog = path2catalog(options.catalog)
+    handler = getHandler(options)
+
+    print 'Refresh metadata at %s' % options.catalog
+    catalog.refreshMetadata(handler)
+    get_transaction().commit()
+
+
+def reindexAll(options, args):
+    """ reindex complete catalog """
+
+    catalog = path2catalog(options.catalog)
+    handler = getHandler(options)
+
+    print 'Reindexing complete ZCatalog at %s' % options.catalog
+    catalog.refreshCatalog(options.clearCatalog, handler)
+    get_transaction().commit()
+
+
+if __name__ == '__main__':
+   
+    parser = OptionParser()
+    parser.add_option('-c', '--catalog', dest='catalog',
+                     help='path to ZCatalog')
+    parser.add_option('-i', '--indexes', action="store_true", dest="listIndexes",
+                     help='list all indexes')
+    parser.add_option('-C', '--clear', action="store_true", dest="clearCatalog", default=False,
+                     help='clear catalog before reindexing the complete catalog (--all only)')
+    parser.add_option('-a', '--all', action="store_true", dest="reindexAll",
+                     help='reindex the complete catalog and update all metadata')
+    parser.add_option('-r', '--reindex', action="store_true", dest="reindexIndexes",
+                     help='reindex specified indexes')
+    parser.add_option('-m', '--metadata', action="store_true", dest="refreshMetadata",
+                     help='refresh all metadata')
+    parser.add_option('-n', '--steps', dest="steps", default=100, type="int",
+                     help='log progress every N objects')
+    parser.add_option('-s', '--silent', action="store_true", dest="silent", default=False,
+                     help='do not log reindexing progress to stdout')
+
+    options,args = parser.parse_args()
+
+    if options.listIndexes: listIndexes(options, args)
+    if options.reindexIndexes: reindexIndexes(options, args)
+    if options.refreshMetadata: refreshMetadata(options, args)
+    if options.reindexAll: reindexAll(options, args)



More information about the Zope-Checkins mailing list