#!/usr/pkg/bin/python import sys, os, re, pwd homedir = pwd.getpwuid(os.geteuid())[5] sys.path.append(os.sep.join([homedir, 'lib', 'python'])) from com.jcomeau import arcane dbdir = os.sep.join([homedir, 'gadfly']) sys.path.append('.') import gadfly def cleanup(string): match = re.compile('([^-]*)--').match(string) if match != None: string = match.group(1) return string.rstrip() try: os.mkdir(dbdir) except: pass # most likely exists already connection = gadfly.gadfly() connection.startup("paypal", dbdir) cursor = connection.cursor() sqlcommands = open('initdb.sql', 'r') continuation = False for line in sqlcommands.readlines(): if continuation: command += cleanup(line) continuation = False else: command = cleanup(line) if len(command) == 0: continue elif re.compile(',$').search(command) != None: continuation = True continue try: if re.compile('^BEGIN\\b', re.IGNORECASE).match(command) != None: pass elif re.compile('^COMMIT\\b', re.IGNORECASE).match(command) != None: connection.commit() else: cursor.execute(command) except: if re.compile('^DROP ', re.IGNORECASE).match(command) != None: pass else: sys.stderr.write('%s: %s\n' % ('cannot process command', command)) raise if re.compile('^SELECT ', re.IGNORECASE).match(command) != None: print cursor.pp() print