29a948bfb6aa28ebe38717655f5d21f0cf8fef24
[Plinn.git] / _zctl / rebuild-catalog.py
1 from argparse import ArgumentParser
2 import os.path
3 from Acquisition import aq_base
4 from zope.site.hooks import setSite
5 from Products.CMFCore.CMFCatalogAware import CMFCatalogAware
6 import transaction
7
8 count = 0
9
10 def checkContents(ob) :
11 global count
12 try :
13 print ob.absolute_url()
14 except Exception, e:
15 print >> errorLog, '%s/%s' % (ob.aq_parent.absolute_url(), ob.aq_base.id)
16 print >> errorLog, getattr(ob, 'portal_type', getattr(ob, 'meta_type', ''))
17 print >> errorLog, e
18 print >> errorLog
19
20 if isinstance(aq_base(ob), CMFCatalogAware) :
21 assert ob._getCatalogTool()
22 try :
23 ctool.indexObject(ob)
24 count = count + 1
25 if count % 1000 == 0 :
26 transaction.commit()
27 print count, 'commit.'
28 except Exception, e:
29 print >> errorLog, '%s/%s' % (ob.aq_parent.absolute_url(), ob.aq_base.id)
30 print >> errorLog, e
31
32 if hasattr(aq_base(ob), 'objectItems') :
33 obs = ob.objectItems()
34 for k, v in obs :
35 checkContents(v)
36
37
38 parser = ArgumentParser(description="Rebuild entire catalog by walking contents tree.")
39 parser.add_argument('portal_path')
40 parser.add_argument('--error-log', help='Error log file. Default: ~/catalog-rebuild-error.log',
41 default='~/catalog-rebuild-error.log', required=False, dest='errorLogPath')
42
43 args = parser.parse_args()
44 portal = app.unrestrictedTraverse(args.portal_path)
45 setSite(portal)
46 ctool = portal.portal_catalog
47 errorLogPath = os.path.expanduser(args.errorLogPath)
48 errorLog = open(errorLogPath, 'w')
49
50 try :
51 checkContents(portal)
52 transaction.commit()
53 print count
54 print 'Done.'
55 finally :
56 errorLog.close()
57