-
Notifications
You must be signed in to change notification settings - Fork 1
/
odbctest.py
170 lines (147 loc) · 5.7 KB
/
odbctest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys
import getopt
import getpass
reload(sys)
sys.setdefaultencoding('utf-8')
from query_tester import QueryTester
from query_runner import DatabaseConnection
COMPARE_MODE = "COMPARE"
GENERATE_MODE = "GENERATE"
sys.settrace
class Configuration(object):
def set_mode(self, mode_string):
self.mode = mode_string
def set_odbcdriver(self, odbcdriver):
self.odbcdriver = odbcdriver
def set_server(self, server):
self.server = server
def set_port(self, port):
self.port = port
def set_database(self, database):
self.database = database
def set_username(self, username):
self.username = username
def set_password(self, password):
self.password = password
def set_querydir(self, querydir):
self.querydir = querydir
def set_queryfile(self, queryfile):
self.queryfile = queryfile
def set_resultsdir(self, resultsdir):
self.resultsdir = resultsdir
def set_expecteddir(self, expecteddir):
self.expecteddir = expecteddir
def __str__(self):
return str(self.__dict__)
def dict(self):
return self.__dict__
def main(argv):
opts, args = getopt.getopt(argv, "hm:o:s:d:u:p:q:r:e:", [
"help", "mode=", "odbcdriver=", "server=", "database=", "username=",
"password=", "querydir=", "resultsdir=", "expecteddir="
])
# if password wasn't provided in cli, ask for it
config = Configuration()
for opt, value in opts:
if opt in ['-h', '--help']:
print_help()
return
elif opt in ['-m', '--mode']:
config.set_mode(value)
elif opt in ['-o', '--odbcdriver']:
config.set_odbcdriver(value)
elif opt in ['-s', '--server']:
parts = value.split(":")
config.set_server(parts[0])
if (len(parts) == 2):
config.set_port(parts[1])
elif opt in ['-d', '--database']:
config.set_database(value)
elif opt in ['-u', '--username']:
config.set_username(value)
elif opt in ['-p', '--password']:
if value == ".": value = getpass.getpass()
config.set_password(value)
elif opt in ['-q', '--querydir']:
parts = value.split("/")
print parts
config.set_querydir("/".join(parts[:-1])
if value.endswith(".xml") else value)
config.set_queryfile(parts[-1] if value.endswith(".xml") else None)
print config
elif opt in ['-r', '--resultsdir']:
config.set_resultsdir(value)
elif opt in ['-e', '--expecteddir']:
config.set_expecteddir(value)
if not hasattr(config, 'mode'):
help_and_exit('No operation mode specified')
if not hasattr(config, 'odbcdriver'):
help_and_exit('No odbcdriver specified.')
if not hasattr(config, 'server'):
help_and_exit('No server specified')
if not hasattr(config, 'port'):
help_and_exit('No server port specified')
if not hasattr(config, 'database'):
help_and_exit('No database name specified')
if not hasattr(config, 'username'):
help_and_exit('No username specified')
if not hasattr(config, 'password'):
help_and_exit('No password specified')
if not hasattr(config, 'querydir'):
help_and_exit(
'You should specify an xml or a directory containing queries.')
if not hasattr(config, 'resultsdir'):
help_and_exit('You should specify a directory to save results')
if config.mode == COMPARE_MODE and not hasattr(config, 'expecteddir'):
help_and_exit('For ' + COMPARE_MODE +
' mode there has to be expecteddir specified')
db_conn = DatabaseConnection(config.odbcdriver, config.server, config.port,
config.database, config.username,
config.password)
tester = QueryTester(db_conn, config.querydir, config.resultsdir,
config.database, config.expecteddir
if hasattr(config, "expecteddir") else None)
result = True
if config.mode == GENERATE_MODE:
result = tester.generate_results(
config.queryfile if hasattr(config, "queryfile") else None)
elif config.mode == COMPARE_MODE:
result = tester.compare_results(
config.queryfile if hasattr(config, "queryfile") else None)
if not result:
sys.exit(1)
def help_and_exit(message):
print message
print_help()
sys.exit(2)
def print_help():
print "To use this script:"
print "#####"
print "GENERATE mode:"
print "odbctest.py -m GENERATE -o <odbcdriver> -s <server>:<port> -d <database> -u <username> -p <password> -q <queryfile> -r <resultsdir>"
print "or"
print "odbctest.py"
print " --mode GENERATE"
print " --odbcdriver <odbcdriver>"
print " --server <server>:<port>"
print " --database <database>"
print " --username <username>"
print " --password <password>"
print " --queryfile <queryfile>"
print " --resultsdir <resultsdir>"
print "#####"
print "COMPARE mode:"
print "odbctest.py -m COMPARE -o <odbcdriver> -s <server>:<port> -d <database> -u <username> -p <password> -q <queryfile> -r <resultsdir> -e <expectedresultsdir>"
print "or"
print "odbctest.py"
print " --mode COMPARE"
print " --odbcdriver <odbcdriver>"
print " --server <server>:<port>"
print " --database <database>"
print " --username <username>"
print " --password <password>"
print " --queryfile <queryfile>"
print " --resultsdir <resultsdir>"
print " --expecteddir <expectedresultsdir>"
if __name__ == "__main__":
main(sys.argv[1:])