44import argparse
55import psycopg2 as pg
66import row_processor as Processor
7+ import six
78
89# Special rules needed for certain tables (esp. for old database dumps)
910specialRules = {
@@ -45,7 +46,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
4546 pre = open ('./sql/' + table + '_pre.sql' ).read ()
4647 post = open ('./sql/' + table + '_post.sql' ).read ()
4748 except IOError as e :
48- print >> sys . stderr , "Could not load pre/post sql. Are you running from the correct path?"
49+ six . print_ ( "Could not load pre/post sql. Are you running from the correct path?" , file = sys . stderr )
4950 sys .exit (- 1 )
5051
5152 dbConnectionParam = "dbname={}" .format (dbname )
@@ -68,20 +69,20 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
6869 with pg .connect (dbConnectionParam ) as conn :
6970 with conn .cursor () as cur :
7071 try :
71- with open (dbFile ) as xml :
72+ with open (dbFile , 'rb' ) as xml :
7273 # Pre-processing (dropping/creation of tables)
73- print ('Pre-processing ...' )
74+ six . print_ ('Pre-processing ...' )
7475 if pre != '' :
7576 cur .execute (pre )
7677 conn .commit ()
77- print ('Pre-processing took {:.1f} seconds' .format (time .time () - start_time ))
78+ six . print_ ('Pre-processing took {:.1f} seconds' .format (time .time () - start_time ))
7879
7980 # Handle content of the table
8081 start_time = time .time ()
81- print ('Processing data ...' )
82+ six . print_ ('Processing data ...' )
8283 for rows in Processor .batch (Processor .parse (xml ), 500 ):
8384 valuesStr = ',\n ' .join (
84- [ _createCmdTuple (cur , keys , tmpl , row_attribs )
85+ [ _createCmdTuple (cur , keys , tmpl , row_attribs ). decode ( 'utf-8' )
8586 for row_attribs in rows
8687 ]
8788 )
@@ -91,26 +92,26 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
9192 ' VALUES\n ' + valuesStr + ';'
9293 cur .execute (cmd )
9394 conn .commit ()
94- print ('Table processing took {:.1f} seconds' .format (time .time () - start_time ))
95+ six . print_ ('Table processing took {:.1f} seconds' .format (time .time () - start_time ))
9596
9697 # Post-processing (creation of indexes)
9798 start_time = time .time ()
98- print ('Post processing ...' )
99+ six . print_ ('Post processing ...' )
99100 if post != '' :
100101 cur .execute (post )
101102 conn .commit ()
102- print ('Post processing took {} seconds' .format (time .time () - start_time ))
103+ six . print_ ('Post processing took {} seconds' .format (time .time () - start_time ))
103104
104105 except IOError as e :
105- print ("Could not read from file {}." .format (dbFile ), file = sys .stderr )
106- print ("IOError: {0}" .format (e .strerror ), file = sys .stderr )
106+ six . print_ ("Could not read from file {}." .format (dbFile ), file = sys .stderr )
107+ six . print_ ("IOError: {0}" .format (e .strerror ), file = sys .stderr )
107108 except pg .Error as e :
108- print ("Error in dealing with the database." , file = sys .stderr )
109- print ("pg.Error ({0}): {1}" .format (e .pgcode , e .pgerror ), file = sys .stderr )
110- print (str (e ), file = sys .stderr )
109+ six . print_ ("Error in dealing with the database." , file = sys .stderr )
110+ six . print_ ("pg.Error ({0}): {1}" .format (e .pgcode , e .pgerror ), file = sys .stderr )
111+ six . print_ (str (e ), file = sys .stderr )
111112 except pg .Warning as w :
112- print ("Warning from the database." , file = sys .stderr )
113- print ("pg.Warning: {0}" .format (str (w )), file = sys .stderr )
113+ six . print_ ("Warning from the database." , file = sys .stderr )
114+ six . print_ ("pg.Warning: {0}" .format (str (w )), file = sys .stderr )
114115
115116
116117
@@ -268,10 +269,17 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
268269 'CreationDate' ,
269270 'UserId' ,
270271 ]
272+
273+ try :
274+ # Python 2/3 compatibility
275+ input = raw_input
276+ except NameError :
277+ pass
278+
271279choice = input ('This will drop the {} table. Are you sure [y/n]?' .format (table ))
272280
273281if len (choice ) > 0 and choice [0 ].lower () == 'y' :
274282 handleTable (table , keys , args .dbname , args .file , args .host , args .port , args .username , args .password )
275283else :
276- print ("Cancelled." )
284+ six . print_ ("Cancelled." )
277285
0 commit comments