vpackager / src / 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
import os

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

dbpath = '/tmp/vpackager.db'

def __verify_tables():
    ''' Verify that the essential tables for the vpackager environment
    exist'''
    #if os.path.exists(dbpath):
        #        os.remove(dbpath)
    #return True
    # FIXME: ^^^ Remove this before final release ^^

    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute('select name from sqlite_master where type="table"')
    res = c.fetchall()
    c.close()
    db.close()
    if len(res) > 0:
        return False
    return True

def create_vpackager_db():
    ''' Create the essential tables for the vpackager environment'''
    if __verify_tables() is True:
        __create_queue_table()
        __create_history_table()
        return
    return

def __create_queue_table():
    """ Create the job queue table. NEVER to be called from the program
    itself!"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute("""create table queue
    (id integer primary key autoincrement,
    app text, ver text, srcURI text, descURI text, release text, type text,
    install text, patches text)""")
    db.commit()
    c.close()
    db.close()

    return

def __create_history_table():
    """ Create the job history table. NEVER to be called from the program
    itself!"""
    db = sqlite3.connect(dbpath)
    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,
    release text,
    type text,
    patches text,
    buildstart text,
    buildend text,
    stdout text,
    result text)"""
    )

    db.commit()
    c.close()
    db.close()

    return

def NewJob(app, ver, srcURI, descURI, release, _type, install,
        patches):

    """ Add a job to the job queue"""
    db = sqlite3.connect(dbpath)
    t = (app, ver, srcURI, descURI, release, _type, install, patches)
    c = db.cursor()
    c.execute("""insert into queue (app, ver, srcURI, descURI, release, Type,
    install, patches) values (?,?,?,?,?,?,?,?)""", t)

    db.commit()
    c.close()
    db.close()

def RemoveJob(_id):
    """ Delete a job from the job queue"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    t = (_id,)
    c.execute('delete from queue where id=?', t)
    db.commit()
    c.close()
    db.close()

def LogJobStart(jobid):
    """ Move job from queue to history and add the timestamp to the buidl
    start"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    t = (jobid,)
    c.execute('''select id,app,ver,srcURI,descURI,release,type from queue where
    id=?''',t)
    res = c.next()
    c.execute('''insert into history (id,app,ver,srcURI,descURI,
    release,type,buildstart,result)
    values (?,?,?,?,?,?,?,CURRENT_TIMESTAMP,'Running')''',res)
    t = (jobid,)
    c.execute('delete from queue where id=?',t)
    c.close()
    db.commit()
    db.close()

def LogJobEnd(jobid, stdoutpath, result):
    """ Update the job history to log the end time and end result of the
    build."""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    t = (jobid,)
    c.execute('''update history set buildend=CURRENT_TIMESTAMP where
            id=?''',t)
    t = (stdoutpath, jobid)
    c.execute('''update history set stdout=? where id=?''',t)
    t = (result, jobid)
    c.execute('''update history set result=? where id=?''',t)
    c.close()
    db.commit()
    db.close()

def GetJobPatches(jobID):
    t = (jobID,)
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute('select patches from queue where id=?',t)
    try:
        ret = c.next()
    except StopIteration:
        ret = None
    c.close()
    db.close()
    return ret

def GetJobProp(jobid, prop):
    ''' Returns jobid's propperty (prop) '''
    t = (prop,jobid)
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute('select %s from queue where id=%s'%(prop, jobid))
    try:
        ret = c.next()
    except StopIteration:
        ret = None
    c.close()
    db.close()
    if ret:
        return ret[0]
    return None

def GetTaskProp(taskid, prop):
    """ Returns Taskid's property (prop) """
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute('select %s from history where id=%s'% (prop,taskid))
    try:
        ret = c.next()
    except StopIteration:
        ret = None
    c.close()
    if ret:
        return ret[0]
    return None

def GetTaskDetails(jobid):
    """ Returns all information from the job history about jobid"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    t = (jobid,)
    c.execute('select * from history where id=?',t)
    try:
        ret = c.next()
    except StopIteration:
        ret = None
    c.close()
    db.close()
    return ret

def GetNextJob():
    """ Find the next job in line to be built"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    c.execute('select id from queue')
    try:
        ret = c.next()
    except StopIteration:
        ret = None
    c.close()
    db.close()
    return ret

def GetQueue():
    """ Return a list of all pending jobs"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    ret = c.execute('select id,app,ver,release from queue').fetchall()
    #ret = c.execute('select * from queue').fetchall()
    c.close()
    db.close()
    return ret

def GetHistory():
    """ Return a list of all processed jobs"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    ret = c.execute('\
            select id,app,ver,buildstart,buildend,result \
            from history').fetchall()
    #ret = c.execute('select * from history').fetchall()
    c.close()
    db.close()
    return ret

def WipeHistory():
    """ Delete the entire build history """
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    ret = c.execute('delete from history where id > 0')
    c.close()
    db.close()
    return
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.