db_queue.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 """
00004 usage: %(progname)s [args]
00005 """
00006 
00007 import nstart
00008 import os, sys, string, time, getopt
00009 
00010 from pyclearsilver.log import *
00011 
00012 import config
00013 
00014 from pyclearsilver import odb, hdfhelp, odb_sqlite3
00015 from pyclearsilver import CSPage
00016 
00017 from pyclearsilver.odb import *
00018 
00019 gDBPath = "host"
00020 gDBFilename = "queue"
00021 gDBTablePrefix = "queue"
00022 
00023 class QueueDB(odb.Database):
00024   def __init__(self, conn, debug=0):
00025     odb.Database.__init__(self, conn, debug=debug)
00026 
00027     self.addTable("queue", gDBTablePrefix + "_queue", QueueTable,
00028                   rowClass=CommandRecord)
00029 
00030   def defaultRowClass(self):
00031     return hdfhelp.HdfRow
00032   def defaultRowListClass(self):
00033     return hdfhelp.HdfItemList
00034 
00035 class QueueTable(odb.Table):
00036   def _defineRows(self):
00037     self.d_addColumn("qid",kInteger,None,primarykey = 1,
00038                      autoincrement = 1)
00039 
00040     self.d_addColumn("username",kVarString)
00041     self.d_addColumn("cmd",kVarString, indexed=1)
00042     self.d_addColumn("data",kVarString)
00043     self.d_addColumn("startDate", kInteger, default=0)  
00044     ## when to activate the command
00045 
00046   def getCommands(self, cmd, when=None):
00047     if when:
00048       rows = self.fetchRows(('cmd', cmd), where="startDate <= %s" % when)
00049     else:
00050       rows = self.fetchRows(('cmd', cmd))
00051     return rows
00052 
00053   def newCommand(self, username, cmd, startDate, data=""):
00054     row = self.newRow()
00055     row.username = username
00056     row.startDate = startDate
00057     row.cmd = cmd
00058     row.data = data
00059     row.save()
00060 
00061     return row
00062     
00063 class CommandRecord(odb.Row):
00064   pass
00065     
00066 
00067 def fullDBPath(path_to_store):
00068   return os.path.join(path_to_store, gDBFilename + ".db3")
00069 
00070 def initSchema(create=0, timeout=None):
00071   if timeout is None: timeout = 600
00072 
00073   path = config.getSiteDBPath("host")
00074 
00075   if create == 1:
00076     config.createDBPath(path)
00077 
00078   conn = odb_sqlite3.Connection(fullDBPath(path),
00079                               autocommit=0,
00080                               timeout=timeout)
00081 
00082   db = QueueDB(conn,debug=debug)
00083 
00084   if create:
00085     db.createTables()
00086     db.synchronizeSchema()
00087     db.createIndices()
00088 
00089     if config.gWebUserID is not None and config.gWebGroupID is not None:
00090       config.webChown(fullDBPath(path))
00091 
00092   return db
00093 
00094 def exists():
00095   path = config.getSiteDBPath("host")
00096   fn = fullDBPath(path)
00097   if os.path.exists(fn): 
00098     return 1
00099   return 0
00100   
00101 
00102 def createDB():
00103   db = initSchema(create=1)
00104   return db
00105   
00106   
00107 def test():
00108   db = initSchema()
00109 
00110   rows = db.queue.fetchAllRows()
00111   for row in rows:
00112     print row.username, row.cmd, row.data
00113 
00114 
00115 def usage(progname):
00116   print __doc__ % vars()
00117 
00118 def main(argv, stdout, environ):
00119   progname = argv[0]
00120   optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00121 
00122   testflag = 0
00123   for (field, val) in optlist:
00124     if field == "--help":
00125       usage(progname)
00126       return
00127     elif field == "--debug":
00128       debugfull()
00129     elif field == "--test":
00130       testflag = 1
00131 
00132   if testflag:
00133     test()
00134     return
00135 
00136   db = initSchema(create=1)
00137 
00138 
00139 
00140 if __name__ == "__main__":
00141   main(sys.argv, sys.stdout, os.environ)
00142 
00143   
00144   


webui
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:36:00