Module sqlite_undump
[hide private]
[frames] | no frames]

Source Code for Module sqlite_undump

 1  #!/usr/bin/env python-beta 
 2  import re 
 3  import sqlite3 as sqlite 
 4   
 5  junk_re = r'\bENGINE=MyISAM\b|\bDEFAULT CHARSET=latin1\b' \ 
 6            r'|^\s*(UN)?LOCK.*?$' \ 
 7            r'|,\s*\n\s*KEY `[^`]*` \([^()]*\)' \ 
 8            r'|on\s+update\s+[^\s,]*' 
 9  junk_rec = re.compile (junk_re, re.MULTILINE) 
10  insert_re = r'INSERT\s(.*)\sVALUES\s(.*)' 
11  insert_rec = re.compile (insert_re) 
12  comma_re = r'\),\(' 
13  comma_rec = re.compile (comma_re) 
14  quote_re = r"'((?:[^'\\]|\\.)*)'" 
15  quote_rec = re.compile (quote_re) 
16  unique_re = r'UNIQUE KEY `([^`]*)` \(([^()]*)\)' 
17  unique_rec = re.compile (unique_re) 
18   
19 -def sqlite_undump (database, dump):
20 connection = sqlite.connect (database) 21 command = '' 22 if isinstance (dump, str): 23 dump = open (dump, 'r') 24 for line in dump: 25 command += line 26 if not command.startswith ('CREATE') or sqlite.complete_statement (command): 27 command = junk_rec.sub ('', command) 28 command = quote_rec.sub ( 29 lambda match: "'" + match.group (1).replace ("\\'", "''") + "'", 30 command) 31 command = unique_rec.sub (lambda match: 'UNIQUE (' + match.group (2) + ')', 32 command) 33 match = insert_rec.match (command) 34 if match: 35 parts = ['(' + part + ')' for part in comma_rec.split (match.group (2))] 36 parts[0] = parts[0][1:] 37 parts[-1] = parts[-1][:-1] 38 for part in parts: 39 command = 'INSERT %s VALUES %s' % (match.group (1), part) 40 print command 41 connection.execute (command) 42 else: 43 print command 44 connection.execute (command) 45 command = '' 46 print 'Done.'
47
48 -def main ():
49 import bz2, gzip, sys 50 database, dump = sys.argv[1:] 51 if dump.endswith ('.bz2'): 52 dump = bz2.BZ2File (dump) 53 elif dump.endswith ('.gz'): 54 dump = gzip.GzipFile (dump) 55 sqlite_undump (database, dump)
56 57 if __name__ == '__main__': main () 58