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

Source Code for Module 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 Utils import getHierarchyObj 
 30   
 31  from ZCmdBase import ZCmdBase 
 32   
33 -class BasicLoader(ZCmdBase):
34 '''Load a machine''' 35
36 - def __init__(self, noopts=0, app=None, ignoreComments=True):
37 '''Handle command line options, get app instance,and setup log file''' 38 ZCmdBase.__init__(self, noopts, app) 39 self.lineNumber = 0 40 self.ignoreComments = ignoreComments
41 42
43 - def setfields(self, fieldnames, line, delimiter='|'):
44 fields = line.split(delimiter) 45 for i in range(len(fields)): 46 setattr(self, fieldnames[i], fields[i])
47 48
49 - def loadDatabase(self):
50 '''do the load''' 51 if self.filename and os.path.exists(self.filename): 52 lines = open(self.filename).readlines() 53 else: 54 self.log.critical("filename %s not found" % self.filename) 55 sys.exit(1) 56 57 for line in lines: 58 self.lineNumber += 1 59 line = line.strip() 60 if not line or (self.ignoreComments and line[0] == '#'): continue 61 try: 62 quit = self.loaderBody(line) 63 if quit == True: break # return True to stop 64 except: 65 self.log.exception("Line Number %i" % (self.lineNumber)) 66 if (not self.options.noCommit 67 and not self.lineNumber % self.options.commitCount): 68 trans = transaction.get() 69 trans.note('Initial load using %s' % self.__class__.__name__) 70 trans.commit() 71 self.app._p_jar.sync() 72 73 if self.options.noCommit: 74 self.log.info("No commit has been made.") 75 else: 76 trans = transaction.get() 77 trans.note('Initial load using %s' % self.__class__.__name__) 78 trans.commit()
79 80
81 - def buildOptions(self):
82 self.usage = "%prog [options] file" 83 ZCmdBase.buildOptions(self) 84 85 self.parser.add_option('-x', '--commitCount', 86 dest='commitCount', 87 default=20, 88 type="int", 89 help='how many lines should be loaded before commit') 90 91 self.parser.add_option('-n', '--noCommit', 92 dest='noCommit', 93 action="store_true", 94 default=0, 95 help='Do not store changes to the Dmd (for debugging)')
96 97
98 - def parseOptions(self):
99 ZCmdBase.parseOptions(self) 100 if len(self.args) > 0: 101 self.filename = self.args[0]
102 103 104 if __name__ == "__main__": 105 loader = BasicLoader() 106 loader.loadDatabase() 107 print "Database Load is finished!" 108