diff options
author | sean finey <seanius@debian.org> | 2009-02-11 23:14:07 +0100 |
---|---|---|
committer | sean finey <seanius@debian.org> | 2009-02-11 23:14:07 +0100 |
commit | aa62df8715ce930e473733ab0de0bc10ef6446d6 (patch) | |
tree | d177b9cb77d105e7faf2f289c8bef51b1b2094bd | |
parent | 965623e3f65f95575daa8cdb822cf88317be7a7d (diff) | |
download | patch-tracker-aa62df8715ce930e473733ab0de0bc10ef6446d6.tar.gz |
mod_python -> wsgi
i hear it's the new black. code change requirements weren't
very big at all, and apparently this will work better/stabler
on lenny systems, so...
-rwxr-xr-x | pagehandler.py | 25 | ||||
-rwxr-xr-x | patchtracker/ReqHandler.py | 106 |
2 files changed, 77 insertions, 54 deletions
diff --git a/pagehandler.py b/pagehandler.py index 477f540..f9a293d 100755 --- a/pagehandler.py +++ b/pagehandler.py @@ -1,15 +1,14 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# main (mod_python) request handler for patch-tracking system +# main (wsgi) request handler for patch-tracking system # this handler file basically farms out all its work to the ReqHandler # module, which in turn splits up the request URI and acts accordingly. -from mod_python import apache import os import sys -def handler(req): +def application(env, resp): # the apache config sets a hint of where we are installed, which is # needed in mod_python since cwd is not automatically set try: @@ -19,7 +18,21 @@ def handler(req): pass import patchtracker.ReqHandler as ReqHandler - ReqHandler.Client.req = req + try: + cmdh = ReqHandler.CmdHandler(env) + resp(cmdh.status, cmdh.headers) + return cmdh.output() + except ReqHandler.ReqHandlerException, e: + resp(e.status, [('Content-Type', 'text/html')]) + return ReqHandler.ErrorCmd(str(e), e.status).output() - req.write(ReqHandler.CmdHandler(req).output()) - return apache.OK +if __name__ == '__main__': + from wsgiref import simple_server as ss + print "pagehandler test server running..." + server = ss.WSGIServer( ('',8080), ss.WSGIRequestHandler) + server.set_app(application) + while True: + try: + server.handle_request() + except IOError, e: + print "adsf: ",e diff --git a/patchtracker/ReqHandler.py b/patchtracker/ReqHandler.py index 7f0dbc0..a41114f 100755 --- a/patchtracker/ReqHandler.py +++ b/patchtracker/ReqHandler.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -from mod_python import apache, util - import cgi import os import sys @@ -16,24 +14,28 @@ from pygments.lexers import DiffLexer from pygments.formatters import HtmlFormatter import patchtracker.SourceArchive as SourceArchive -def error(msg, code=apache.HTTP_INTERNAL_SERVER_ERROR): - Client.req.status = code - Client.req.write(str(ErrorTemplate(msg))) - raise apache.SERVER_RETURN, apache.OK +class ReqHandlerException(Exception): + def __init__(self, msg, code="500 Oh noes"): + Exception.__init__(self, msg) + self.status=code + +class Cmd: + def __init__(self): + self.content_type = 'text/html' + self.status = "200 OK" -class Error404Cmd: - def __init__(self, msg): - Client.req.status = apache.HTTP_NOT_FOUND +class ErrorCmd(Cmd): + def __init__(self, msg, code="500 Oh noes"): + Cmd.__init__(self) + self.status = code self.msg = msg def output(self): return str(ErrorTemplate(self.msg)) -class Client: - req = None - -class PatchCmd: +class PatchCmd(Cmd): def __init__(self, args): + Cmd.__init__(self) self.db = PatchTrackerDB() self.patchtype,mode,pkgname,version = args[0:4] self.parsemode(mode) @@ -51,7 +53,7 @@ class PatchCmd: self.patchname = os.sep.join(args[4:]) self.content = dh.filterdiff(include=self.patchname) else: - error("unhandled patch type '%s'"%(self.patchtype)) + raise ReqHandlerException("unhandled patch type '%s'"%(self.patchtype)) self.pkgname = pkgname self.version = version @@ -59,26 +61,28 @@ class PatchCmd: if mode == "view" or mode == "dl": self.mode = mode else: - error("unhandled display mode '%s'"%(mode)) + raise ReqHandlerException("unhandled display mode '%s'"%(mode)) + if mode == "dl": + self.content_type = "text/x-diff" def make_diffhandler(self, pkgname, vers): dfile = self.db.findDiffGz(pkgname,vers) if dfile: return DiffGzHandler(dfile) else: - error("can not find diff file for %s / %s"%(pkgname,vers)) + raise ReqHandlerException("can not find diff file for %s / %s"%(pkgname,vers)) def output(self): if self.mode == "dl": - Client.req.content_type = "text/x-diff" return str(self.content) else: return str(PatchTemplate(pkg=self.pkgname,vers=self.version, patch=self.content,name=self.patchname, patchtype=self.patchtype)) -class PackageCmd: +class PackageCmd(Cmd): def __init__(self, args): + Cmd.__init__(self) db = PatchTrackerDB() self.name = args[0] if len(args) > 1: @@ -90,25 +94,26 @@ class PackageCmd: if len(self.name) >= 3: self.toc = db.findCollection(package="%"+self.name+"%", version=version) - def output(self): plist = self.toc.getletter(self.name) if len(self.name) < 3: - error("search terms must be 3 or more letters...") - if not plist or len(plist) == 0: - error("can't find any package named or containing '%s'"%self.name) + raise ReqHandlerException("search terms must be 3 or more letters...") + elif not plist or len(plist) == 0: + raise ReqHandlerException("can't find any package named or containing '%s'"%self.name, code="404 ENOPKG kthxbye") + + def output(self): + p = self.toc.getpackage(self.name) + # if there is no match, or if multiple versions were returned + if not p or len(set(map(lambda x: x.version, p.values()))) > 1: + querydesc = "package name contains" + return str(SearchResultsTemplate(self.name, querydesc, self.toc)) else: - p = self.toc.getpackage(self.name) - # if there is no match, or if multiple versions were returned - if not p or len(set(map(lambda x: x.version, p.values()))) > 1: - querydesc = "package name contains" - return str(SearchResultsTemplate(self.name, querydesc, self.toc)) - else: - return str(PackageVersTemplate(p.popitem()[1])) - -class IndexCmd: + return str(PackageVersTemplate(p.popitem()[1])) + +class IndexCmd(Cmd): def __init__(self, args): + Cmd.__init__(self) if len(args) < 1 or not len(args[0]): - error("please provide a letter on which to index") + raise ReqHandlerException("please provide a letter on which to index") else: self.db = PatchTrackerDB() self.letter = args[0] @@ -117,10 +122,11 @@ class IndexCmd: def output(self): return str(LetterTocTemplate(self.letter, self.toc)) -class MaintCmd: +class MaintCmd(Cmd): def __init__(self, args): + Cmd.__init__(self) if len(args) < 1 or not len(args[0]): - error("please provide a email address on which to index") + raise ReqHandlerException("please provide a email address on which to index") else: self.db = PatchTrackerDB() self.email = args[0] @@ -129,18 +135,20 @@ class MaintCmd: def output(self): return str(SearchResultsTemplate(self.email, "maintainer email", self.toc)) -class JumpCmd: - def __init__(self): - form = util.FieldStorage(Client.req) +class JumpCmd(Cmd): + def __init__(self, env): + Cmd.__init__(self) + form = cgi.FieldStorage(fp=env['wsgi.input'],environ=env) self.name = form.getfirst("package") + self.uri = "%s/package/%s"%(Conf.root_url, self.name) + self.status = "302 Try this other place kthx" def output(self): - uri = "http://%s%s/package/%s"%(Client.req.hostname, Conf.root_url, self.name) - Client.req.headers_out["Location"] = uri - raise apache.SERVER_RETURN, apache.HTTP_MOVED_TEMPORARILY + return "" -class FrontPageCmd: +class FrontPageCmd(Cmd): def __init__(self): + Cmd.__init__(self) self.db = PatchTrackerDB() self.index = self.db.findIndices() @@ -148,11 +156,9 @@ class FrontPageCmd: return str(FrontPageTemplate(self.index)) class CmdHandler: - def __init__(self, req): - req.content_type = "text/html" - uri=req.uri - if len(uri)<len(Conf.root_url) or uri[0:len(Conf.root_url)]!=Conf.root_url: - error("Invalid URL!") + def __init__(self, env): + self.headers = [] + uri=Conf.root_url+env['PATH_INFO'] args = uri[len(Conf.root_url)+1:].split("/") cmdarg = args[0] @@ -163,13 +169,17 @@ class CmdHandler: elif cmdarg == "index": self.cmd = IndexCmd(args[1:]) elif cmdarg == "jump": - self.cmd = JumpCmd() + self.cmd = JumpCmd(env) + self.headers.append( ('Location', self.cmd.uri) ) elif cmdarg == "email": self.cmd = MaintCmd(args[1:]) elif not len(cmdarg): self.cmd = FrontPageCmd() else: - self.cmd = Error404Cmd("invalid command/location '%s'"%(cmdarg)) + self.cmd = ErrorCmd("invalid command/location '%s'"%(cmdarg), "404 Not found") + + self.headers.append( ('Content-type', self.cmd.content_type) ) + self.status = self.cmd.status def output(self): return self.cmd.output() |