00001
00002
00003 """
00004 usage: %(progname)s [args]
00005
00006 """
00007
00008 import os, sys, string, time, getopt
00009
00010 import roslib
00011 roslib.load_manifest("webui")
00012 from pyclearsilver.log import *
00013
00014 from webui import config
00015
00016 from pyclearsilver import odb, hdfhelp, odb_sqlite3
00017 from pyclearsilver import CSPage
00018
00019 from pyclearsilver.odb import *
00020
00021 from pyclearsilver import odb
00022 from pyclearsilver import hdfhelp
00023
00024 from launchman import app
00025
00026 def path2taskid(path):
00027 p = []
00028 package = None
00029
00030 while path != "/":
00031 base, fn = os.path.split(path)
00032
00033 p.insert(0, fn)
00034 if os.path.exists(os.path.join(base, "manifest.xml")):
00035 base, package = os.path.split(base)
00036 break
00037 path = base
00038 if path == "/":
00039 return None
00040
00041 fn = apply(os.path.join, p)
00042
00043 return os.path.join(package, fn).replace(".app", "")
00044
00045 class WebUIDB(odb.Database):
00046 def __init__(self,conn,debug=0):
00047 odb.Database.__init__(self, conn, debug=debug)
00048
00049 self.addTable("robots", "ir_robots", RobotTable, rowClass=Robot)
00050 self.addTable("apps", "ir_apps", ApplicationTable, rowClass=Application)
00051 self.addTable("appgroups", "ir_app_groups", AppGroupTable, rowClass=AppGroup)
00052
00053 def defaultRowClass(self):
00054 return hdfhelp.HdfRow
00055 def defaultRowListClass(self):
00056 return hdfhelp.HdfItemList
00057
00058
00059
00060
00061 class RobotTable(odb.Table):
00062 def _defineRows(self):
00063 self.d_addColumn("robotid", kInteger,primarykey=1, unique=1,
00064 autoincrement=1)
00065 self.d_addColumn("name", kVarString)
00066
00067 class Robot(hdfhelp.HdfRow):
00068 pass
00069
00070 class ApplicationTable(odb.Table):
00071 def _defineRows(self):
00072 self.d_addColumn("appid", kInteger,primarykey=1, unique=1,
00073 autoincrement=1)
00074 self.d_addColumn("taskid", kVarString, unique=1)
00075
00076 def installAppWithPath(self, appfn):
00077 appfn = os.path.abspath(appfn)
00078
00079 taskid = path2taskid(appfn)
00080 return self.installApp(taskid)
00081
00082 def installApp(self, taskid):
00083 row = self.newRow()
00084 row.taskid = taskid
00085 row.save()
00086 return row
00087
00088 def removeApp(self, taskid):
00089 row = self.lookup(taskid=taskid)
00090 row.delete()
00091
00092 def listApps(self):
00093 rows = self.fetchAllRows()
00094 return rows
00095
00096 class Application(hdfhelp.HdfRow):
00097 def fetchApp(self, prefix, hdf):
00098 _app = app.App(str(self.taskid))
00099 doc = _app.load_yaml()
00100
00101 hdf.setValue(prefix + "." + "package", _app.package)
00102
00103 for key, val in doc.items():
00104 if val is not None:
00105 hdf.setValue(prefix + "." + key, val)
00106 else:
00107 hdf.setValue(prefix + "." + key, '')
00108
00109
00110 class AppGroupTable(odb.Table):
00111 def _defineRows(self):
00112 self.d_addColumn("id", kVarString, primarykey=1, autoincrement=1)
00113 self.d_addColumn("userid", kInteger)
00114 self.d_addColumn("appids", kVarString)
00115 self.d_addColumn("category", kVarString)
00116
00117 class AppGroup(hdfhelp.HdfRow):
00118 def appIdList(self):
00119 return self.appids.split(',')
00120
00121
00122
00123
00124
00125 def fullDBPath(path_to_store):
00126 return os.path.join(path_to_store, "webui.db3")
00127
00128 def initSchema(create=0, timeout=None):
00129 if timeout is None: timeout = 600
00130
00131 path = config.getDBPath("webui")
00132
00133 if create == 1:
00134 config.createDBPath(path)
00135
00136 conn = odb_sqlite3.Connection(fullDBPath(path),
00137 timeout=timeout)
00138
00139 db = WebUIDB(conn,debug=debug)
00140
00141 if create:
00142 db.createTables()
00143 db.synchronizeSchema()
00144 db.createIndices()
00145
00146 if 0:
00147 if config.gWebUserID is not None and config.gWebGroupID is not None:
00148 config.webChown(fullDBPath(path))
00149
00150
00151 return db
00152
00153 def exists():
00154 path = config.getDBPath("webui")
00155 fn = fullDBPath(path)
00156 if os.path.exists(fn):
00157 return 1
00158 return 0
00159
00160
00161 def test():
00162 pass
00163
00164 def usage(progname):
00165 print __doc__ % vars()
00166
00167 def main(argv, stdout, environ):
00168 progname = argv[0]
00169 optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00170
00171 testflag = 0
00172
00173 for (field, val) in optlist:
00174 if field == "--help":
00175 usage(progname)
00176 return
00177 elif field == "--debug":
00178 debugfull()
00179 elif field == "--test":
00180 testflag = 1
00181
00182 if testflag:
00183 test()
00184 return
00185
00186 db = initSchema(create=1)
00187
00188
00189
00190 if __name__ == "__main__":
00191 main(sys.argv, sys.stdout, os.environ)
00192
00193
00194