[Zope-Checkins] CVS: Zope/utilities - reindex_catalog.py:1.1.2.1

Andreas Jung andreas at andreas-jung.com
Tue Aug 3 04:16:27 EDT 2004


Update of /cvs-repository/Zope/utilities
In directory cvs.zope.org:/tmp/cvs-serv2099/utilities

Added Files:
      Tag: Zope-2_7-branch
	reindex_catalog.py 
Log Message:
     - added utilities/reindex_catalog.py to perform ZCatalog maintenance
       operations from the command line (through zopectl)


=== Added File Zope/utilities/reindex_catalog.py ===
##############################################################################
#
# 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: reindex_catalog.py,v 1.1.2.1 2004/08/03 08:16:27 andreasjung Exp $
"""

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