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

# Source Code for Module Products.ZenUtils.EggifyZenPack

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2008, 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  __doc__ = "Convert old-style zenpacks to zenpack eggs"
    14
    15  import Globals
    16  from Products.ZenUtils.ZenScriptBase import ZenScriptBase
    17  from Products.ZenModel.ZenPack import ZenPackException
    18  from Utils import zenPath
    19  import ZenPackCmd
    20  import os, sys
    21  import shutil
    22
    23
    24 -class Eggify(ZenScriptBase):
    25      "Eggify ZenPacks"
    26
    27 -    def run(self):
    28          if not self.options.newid:
    29              raise ZenPackException('You must specify a new id with the'
    30                  ' --newid option.')
    31          zpName = self.args[0]
    32          if not os.path.isdir(zenPath('Products', zpName)):
    33              raise ZenPackException('Can not locate %s.' % zpName +
    34                  ' This command only operates on installed ZenPacks.')
    35          (possible, msgOrId) = ZenPackCmd.CanCreateZenPack(
    36                                                      None, self.options.newid)
    37          if possible:
    38              newId = msgOrId
    39          else:
    40              raise ZenPackException('Unable to eggify %s: %s' % (
    41                                                              zpName, msgOrId))
    42
    43          self.connect()
    44
    45          # Create new zenpack
    46          eggDir = ZenPackCmd.CreateZenPack(newId, prevZenPackName=zpName)
    47
    48          # Copy old one into new one
    49          parts = newId.split('.')
    50          zpDir = os.path.join(eggDir, *parts)
    51          os.system('rm -rf %s' % zpDir)
    52          shutil.copytree(zenPath('Products', zpName), zpDir, symlinks=False)
    53
    54          # Create a skins directory if there isn't one
    55          skinsDir = os.path.join(zpDir, 'skins', zpName)
    56          if not os.path.isdir(skinsDir):
    57              os.makedirs(skinsDir)
    58
    59          # Install it
    60          ZenPackCmd.InstallEggAndZenPack(self.dmd, eggDir, link=True)
    61
    62          # Confirm pack is eggified
    63          pack = self.dmd.ZenPackManager.packs._getOb(zpName, None)
    64          success = pack and pack.isEggPack()
    65
    66          if success:
    67              print('%s successfully converted to %s.' % (zpName, newId))
    68              print('The code for %s is located at $ZENHOME/ZenPacks/%s' %
    69                      (newId, newId))
    70              print('NOTE: If your ZenPack provides DataSource classes '
    71                      'or any other python class for objects that are stored '
    72                      'in the object database then further steps are '
    73                      'required.  Please see the Zenoss Developer Guide for '
    74                      'more instructions.')
    75
    76 -    def buildOptions(self):
    77          self.parser.add_option('--newid',
    78                                 dest='newid',
    79                                 default=None,
    80                                 help='Specify a new name for this ZenPack. '
    81                                  'It must contain at least three package names '
    82                                  'separated by periods, the first one being '
    83                                  '"ZenPacks"')
    84          ZenScriptBase.buildOptions(self)
    85
    86
    87  if __name__ == '__main__':
    88      e = Eggify()
    89      try:
    90          e.run()
    91      except ZenPackException, e:
    92          import sys
    93          sys.stderr.write('%s\n' % str(e))
    94          sys.exit(-1)
    95