hgwebdir_mod.py
290 lines
| 11.4 KiB
| text/x-python
|
PythonLexer
|
r2391 | # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories. | ||
|
r2356 | # | ||
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | ||||
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
|
r2356 | # | ||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r6391 | import os | ||
|
r2356 | from mercurial.i18n import gettext as _ | ||
|
r6217 | from mercurial.repo import RepoError | ||
|
r5976 | from mercurial import ui, hg, util, templater, templatefilters | ||
from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen,\ | ||||
|
r5993 | get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR | ||
|
r3877 | from hgweb_mod import hgweb | ||
|
r5566 | from request import wsgirequest | ||
|
r2356 | |||
# This is a stopgap | ||||
class hgwebdir(object): | ||||
|
r4079 | def __init__(self, config, parentui=None): | ||
|
r2356 | def cleannames(items): | ||
|
r5584 | return [(util.pconvert(name).strip('/'), path) | ||
|
r5063 | for name, path in items] | ||
|
r2356 | |||
|
r5601 | self.parentui = parentui or ui.ui(report_untrusted=False, | ||
interactive = False) | ||||
|
r4080 | self.motd = None | ||
self.style = None | ||||
|
r4462 | self.stripecount = None | ||
|
r2356 | self.repos_sorted = ('name', False) | ||
|
r6221 | self._baseurl = None | ||
|
r2356 | if isinstance(config, (list, tuple)): | ||
self.repos = cleannames(config) | ||||
self.repos_sorted = ('', False) | ||||
elif isinstance(config, dict): | ||||
self.repos = cleannames(config.items()) | ||||
self.repos.sort() | ||||
else: | ||||
|
r4051 | if isinstance(config, util.configparser): | ||
cp = config | ||||
else: | ||||
cp = util.configparser() | ||||
cp.read(config) | ||||
|
r2356 | self.repos = [] | ||
|
r3221 | if cp.has_section('web'): | ||
if cp.has_option('web', 'motd'): | ||||
self.motd = cp.get('web', 'motd') | ||||
if cp.has_option('web', 'style'): | ||||
|
r3223 | self.style = cp.get('web', 'style') | ||
|
r4462 | if cp.has_option('web', 'stripes'): | ||
self.stripecount = int(cp.get('web', 'stripes')) | ||||
|
r6221 | if cp.has_option('web', 'baseurl'): | ||
self._baseurl = cp.get('web', 'baseurl') | ||||
|
r2356 | if cp.has_section('paths'): | ||
self.repos.extend(cleannames(cp.items('paths'))) | ||||
if cp.has_section('collections'): | ||||
for prefix, root in cp.items('collections'): | ||||
|
r6287 | for path in util.walkrepos(root, followsym=True): | ||
|
r2356 | repo = os.path.normpath(path) | ||
name = repo | ||||
if name.startswith(prefix): | ||||
name = name[len(prefix):] | ||||
self.repos.append((name.lstrip(os.sep), repo)) | ||||
self.repos.sort() | ||||
|
r2535 | def run(self): | ||
|
r2538 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): | ||
|
r2535 | raise RuntimeError("This function is only intended to be called while running as a CGI script.") | ||
import mercurial.hgweb.wsgicgi as wsgicgi | ||||
|
r5566 | wsgicgi.launch(self) | ||
def __call__(self, env, respond): | ||||
req = wsgirequest(env, respond) | ||||
self.run_wsgi(req) | ||||
return req | ||||
|
r2535 | |||
def run_wsgi(self, req): | ||||
|
r2356 | |||
|
r5601 | try: | ||
try: | ||||
|
r5603 | |||
|
r5601 | virtual = req.env.get("PATH_INFO", "").strip('/') | ||
|
r5993 | tmpl = self.templater(req) | ||
|
r6391 | ctype = tmpl('mimetype', encoding=util._encoding) | ||
ctype = templater.stringify(ctype) | ||||
|
r5760 | |||
|
r5603 | # a static file | ||
if virtual.startswith('static/') or 'static' in req.form: | ||||
|
r5601 | static = os.path.join(templater.templatepath(), 'static') | ||
|
r5603 | if virtual.startswith('static/'): | ||
fname = virtual[7:] | ||||
else: | ||||
fname = req.form['static'][0] | ||||
|
r5601 | req.write(staticfile(static, fname, req)) | ||
|
r5603 | return | ||
# top-level index | ||||
elif not virtual: | ||||
|
r5993 | req.respond(HTTP_OK, ctype) | ||
|
r5965 | req.write(self.makeindex(req, tmpl)) | ||
|
r5603 | return | ||
|
r5601 | |||
|
r5603 | # nested indexes and hgwebs | ||
|
r6210 | |||
|
r5603 | repos = dict(self.repos) | ||
while virtual: | ||||
real = repos.get(virtual) | ||||
if real: | ||||
req.env['REPO_NAME'] = virtual | ||||
try: | ||||
repo = hg.repository(self.parentui, real) | ||||
hgweb(repo).run_wsgi(req) | ||||
|
r5601 | return | ||
|
r5603 | except IOError, inst: | ||
|
r5993 | msg = inst.strerror | ||
raise ErrorResponse(HTTP_SERVER_ERROR, msg) | ||||
|
r6217 | except RepoError, inst: | ||
|
r5993 | raise ErrorResponse(HTTP_SERVER_ERROR, str(inst)) | ||
|
r5601 | |||
|
r5603 | # browse subdirectories | ||
subdir = virtual + '/' | ||||
if [r for r in repos if r.startswith(subdir)]: | ||||
|
r5993 | req.respond(HTTP_OK, ctype) | ||
|
r5965 | req.write(self.makeindex(req, tmpl, subdir)) | ||
|
r5603 | return | ||
up = virtual.rfind('/') | ||||
if up < 0: | ||||
break | ||||
virtual = virtual[:up] | ||||
# prefixes not found | ||||
|
r5993 | req.respond(HTTP_NOT_FOUND, ctype) | ||
req.write(tmpl("notfound", repo=virtual)) | ||||
|
r5760 | |||
|
r5601 | except ErrorResponse, err: | ||
|
r5993 | req.respond(err.code, ctype) | ||
req.write(tmpl('error', error=err.message or '')) | ||||
|
r5601 | finally: | ||
tmpl = None | ||||
def makeindex(self, req, tmpl, subdir=""): | ||||
|
r2356 | def archivelist(ui, nodeid, url): | ||
|
r3556 | allowed = ui.configlist("web", "allow_archive", untrusted=True) | ||
|
r3262 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: | ||
|
r3556 | if i[0] in allowed or ui.configbool("web", "allow" + i[0], | ||
untrusted=True): | ||||
|
r3262 | yield {"type" : i[0], "extension": i[1], | ||
"node": nodeid, "url": url} | ||||
|
r2356 | |||
|
r4841 | def entries(sortcolumn="", descending=False, subdir="", **map): | ||
|
r3365 | def sessionvars(**map): | ||
fields = [] | ||||
|
r5915 | if 'style' in req.form: | ||
|
r3365 | style = req.form['style'][0] | ||
if style != get('web', 'style', ''): | ||||
fields.append(('style', style)) | ||||
separator = url[-1] == '?' and ';' or '?' | ||||
for name, value in fields: | ||||
yield dict(name=name, value=value, separator=separator) | ||||
separator = ';' | ||||
|
r2356 | rows = [] | ||
|
r4462 | parity = paritygen(self.stripecount) | ||
|
r2356 | for name, path in self.repos: | ||
|
r4841 | if not name.startswith(subdir): | ||
continue | ||||
|
r4843 | name = name[len(subdir):] | ||
|
r4841 | |||
|
r5601 | u = ui.ui(parentui=self.parentui) | ||
|
r2356 | try: | ||
u.readconfig(os.path.join(path, '.hg', 'hgrc')) | ||||
|
r5332 | except Exception, e: | ||
u.warn(_('error reading %s/.hg/hgrc: %s\n' % (path, e))) | ||||
continue | ||||
|
r3556 | def get(section, name, default=None): | ||
return u.config(section, name, default, untrusted=True) | ||||
|
r2356 | |||
|
r4709 | if u.configbool("web", "hidden", untrusted=True): | ||
continue | ||||
|
r6459 | parts = [name] | ||
if 'PATH_INFO' in req.env: | ||||
parts.insert(0, req.env['PATH_INFO'].rstrip('/')) | ||||
|
r5579 | if req.env['SCRIPT_NAME']: | ||
|
r5760 | parts.insert(0, req.env['SCRIPT_NAME']) | ||
|
r5579 | url = ('/'.join(parts).replace("//", "/")) + '/' | ||
|
r2356 | |||
# update time with local timezone | ||||
try: | ||||
d = (get_mtime(path), util.makedate()[1]) | ||||
except OSError: | ||||
continue | ||||
|
r5779 | contact = get_contact(get) | ||
|
r2356 | description = get("web", "description", "") | ||
name = get("web", "name", name) | ||||
row = dict(contact=contact or "unknown", | ||||
contact_sort=contact.upper() or "unknown", | ||||
name=name, | ||||
name_sort=name, | ||||
url=url, | ||||
description=description or "unknown", | ||||
description_sort=description.upper() or "unknown", | ||||
lastchange=d, | ||||
lastchange_sort=d[1]-d[0], | ||||
|
r3365 | sessionvars=sessionvars, | ||
|
r2356 | archives=archivelist(u, "tip", url)) | ||
if (not sortcolumn | ||||
or (sortcolumn, descending) == self.repos_sorted): | ||||
# fast path for unsorted output | ||||
|
r4462 | row['parity'] = parity.next() | ||
|
r2356 | yield row | ||
else: | ||||
rows.append((row["%s_sort" % sortcolumn], row)) | ||||
if rows: | ||||
rows.sort() | ||||
if descending: | ||||
rows.reverse() | ||||
for key, row in rows: | ||||
|
r4462 | row['parity'] = parity.next() | ||
|
r2356 | yield row | ||
|
r5601 | sortable = ["name", "description", "contact", "lastchange"] | ||
sortcolumn, descending = self.repos_sorted | ||||
|
r5915 | if 'sort' in req.form: | ||
|
r5601 | sortcolumn = req.form['sort'][0] | ||
descending = sortcolumn.startswith('-') | ||||
if descending: | ||||
sortcolumn = sortcolumn[1:] | ||||
if sortcolumn not in sortable: | ||||
sortcolumn = "" | ||||
|
r4841 | |||
|
r5601 | sort = [("sort_%s" % column, | ||
"%s%s" % ((not descending and column == sortcolumn) | ||||
and "-" or "", column)) | ||||
for column in sortable] | ||||
|
r5928 | |||
|
r6221 | if self._baseurl is not None: | ||
req.env['SCRIPT_NAME'] = self._baseurl | ||||
|
r5965 | return tmpl("index", entries=entries, subdir=subdir, | ||
sortcolumn=sortcolumn, descending=descending, | ||||
**dict(sort)) | ||||
|
r5602 | |||
def templater(self, req): | ||||
def header(**map): | ||||
|
r6391 | yield tmpl('header', encoding=util._encoding, **map) | ||
|
r5602 | |||
def footer(**map): | ||||
yield tmpl("footer", **map) | ||||
def motd(**map): | ||||
if self.motd is not None: | ||||
yield self.motd | ||||
else: | ||||
yield config('web', 'motd', '') | ||||
def config(section, name, default=None, untrusted=True): | ||||
return self.parentui.config(section, name, default, untrusted) | ||||
|
r6221 | if self._baseurl is not None: | ||
req.env['SCRIPT_NAME'] = self._baseurl | ||||
|
r5602 | url = req.env.get('SCRIPT_NAME', '') | ||
if not url.endswith('/'): | ||||
url += '/' | ||||
staticurl = config('web', 'staticurl') or url + 'static/' | ||||
if not staticurl.endswith('/'): | ||||
staticurl += '/' | ||||
style = self.style | ||||
if style is None: | ||||
style = config('web', 'style', '') | ||||
|
r5915 | if 'style' in req.form: | ||
|
r5602 | style = req.form['style'][0] | ||
if self.stripecount is None: | ||||
self.stripecount = int(config('web', 'stripes', 1)) | ||||
mapfile = style_map(templater.templatepath(), style) | ||||
|
r5976 | tmpl = templater.templater(mapfile, templatefilters.filters, | ||
|
r5602 | defaults={"header": header, | ||
"footer": footer, | ||||
"motd": motd, | ||||
"url": url, | ||||
"staticurl": staticurl}) | ||||
return tmpl | ||||