##// END OF EJS Templates
hgwebdir: wrap {entries} with mappinggenerator...
Yuya Nishihara -
r37526:30a7b328 default
parent child Browse files
Show More
@@ -1,536 +1,541 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 of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 from __future__ import absolute_import
10 10
11 11 import gc
12 12 import os
13 13 import time
14 14
15 15 from ..i18n import _
16 16
17 17 from .common import (
18 18 ErrorResponse,
19 19 HTTP_SERVER_ERROR,
20 20 cspvalues,
21 21 get_contact,
22 22 get_mtime,
23 23 ismember,
24 24 paritygen,
25 25 staticfile,
26 26 statusmessage,
27 27 )
28 28
29 29 from .. import (
30 30 configitems,
31 31 encoding,
32 32 error,
33 33 hg,
34 34 profiling,
35 35 pycompat,
36 36 scmutil,
37 37 templater,
38 templateutil,
38 39 ui as uimod,
39 40 util,
40 41 )
41 42
42 43 from . import (
43 44 hgweb_mod,
44 45 request as requestmod,
45 46 webutil,
46 47 wsgicgi,
47 48 )
48 49 from ..utils import dateutil
49 50
50 51 def cleannames(items):
51 52 return [(util.pconvert(name).strip('/'), path) for name, path in items]
52 53
53 54 def findrepos(paths):
54 55 repos = []
55 56 for prefix, root in cleannames(paths):
56 57 roothead, roottail = os.path.split(root)
57 58 # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below
58 59 # /bar/ be served as as foo/N .
59 60 # '*' will not search inside dirs with .hg (except .hg/patches),
60 61 # '**' will search inside dirs with .hg (and thus also find subrepos).
61 62 try:
62 63 recurse = {'*': False, '**': True}[roottail]
63 64 except KeyError:
64 65 repos.append((prefix, root))
65 66 continue
66 67 roothead = os.path.normpath(os.path.abspath(roothead))
67 68 paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse)
68 69 repos.extend(urlrepos(prefix, roothead, paths))
69 70 return repos
70 71
71 72 def urlrepos(prefix, roothead, paths):
72 73 """yield url paths and filesystem paths from a list of repo paths
73 74
74 75 >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq]
75 76 >>> conv(urlrepos(b'hg', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
76 77 [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')]
77 78 >>> conv(urlrepos(b'', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
78 79 [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')]
79 80 """
80 81 for path in paths:
81 82 path = os.path.normpath(path)
82 83 yield (prefix + '/' +
83 84 util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
84 85
85 86 def readallowed(ui, req):
86 87 """Check allow_read and deny_read config options of a repo's ui object
87 88 to determine user permissions. By default, with neither option set (or
88 89 both empty), allow all users to read the repo. There are two ways a
89 90 user can be denied read access: (1) deny_read is not empty, and the
90 91 user is unauthenticated or deny_read contains user (or *), and (2)
91 92 allow_read is not empty and the user is not in allow_read. Return True
92 93 if user is allowed to read the repo, else return False."""
93 94
94 95 user = req.remoteuser
95 96
96 97 deny_read = ui.configlist('web', 'deny_read', untrusted=True)
97 98 if deny_read and (not user or ismember(ui, user, deny_read)):
98 99 return False
99 100
100 101 allow_read = ui.configlist('web', 'allow_read', untrusted=True)
101 102 # by default, allow reading if no allow_read option has been set
102 103 if not allow_read or ismember(ui, user, allow_read):
103 104 return True
104 105
105 106 return False
106 107
107 108 def archivelist(ui, nodeid, url):
108 109 allowed = ui.configlist('web', 'allow_archive', untrusted=True)
109 110 archives = []
110 111
111 112 for typ, spec in hgweb_mod.archivespecs.iteritems():
112 113 if typ in allowed or ui.configbool('web', 'allow' + typ,
113 114 untrusted=True):
114 115 archives.append({
115 116 'type': typ,
116 117 'extension': spec[2],
117 118 'node': nodeid,
118 119 'url': url,
119 120 })
120 121
121 122 return archives
122 123
123 124 def rawindexentries(ui, repos, req, subdir=''):
124 125 descend = ui.configbool('web', 'descend')
125 126 collapse = ui.configbool('web', 'collapse')
126 127 seenrepos = set()
127 128 seendirs = set()
128 129 for name, path in repos:
129 130
130 131 if not name.startswith(subdir):
131 132 continue
132 133 name = name[len(subdir):]
133 134 directory = False
134 135
135 136 if '/' in name:
136 137 if not descend:
137 138 continue
138 139
139 140 nameparts = name.split('/')
140 141 rootname = nameparts[0]
141 142
142 143 if not collapse:
143 144 pass
144 145 elif rootname in seendirs:
145 146 continue
146 147 elif rootname in seenrepos:
147 148 pass
148 149 else:
149 150 directory = True
150 151 name = rootname
151 152
152 153 # redefine the path to refer to the directory
153 154 discarded = '/'.join(nameparts[1:])
154 155
155 156 # remove name parts plus accompanying slash
156 157 path = path[:-len(discarded) - 1]
157 158
158 159 try:
159 160 r = hg.repository(ui, path)
160 161 directory = False
161 162 except (IOError, error.RepoError):
162 163 pass
163 164
164 165 parts = [
165 166 req.apppath.strip('/'),
166 167 subdir.strip('/'),
167 168 name.strip('/'),
168 169 ]
169 170 url = '/' + '/'.join(p for p in parts if p) + '/'
170 171
171 172 # show either a directory entry or a repository
172 173 if directory:
173 174 # get the directory's time information
174 175 try:
175 176 d = (get_mtime(path), dateutil.makedate()[1])
176 177 except OSError:
177 178 continue
178 179
179 180 # add '/' to the name to make it obvious that
180 181 # the entry is a directory, not a regular repository
181 182 row = {'contact': "",
182 183 'contact_sort': "",
183 184 'name': name + '/',
184 185 'name_sort': name,
185 186 'url': url,
186 187 'description': "",
187 188 'description_sort': "",
188 189 'lastchange': d,
189 190 'lastchange_sort': d[1] - d[0],
190 191 'archives': [],
191 192 'isdirectory': True,
192 193 'labels': [],
193 194 }
194 195
195 196 seendirs.add(name)
196 197 yield row
197 198 continue
198 199
199 200 u = ui.copy()
200 201 try:
201 202 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
202 203 except Exception as e:
203 204 u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
204 205 continue
205 206
206 207 def get(section, name, default=uimod._unset):
207 208 return u.config(section, name, default, untrusted=True)
208 209
209 210 if u.configbool("web", "hidden", untrusted=True):
210 211 continue
211 212
212 213 if not readallowed(u, req):
213 214 continue
214 215
215 216 # update time with local timezone
216 217 try:
217 218 r = hg.repository(ui, path)
218 219 except IOError:
219 220 u.warn(_('error accessing repository at %s\n') % path)
220 221 continue
221 222 except error.RepoError:
222 223 u.warn(_('error accessing repository at %s\n') % path)
223 224 continue
224 225 try:
225 226 d = (get_mtime(r.spath), dateutil.makedate()[1])
226 227 except OSError:
227 228 continue
228 229
229 230 contact = get_contact(get)
230 231 description = get("web", "description")
231 232 seenrepos.add(name)
232 233 name = get("web", "name", name)
233 234 row = {'contact': contact or "unknown",
234 235 'contact_sort': contact.upper() or "unknown",
235 236 'name': name,
236 237 'name_sort': name,
237 238 'url': url,
238 239 'description': description or "unknown",
239 240 'description_sort': description.upper() or "unknown",
240 241 'lastchange': d,
241 242 'lastchange_sort': d[1] - d[0],
242 243 'archives': archivelist(u, "tip", url),
243 244 'isdirectory': None,
244 245 'labels': u.configlist('web', 'labels', untrusted=True),
245 246 }
246 247
247 248 yield row
248 249
249 def indexentries(ui, repos, req, stripecount, sortcolumn='',
250 descending=False, subdir=''):
251
250 def _indexentriesgen(context, ui, repos, req, stripecount, sortcolumn,
251 descending, subdir):
252 252 rows = rawindexentries(ui, repos, req, subdir=subdir)
253 253
254 254 sortdefault = None, False
255 255
256 256 if sortcolumn and sortdefault != (sortcolumn, descending):
257 257 sortkey = '%s_sort' % sortcolumn
258 258 rows = sorted(rows, key=lambda x: x[sortkey],
259 259 reverse=descending)
260 260
261 261 for row, parity in zip(rows, paritygen(stripecount)):
262 262 row['parity'] = parity
263 263 yield row
264 264
265 def indexentries(ui, repos, req, stripecount, sortcolumn='',
266 descending=False, subdir=''):
267 args = (ui, repos, req, stripecount, sortcolumn, descending, subdir)
268 return templateutil.mappinggenerator(_indexentriesgen, args=args)
269
265 270 class hgwebdir(object):
266 271 """HTTP server for multiple repositories.
267 272
268 273 Given a configuration, different repositories will be served depending
269 274 on the request path.
270 275
271 276 Instances are typically used as WSGI applications.
272 277 """
273 278 def __init__(self, conf, baseui=None):
274 279 self.conf = conf
275 280 self.baseui = baseui
276 281 self.ui = None
277 282 self.lastrefresh = 0
278 283 self.motd = None
279 284 self.refresh()
280 285
281 286 def refresh(self):
282 287 if self.ui:
283 288 refreshinterval = self.ui.configint('web', 'refreshinterval')
284 289 else:
285 290 item = configitems.coreitems['web']['refreshinterval']
286 291 refreshinterval = item.default
287 292
288 293 # refreshinterval <= 0 means to always refresh.
289 294 if (refreshinterval > 0 and
290 295 self.lastrefresh + refreshinterval > time.time()):
291 296 return
292 297
293 298 if self.baseui:
294 299 u = self.baseui.copy()
295 300 else:
296 301 u = uimod.ui.load()
297 302 u.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir')
298 303 u.setconfig('ui', 'nontty', 'true', 'hgwebdir')
299 304 # displaying bundling progress bar while serving feels wrong and may
300 305 # break some wsgi implementations.
301 306 u.setconfig('progress', 'disable', 'true', 'hgweb')
302 307
303 308 if not isinstance(self.conf, (dict, list, tuple)):
304 309 map = {'paths': 'hgweb-paths'}
305 310 if not os.path.exists(self.conf):
306 311 raise error.Abort(_('config file %s not found!') % self.conf)
307 312 u.readconfig(self.conf, remap=map, trust=True)
308 313 paths = []
309 314 for name, ignored in u.configitems('hgweb-paths'):
310 315 for path in u.configlist('hgweb-paths', name):
311 316 paths.append((name, path))
312 317 elif isinstance(self.conf, (list, tuple)):
313 318 paths = self.conf
314 319 elif isinstance(self.conf, dict):
315 320 paths = self.conf.items()
316 321
317 322 repos = findrepos(paths)
318 323 for prefix, root in u.configitems('collections'):
319 324 prefix = util.pconvert(prefix)
320 325 for path in scmutil.walkrepos(root, followsym=True):
321 326 repo = os.path.normpath(path)
322 327 name = util.pconvert(repo)
323 328 if name.startswith(prefix):
324 329 name = name[len(prefix):]
325 330 repos.append((name.lstrip('/'), repo))
326 331
327 332 self.repos = repos
328 333 self.ui = u
329 334 encoding.encoding = self.ui.config('web', 'encoding')
330 335 self.style = self.ui.config('web', 'style')
331 336 self.templatepath = self.ui.config('web', 'templates', untrusted=False)
332 337 self.stripecount = self.ui.config('web', 'stripes')
333 338 if self.stripecount:
334 339 self.stripecount = int(self.stripecount)
335 340 prefix = self.ui.config('web', 'prefix')
336 341 if prefix.startswith('/'):
337 342 prefix = prefix[1:]
338 343 if prefix.endswith('/'):
339 344 prefix = prefix[:-1]
340 345 self.prefix = prefix
341 346 self.lastrefresh = time.time()
342 347
343 348 def run(self):
344 349 if not encoding.environ.get('GATEWAY_INTERFACE',
345 350 '').startswith("CGI/1."):
346 351 raise RuntimeError("This function is only intended to be "
347 352 "called while running as a CGI script.")
348 353 wsgicgi.launch(self)
349 354
350 355 def __call__(self, env, respond):
351 356 baseurl = self.ui.config('web', 'baseurl')
352 357 req = requestmod.parserequestfromenv(env, altbaseurl=baseurl)
353 358 res = requestmod.wsgiresponse(req, respond)
354 359
355 360 return self.run_wsgi(req, res)
356 361
357 362 def run_wsgi(self, req, res):
358 363 profile = self.ui.configbool('profiling', 'enabled')
359 364 with profiling.profile(self.ui, enabled=profile):
360 365 try:
361 366 for r in self._runwsgi(req, res):
362 367 yield r
363 368 finally:
364 369 # There are known cycles in localrepository that prevent
365 370 # those objects (and tons of held references) from being
366 371 # collected through normal refcounting. We mitigate those
367 372 # leaks by performing an explicit GC on every request.
368 373 # TODO remove this once leaks are fixed.
369 374 # TODO only run this on requests that create localrepository
370 375 # instances instead of every request.
371 376 gc.collect()
372 377
373 378 def _runwsgi(self, req, res):
374 379 try:
375 380 self.refresh()
376 381
377 382 csp, nonce = cspvalues(self.ui)
378 383 if csp:
379 384 res.headers['Content-Security-Policy'] = csp
380 385
381 386 virtual = req.dispatchpath.strip('/')
382 387 tmpl = self.templater(req, nonce)
383 388 ctype = tmpl.render('mimetype', {'encoding': encoding.encoding})
384 389
385 390 # Global defaults. These can be overridden by any handler.
386 391 res.status = '200 Script output follows'
387 392 res.headers['Content-Type'] = ctype
388 393
389 394 # a static file
390 395 if virtual.startswith('static/') or 'static' in req.qsparams:
391 396 if virtual.startswith('static/'):
392 397 fname = virtual[7:]
393 398 else:
394 399 fname = req.qsparams['static']
395 400 static = self.ui.config("web", "static", None,
396 401 untrusted=False)
397 402 if not static:
398 403 tp = self.templatepath or templater.templatepaths()
399 404 if isinstance(tp, str):
400 405 tp = [tp]
401 406 static = [os.path.join(p, 'static') for p in tp]
402 407
403 408 staticfile(static, fname, res)
404 409 return res.sendresponse()
405 410
406 411 # top-level index
407 412
408 413 repos = dict(self.repos)
409 414
410 415 if (not virtual or virtual == 'index') and virtual not in repos:
411 416 return self.makeindex(req, res, tmpl)
412 417
413 418 # nested indexes and hgwebs
414 419
415 420 if virtual.endswith('/index') and virtual not in repos:
416 421 subdir = virtual[:-len('index')]
417 422 if any(r.startswith(subdir) for r in repos):
418 423 return self.makeindex(req, res, tmpl, subdir)
419 424
420 425 def _virtualdirs():
421 426 # Check the full virtual path, each parent, and the root ('')
422 427 if virtual != '':
423 428 yield virtual
424 429
425 430 for p in util.finddirs(virtual):
426 431 yield p
427 432
428 433 yield ''
429 434
430 435 for virtualrepo in _virtualdirs():
431 436 real = repos.get(virtualrepo)
432 437 if real:
433 438 # Re-parse the WSGI environment to take into account our
434 439 # repository path component.
435 440 req = requestmod.parserequestfromenv(
436 441 req.rawenv, reponame=virtualrepo,
437 442 altbaseurl=self.ui.config('web', 'baseurl'))
438 443 try:
439 444 # ensure caller gets private copy of ui
440 445 repo = hg.repository(self.ui.copy(), real)
441 446 return hgweb_mod.hgweb(repo).run_wsgi(req, res)
442 447 except IOError as inst:
443 448 msg = encoding.strtolocal(inst.strerror)
444 449 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
445 450 except error.RepoError as inst:
446 451 raise ErrorResponse(HTTP_SERVER_ERROR, bytes(inst))
447 452
448 453 # browse subdirectories
449 454 subdir = virtual + '/'
450 455 if [r for r in repos if r.startswith(subdir)]:
451 456 return self.makeindex(req, res, tmpl, subdir)
452 457
453 458 # prefixes not found
454 459 res.status = '404 Not Found'
455 460 res.setbodygen(tmpl.generate('notfound', {'repo': virtual}))
456 461 return res.sendresponse()
457 462
458 463 except ErrorResponse as e:
459 464 res.status = statusmessage(e.code, pycompat.bytestr(e))
460 465 res.setbodygen(tmpl.generate('error', {'error': e.message or ''}))
461 466 return res.sendresponse()
462 467 finally:
463 468 tmpl = None
464 469
465 470 def makeindex(self, req, res, tmpl, subdir=""):
466 471 self.refresh()
467 472 sortable = ["name", "description", "contact", "lastchange"]
468 473 sortcolumn, descending = None, False
469 474 if 'sort' in req.qsparams:
470 475 sortcolumn = req.qsparams['sort']
471 476 descending = sortcolumn.startswith('-')
472 477 if descending:
473 478 sortcolumn = sortcolumn[1:]
474 479 if sortcolumn not in sortable:
475 480 sortcolumn = ""
476 481
477 482 sort = [("sort_%s" % column,
478 483 "%s%s" % ((not descending and column == sortcolumn)
479 484 and "-" or "", column))
480 485 for column in sortable]
481 486
482 487 self.refresh()
483 488
484 489 entries = indexentries(self.ui, self.repos, req,
485 490 self.stripecount, sortcolumn=sortcolumn,
486 491 descending=descending, subdir=subdir)
487 492
488 493 mapping = {
489 494 'entries': entries,
490 495 'subdir': subdir,
491 496 'pathdef': hgweb_mod.makebreadcrumb('/' + subdir, self.prefix),
492 497 'sortcolumn': sortcolumn,
493 498 'descending': descending,
494 499 }
495 500 mapping.update(sort)
496 501 res.setbodygen(tmpl.generate('index', mapping))
497 502 return res.sendresponse()
498 503
499 504 def templater(self, req, nonce):
500 505
501 506 def motd(**map):
502 507 if self.motd is not None:
503 508 yield self.motd
504 509 else:
505 510 yield config('web', 'motd')
506 511
507 512 def config(section, name, default=uimod._unset, untrusted=True):
508 513 return self.ui.config(section, name, default, untrusted)
509 514
510 515 vars = {}
511 516 styles, (style, mapfile) = hgweb_mod.getstyle(req, config,
512 517 self.templatepath)
513 518 if style == styles[0]:
514 519 vars['style'] = style
515 520
516 521 sessionvars = webutil.sessionvars(vars, r'?')
517 522 logourl = config('web', 'logourl')
518 523 logoimg = config('web', 'logoimg')
519 524 staticurl = (config('web', 'staticurl')
520 525 or req.apppath + '/static/')
521 526 if not staticurl.endswith('/'):
522 527 staticurl += '/'
523 528
524 529 defaults = {
525 530 "encoding": encoding.encoding,
526 531 "motd": motd,
527 532 "url": req.apppath + '/',
528 533 "logourl": logourl,
529 534 "logoimg": logoimg,
530 535 "staticurl": staticurl,
531 536 "sessionvars": sessionvars,
532 537 "style": style,
533 538 "nonce": nonce,
534 539 }
535 540 tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
536 541 return tmpl
General Comments 0
You need to be logged in to leave comments. Login now