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

# Source Code for Module Products.ZenUtils.zproprmlocal

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