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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#!/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.findMasterIndex()
def output(self):
print "Content-Type: text/html\n\n"
print FrontPageTemplate(self.index)
class CmdHandler:
def __init__(self, uri):
if len(uri)<len(Conf.root_url) or uri[0:len(Conf.root_url)]!=Conf.root_url:
error("Invalid URL!")
args = uri[len(Conf.root_url)+1:].split("/")
cmdarg = args[0]
if cmdarg == "patch":
self.cmd = PatchCmd(args[1:])
elif cmdarg == "package":
self.cmd = PackageCmd(args[1:])
elif cmdarg == "index":
self.cmd = IndexCmd(args[1:])
elif cmdarg == "jump":
self.cmd = JumpCmd()
elif not len(cmdarg):
self.cmd = FrontPageCmd()
else:
error("invalid command/location %s"%(cmdarg))
def output(self):
self.cmd.output()
if __name__ == "__main__":
uri = os.getenv("REQUEST_URI")
if not uri:
uri = sys.argv[1]
else:
import cgitb;
cgitb.enable()
CmdHandler(uri).output()
|