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

# Source Code for Module Products.ZenRelations.ExportRM

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