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