Source

vpackager / vpackager / dbutils.py

#!/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 sqlite3

__author__ = 'M0E-lnx'
__author_email__ = 'moc.liamg@xnl.E0M'[::-1]
__version__ = '0.1'

db = sqlite3.connect('/tmp/vpackager.db')

def __create_queue_table():
    c = db.cursor()
    c.execute("""create table queue
    (id integer primary key autoincrement,
    app text, ver text, srcURI text, descURI text, release text)""")
    db.commit()
    c.close()

    return

def __create_history_table():
    c = db.cursor()
    # The id integer comes from the job queue table, so we can have some
    # consistancy here.
    c.execute("""
    create table history
    (id integer primary key,
    app text,
    ver text,
    srcURI text,
    descURI text,
    buildstart text,
    buildend text,
    stdout text,
    result text)"""
    )

    db.commit()
    c.close()

    return

def NewJob(app, ver, srcURI, descURI, release):
    t = (app, ver, srcURI, descURI, release)
    c = db.cursor()
    c.execute("insert into queue (app, ver, srcURI, descURI, release) values (?,?,?,?,?)", t)

    db.commit()
    c.close()

def RemoveJob(_id):
    c = db.cursor()
    t = (_id,)
    c.execute('delete from queue where id=?', t)
    db.commit()
    c.close()

def LogJobStart(jobid, app, ver, src, desc, starttime):
    t = (jobid, app, ver, src, desc, starttime)
    c = db.cursor()
    c.execute('''insert into history (id, app, ver, srcURI, descURI,
    buildstart) values (?,?,?,?,?,?)''', t)

    db.commit()
    c.close()


def GetQueue():
    c = db.cursor()
    ret = c.execute('select * from queue').fetchall()
    c.close()
    return ret

def GetHistory():
    c = db.cursor()
    ret = c.execute('select * from history').fetchall()
    c.close()
    return ret