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'

dbpath = '/tmp/vpackager.db'

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)""")
    db.commit()
    c.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,
    buildstart text,
    buildend text,
    stdout text,
    result text)"""
    )

    db.commit()
    c.close()

    return

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

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

    db.commit()
    c.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()

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()

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()

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

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

def GetHistory():
    """ Return a list of all processed jobs"""
    db = sqlite3.connect(dbpath)
    c = db.cursor()
    ret = c.execute('select * from history').fetchall()
    c.close()
    return ret
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.