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

# Source Code for Module Products.ZenUtils.zproprmlocal

     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  #! /usr/bin/env python
    14  __doc__ = "Remove local value of zProperty from Devices"
    15
    16  import Globals
    17  from Products.ZenUtils.ZCmdBase import ZCmdBase
    18  from Acquisition import aq_base
    19  import transaction
    20  import sys
    21
    22 -class ZPropRmLocal(ZCmdBase):
    23      "Remove local value of zProperty from Devices"
    24
    25 -    def run(self):
    26          msg = []
    27
    28          if not self.options.deviceClass:
    29              msg.append('You must specify a device class with the'
    30                          ' --class option.')
    31
    32          if not self.options.zPropName:
    33              msg.append('You must specify a zProperty name with the'
    34                          ' --zproperty option.')
    35
    36          if msg:
    37              print('\n'.join(msg))
    38              sys.exit(0)
    39
    40          try:
    41              devClass = self.dmd.Devices.getOrganizer(self.options.deviceClass)
    42          except KeyError:
    43              print('Unable to locate device class %s' %
    44                                  self.options.deviceClass)
    45              sys.exit(0)
    46
    47          devs = []
    48          if self.options.recurse:
    49              devList = devClass.getSubDevicesGen()
    50          else:
    51              devList = devClass.devices()
    52          for dev in devList:
    53              if hasattr(aq_base(dev), self.options.zPropName):
    54                  dev._delProperty(self.options.zPropName)
    55                  devs.append(dev)
    56
    57          transaction.commit()
    58
    59          print('Deleted %s from %s devices.' % (
    60                              self.options.zPropName, len(devs)))
    61          for d in devs:
    62              sys.stdout.write(d.getPrimaryId())
    63
    64
    65 -    def buildOptions(self):
    66          ZCmdBase.buildOptions(self)
    67          self.parser.add_option('--class',
    68                                 dest='deviceClass',
    69                                 default='/',
    70                                 help="Device class to operate on")
    71          self.parser.add_option('--zproperty',
    72                                 dest='zPropName',
    73                                 default=None,
    74                                 help="Name of the zProperty to remove from"
    75                                      " devices")
    76          self.parser.add_option('-r', '--recurse',
    77                                 dest='recurse',
    78                                 action="store_true",
    79                                 default=False,
    80                                 help="Recurse into subclasses of --class")
    81
    82  if __name__ == '__main__':
    83      zp = ZPropRmLocal()
    84      zp.run()
    85