-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemaSpyCleanup.py
More file actions
executable file
·325 lines (264 loc) · 10.8 KB
/
schemaSpyCleanup.py
File metadata and controls
executable file
·325 lines (264 loc) · 10.8 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/opt/python3.12/bin/python3
# Name: schemaSpyCleanup.py
# Purpose: to make MGI-specific customizations to the output HTML files
# generated by schemaSpy.
# Notes: schemaSpy is a schema documentation generator hosted on SourceForge
# and released under the lesser GPL license. Since we are only altering
# its output, rather than the source code itself, we will hopefully be
# okay license-wise.
import os
import re
import sys
import getopt
import runCommand
import dbManager
dbm = None # to be dbManager.postgresManager object
USAGE='''Usage: %s [-a|-d|-i] <target file> <server> <database> <user> <password>
Purpose:
to make a few alterations to an HTML file generated by schemaSpy.
Where possible, we always:
* improve the Indexes section for tables
* add MGI branding
Options:
-a : remove the "Anomalies" tab from the top of the page
-d : remove the "Donate" tab from the top of the page
-i : note that the file has no Index information, so do not give an
error when none is found in the database
Required Parameters:
target file : the HTML file to edit and replace
''' % sys.argv[0]
###--- globals ---###
BRANDING = '''
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD WIDTH="100%">
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD WIDTH="20%" VALIGN="center" ALIGN="left">
<A HREF="http://www.informatics.jax.org/" border=0><IMG SRC="http://www.informatics.jax.org/webshare/images/mgi_logo.jpg" BORDER=0 HEIGHT="70" WIDTH="160" ALT="Mouse Genome Informatics"></A>
</TD>
<TD WIDTH="60%" ALIGN="center" VALIGN="center" BGCOLOR="#ffffff">
<FONT COLOR="#000000" SIZE=5 FACE="Arial,Helvetica">
Schema Browser
</FONT>
</TD>
<TD WIDTH="20%" VALIGN="center" ALIGN="center" BGCOLOR="#ffffff">
</TD>
</TR>
<TR>
<TD COLSPAN=3 style="background-color:#0000ff;">
<FONT face="Arial,Helvetica" color="#ffffff">
<B> Mouse Genome Informatics</B>
</FONT>
</TD>
</TR>
<TR>
<TD>
<FONT SIZE=-1 FACE="Arial,Helvetica">
<CENTER>
<A HREF="http://www.informatics.jax.org/" vlink="#0000ff">MGI Home</A>
<a href="http://www.informatics.jax.org/mgihome/help/help.shtml" vlink="#0000ff">Help</A>
</CENTER>
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR><TD> </TD></TR>
</TABLE>
'''
HOST = None
DATABASE = None
SCHEMA = None
USER = None
PASSWORD = None
PATH = None
TABLE = None
STRIP_DONATE_TAB = False
STRIP_ANOMALIES_TAB = False
SKIP_INDEXES = False
###--- functions ---###
def bailout (message, showUsage = False):
if showUsage:
sys.stderr.write (USAGE)
sys.stderr.write ('Error: %s\n' % message)
sys.exit(1)
def processCommandLine():
global HOST, DATABASE, USER, PASSWORD, PATH, TABLE, SCHEMA
global STRIP_DONATE_TAB, STRIP_ANOMALIES_TAB, SKIP_INDEXES
global dbm
try:
(options, args) = getopt.getopt (sys.argv[1:], 'adi')
except:
bailout ('Invalid command-line')
for (option, value) in options:
if option == '-a':
STRIP_ANOMALIES_TAB = True
elif option == '-d':
STRIP_DONATE_TAB = True
elif option == '-i':
SKIP_INDEXES = True
else:
bailout ('Unknown flag: %s' % option)
if len(args) < 6:
bailout ('Too few parameters')
elif len(args) > 6:
bailout ('Too many parameters')
PATH = args[0]
TABLE = os.path.basename(PATH).replace('.html', '')
HOST = args[1]
DATABASE = args[2]
SCHEMA = args[3]
USER = args[4]
PASSWORD = args[5]
dbm = dbManager.postgresManager(HOST, DATABASE, USER, PASSWORD)
dbm.setReturnAsMGI(True)
return
def analyzeColumns (columns):
columns = [x.strip() for x in columns.split(',')]
columnsOnly = []
directions = []
for column in columns:
items = column.split()
if (len(items) == 2) and (items[1] == 'DESC'):
col = items[0]
direction = 'Desc'
else:
col = column
direction = 'Asc'
# trim data types from function-based
# columns
col = re.sub('::[^)]+', '', col)
columnsOnly.append (col)
directions.append (direction)
return columnsOnly, directions
q = re.compile('\\((.*)\\)$')
def analyzeCreateIndexStatement (line):
global q
match = q.search(line.strip())
if match:
columnsOnly, directions = analyzeColumns(match.group(1))
return columnsOnly, directions
return None
def getIndexDataSQL():
# try to get the index data using direct SQL (skip using psql, as it
# was problematic)
sqlFile = 'getIndexes.sql'
fp = open(sqlFile, 'r')
lines = fp.readlines()
fp.close()
lines = [x.rstrip() for x in lines]
cmd = ' '.join(lines)
cmd = cmd.replace('MY_TABLE_NAME', '%s' % TABLE)
results = dbm.execute(cmd)
indexes = []
for line in results:
attributes = []
name = line['relname']
isPrimary = line['indisprimary']
isUnique = line['indisunique']
isClustered = line['indisclustered']
sql = line['indexSql'].lower()
constraint = line['indexConstraint']
print(line)
print(isPrimary, isUnique, isClustered)
out = analyzeCreateIndexStatement(sql)
if not out:
continue
columnsOnly, directions = out
if isPrimary:
attributes.append ('Primary key')
elif isUnique:
attributes.append ('Must be unique')
else:
attributes.append ('Performance')
if isClustered:
attributes.append ('Used to cluster data')
indexes.append ( (name, attributes, columnsOnly, directions) )
return indexes
def readFile():
fp = open(PATH, 'r')
lines = fp.readlines()
fp.close()
return lines
def cleanup(lines):
indexes = getIndexDataSQL()
cleanLines = []
beforeBranding = 0 # before we've added MGI branding
beforeIndexes = 1 # before we get to the Indexes section
beforeRows = 2 # before we find the data rows for Indexes
inRows = 3 # while we are in the data rows for Indexes
afterIndexes = 4 # after we've done the Indexes section
status = beforeBranding
for line in lines:
if STRIP_DONATE_TAB:
if line.find('>Donate<') >= 0:
if line.find('sourceforge') >= 0:
# skip the Donate tab
continue
if STRIP_ANOMALIES_TAB:
if line.find('>Anomalies<') >= 0:
if line.find('anomalies.html') >= 0:
# skip the Anomalies tab
continue
if status == beforeBranding:
if line.find('headerHolder') >= 0:
# add branding and begin looking for the
# Indexes section
cleanLines.append (BRANDING)
status = beforeIndexes
elif status == beforeIndexes:
if line.find('>Indexes:<') >= 0:
# note that we've hit the start of the
# Indexes section
status = beforeRows
elif status == beforeRows:
if line.find('<tbody>') >= 0:
# note that we have found the start of the
# data rows of the Indexes section
status = inRows
elif status == inRows:
if line.find('</table>') >= 0:
status = afterIndexes
# add in our custom data rows for the Indexes
# table
for (name, attr, cols, dirs) in indexes:
if 'Primary key' in attr:
class1 = 'primaryKey'
else:
class1 = 'indexedColumn'
cell3 = []
for dir in dirs:
if dir == 'Asc':
full = 'Ascending'
else:
full = 'Descending'
cell3.append ("<span title='%s'>%s</span>" % (full, dir))
cleanLines.append (" <tr>\n")
cleanLines.append (" <td class='%s'>%s</td>\n" % (class1, ' + '.join (cols)) )
cleanLines.append (" <td class='detail'>%s</td>\n" % ', '.join (attr))
cleanLines.append (" <td class='detail' style='text-align:left;'>%s</td>\n" % '/'.join(cell3))
cleanLines.append (" <td class='constraint' style='text-align:left;'>%s</td>\n" % name)
cleanLines.append (" </tr>")
else:
# skip all data rows for the Indexes table, as
# we are going to replace them
continue
cleanLines.append (line)
return cleanLines
def writeFile (lines):
fp = open (PATH, 'w')
for line in lines:
fp.write(line)
fp.close()
return
def main():
processCommandLine()
lines = cleanup(readFile())
writeFile (lines)
return
###--- main program ---###
if __name__ == '__main__':
main()