-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlDumpParse.py
More file actions
executable file
·160 lines (115 loc) · 3.93 KB
/
sqlDumpParse.py
File metadata and controls
executable file
·160 lines (115 loc) · 3.93 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
#!/usr/bin/python3
import argparse
import csv
import sys
import time
##------------------------------------------------------------------------------
def cleanToken ( a ):
double_chars = [ '||', ' |', '| ' ]
for d in double_chars:
if ( a.find(d) >= 0 ):
b = a.replace ( d, '|' )
a = b
if ( a.startswith(d) ): a = a[2:]
if ( a.endswith(d) ): a = a[:-2]
double_chars = [ ' ' ]
for d in double_chars:
if ( a.find(d) >= 0 ):
b = a.replace ( d, ' ' )
a = b
if ( a.startswith(d) ): a = a[2:]
if ( a.endswith(d) ): a = a[:-2]
spec_chars = [ '\\', '\r', '\n', '\t' ]
for s in spec_chars:
if ( a.find(s) >= 0 ):
b = a.replace ( s, '' )
a = b
if ( a.startswith(s) ): a = a[1:]
if ( a.endswith(s) ): a = a[:-1]
if ( a.startswith(' ') ): a = a[1:]
if ( a.endswith(' ') ): a = a[:-1]
if ( a == '{}' ): a = ''
if ( a == '[]' ): a = ''
if ( a.lower() == 'na' ): a = ''
if ( a.lower() == 'n/a' ): a = ''
if ( a.lower() == 'unspecified' ): a = ''
if ( a == ':' ): a = ''
return ( a )
##------------------------------------------------------------------------------
def parseHeaderLine ( aLine ):
tokens = aLine.split()
outFilename = tokens[1] + ".tsv"
fhOut = open ( outFilename, 'w' )
i1 = aLine.find("(")
i2 = aLine.find(")")
csv = aLine[i1+1:i2]
tokens = csv.split(", ")
print ( tokens )
nTokens = len(tokens)
fhOut.write ( '\t'.join(tokens) )
fhOut.write ( '\n' )
return ( nTokens, tokens, fhOut )
##------------------------------------------------------------------------------
def parseDataLine ( aLine, nExp ):
tsv = aLine[:-1]
tokens = tsv.split("\t")
nTokens = len(tokens)
if ( nTokens != nExp ):
print ( " ERROR ... unexpected number of tokens ??? !!! " )
print ( nExp, nTokens )
print ( tokens )
sys.exit(-1)
newList = []
for t in tokens:
if ( len(t) == 2 ):
if ( ord(t[0]) == 92 ):
if ( ord(t[1]) == 78 ):
t = ''
newList += [t]
return ( newList )
##------------------------------------------------------------------------------
def parseSQLdumpFile ( fhDmp ):
fhDmp.seek(0)
lineNo = 0
startTable = False
for aLine in fhDmp:
if ( aLine.startswith("COPY ") ):
startTable = True
print ( " here we are at the start of a data block ... " )
( nTokens, tokens, fhOut ) = parseHeaderLine ( aLine )
numLines = 0
elif ( ord(aLine[0]) == 92 ):
print ( ord(aLine[1]) )
print ( " here we are at the end of the data ... ", numLines )
print ( aLine )
fhOut.close()
startTable = False
else:
if ( startTable ):
tokens = parseDataLine ( aLine, nTokens )
fhOut.write ( '\t'.join(tokens) )
fhOut.write ( '\n' )
numLines += 1
##------------------------------------------------------------------------------
def main ( args ):
print ( args )
try:
fhDmp = open ( args.sqlDmpFile, 'r' )
except:
print ( " Failed to open log file <{}> ... EXITING ".format ( args.sqlDmpFile ) )
sys.exit(-1)
parseSQLdumpFile ( fhDmp )
print ( " " )
print ( " DONE!!! " )
print ( " " )
##------------------------------------------------------------------------------
if __name__ == '__main__':
t0 = time.time()
parser = argparse.ArgumentParser()
## the first argument is required
parser.add_argument ( '-f', '--inputSQLdumpFileName', action='store', help='input log file (output of genparse)', required=True, dest='sqlDmpFile', type=str )
args = parser.parse_args()
main ( args )
t1 = time.time()
## print ( ' --> time taken in seconds: {dt}'.format(dt=(t1-t0)) )
##------------------------------------------------------------------------------