#!/usr/bin/python # -*- coding: utf-8 -*- import cgi import os import sys import patchtracker.Conf as Conf from patchtracker.Templates import ErrorTemplate, PatchTemplate, PackageVersTemplate, LetterTocTemplate, FrontPageTemplate, SearchResultsTemplate from patchtracker.DiffGzHandler import DiffGzHandler import patchtracker.DB as DB from patchtracker.DB import PatchTrackerDB import pygments from pygments.lexers import DiffLexer from pygments.formatters import HtmlFormatter import patchtracker.SourceArchive as SourceArchive def error(msg): print "Content-Type: text/html\n\n" print ErrorTemplate(msg) sys.exit(1) class PatchCmd: def __init__(self, args): self.patchtype,mode,pkgname,version = args[0:4] self.parsemode(mode) dh = self.make_diffhandler(pkgname,version) if self.patchtype == "series": self.patchname = os.sep.join(args[4:]) self.content = dh.series().fetch(self.patchname) elif self.patchtype == "debianonly": self.patchname = "debian-dir only changes" self.content = dh.debiandir() elif self.patchtype == "nondebian": self.patchname = "direct (non packaging) changes" self.content = dh.nondebiandir() elif self.patchtype == "misc": self.patchname = os.sep.join(args[4:]) self.content = dh.filterdiff(include=self.patchname) else: error("unhandled patch type '%s'"%(self.patchtype)) self.pkgname = pkgname self.version = version def parsemode(self, mode): if mode == "view" or mode == "dl": self.mode = mode else: error("unhandled display mode '%s'"%(mode)) # 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 if vers.find(":") >= 0: diffvers = ":".join(vers.split(":")[1:]) else: diffvers = vers dfile = pkgname+"_"+diffvers+".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: error("can not find diff file for %s / %s"%(pkgname,diffvers)) 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, patchtype=self.patchtype) class PackageCmd: def __init__(self, args): db = PatchTrackerDB() self.name = args[0] if len(args) > 1: version = args[1] else: version = None self.toc = db.findCollection(package=self.name, version=version) if self.toc.size() == 0: self.toc = db.findCollection(package="%"+self.name+"%", version=version) def output(self): plist = self.toc.getletter(self.name) if not plist or len(plist) == 0: error("can't find any package named or containing '%s'"%self.name) else: p = self.toc.getpackage(self.name) print "Content-Type: text/html\n\n" # 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: print SearchResultsTemplate(self.name, self.toc) else: print PackageVersTemplate(p.popitem()[1]) class IndexCmd: def __init__(self, args): if len(args) < 1 or not len(args[0]): error("please provide a letter on which to index") else: self.db = PatchTrackerDB() self.letter = args[0] self.toc = self.db.findLetterToc(self.letter) def output(self): print "Content-Type: text/html\n\n" print LetterTocTemplate(self.letter, self.toc) class JumpCmd: def __init__(self): form = cgi.FieldStorage() self.name = form.getfirst("package") def output(self): print "Location: http://%s%s/package/%s\n\n"%( os.getenv("HTTP_HOST"), Conf.root_url, self.name) class FrontPageCmd: def __init__(self): self.db = PatchTrackerDB() self.index = self.db.findIndices() def output(self): print "Content-Type: text/html\n\n" print FrontPageTemplate(self.index) class CmdHandler: def __init__(self, uri): if len(uri)