Source

vpackager / dbconn.py

Full commit
#!/usr/bin/env python

#    This file is part of vpackager.
#
#    vpackager is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v2 as published by
#    the Free Software Foundation.
#
#    vpackager is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with vpackager.  If not, see <http://www.gnu.org/licenses/>.

import MySQLdb as dbase

db = None

def open_connection():
    global db
    db = dbase.connect(host='localhost', user='root', db='buildbot')
    return 0

def close_connection():
    global db
    db.close()
    return 0
def refresh_todo():
    '''Read the todo list from the database'''
    global db
    open_connection()
    if db.cursor is None: return 1
    cursor = db.cursor()
    cursor.execute("""SELECT * FROM todo ORDER BY id""")
    res = []
    for row in cursor.fetchall():
        res.append(row)
    cursor.close()
    
    close_connection()
    
    return res

def push_data(sql_data):
    global db
    #print sql_data
    open_connection()
    cursor = db.cursor()
    cursor.execute(sql_data)
    db.commit()
    close_connection()
    

def refresh_build_history():
    '''Read the build history from the database'''
    global db
    open_connection()
    if db.cursor is None: return 1
    cursor = db.cursor()
    cursor.execute("""SELECT * FROM en_history ORDER BY id""")
    res = []
    for row in cursor.fetchall():
        res.append(row)
    cursor.close()
    close_connection()
    return res

def log_build_attempt(srcpath, stdoutpath, buildresult):
    global db
    open_connection()
    cursor = db.cursor()
    if db.cursor is None: return 1
    cursor.execute("""INSERT INTO history (srcpath, stdout, result) values (
        '%s','%s','%s')"""% (srcpath, stdoutpath, buildresult))
    cursor.execute("""DELETE FROM todo WHERE srcpath='%s'"""% srcpath)
    db.commit()
    close_connection()
    return 0
    

if __name__ == '__main__':
    print refresh_todo()