Package Products :: Package ZenRelations :: Module ExportRM |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.ExportRM

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__="""ExportRM
     12
     13  Export RelationshipManager objects from a Zope database
     14  """
     15
     16  import sys
     17  import datetime
     18
     19  import Globals
     20
     21  from Products.ZenUtils.ZCmdBase import ZCmdBase
     22
     23 -class ExportRM(ZCmdBase):
     24      """
     25      Wrapper class around exportXml() to create XML exports of relations.
     26      """
     27
     28 -    def __init__(self):
     29          """
     30          Initializer that creates an output file, or if nothing is specified
     31          with the command-line option --outfile, sends to stdout.
     32          """
     33          ZCmdBase.__init__(self)
     34          if not self.options.outfile:
     35              self.outfile = sys.stdout
     36          else:
     37              self.outfile = open(self.options.outfile, 'w')
     38
     39
     40
     41 -    def buildOptions(self):
     42          """
     43          Command-line options setup
     44          """
     45          ZCmdBase.buildOptions(self)
     46
     47          self.parser.add_option('-o', '--outfile',
     48                      dest="outfile",
     49                      help="Output file for exporting XML objects. Default is stdout")
     50
     51          self.parser.add_option('--ignore', action="append",
     52                      dest="ignorerels", default=[],
     53                      help="Relations that should be ignored.  Every relation to" + \
     54                 " ignore must be specified with a separate --ignorerels option." )
     55
     56
     57 -    def getVersion(self):
     58          """
     59          Gather our current version information
     60
     61          @return: Zenoss version information
     62          @rtype: string
     63          """
     64          from Products.ZenModel.ZenossInfo import ZenossInfo
     65          zinfo = ZenossInfo('')
     66          return str(zinfo.getZenossVersion())
     67
     68
     69 -    def getServerName(self):
     70          """
     71          Gather our Zenoss server name
     72
     73          @return: Zenoss server name
     74          @rtype: string
     75          """
     76          import socket
     77          return socket.gethostname()
     78
     79
     80 -    def export(self, root=None):
     81          """
     82          Create XML header and then call exportXml() for all objects starting at root.
     83
     84          @param root: DMD object root
     85          @type root: object
     86          """
     87
     88          if not root:
     89              root = self.dataroot
     90
     91          if not hasattr(root, "exportXml"):
     92              print  "ERROR: Root object for %s is not exportable (exportXml not found)" % root
     93              sys.exit(1)
     94
     95          export_date = datetime.datetime.now()
     96          version = self.getVersion()
     97          server = self.getServerName()
     98
     99          # TODO: When the DTD gets created, add the reference here
    100          self.outfile.write( """<?xml version="1.0" encoding="ISO-8859-1" ?>
    101
    102  <!--
    103      Zenoss RelationshipManager export completed on %s
    104
    105      Use ImportRM to import this file.
    106
    107      For more information about Zenoss, go to http://www.zenoss.com
    108   -->
    109
    110  <objects version="%s" export_date="%s" zenoss_server="%s" >\n""" % \
    111           ( export_date, version, export_date, server ))
    112
    113
    114          # Pass off all the hard work to the objects
    115          root.exportXml(self.outfile, self.options.ignorerels, True)
    116
    117          # Write the ending element
    118          self.outfile.write( "</objects>\n" )
    119
    120
    121  if __name__ == '__main__':
    122      ex = ExportRM()
    123      ex.export()
    124