1
2
3
4
5
6
7
8
9
10
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
34 '''Load a machine'''
35
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
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
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
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
102
103
104 if __name__ == "__main__":
105 loader = BasicLoader()
106 loader.loadDatabase()
107 print "Database Load is finished!"
108