-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.py
executable file
·130 lines (89 loc) · 3.83 KB
/
filter.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
#!/usr/bin/python
import os.path
import os
import re
from enum import Enum
from enum import IntEnum
# --- SCRIPT REQUIREMENTS ---
#
# Requires Python Version 2.6.6 or above.
# Requires Python 3.4 Enum Backported
# - enum34 1.1.6
#
# --- DEFINING ENUMERATORS ---
# Defined as IntEnums so they are compatible with Integers
# Columns in 2D array
class FILE(IntEnum):
Line = 0
String = 1
Stripped = 2
Save = 3
# --- SUB FUNCTIONS ---
# Function to return an index minus one
# Recieves one integer
def indexMinusOne(index_given):
return index_given - 1
# Function to remove new line characters from a string
# Recieves one string to remove new lines from
def removeNewlines(string):
string = string.replace('\n', '')
return string
# --- BEGIN MAIN ---
def filter(importfile):
# Used to Store all lines from the file
sqlcmds = []
# Define counters
lineno = 1
counter = 0
lines_removed = 0
print
print('Checking ' + str(importfile))
if os.path.exists(importfile) and os.path.isfile(importfile):
# Write the information from the sql log to the array in a 2D format
with open(importfile) as import_file:
for line in import_file:
# Append List to the current position
sqlcmds.append([])
sqlcmds[counter].insert(FILE.Line.value, lineno)
sqlcmds[counter].insert(FILE.String.value, removeNewlines(line))
# Strip whitespace from before and after the lines string and then store
sqlcmds[counter].insert(FILE.Stripped.value, sqlcmds[counter][FILE.String.value].strip())
# If this line is a COMMIT only line
if sqlcmds[counter][FILE.Stripped.value] == 'COMMIT;' or sqlcmds[counter][FILE.Stripped.value] == 'ROLLBACK;':
# Check the previous line to see if it was an START TRANSACTION only line
# If this is the case then this is a useless command that can be filtered as it does nothing
if sqlcmds[indexMinusOne(counter)][FILE.String.value] == 'START TRANSACTION;':
sqlcmds[indexMinusOne(counter)].insert(FILE.Save.value, '0')
sqlcmds[counter].insert(FILE.Save.value, '0')
lines_removed += 1
lineno += 1
counter += 1
# --- STORE INFORMATION ---
print('Found ' + str(lines_removed) + ' Empty Transaction/s')
print('Removed ' + str(lines_removed * 2) + ' Lines')
# Only Try to Remove Empty Transactions if There Were Some
if lines_removed != 0:
print('Creating Temporary file...')
file_export = importfile
file_tmp = str(importfile) + "_tmp"
# Rename the file to a tmp
os.rename(importfile, str(file_tmp))
export = open(file_export, "a")
print('Removing Empty Transactions...')
# Reiterate through the array to store information
for x in range(len(sqlcmds)):
# If the Save has not been set then set it
if len(sqlcmds[x]) != 4:
sqlcmds[x].insert(FILE.Save.value, '1')
# If the row is set to be saved
if sqlcmds[x][FILE.Save.value] == '1':
write = sqlcmds[x][FILE.Stripped.value] + "\n"
export.write(write)
export.close()
print('Empty Transactions Removed Successfully')
# --- DELETE TEMP FILE ---
os.remove(file_tmp)
print('Temporary File Removed')
# --- IF FILE DOES NOT EXIST ---
else:
print("No Export File to Filter...")