-
Notifications
You must be signed in to change notification settings - Fork 12
/
mysqltransformp.py
executable file
·54 lines (48 loc) · 1.77 KB
/
mysqltransformp.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
#!/usr/bin/python
import argparse
import bz2
import datetime
import os
import re
def main():
destination_default = 'mysql-dumps-' + str(datetime.datetime.now().date())
parser = argparse.ArgumentParser()
parser.add_argument('sql_dump', help='the SQL file to transform in multiple files')
parser.add_argument('-d', '--destination', help='name of the folder to be created', default=destination_default)
arguments = parser.parse_args()
# If the destionation folder does not exist, then create it.
if not os.path.exists(arguments.destination):
os.makedirs(arguments.destination)
try:
file_object = open(arguments.sql_dump, 'r')
sql_buffer = tablename = filename = table_header = ''
header_found = False
# Read the input file line by line.
for line in file_object:
# Detect if this is a new table.
if _is_new_sql_table(line):
header_found = True
if len(sql_buffer) and len(filename):
_save_table(table_header + sql_buffer, filename)
print 'Saved: ' + tablename
sql_buffer = ''
tablename = _get_tablename(line)
print 'Detected table: ' + tablename
filename = arguments.destination + '/' + tablename + '.sql'
elif not header_found:
table_header += line
sql_buffer += line
print 'Saving: ' + filename
_save_table(table_header + sql_buffer, filename)
finally:
file_object.close()
def _is_new_sql_table(line):
return re.search('^-- Table structure for table.*', line) != None
def _get_tablename(line):
return re.search('^-- Table structure for table `([^ ]*)`.*', line).group(1)
def _save_table(sql_buffer, filename):
file_object = bz2.BZ2File(filename + '.bz2', 'w')
file_object.write(sql_buffer)
file_object.close()
if __name__ == '__main__':
main()