-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.py
executable file
·183 lines (154 loc) · 6.63 KB
/
conf.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
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
import os.path
import os
import re
import ConfigParser
from enum import Enum
from enum import IntEnum
from optparse import OptionParser
import lock
# ##############################
# --- SCRIPT REQUIREMENTS ---
# ##############################
#
# Requires Python Version 2.6.6 or above.
# Requires Python 3.4 Enum Backported
# - enum34 1.1.6
# Configuration Options
class CONFIG(IntEnum):
Database = 0
IgnoreTables = 1
IgnoreText = 2
Export = 3
Filter = 4
Pass = 5
Run = 6
class GLOBAL(IntEnum):
Export = 0
Record = 1
parser = OptionParser()
try:
config_dir = os.path.dirname(os.path.realpath(__file__)) + "/config/"
except:
print("Error inializing conf module. Cannot find file path.")
lock.exit()
def getConfig(config_file):
config_file = config_dir + config_file
# If a configuration is specified
if config_file:
# If the file specified either does not exist or is not a file
if not os.path.exists(config_file) and os.path.isfile(config_file):
print("Specified config file does not exist or is not a file")
lock.exit()
else:
try:
config = ConfigParser.ConfigParser()
config.read(config_file)
except:
print("Error Reading Config file")
lock.exit()
# FETCH DATABASES AND DATABASE SPECIFIC CONFIGURATIONS
# Define a dictionary for the config of the databases
configuration = {}
# FETCH Export Directory
# Try to fetch the export directory
try:
configuration["Global"] = []
configuration["Global"].insert(GLOBAL.Export.value, config.get('SYSTEM', 'export'))
except:
print("Error fetching export directory")
lock.exit()
# FETCH RECORDABLE QUERIES
# Define an array for the recordable sql queries, processed and unprocessed
record = []
count = 0
# Try to fetch the recordable sql queries from the config and input them into the array
try:
configuration["Global"].append([])
record = config.get('SYSTEM', 'record').split(',')
if len(record) == 0:
print("Unable to read record from system")
lock.exit()
elif record[0] == "":
print("No SQL Queries specified for recording")
lock.exit()
else:
# We strip them individually so that SQL commands with a space do not have them removed
for sqlcmd in record:
sqlcmd = sqlcmd.strip()
configuration["Global"][GLOBAL.Record.value].insert(count, sqlcmd)
count += 1
except:
print("Unable to read record from system")
lock.exit()
# Try to split the databases from the configuration into a list with whitespace removed
try:
databases = re.sub('[\s+]', '', config.get('SYSTEM', 'databases')).split(',')
except:
print("Databases incorrectly specified in configuration")
lock.exit()
# Using the split up list of databases as a basis for the number of entries in the list
# Begin moving the config to the configuration list
for db in databases:
# Try and get the user for the current entry and store them in a variable
try:
current_user = config.get(db, 'user')
except:
print("Unable to find the user value in the configuration for %(dbs)s in the configuration file" % {'dbs' : db})
lock.exit()
# Set the dictionary value for the current user as a list in configuration
configuration[current_user] = []
# Insert to that list at the database index the database name
configuration[current_user].insert(CONFIG.Database.value, db)
# Try and separate out the tables to ignore for this user and then insert them into the configuration dictionary at the
# ignoretables index of the user element
try:
ignore_tables = re.sub('[\s+]', '', config.get(db, 'ignore_tables')).split(',')
configuration[current_user].insert(CONFIG.IgnoreTables.value, ignore_tables)
except:
print(configuration)
print("Unable to read specified ignore_tables for %(dbs)s" %{'dbs' : db})
lock.exit()
# Try to separate out the text to ignore for this user and then insert them into the configuration dictionary at the
# ignoretext index of the user element
try:
ignore_text = re.sub('[\s+]', '', config.get(db, 'ignore_text')).split(',')
configuration[current_user].insert(CONFIG.IgnoreText.value, ignore_text)
except:
print(configuration)
print("Unable to read specified ignore_text for %(dbs)s" %{'dbs' : db})
lock.exit()
# Add backticks around any tables that need to be ignored
if configuration[current_user][CONFIG.IgnoreTables.value][0] != '':
for x in range(len(configuration[current_user][CONFIG.IgnoreTables.value])):
configuration[current_user][CONFIG.IgnoreTables.value][x] = '`' + configuration[current_user][CONFIG.IgnoreTables.value][x] + '`'
# Set the database export location
try:
configuration[current_user].insert(CONFIG.Export.value, configuration["Global"][GLOBAL.Export.value] + db)
except:
print("Unable to set database export location")
lock.exit()
# Try to get the filter value from the user
try:
configuration[current_user].insert(CONFIG.Filter.value, config.get(db, 'filter_output'))
except:
configuration[current_user].insert(CONFIG.Filter.value, '0')
# Try to fetch the users password
try:
configuration[current_user].insert(CONFIG.Pass.value, config.get(db, 'password'))
except:
configuration[current_user].insert(CONFIG.Pass.value, 'None')
# Try to separate out databases to run queries on, if there are multiple, then store
try:
runon = re.sub('[\s+]', '', config.get(db, 'runon')).split(',')
configuration[current_user].insert(CONFIG.Run.value, runon)
except:
print(runon)
print("Unable to read specified runon configuration")
lock.exit()
configuration["Unknown User"] = []
configuration["Unknown User"].insert(CONFIG.Database.value, "None")
configuration["Unknown User"].insert(CONFIG.IgnoreTables.value, "None")
configuration["Unknown User"].insert(CONFIG.Filter.value, "0")
configuration["Unknown User"].insert(CONFIG.Pass.value, "None")
return configuration