style thème sombre.
[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 errorLog = None
10
11 def checkContents(ob) :
12 global count
13 global errorLog
14 try :
15 print ob.absolute_url()
16 except Exception, e:
17 print >> errorLog, '%s/%s' % (ob.aq_parent.absolute_url(), ob.aq_base.id)
18 print >> errorLog, getattr(ob, 'portal_type', getattr(ob, 'meta_type', ''))
19 print >> errorLog, e
20 print >> errorLog
21
22 if isinstance(aq_base(ob), CMFCatalogAware) :
23 ctool = ob._getCatalogTool()
24 assert ctool
25 try :
26 ctool.indexObject(ob)
27 count = count + 1
28 if count % 1000 == 0 :
29 transaction.commit()
30 print count, 'commit.'
31 except Exception, e:
32 print >> errorLog, '%s/%s' % (ob.aq_parent.absolute_url(), ob.aq_base.id)
33 print >> errorLog, e
34
35 if hasattr(aq_base(ob), 'objectItems') :
36 obs = ob.objectItems()
37 for k, v in obs :
38 checkContents(v)
39
40
41 def indexMemberdata(portal) :
42 mtool = portal.portal_membership
43 ctool = portal.portal_catalog
44 for m in mtool.getOtherMembers([]) :
45 ctool.indexObject(m)
46
47 def main(path) :
48 portal = app.unrestrictedTraverse(path)
49 setSite(portal)
50 errorLogPath = os.path.expanduser(args.errorLogPath)
51 global errorLog
52 errorLog = open(errorLogPath, 'w')
53
54 try :
55 checkContents(portal)
56 transaction.commit()
57 indexMemberdata(portal)
58 transaction.commit()
59 print count
60 print 'Done.'
61 finally :
62 errorLog.close()
63
64
65
66 if __name__ == '__main__':
67 parser = ArgumentParser(description="Rebuild entire catalog by walking contents tree.")
68 parser.add_argument('portal_path')
69 parser.add_argument('--error-log', help='Error log file. Default: ~/catalog-rebuild-error.log',
70 default='~/catalog-rebuild-error.log', required=False, dest='errorLogPath')
71
72 args = parser.parse_args()
73 main(args.portal_path)
74