db_queue.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [args]
5 """
6 
7 import nstart
8 import os, sys, string, time, getopt
9 
10 from pyclearsilver.log import *
11 
12 import config
13 
14 from pyclearsilver import odb, hdfhelp, odb_sqlite3
15 from pyclearsilver import CSPage
16 
17 from pyclearsilver.odb import *
18 
19 gDBPath = "host"
20 gDBFilename = "queue"
21 gDBTablePrefix = "queue"
22 
23 class QueueDB(odb.Database):
24  def __init__(self, conn, debug=0):
25  odb.Database.__init__(self, conn, debug=debug)
26 
27  self.addTable("queue", gDBTablePrefix + "_queue", QueueTable,
28  rowClass=CommandRecord)
29 
30  def defaultRowClass(self):
31  return hdfhelp.HdfRow
33  return hdfhelp.HdfItemList
34 
35 class QueueTable(odb.Table):
36  def _defineRows(self):
37  self.d_addColumn("qid",kInteger,None,primarykey = 1,
38  autoincrement = 1)
39 
40  self.d_addColumn("username",kVarString)
41  self.d_addColumn("cmd",kVarString, indexed=1)
42  self.d_addColumn("data",kVarString)
43  self.d_addColumn("startDate", kInteger, default=0)
44  ## when to activate the command
45 
46  def getCommands(self, cmd, when=None):
47  if when:
48  rows = self.fetchRows(('cmd', cmd), where="startDate <= %s" % when)
49  else:
50  rows = self.fetchRows(('cmd', cmd))
51  return rows
52 
53  def newCommand(self, username, cmd, startDate, data=""):
54  row = self.newRow()
55  row.username = username
56  row.startDate = startDate
57  row.cmd = cmd
58  row.data = data
59  row.save()
60 
61  return row
62 
63 class CommandRecord(odb.Row):
64  pass
65 
66 
67 def fullDBPath(path_to_store):
68  return os.path.join(path_to_store, gDBFilename + ".db3")
69 
70 def initSchema(create=0, timeout=None):
71  if timeout is None: timeout = 600
72 
73  path = config.getSiteDBPath("host")
74 
75  if create == 1:
76  config.createDBPath(path)
77 
78  conn = odb_sqlite3.Connection(fullDBPath(path),
79  autocommit=0,
80  timeout=timeout)
81 
82  db = QueueDB(conn,debug=debug)
83 
84  if create:
85  db.createTables()
86  db.synchronizeSchema()
87  db.createIndices()
88 
89  if config.gWebUserID is not None and config.gWebGroupID is not None:
90  config.webChown(fullDBPath(path))
91 
92  return db
93 
94 def exists():
95  path = config.getSiteDBPath("host")
96  fn = fullDBPath(path)
97  if os.path.exists(fn):
98  return 1
99  return 0
100 
101 
102 def createDB():
103  db = initSchema(create=1)
104  return db
105 
106 
107 def test():
108  db = initSchema()
109 
110  rows = db.queue.fetchAllRows()
111  for row in rows:
112  print row.username, row.cmd, row.data
113 
114 
115 def usage(progname):
116  print __doc__ % vars()
117 
118 def main(argv, stdout, environ):
119  progname = argv[0]
120  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
121 
122  testflag = 0
123  for (field, val) in optlist:
124  if field == "--help":
125  usage(progname)
126  return
127  elif field == "--debug":
128  debugfull()
129  elif field == "--test":
130  testflag = 1
131 
132  if testflag:
133  test()
134  return
135 
136  db = initSchema(create=1)
137 
138 
139 
140 if __name__ == "__main__":
141  main(sys.argv, sys.stdout, os.environ)
142 
143 
144 
def fullDBPath(path_to_store)
Definition: db_queue.py:67
def defaultRowListClass(self)
Definition: db_queue.py:32
def initSchema(create=0, timeout=None)
Definition: db_queue.py:70
def __init__(self, conn, debug=0)
Definition: db_queue.py:24
def getCommands(self, cmd, when=None)
Definition: db_queue.py:46
def usage(progname)
Definition: db_queue.py:115
def main(argv, stdout, environ)
Definition: db_queue.py:118
def newCommand(self, username, cmd, startDate, data="")
Definition: db_queue.py:53


webui
Author(s): Scott Hassan
autogenerated on Mon Jun 10 2019 15:51:24