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

# Source Code for Module Products.ZenUtils.BasicLoader

      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
     14  __doc__="""BasicLoader.py
     15
     16  BasicLoader provides functionality for batch database loaders
     17  it has a main loop that will the method loaderBody which should
     18  be defined in sub classes of BasicLoader to actually load data.
     19
     20  $Id: BasicLoader.py,v 1.14 2004/04/07 00:52:46 edahl Exp $"""
     21
     22  __version__ = "$Revision: 1.14 $"[11:-2]
     23
     24  import sys
     25  import os
     26
     27  import transaction
     28
     29  from ZCmdBase import ZCmdBase
     30
     31 -class BasicLoader(ZCmdBase):
     32      '''Load a machine'''
     33
     34 -    def __init__(self, noopts=0, app=None, ignoreComments=True):
     35          '''Handle command line options, get app instance,and setup log file'''
     36          ZCmdBase.__init__(self, noopts, app)
     37          self.lineNumber = 0
     38          self.ignoreComments = ignoreComments
     39
     40
     41 -    def setfields(self, fieldnames, line, delimiter='|'):
     42          fields = line.split(delimiter)
     43          for i in range(len(fields)):
     44              setattr(self, fieldnames[i], fields[i])
     45
     46
     47 -    def loadDatabase(self):
     48          '''do the load'''
     49          if self.filename and os.path.exists(self.filename):
     50              lines = open(self.filename).readlines()
     51          else:
     52              self.log.critical("filename %s not found" % self.filename)
     53              sys.exit(1)
     54
     55          for line in lines:
     56              self.lineNumber += 1
     57              line = line.strip()
     58              if not line or (self.ignoreComments and line[0] == '#'): continue
     59              try:
     60                  quit = self.loaderBody(line)
     61                  if quit == True: break # return True to stop
     62              except:
     63                  self.log.exception("Line Number %i" % (self.lineNumber))
     64              if (not self.options.noCommit
     65                  and not self.lineNumber % self.options.commitCount):
     66                  trans = transaction.get()
     67                  trans.note('Initial load using %s' % self.__class__.__name__)
     68                  trans.commit()
     69                  self.app._p_jar.sync()
     70
     71          if self.options.noCommit:
     72              self.log.info("No commit has been made.")
     73          else:
     74              trans = transaction.get()
     75              trans.note('Initial load using %s' % self.__class__.__name__)
     76              trans.commit()
     77
     78
     79 -    def buildOptions(self):
     80          self.usage = "%prog [options] file"
     81          ZCmdBase.buildOptions(self)
     82
     83          self.parser.add_option('-x', '--commitCount',
     84                      dest='commitCount',
     85                      default=20,
     86                      type="int",
     87                      help='how many lines should be loaded before commit')
     88
     89          self.parser.add_option('-n', '--noCommit',
     90                      dest='noCommit',
     91                      action="store_true",
     92                      default=0,
     93                      help='Do not store changes to the Dmd (for debugging)')
     94
     95
     96 -    def parseOptions(self):
     97          ZCmdBase.parseOptions(self)
     98          if len(self.args) > 0:
     99              self.filename = self.args[0]
    100
    101
    102  if __name__ == "__main__":
    103      loader = BasicLoader()
    104      loader.loadDatabase()
    105      print "Database Load is finished!"
    106