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

# Source Code for Module Products.ZenUtils.BasicLoader

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