Package ZenUtils :: Module ToManyRebuildKeys
[hide private]
[frames] | no frames]

Source Code for Module ZenUtils.ToManyRebuildKeys

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 as published by 
 8  # the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13   
14  __doc__="""CmdBase 
15   
16  Add data base access functions for command line programs 
17   
18  $Id: ToManyRebuildKeys.py,v 1.2 2004/10/19 22:28:59 edahl Exp $""" 
19   
20  __version__ = "$Revision: 1.2 $"[11:-2] 
21   
22  import gc 
23   
24  from Acquisition import aq_parent 
25   
26  from Products.ZenUtils.Utils import getSubObjectsMemo 
27   
28  from ZCmdBase import ZCmdBase 
29   
30  from transaction import get_transaction 
31   
32 -class ToManyRebuildKeys(ZCmdBase):
33
34 - def rebuild(self):
35 ccount = 0 36 for tomany in getSubObjectsMemo(self.dmd, self.filter, self.decend): 37 self.log.debug("rebuilding keys for relation %s on object %s" % 38 (tomany.getId(), aq_parent(tomany).getId())) 39 ccount += tomany.rebuildKeys(self.log) 40 if ccount >= self.options.commitCount and not self.options.noCommit: 41 trans = get_transaction() 42 trans.note('ToManyRebuildKeys rebuilt keys') 43 trans.commit() 44 ccount = 0 45 self.dmd._p_jar.sync() 46 gc.collect() 47 if self.options.noCommit: 48 self.log.info("not commiting any changes") 49 else: 50 trans = get_transaction() 51 trans.note('ToManyRebuildKeys rebuilt keys') 52 trans.commit()
53 54
55 - def filter(self, obj):
56 return obj.meta_type == "To Many Relationship"
57 58
59 - def decend(self, obj):
60 from Products.ZenModel.ZenModelRM import ZenModelRM 61 from Products.ZenRelations.ToManyRelationship \ 62 import ToManyRelationship 63 from Products.ZenRelations.ToOneRelationship \ 64 import ToOneRelationship 65 return ( 66 isinstance(obj, ZenModelRM) or 67 isinstance(obj, ToManyRelationship))
68 #isinstance(obj, ToOneRelationship)) 69 70
71 - def buildOptions(self):
72 ZCmdBase.buildOptions(self) 73 self.parser.add_option('-x', '--commitCount', 74 dest='commitCount', 75 default=1000, 76 type="int", 77 help='how many lines should be loaded before commit') 78 79 self.parser.add_option('-n', '--noCommit', 80 dest='noCommit', 81 action="store_true", 82 default=0, 83 help='Do not store changes to the Dmd (for debugging)')
84 85 86 if __name__ == "__main__": 87 tmbk = ToManyRebuildKeys() 88 tmbk.rebuild() 89