<s> import os <EOL> import sys <EOL> if '<STR_LIT>' in os . environ : <EOL> sys . path . insert ( <NUM_LIT:0> , os . environ [ '<STR_LIT>' ] ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . environ [ '<STR_LIT>' ] + "<STR_LIT>" ) <EOL> import re <EOL> from conary . dbstore import sqlerrors <EOL> from conary . server import schema <EOL> from conary . dbstore . sqllib import CaselessDict <EOL> class PrintDatabase : <EOL> def __init__ ( self , showTables = True , driver = "<STR_LIT>" ) : <EOL> self . tables = self . views = self . sequences = CaselessDict ( ) <EOL> self . tempTables = CaselessDict ( ) <EOL> self . version = <NUM_LIT:0> <EOL> self . showTables = showTables <EOL> self . statements = [ ] <EOL> self . driver = driver <EOL> if self . driver == "<STR_LIT>" : <EOL> from conary . dbstore import sqlite_drv as drv <EOL> elif driver == "<STR_LIT>" : <EOL> from conary . dbstore import postgresql_drv as drv <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" , driver ) <EOL> self . keywords = drv . KeywordDict ( ) <EOL> def connect ( self , * args , ** kwargs ) : <EOL> pass <EOL> def transaction ( self ) : <EOL> pass <EOL> def commit ( self ) : <EOL> pass <EOL> def rollback ( self ) : <EOL> pass <EOL> def cursor ( self ) : <EOL> return self <EOL> def loadSchema ( self ) : <EOL> pass <EOL> def __skip_delete ( self , sql ) : <EOL> delfrom = re . compile ( "<STR_LIT>" ) <EOL> if delfrom . match ( sql ) : <EOL> raise sqlerrors . DatabaseError <EOL> return False <EOL> def __skip_tempTables ( self , sql ) : <EOL> tmptbl = re . compile ( "<STR_LIT>" ) <EOL> m = tmptbl . match ( sql ) <EOL> if m is not None : <EOL> d = m . groupdict ( ) <EOL> self . tempTables . setdefault ( d [ "<STR_LIT>" ] . strip ( ) , [ ] ) <EOL> return True <EOL> return False <EOL> def __skip_Indexes ( self , sql , skipAll = False ) : <EOL> tmpidx = re . compile ( "<STR_LIT>" ) <EOL> m = tmpidx . match ( sql ) <EOL> if m is not None : <EOL> d = m . groupdict ( ) <EOL> if skipAll or d [ "<STR_LIT>" ] in self . tempTables : <EOL> return True <EOL> return False <EOL> def __skip_Triggers ( self , sql , skipAll = False ) : <EOL> tmptrg = re . compile ( "<STR_LIT>" ) <EOL> if tmptrg . match ( sql ) : <EOL> return skipAll <EOL> return False <EOL> def __skip_Tables ( self , sql , skipAll = False ) : <EOL> tbl = re . compile ( <EOL> "<STR_LIT>" <EOL> ) <EOL> m = tbl . match ( sql ) <EOL> if m is not None : <EOL> d = m . groupdict ( ) <EOL> if d [ "<STR_LIT>" ] : self . tables . setdefault ( d [ "<STR_LIT>" ] . strip ( ) , [ ] ) <EOL> if d [ "<STR_LIT>" ] : self . views . setdefault ( d [ "<STR_LIT>" ] . strip ( ) , True ) <EOL> return skipAll <EOL> return False <EOL> def execute ( self , sql , * args , ** kwargs ) : <EOL> sql = sql . strip ( ) <EOL> if args and "<STR_LIT:?>" in sql : <EOL> return <EOL> if self . __skip_delete ( sql ) : <EOL> return <EOL> if self . __skip_tempTables ( sql ) : <EOL> return <EOL> if self . __skip_Indexes ( sql , self . showTables ) : <EOL> return <EOL> if self . __skip_Triggers ( sql , self . showTables ) : <EOL> return <EOL> if self . __skip_Tables ( sql , not self . showTables ) : <EOL> return <EOL> into = re . compile ( "<STR_LIT>" ) <EOL> if into . match ( sql ) : <EOL> return <EOL> self . statements . append ( sql ) <EOL> def createTrigger ( self , table , column , onAction , pinned = False ) : <EOL> onAction = onAction . lower ( ) <EOL> name = "<STR_LIT>" % ( table , onAction ) <EOL> assert ( onAction in [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> create = "<STR_LIT>" <EOL> if self . driver == "<STR_LIT>" : <EOL> funcName = "<STR_LIT>" % name <EOL> if pinned : <EOL> self . execute ( """<STR_LIT>""" % ( funcName , column , column ) ) <EOL> else : <EOL> self . execute ( """<STR_LIT>""" % ( funcName , column ) ) <EOL> self . execute ( """<STR_LIT>""" % ( name , onAction , table , funcName ) ) <EOL> return <EOL> if self . driver == "<STR_LIT>" : <EOL> when = "<STR_LIT>" <EOL> sql = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( table , column ) ) <EOL> elif self . driver == "<STR_LIT>" : <EOL> when = "<STR_LIT>" <EOL> if pinned : <EOL> sql = "<STR_LIT>" % ( column , column ) <EOL> else : <EOL> sql = "<STR_LIT>" % ( column , ) <EOL> create = "<STR_LIT>" <EOL> elif self . driver == "<STR_LIT>" : <EOL> return <EOL> else : <EOL> raise NotImplementedError <EOL> sql = """<STR_LIT>""" % ( create , name , when . upper ( ) , onAction . upper ( ) , table , sql ) <EOL> self . execute ( sql ) <EOL> return True <EOL> def createIndex ( self , table , name , columns , unique = False ) : <EOL> if unique : <EOL> unique = "<STR_LIT>" <EOL> else : <EOL> unique = "<STR_LIT>" <EOL> sql = "<STR_LIT>" % ( <EOL> unique , name , table , columns ) <EOL> self . execute ( sql ) <EOL> return True <EOL> def setVersion ( self , version ) : <EOL> self . version = version <EOL> def getVersion ( self ) : <EOL> return self . version <EOL> def getTables ( driver = "<STR_LIT>" ) : <EOL> pd = PrintDatabase ( True , driver ) <EOL> schema . loadSchema ( pd ) <EOL> return pd . statements <EOL> def getIndexes ( driver = "<STR_LIT>" ) : <EOL> pd = PrintDatabase ( False , driver ) <EOL> schema . loadSchema ( pd ) <EOL> return pd . statements <EOL> def write_stmt ( driver , stmt ) : <EOL> sys . stdout . write ( "<STR_LIT>" % ( stmt , ) ) <EOL> if driver == "<STR_LIT>" : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> sys . stdout . write ( "<STR_LIT:\n>" ) <EOL> sys . stdout . flush ( ) <EOL> import getopt <EOL> DRIVERS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> driver = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ( opts , args ) = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , DRIVERS + [ "<STR_LIT>" ] ) <EOL> for opt , val in opts : <EOL> if opt == "<STR_LIT>" or opt . startswith ( "<STR_LIT>" ) : <EOL> driver = "<STR_LIT>" <EOL> elif opt == "<STR_LIT>" or opt . startswith ( "<STR_LIT>" ) : <EOL> driver = "<STR_LIT>" <EOL> elif opt == "<STR_LIT>" or opt . startswith ( "<STR_LIT>" ) : <EOL> driver = "<STR_LIT>" <EOL> elif opt == "<STR_LIT>" or opt . startswith ( "<STR_LIT>" ) : <EOL> driver = "<STR_LIT>" <EOL> elif opt in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> print "<STR_LIT>" % ( sys . argv [ <NUM_LIT:0> ] , ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> assert ( driver in DRIVERS ) <EOL> for x in getTables ( driver ) : <EOL> write_stmt ( driver , x ) <EOL> for x in getIndexes ( driver ) : <EOL> write_stmt ( driver , x ) </s>
