3 import os, sys, string, time, string
6 import roslib; roslib.load_manifest(
'webui')
8 from pyclearsilver
import httpResponses
17 warnings.resetwarnings()
18 warnings.filterwarnings(
"ignore")
22 import neo_cgi, neo_cs, neo_util
24 from pyclearsilver
import CSPage
27 mimetypes.init([
"/etc/mime.types"])
35 if not hasattr(gConfig,
"gRequireUsername"): gConfig.gRequireUsername = 0
36 if not hasattr(gConfig,
"gDataFilePaths"): gConfig.gDataFilePaths = []
41 return string.split(path,
'/')[1:]
44 cmd = [
"rospack",
"find", pkg]
45 pkgpath = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()
56 self.
path = self.context.environ.get(
"PATH_INFO",
'')
58 script_name = self.context.environ.get(
"SCRIPT_NAME",
'')
62 self.
path = script_name
72 if not rpath: rpath =
'/' 74 if rpath ==
"/": rpath = gConfig.gDefaultPage
80 if len(self.
path) == 0:
81 warn(
"no such path", self.
path)
92 module = gConfig.gDefaultModule
94 if hasattr(gConfig,
"gDataFilePaths"):
95 if self.
path[0]
in gConfig.gDataFilePaths:
96 fn = apply(os.path.join, [self.
cwd,] + self.
path)
99 if gConfig.gRequireUsername:
100 username = self.
path[0]
107 if len(self.
path) > 1:
108 module = self.
path[n]
115 module, app_id = self.
path[n:n+2]
116 taskid = string.join([module, app_id],
"/")
121 fn = apply(os.path.join, [modpath] + [module] + self.
path[n:])
122 moduleRootPath = modpath
123 handlerRoot = apply(os.path.join, [modpath, module,
"cgibin"])
124 moduleTemplatePath = apply(os.path.join, [modpath, module,
"templates"])
127 moduleRootPath = apply(os.path.join, [self.
cwd,
"mod", module])
128 handlerRoot = apply(os.path.join, [self.
cwd,
"mod", module,
"cgibin"])
129 moduleTemplatePath = apply(os.path.join, [self.
cwd,
"mod", module,
"templates"])
131 fn = apply(os.path.join, [self.
cwd, moduleRootPath,] + self.
path[n:])
133 systemTemplatePath = apply(os.path.join, [self.
cwd,
"mod",
"webui",
"templates"])
134 systemJLIBPath = apply(os.path.join, [self.
cwd,
"mod",
"webui",
"jslib"])
139 if os.path.isfile(fn):
143 sys.path.insert(0, os.path.abspath(self.
cwd))
144 if modpath: sys.path.insert(0, os.path.abspath(modpath))
145 sys.path.insert(0, os.path.abspath(moduleRootPath))
154 for m
in range(len(self.
path)-1, n-2, -1):
155 handlerPath = apply(os.path.join, [handlerRoot, ] + self.
path[n:m+1])
157 if os.path.isdir(handlerPath):
158 sys.path.insert(0, handlerPath)
159 if os.path.isfile(handlerPath):
break 160 if os.path.isdir(handlerPath):
break 162 if m+1 == len(self.
path):
165 pathinfo = apply(os.path.join, self.
path[m+1:])
167 if os.path.isdir(handlerPath):
168 modulePath = handlerPath
169 moduleFilename = app_id +
"_index.py" 170 handlerPath = os.path.join(self.
cwd, modulePath, moduleFilename)
172 modulePath, moduleFilename = os.path.split(handlerPath)
174 if not os.path.isfile(handlerPath):
175 self.
error(404, handlerPath +
" doesn't exist2")
180 moduleName, ext = os.path.splitext(moduleFilename)
184 module = __import__(
"%s.cgibin.%s" % (module, moduleName, ), {}, {}, (
None,))
186 module = __import__(
"mod.%s.cgibin.%s" % (module, moduleName), {}, {}, (
None,))
188 page = module.run(self.
context)
190 proxy_path = page.ncgi.hdf.getValue(
"HTTP.Soap.Action",
"")
191 if proxy_path
and not gConfig.gBaseURL.startswith(proxy_path):
192 gConfig.gBaseURL = proxy_path + gConfig.gBaseURL
193 gConfig.gROSURL = proxy_path + gConfig.gROSURL
195 page.ncgi.hdf.setValue(
"CGI.BaseURI", gConfig.gBaseURL)
197 page.ncgi.hdf.setValue(
"CGI.taskid", taskid)
199 if gConfig.gRequireUsername:
200 page.ncgi.hdf.setValue(
"CGI.Username", username)
201 page.username = username
203 if hasattr(page,
"checkLoginCookie"):
204 nologin = page._pageparms.get(
"nologin",
False)
208 page.checkLoginCookie()
210 except CSPage.Redirected:
213 page.ncgi.hdf.setValue(
"CGI.PathInfo", pathinfo)
215 page.setPaths([moduleTemplatePath, systemTemplatePath, systemJLIBPath])
223 except AttributeError:
pass 229 def error(self, ecode, reason=None):
230 message = httpResponses.gHTTPResponses[ecode]
232 template = httpResponses.errorMessage_Default
234 template = httpResponses.errorMessage_404
237 hdf.setValue(
"code", str(ecode))
238 if message: hdf.setValue(
"message", message)
239 if reason: hdf.setValue(
"reason", reason)
241 for key,val
in self.context.environ.items():
242 hdf.setValue(
"environ." + key, str(val))
244 self.context.stdout.write(
"Content-Type: text/html\r\n")
245 self.context.setStatus(
None, ecode)
246 self.context.stdout.write(
"Status: %s\r\n" % ecode)
247 self.context.stdout.write(
"\r\n")
250 cs.parseStr(template)
253 self.context.stdout.write(page)
255 warn(
"Error", message, reason)
263 context.setStatus(
None, 200)
265 imagetype, encoding = mimetypes.guess_type(fn)
266 debug(
"imagetype = %s fn = %s" % (imagetype,fn))
271 lines.append(
"Content-Type: %s" % imagetype)
273 lines.append(
"Content-Length: %d" % len(data))
276 mtime = stat.st_mtime
277 mod_str = time.strftime(
"%a, %d %b %Y %H:%M:%S", time.gmtime(mtime))
279 lines.append(
'Last-Modified: %s GMT' % mod_str)
281 expire_time = time.gmtime(time.time() + (360*24*3600))
282 expire_str = time.strftime(
"%a, %d %b %Y %H:%M:%S", expire_time)
283 lines.append(
'Expires: %s GMT' % expire_str)
287 headers = string.join(lines,
"\r\n")
288 context.stdout.write(headers)
290 context.stdout.write(data)
299 from mod_python
import apache
300 self.req.log_error(s, apache.APLOG_WARNING)
305 from mod_python
import apache
312 env = apache.build_cgi_env(req)
316 scriptFilename = self.environ.get(
"SCRIPT_FILENAME",
"")
318 path, fn = os.path.split(scriptFilename)
323 request[
'status'] = str(status)
329 from mod_python
import apache
335 page.mod_python_req = req
346 def main(argv, stdout, environ):
347 context = CSPage.Context()
352 if __name__ ==
"__main__":
353 main(sys.argv, sys.stdout, os.environ)
def error(self, ecode, reason=None)
path
handle the case where the site is located at '/'
def setStatus(self, request, status)
def main(argv, stdout, environ)
def __init__(self, context)
def outputFile(context, fn)