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

# Source Code for Module Products.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 or (at your
     8  # option) any later version as published by 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          return (isinstance(obj, ZenModelRM) or
    64                  isinstance(obj, ToManyRelationship))
    65
    66
    67 -    def buildOptions(self):
    68          ZCmdBase.buildOptions(self)
    69          self.parser.add_option('-x', '--commitCount',
    70                      dest='commitCount',
    71                      default=1000,
    72                      type="int",
    73                      help='how many lines should be loaded before commit')
    74
    75          self.parser.add_option('-n', '--noCommit',
    76                      dest='noCommit',
    77                      action="store_true",
    78                      default=0,
    79                      help='Do not store changes to the Dmd (for debugging)')
    80
    81
    82  if __name__ == "__main__":
    83      tmbk = ToManyRebuildKeys()
    84      tmbk.rebuild()
    85