1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/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
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
class CmdHandler:
def __init__(self, uri):
if len(uri)<len(Conf.root_url) or uri[0:len(Conf.root_url)]!=Conf.root_url:
self.error("Invalid URL!")
self.db = PatchTrackerDB()
args = uri[len(Conf.root_url)+1:].split("/")
print "args:",args
self.cmd = args[0]
if self.cmd == "patch":
patchtype,mode,pkgname,version = args[1:5]
self.parsemode(mode)
dh = self.make_diffhandler(pkgname,version)
if patchtype == "series":
self.patchname = args[5]
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()
elif patchtype == "misc":
patchfile = args[5]
self.patchname = "direct changes for "+patchfile
self.content = dh.filterdiff(include=patchfile)
else:
self.error("unhandled patch type '%s'"%(patchtype))
self.pkgname = pkgname
self.version = version
elif self.cmd == "package":
self.db.setFactory(DB.srcpkg_factory)
self.name = args[1]
self.version = args[2]
self.srcpkg = self.db.findSourcePackage(self)
elif self.cmd == "index":
if len(args) < 2 or not len(args[1]):
self.error("please provide a letter on which to index")
else:
self.letter = args[1]
self.toc = self.db.findLetterToc(self.letter)
elif not len(self.cmd):
self.index = self.db.findMasterIndex()
self.cmd = "frontpage"
else:
self.error("invalid command/location %s"%(self.cmd))
# 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.cmd == "patch":
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)
elif self.cmd == "package":
print "Content-Type: text/html\n\n"
print PackageVersTemplate(self.srcpkg)
elif self.cmd == "index":
print "Content-Type: text/html\n\n"
print LetterTocTemplate(self.letter, self.toc)
elif self.cmd == "frontpage":
print "Content-Type: text/html\n\n"
print FrontPageTemplate(self.index)
if __name__ == "__main__":
uri = os.getenv("REQUEST_URI")
if not uri:
uri = sys.argv[1]
CmdHandler(uri).output()
|