1
2
3
4
5
6
7
8
9
10
11 __doc__="""CmdBase
12
13 Add data base access functions for command line programs
14
15 $Id: ToManyRebuildKeys.py,v 1.2 2004/10/19 22:28:59 edahl Exp $"""
16
17 __version__ = "$Revision: 1.2 $"[11:-2]
18
19 import gc
20
21 from Acquisition import aq_parent
22
23 from Products.ZenUtils.Utils import getSubObjectsMemo
24
25 from ZCmdBase import ZCmdBase
26
27 from transaction import get_transaction
28
30
32 ccount = 0
33 for tomany in getSubObjectsMemo(self.dmd, self.filter, self.decend):
34 self.log.debug("rebuilding keys for relation %s on object %s" %
35 (tomany.getId(), aq_parent(tomany).getId()))
36 ccount += tomany.rebuildKeys(self.log)
37 if ccount >= self.options.commitCount and not self.options.noCommit:
38 trans = get_transaction()
39 trans.note('ToManyRebuildKeys rebuilt keys')
40 trans.commit()
41 ccount = 0
42 self.dmd._p_jar.sync()
43 gc.collect()
44 if self.options.noCommit:
45 self.log.info("not commiting any changes")
46 else:
47 trans = get_transaction()
48 trans.note('ToManyRebuildKeys rebuilt keys')
49 trans.commit()
50
51
53 return obj.meta_type == "To Many Relationship"
54
55
62
63
65 ZCmdBase.buildOptions(self)
66 self.parser.add_option('-x', '--commitCount',
67 dest='commitCount',
68 default=1000,
69 type="int",
70 help='how many lines should be loaded before commit')
71
72 self.parser.add_option('-n', '--noCommit',
73 dest='noCommit',
74 action="store_true",
75 default=0,
76 help='Do not store changes to the Dmd (for debugging)')
77
78
79 if __name__ == "__main__":
80 tmbk = ToManyRebuildKeys()
81 tmbk.rebuild()
82