##// END OF EJS Templates
hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman -
r5965:abe373e1 default
parent child Browse files
Show More
@@ -1,275 +1,275 b''
1 1 # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories.
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 import os
10 10 from mercurial.i18n import gettext as _
11 11 from mercurial import ui, hg, util, templater
12 12 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen, \
13 13 get_contact
14 14 from hgweb_mod import hgweb
15 15 from request import wsgirequest
16 16
17 17 # This is a stopgap
18 18 class hgwebdir(object):
19 19 def __init__(self, config, parentui=None):
20 20 def cleannames(items):
21 21 return [(util.pconvert(name).strip('/'), path)
22 22 for name, path in items]
23 23
24 24 self.parentui = parentui or ui.ui(report_untrusted=False,
25 25 interactive = False)
26 26 self.motd = None
27 27 self.style = None
28 28 self.stripecount = None
29 29 self.repos_sorted = ('name', False)
30 30 if isinstance(config, (list, tuple)):
31 31 self.repos = cleannames(config)
32 32 self.repos_sorted = ('', False)
33 33 elif isinstance(config, dict):
34 34 self.repos = cleannames(config.items())
35 35 self.repos.sort()
36 36 else:
37 37 if isinstance(config, util.configparser):
38 38 cp = config
39 39 else:
40 40 cp = util.configparser()
41 41 cp.read(config)
42 42 self.repos = []
43 43 if cp.has_section('web'):
44 44 if cp.has_option('web', 'motd'):
45 45 self.motd = cp.get('web', 'motd')
46 46 if cp.has_option('web', 'style'):
47 47 self.style = cp.get('web', 'style')
48 48 if cp.has_option('web', 'stripes'):
49 49 self.stripecount = int(cp.get('web', 'stripes'))
50 50 if cp.has_section('paths'):
51 51 self.repos.extend(cleannames(cp.items('paths')))
52 52 if cp.has_section('collections'):
53 53 for prefix, root in cp.items('collections'):
54 54 for path in util.walkrepos(root):
55 55 repo = os.path.normpath(path)
56 56 name = repo
57 57 if name.startswith(prefix):
58 58 name = name[len(prefix):]
59 59 self.repos.append((name.lstrip(os.sep), repo))
60 60 self.repos.sort()
61 61
62 62 def run(self):
63 63 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
64 64 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
65 65 import mercurial.hgweb.wsgicgi as wsgicgi
66 66 wsgicgi.launch(self)
67 67
68 68 def __call__(self, env, respond):
69 69 req = wsgirequest(env, respond)
70 70 self.run_wsgi(req)
71 71 return req
72 72
73 73 def run_wsgi(self, req):
74 74
75 75 try:
76 76 try:
77 77
78 78 virtual = req.env.get("PATH_INFO", "").strip('/')
79 79
80 80 # a static file
81 81 if virtual.startswith('static/') or 'static' in req.form:
82 82 static = os.path.join(templater.templatepath(), 'static')
83 83 if virtual.startswith('static/'):
84 84 fname = virtual[7:]
85 85 else:
86 86 fname = req.form['static'][0]
87 87 req.write(staticfile(static, fname, req))
88 88 return
89 89
90 90 # top-level index
91 91 elif not virtual:
92 92 tmpl = self.templater(req)
93 self.makeindex(req, tmpl)
93 req.write(self.makeindex(req, tmpl))
94 94 return
95 95
96 96 # nested indexes and hgwebs
97 97 repos = dict(self.repos)
98 98 while virtual:
99 99 real = repos.get(virtual)
100 100 if real:
101 101 req.env['REPO_NAME'] = virtual
102 102 try:
103 103 repo = hg.repository(self.parentui, real)
104 104 hgweb(repo).run_wsgi(req)
105 105 return
106 106 except IOError, inst:
107 107 raise ErrorResponse(500, inst.strerror)
108 108 except hg.RepoError, inst:
109 109 raise ErrorResponse(500, str(inst))
110 110
111 111 # browse subdirectories
112 112 subdir = virtual + '/'
113 113 if [r for r in repos if r.startswith(subdir)]:
114 114 tmpl = self.templater(req)
115 self.makeindex(req, tmpl, subdir)
115 req.write(self.makeindex(req, tmpl, subdir))
116 116 return
117 117
118 118 up = virtual.rfind('/')
119 119 if up < 0:
120 120 break
121 121 virtual = virtual[:up]
122 122
123 123 # prefixes not found
124 124 tmpl = self.templater(req)
125 125 req.respond(404, tmpl("notfound", repo=virtual))
126 126
127 127 except ErrorResponse, err:
128 128 tmpl = self.templater(req)
129 129 req.respond(err.code, tmpl('error', error=err.message or ''))
130 130 finally:
131 131 tmpl = None
132 132
133 133 def makeindex(self, req, tmpl, subdir=""):
134 134
135 135 def archivelist(ui, nodeid, url):
136 136 allowed = ui.configlist("web", "allow_archive", untrusted=True)
137 137 for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
138 138 if i[0] in allowed or ui.configbool("web", "allow" + i[0],
139 139 untrusted=True):
140 140 yield {"type" : i[0], "extension": i[1],
141 141 "node": nodeid, "url": url}
142 142
143 143 def entries(sortcolumn="", descending=False, subdir="", **map):
144 144 def sessionvars(**map):
145 145 fields = []
146 146 if 'style' in req.form:
147 147 style = req.form['style'][0]
148 148 if style != get('web', 'style', ''):
149 149 fields.append(('style', style))
150 150
151 151 separator = url[-1] == '?' and ';' or '?'
152 152 for name, value in fields:
153 153 yield dict(name=name, value=value, separator=separator)
154 154 separator = ';'
155 155
156 156 rows = []
157 157 parity = paritygen(self.stripecount)
158 158 for name, path in self.repos:
159 159 if not name.startswith(subdir):
160 160 continue
161 161 name = name[len(subdir):]
162 162
163 163 u = ui.ui(parentui=self.parentui)
164 164 try:
165 165 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
166 166 except Exception, e:
167 167 u.warn(_('error reading %s/.hg/hgrc: %s\n' % (path, e)))
168 168 continue
169 169 def get(section, name, default=None):
170 170 return u.config(section, name, default, untrusted=True)
171 171
172 172 if u.configbool("web", "hidden", untrusted=True):
173 173 continue
174 174
175 175 parts = [req.env['PATH_INFO'], name]
176 176 if req.env['SCRIPT_NAME']:
177 177 parts.insert(0, req.env['SCRIPT_NAME'])
178 178 url = ('/'.join(parts).replace("//", "/")) + '/'
179 179
180 180 # update time with local timezone
181 181 try:
182 182 d = (get_mtime(path), util.makedate()[1])
183 183 except OSError:
184 184 continue
185 185
186 186 contact = get_contact(get)
187 187 description = get("web", "description", "")
188 188 name = get("web", "name", name)
189 189 row = dict(contact=contact or "unknown",
190 190 contact_sort=contact.upper() or "unknown",
191 191 name=name,
192 192 name_sort=name,
193 193 url=url,
194 194 description=description or "unknown",
195 195 description_sort=description.upper() or "unknown",
196 196 lastchange=d,
197 197 lastchange_sort=d[1]-d[0],
198 198 sessionvars=sessionvars,
199 199 archives=archivelist(u, "tip", url))
200 200 if (not sortcolumn
201 201 or (sortcolumn, descending) == self.repos_sorted):
202 202 # fast path for unsorted output
203 203 row['parity'] = parity.next()
204 204 yield row
205 205 else:
206 206 rows.append((row["%s_sort" % sortcolumn], row))
207 207 if rows:
208 208 rows.sort()
209 209 if descending:
210 210 rows.reverse()
211 211 for key, row in rows:
212 212 row['parity'] = parity.next()
213 213 yield row
214 214
215 215 sortable = ["name", "description", "contact", "lastchange"]
216 216 sortcolumn, descending = self.repos_sorted
217 217 if 'sort' in req.form:
218 218 sortcolumn = req.form['sort'][0]
219 219 descending = sortcolumn.startswith('-')
220 220 if descending:
221 221 sortcolumn = sortcolumn[1:]
222 222 if sortcolumn not in sortable:
223 223 sortcolumn = ""
224 224
225 225 sort = [("sort_%s" % column,
226 226 "%s%s" % ((not descending and column == sortcolumn)
227 227 and "-" or "", column))
228 228 for column in sortable]
229 229
230 req.write(tmpl("index", entries=entries, subdir=subdir,
231 sortcolumn=sortcolumn, descending=descending,
232 **dict(sort)))
230 return tmpl("index", entries=entries, subdir=subdir,
231 sortcolumn=sortcolumn, descending=descending,
232 **dict(sort))
233 233
234 234 def templater(self, req):
235 235
236 236 def header(**map):
237 237 ctype = tmpl('mimetype', encoding=util._encoding)
238 238 req.httphdr(templater.stringify(ctype))
239 239 yield tmpl('header', encoding=util._encoding, **map)
240 240
241 241 def footer(**map):
242 242 yield tmpl("footer", **map)
243 243
244 244 def motd(**map):
245 245 if self.motd is not None:
246 246 yield self.motd
247 247 else:
248 248 yield config('web', 'motd', '')
249 249
250 250 def config(section, name, default=None, untrusted=True):
251 251 return self.parentui.config(section, name, default, untrusted)
252 252
253 253 url = req.env.get('SCRIPT_NAME', '')
254 254 if not url.endswith('/'):
255 255 url += '/'
256 256
257 257 staticurl = config('web', 'staticurl') or url + 'static/'
258 258 if not staticurl.endswith('/'):
259 259 staticurl += '/'
260 260
261 261 style = self.style
262 262 if style is None:
263 263 style = config('web', 'style', '')
264 264 if 'style' in req.form:
265 265 style = req.form['style'][0]
266 266 if self.stripecount is None:
267 267 self.stripecount = int(config('web', 'stripes', 1))
268 268 mapfile = style_map(templater.templatepath(), style)
269 269 tmpl = templater.templater(mapfile, templater.common_filters,
270 270 defaults={"header": header,
271 271 "footer": footer,
272 272 "motd": motd,
273 273 "url": url,
274 274 "staticurl": staticurl})
275 275 return tmpl
General Comments 0
You need to be logged in to leave comments. Login now