<s> from gitdh . git import GitCommit <EOL> class DatabaseBackend ( object ) : <EOL> @ staticmethod <EOL> def getDatabaseBackend ( config ) : <EOL> dbEngine = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if dbEngine == '<STR_LIT>' : <EOL> db = MySQL ( config ) <EOL> elif dbEngine == '<STR_LIT>' : <EOL> db = MongoDB ( config ) <EOL> elif dbEngine == '<STR_LIT>' : <EOL> db = SQLite ( config ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return db <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> def getQueuedCommits ( self ) : <EOL> pass <EOL> def getAllCommits ( self ) : <EOL> pass <EOL> def insertCommit ( self , commit ) : <EOL> pass <EOL> def setStatusWorking ( self , commit ) : <EOL> self . setStatus ( commit , commit . status [ : commit . status . rfind ( '<STR_LIT:_>' ) ] + '<STR_LIT>' ) <EOL> def setStatusSkipped ( self , commit ) : <EOL> self . setStatus ( commit , commit . status [ : commit . status . rfind ( '<STR_LIT:_>' ) ] + '<STR_LIT>' ) <EOL> def setStatusFinished ( self , commit ) : <EOL> self . setStatus ( commit , commit . status [ : commit . status . rfind ( '<STR_LIT:_>' ) ] + '<STR_LIT>' ) <EOL> def setStatus ( self , commit , status ) : <EOL> pass <EOL> class MySQL ( DatabaseBackend ) : <EOL> def __init__ ( self , config ) : <EOL> import pymysql <EOL> DatabaseBackend . __init__ ( self , config ) <EOL> confSection = config [ '<STR_LIT>' ] <EOL> self . conn = pymysql . connect ( host = confSection [ '<STR_LIT>' ] , port = config . getint ( '<STR_LIT>' , '<STR_LIT>' ) , user = confSection [ '<STR_LIT>' ] , passwd = confSection [ '<STR_LIT>' ] , db = confSection [ '<STR_LIT>' ] ) <EOL> self . cur = self . conn . cursor ( ) <EOL> self . tableName = confSection [ '<STR_LIT>' ] <EOL> def getQueuedCommits ( self ) : <EOL> self . cur . execute ( "<STR_LIT>" . format ( self . tableName ) ) <EOL> commits = [ ] <EOL> for dbCommit in self . cur . fetchall ( ) : <EOL> commits . append ( GitCommit ( dbCommit [ <NUM_LIT:1> ] , dbCommit [ <NUM_LIT:2> ] , dbCommit [ <NUM_LIT:3> ] , dbCommit [ <NUM_LIT:4> ] , dbCommit [ <NUM_LIT:5> ] , dbCommit [ <NUM_LIT:6> ] , id = dbCommit [ <NUM_LIT:0> ] , status = dbCommit [ <NUM_LIT:7> ] ) ) <EOL> return commits <EOL> def getAllCommits ( self ) : <EOL> self . cur . execute ( '<STR_LIT>' . format ( self . tableName ) ) <EOL> commits = [ ] <EOL> for dbCommit in self . cur . fetchall ( ) : <EOL> commits . append ( GitCommit ( dbCommit [ <NUM_LIT:1> ] , dbCommit [ <NUM_LIT:2> ] , dbCommit [ <NUM_LIT:3> ] , dbCommit [ <NUM_LIT:4> ] , dbCommit [ <NUM_LIT:5> ] , dbCommit [ <NUM_LIT:6> ] , id = dbCommit [ <NUM_LIT:0> ] , status = dbCommit [ <NUM_LIT:7> ] ) ) <EOL> return commits <EOL> def insertCommit ( self , commit ) : <EOL> params = ( commit . hash , commit . author , commit . date , commit . message , commit . status , commit . repository , commit . branch ) <EOL> self . cur . execute ( '<STR_LIT>' . format ( self . tableName ) , params ) <EOL> self . cur . connection . commit ( ) <EOL> commit . id = self . cur . lastrowid <EOL> def setStatus ( self , commit , status ) : <EOL> commit . status = status <EOL> if hasattr ( commit , '<STR_LIT:id>' ) and not commit . id is None : <EOL> params = ( status , commit . id ) <EOL> self . cur . execute ( '<STR_LIT>' . format ( self . tableName ) , params ) <EOL> self . cur . connection . commit ( ) <EOL> def __del__ ( self ) : <EOL> try : <EOL> self . cur . close ( ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> self . conn . close ( ) <EOL> except AttributeError : <EOL> pass <EOL> class SQLite ( MySQL ) : <EOL> def __init__ ( self , config ) : <EOL> import sqlite3 <EOL> DatabaseBackend . __init__ ( self , config ) <EOL> confSection = config [ '<STR_LIT>' ] <EOL> self . conn = sqlite3 . connect ( confSection [ '<STR_LIT>' ] ) <EOL> self . cur = self . conn . cursor ( ) <EOL> self . tableName = confSection [ '<STR_LIT>' ] <EOL> self . _createTable ( ) <EOL> def _createTable ( self ) : <EOL> self . cur . execute ( """<STR_LIT>""" . format ( self . tableName ) ) <EOL> self . conn . commit ( ) <EOL> def getQueuedCommits ( self ) : <EOL> self . cur . execute ( "<STR_LIT>" . format ( self . tableName ) ) <EOL> commits = [ ] <EOL> for dbCommit in self . cur . fetchall ( ) : <EOL> commits . append ( GitCommit ( dbCommit [ <NUM_LIT:1> ] , dbCommit [ <NUM_LIT:2> ] , dbCommit [ <NUM_LIT:3> ] , dbCommit [ <NUM_LIT:4> ] , dbCommit [ <NUM_LIT:5> ] , dbCommit [ <NUM_LIT:6> ] , id = dbCommit [ <NUM_LIT:0> ] , status = dbCommit [ <NUM_LIT:7> ] ) ) <EOL> return commits <EOL> def insertCommit ( self , commit ) : <EOL> params = ( commit . hash , commit . author , commit . date , commit . message , commit . status , commit . repository , commit . branch ) <EOL> self . cur . execute ( "<STR_LIT>" . format ( self . tableName ) , params ) <EOL> self . cur . connection . commit ( ) <EOL> commit . id = self . cur . lastrowid <EOL> def setStatus ( self , commit , status ) : <EOL> commit . status = status <EOL> if hasattr ( commit , '<STR_LIT:id>' ) and not commit . id is None : <EOL> params = ( status , commit . id ) <EOL> self . cur . execute ( '<STR_LIT>' . format ( self . tableName ) , params ) <EOL> self . cur . connection . commit ( ) <EOL> class MongoDB ( DatabaseBackend ) : <EOL> def __init__ ( self , config ) : <EOL> import pymongo <EOL> DatabaseBackend . __init__ ( self , config ) <EOL> confSection = config [ '<STR_LIT>' ] <EOL> self . client = pymongo . MongoClient ( host = confSection [ '<STR_LIT>' ] , port = config . getint ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . coll = self . client [ confSection [ '<STR_LIT>' ] ] [ confSection [ '<STR_LIT>' ] ] <EOL> def getQueuedCommits ( self ) : <EOL> jsonCommits = self . coll . find ( { '<STR_LIT:status>' : { '<STR_LIT>' : '<STR_LIT>' } } , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:date>' , '<STR_LIT:message>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ] ) . sort ( '<STR_LIT:date>' ) <EOL> commits = [ ] <EOL> for jsonCommit in jsonCommits : <EOL> commits . append ( GitCommit ( jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT:date>' ] , jsonCommit [ '<STR_LIT:message>' ] , jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT>' ] , id = jsonCommit [ '<STR_LIT>' ] , status = jsonCommit [ '<STR_LIT:status>' ] ) ) <EOL> return commits <EOL> def getAllCommits ( self ) : <EOL> jsonCommits = self . coll . find ( { } , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:date>' , '<STR_LIT:message>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ] ) . sort ( '<STR_LIT:date>' ) <EOL> commits = [ ] <EOL> for jsonCommit in jsonCommits : <EOL> commits . append ( GitCommit ( jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT:date>' ] , jsonCommit [ '<STR_LIT:message>' ] , jsonCommit [ '<STR_LIT>' ] , jsonCommit [ '<STR_LIT>' ] , id = jsonCommit [ '<STR_LIT>' ] , status = jsonCommit [ '<STR_LIT:status>' ] ) ) <EOL> return commits <EOL> def insertCommit ( self , commit ) : <EOL> keys = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:date>' , '<STR_LIT:message>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' } <EOL> commitDict = { } <EOL> for key in keys : <EOL> commitDict [ key ] = getattr ( commit , key ) <EOL> self . coll . insert ( commitDict ) <EOL> commit . id = commitDict [ '<STR_LIT>' ] <EOL> def setStatus ( self , commit , status ) : <EOL> commit . status = status <EOL> if hasattr ( commit , '<STR_LIT:id>' ) and not commit . id is None : <EOL> self . coll . update ( { '<STR_LIT>' : commit . id } , { '<STR_LIT>' : { '<STR_LIT:status>' : status } } ) <EOL> def __del__ ( self ) : <EOL> self . client . disconnect ( ) </s>
