#!/usr/bin/python # -*- coding: utf-8 -*- import cgi import os import sys import patchtracker.Conf as Conf from patchtracker.Writers import ErrorTemplate, PatchTemplate from patchtracker.DiffGzHandler import DiffGzHandler import pygments from pygments.lexers import DiffLexer from pygments.formatters import HtmlFormatter class CmdHandler: def __init__(self, req_uri): args = req_uri.split("/") # XXX this assumes http://site/foo/patch/adsfadf if args[2] == "patch": patchtype,mode,pkgname,version = args[3:7] self.parsemode(mode) dh = self.make_diffhandler(pkgname,version) if patchtype == "series": self.patchname = args[7] self.content = dh.series().fetch(self.patchname) elif patchtype == "debianonly": self.patchname = "debian-dir only changes" self.content = dh.debiandir() elif patchtype == "nondebian": self.patchname = "direct (non packaging) changes" self.content = dh.nondebiandir() else: self.error("unhandled patch type '%s'"%(patchtype)) self.pkgname = pkgname self.version = version else: self.error("unhandled command: '%s'"%(args[2])) # XXX this assumes to much hard coded, but until there's a faster # XXX (i.e. database) way to query sourcepackage/diffs it will # XXX have to do... def make_diffhandler(self, pkgname, vers): file = None dfile = pkgname+"_"+vers+".diff.gz" for comp in ['main', 'contrib', 'non-free']: loc = os.sep.join([Conf.archive_root, 'pool', comp, pkgname[0], pkgname]) try: test = os.sep.join([loc, dfile]) os.stat(test) file = test break except: pass if file: return DiffGzHandler(file) else: self.error("can not find diff file for %s / %s"%(pkgname,vers)) def parsemode(self, mode): if mode == "view" or mode == "dl": self.mode = mode else: self.error("unhandled display mode '%s'"%(mode)) def error(self, msg): print "Content-Type: text/html\n\n" print ErrorTemplate(msg) sys.exit(1) def output(self): if self.mode == "dl": print "Content-Type: text/x-diff\n\n" print self.content else: print "Content-Type: text/html\n\n" print PatchTemplate(pkg=self.pkgname,vers=self.version,patch=self.content,name=self.patchname) if __name__ == "__main__": uri = os.getenv("REQUEST_URI") CmdHandler(uri).output()