hgwebdir_mod.py
463 lines
| 17.7 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 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2356 | |||
|
r13821 | import os, re, time | ||
|
r7225 | from mercurial.i18n import _ | ||
|
r13975 | from mercurial import ui, hg, scmutil, util, templater | ||
|
r14076 | from mercurial import error, encoding | ||
|
r19032 | from common import ErrorResponse, get_mtime, staticfile, paritygen, ismember, \ | ||
|
r5993 | get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR | ||
|
r18258 | from hgweb_mod import hgweb, makebreadcrumb | ||
|
r5566 | from request import wsgirequest | ||
|
r8216 | import webutil | ||
|
r2356 | |||
|
r8215 | def cleannames(items): | ||
return [(util.pconvert(name).strip('/'), path) for name, path in items] | ||||
|
r2356 | |||
|
r8529 | def findrepos(paths): | ||
|
r9723 | repos = [] | ||
|
r8529 | for prefix, root in cleannames(paths): | ||
roothead, roottail = os.path.split(root) | ||||
|
r17104 | # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below | ||
# /bar/ be served as as foo/N . | ||||
# '*' will not search inside dirs with .hg (except .hg/patches), | ||||
# '**' will search inside dirs with .hg (and thus also find subrepos). | ||||
|
r8529 | try: | ||
recurse = {'*': False, '**': True}[roottail] | ||||
except KeyError: | ||||
|
r9723 | repos.append((prefix, root)) | ||
|
r8529 | continue | ||
|
r11677 | roothead = os.path.normpath(os.path.abspath(roothead)) | ||
|
r13975 | paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse) | ||
|
r13402 | repos.extend(urlrepos(prefix, roothead, paths)) | ||
|
r9723 | return repos | ||
|
r8529 | |||
|
r13402 | def urlrepos(prefix, roothead, paths): | ||
"""yield url paths and filesystem paths from a list of repo paths | ||||
|
r13538 | >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq] | ||
>>> conv(urlrepos('hg', '/opt', ['/opt/r', '/opt/r/r', '/opt'])) | ||||
|
r13403 | [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')] | ||
|
r13538 | >>> conv(urlrepos('', '/opt', ['/opt/r', '/opt/r/r', '/opt'])) | ||
|
r13402 | [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')] | ||
""" | ||||
for path in paths: | ||||
path = os.path.normpath(path) | ||||
|
r13403 | yield (prefix + '/' + | ||
util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path | ||||
|
r13402 | |||
|
r15003 | def geturlcgivars(baseurl, port): | ||
""" | ||||
Extract CGI variables from baseurl | ||||
>>> geturlcgivars("http://host.org/base", "80") | ||||
('host.org', '80', '/base') | ||||
>>> geturlcgivars("http://host.org:8000/base", "80") | ||||
('host.org', '8000', '/base') | ||||
>>> geturlcgivars('/base', 8000) | ||||
('', '8000', '/base') | ||||
>>> geturlcgivars("base", '8000') | ||||
('', '8000', '/base') | ||||
>>> geturlcgivars("http://host", '8000') | ||||
('host', '8000', '/') | ||||
>>> geturlcgivars("http://host/", '8000') | ||||
('host', '8000', '/') | ||||
""" | ||||
u = util.url(baseurl) | ||||
name = u.host or '' | ||||
if u.port: | ||||
port = u.port | ||||
path = u.path or "" | ||||
if not path.startswith('/'): | ||||
path = '/' + path | ||||
return name, str(port), path | ||||
|
r2356 | class hgwebdir(object): | ||
|
r8371 | refreshinterval = 20 | ||
|
r8215 | |||
|
r8191 | def __init__(self, conf, baseui=None): | ||
|
r8371 | self.conf = conf | ||
self.baseui = baseui | ||||
self.lastrefresh = 0 | ||||
|
r9903 | self.motd = None | ||
|
r8371 | self.refresh() | ||
|
r2356 | |||
|
r8371 | def refresh(self): | ||
if self.lastrefresh + self.refreshinterval > time.time(): | ||||
return | ||||
if self.baseui: | ||||
|
r11239 | u = self.baseui.copy() | ||
|
r2356 | else: | ||
|
r12696 | u = ui.ui() | ||
|
r11239 | u.setconfig('ui', 'report_untrusted', 'off') | ||
|
r16754 | u.setconfig('ui', 'nontty', 'true') | ||
|
r8136 | |||
|
r8529 | if not isinstance(self.conf, (dict, list, tuple)): | ||
map = {'paths': 'hgweb-paths'} | ||||
|
r13214 | if not os.path.exists(self.conf): | ||
raise util.Abort(_('config file %s not found!') % self.conf) | ||||
|
r11239 | u.readconfig(self.conf, remap=map, trust=True) | ||
|
r13667 | paths = [] | ||
for name, ignored in u.configitems('hgweb-paths'): | ||||
for path in u.configlist('hgweb-paths', name): | ||||
paths.append((name, path)) | ||||
|
r8529 | elif isinstance(self.conf, (list, tuple)): | ||
paths = self.conf | ||||
elif isinstance(self.conf, dict): | ||||
paths = self.conf.items() | ||||
|
r8345 | |||
|
r11239 | repos = findrepos(paths) | ||
for prefix, root in u.configitems('collections'): | ||||
prefix = util.pconvert(prefix) | ||||
|
r13975 | for path in scmutil.walkrepos(root, followsym=True): | ||
|
r11239 | repo = os.path.normpath(path) | ||
name = util.pconvert(repo) | ||||
if name.startswith(prefix): | ||||
name = name[len(prefix):] | ||||
repos.append((name.lstrip('/'), repo)) | ||||
self.repos = repos | ||||
self.ui = u | ||||
|
r8859 | encoding.encoding = self.ui.config('web', 'encoding', | ||
encoding.encoding) | ||||
|
r8621 | self.style = self.ui.config('web', 'style', 'paper') | ||
|
r11649 | self.templatepath = self.ui.config('web', 'templates', None) | ||
|
r8621 | self.stripecount = self.ui.config('web', 'stripes', 1) | ||
if self.stripecount: | ||||
self.stripecount = int(self.stripecount) | ||||
self._baseurl = self.ui.config('web', 'baseurl') | ||||
|
r18515 | prefix = self.ui.config('web', 'prefix', '') | ||
if prefix.startswith('/'): | ||||
prefix = prefix[1:] | ||||
if prefix.endswith('/'): | ||||
prefix = prefix[:-1] | ||||
self.prefix = prefix | ||||
|
r8371 | self.lastrefresh = time.time() | ||
|
r2356 | |||
|
r2535 | def run(self): | ||
|
r2538 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): | ||
|
r8663 | raise RuntimeError("This function is only intended to be " | ||
"called while running as a CGI script.") | ||||
|
r2535 | import mercurial.hgweb.wsgicgi as wsgicgi | ||
|
r5566 | wsgicgi.launch(self) | ||
def __call__(self, env, respond): | ||||
req = wsgirequest(env, respond) | ||||
|
r6797 | return self.run_wsgi(req) | ||
|
r2535 | |||
|
r7336 | def read_allowed(self, ui, req): | ||
"""Check allow_read and deny_read config options of a repo's ui object | ||||
to determine user permissions. By default, with neither option set (or | ||||
both empty), allow all users to read the repo. There are two ways a | ||||
user can be denied read access: (1) deny_read is not empty, and the | ||||
user is unauthenticated or deny_read contains user (or *), and (2) | ||||
allow_read is not empty and the user is not in allow_read. Return True | ||||
if user is allowed to read the repo, else return False.""" | ||||
user = req.env.get('REMOTE_USER') | ||||
|
r7575 | deny_read = ui.configlist('web', 'deny_read', untrusted=True) | ||
|
r19032 | if deny_read and (not user or ismember(ui, user, deny_read)): | ||
|
r7336 | return False | ||
|
r7575 | allow_read = ui.configlist('web', 'allow_read', untrusted=True) | ||
|
r7336 | # by default, allow reading if no allow_read option has been set | ||
|
r19032 | if (not allow_read) or ismember(ui, user, allow_read): | ||
|
r7336 | return True | ||
return False | ||||
|
r2535 | def run_wsgi(self, req): | ||
|
r5601 | try: | ||
try: | ||||
|
r8371 | self.refresh() | ||
|
r5603 | |||
|
r5601 | virtual = req.env.get("PATH_INFO", "").strip('/') | ||
|
r5993 | tmpl = self.templater(req) | ||
|
r7948 | ctype = tmpl('mimetype', encoding=encoding.encoding) | ||
|
r6391 | ctype = templater.stringify(ctype) | ||
|
r5760 | |||
|
r5603 | # a static file | ||
if virtual.startswith('static/') or 'static' in req.form: | ||||
if virtual.startswith('static/'): | ||||
fname = virtual[7:] | ||||
else: | ||||
fname = req.form['static'][0] | ||||
|
r18191 | static = self.ui.config("web", "static", None, | ||
untrusted=False) | ||||
if not static: | ||||
tp = self.templatepath or templater.templatepath() | ||||
if isinstance(tp, str): | ||||
tp = [tp] | ||||
static = [os.path.join(p, 'static') for p in tp] | ||||
|
r18645 | staticfile(static, fname, req) | ||
return [] | ||||
|
r5603 | |||
# top-level index | ||||
elif not virtual: | ||||
|
r5993 | req.respond(HTTP_OK, ctype) | ||
|
r7396 | return self.makeindex(req, tmpl) | ||
|
r5601 | |||
|
r5603 | # nested indexes and hgwebs | ||
|
r6210 | |||
|
r5603 | repos = dict(self.repos) | ||
|
r13066 | virtualrepo = virtual | ||
while virtualrepo: | ||||
real = repos.get(virtualrepo) | ||||
|
r5603 | if real: | ||
|
r13066 | req.env['REPO_NAME'] = virtualrepo | ||
|
r5603 | try: | ||
|
r8191 | repo = hg.repository(self.ui, real) | ||
|
r6797 | return hgweb(repo).run_wsgi(req) | ||
|
r5603 | except IOError, inst: | ||
|
r5993 | msg = inst.strerror | ||
raise ErrorResponse(HTTP_SERVER_ERROR, msg) | ||||
|
r7637 | except error.RepoError, inst: | ||
|
r5993 | raise ErrorResponse(HTTP_SERVER_ERROR, str(inst)) | ||
|
r5601 | |||
|
r13066 | up = virtualrepo.rfind('/') | ||
|
r5603 | if up < 0: | ||
break | ||||
|
r13066 | virtualrepo = virtualrepo[:up] | ||
# browse subdirectories | ||||
subdir = virtual + '/' | ||||
if [r for r in repos if r.startswith(subdir)]: | ||||
req.respond(HTTP_OK, ctype) | ||||
return self.makeindex(req, tmpl, subdir) | ||||
|
r5603 | |||
# prefixes not found | ||||
|
r5993 | req.respond(HTTP_NOT_FOUND, ctype) | ||
|
r7396 | return tmpl("notfound", repo=virtual) | ||
|
r5760 | |||
|
r5601 | except ErrorResponse, err: | ||
|
r7740 | req.respond(err, ctype) | ||
|
r7396 | return 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) | ||
|
r13436 | archives = [] | ||
|
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): | ||||
|
r13436 | archives.append({"type" : i[0], "extension": i[1], | ||
"node": nodeid, "url": url}) | ||||
return archives | ||||
|
r2356 | |||
|
r10600 | def rawentries(subdir="", **map): | ||
|
r9363 | |||
descend = self.ui.configbool('web', 'descend', True) | ||||
|
r16239 | collapse = self.ui.configbool('web', 'collapse', False) | ||
seenrepos = set() | ||||
seendirs = set() | ||||
|
r2356 | for name, path in self.repos: | ||
|
r9363 | |||
|
r4841 | if not name.startswith(subdir): | ||
continue | ||||
|
r4843 | name = name[len(subdir):] | ||
|
r16239 | directory = False | ||
if '/' in name: | ||||
if not descend: | ||||
continue | ||||
nameparts = name.split('/') | ||||
rootname = nameparts[0] | ||||
if not collapse: | ||||
pass | ||||
elif rootname in seendirs: | ||||
continue | ||||
elif rootname in seenrepos: | ||||
pass | ||||
else: | ||||
directory = True | ||||
name = rootname | ||||
# redefine the path to refer to the directory | ||||
discarded = '/'.join(nameparts[1:]) | ||||
# remove name parts plus accompanying slash | ||||
path = path[:-len(discarded) - 1] | ||||
parts = [name] | ||||
if 'PATH_INFO' in req.env: | ||||
parts.insert(0, req.env['PATH_INFO'].rstrip('/')) | ||||
if req.env['SCRIPT_NAME']: | ||||
parts.insert(0, req.env['SCRIPT_NAME']) | ||||
url = re.sub(r'/+', '/', '/'.join(parts) + '/') | ||||
# show either a directory entry or a repository | ||||
if directory: | ||||
# get the directory's time information | ||||
try: | ||||
d = (get_mtime(path), util.makedate()[1]) | ||||
except OSError: | ||||
continue | ||||
|
r17838 | # add '/' to the name to make it obvious that | ||
# the entry is a directory, not a regular repository | ||||
|
r20677 | row = {'contact': "", | ||
'contact_sort': "", | ||||
'name': name + '/', | ||||
'name_sort': name, | ||||
'url': url, | ||||
'description': "", | ||||
'description_sort': "", | ||||
'lastchange': d, | ||||
'lastchange_sort': d[1]-d[0], | ||||
'archives': [], | ||||
'isdirectory': True} | ||||
|
r16239 | |||
seendirs.add(name) | ||||
yield row | ||||
|
r9363 | continue | ||
|
r4841 | |||
|
r8191 | u = self.ui.copy() | ||
|
r2356 | try: | ||
u.readconfig(os.path.join(path, '.hg', 'hgrc')) | ||||
|
r5332 | except Exception, e: | ||
|
r6913 | u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e)) | ||
|
r5332 | 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 | ||||
|
r7336 | if not self.read_allowed(u, req): | ||
continue | ||||
|
r2356 | # update time with local timezone | ||
try: | ||||
|
r10078 | r = hg.repository(self.ui, path) | ||
|
r13796 | except IOError: | ||
u.warn(_('error accessing repository at %s\n') % path) | ||||
continue | ||||
|
r12038 | except error.RepoError: | ||
u.warn(_('error accessing repository at %s\n') % path) | ||||
continue | ||||
try: | ||||
|
r10078 | d = (get_mtime(r.spath), util.makedate()[1]) | ||
|
r2356 | except OSError: | ||
continue | ||||
|
r5779 | contact = get_contact(get) | ||
|
r2356 | description = get("web", "description", "") | ||
name = get("web", "name", name) | ||||
|
r20677 | row = {'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], | ||||
'archives': archivelist(u, "tip", url), | ||||
'isdirectory': None, | ||||
} | ||||
|
r16239 | |||
seenrepos.add(name) | ||||
|
r10600 | yield row | ||
sortdefault = None, False | ||||
def entries(sortcolumn="", descending=False, subdir="", **map): | ||||
rows = rawentries(subdir=subdir, **map) | ||||
if sortcolumn and sortdefault != (sortcolumn, descending): | ||||
sortkey = '%s_sort' % sortcolumn | ||||
rows = sorted(rows, key=lambda x: x[sortkey], | ||||
reverse=descending) | ||||
for row, parity in zip(rows, paritygen(self.stripecount)): | ||||
row['parity'] = parity | ||||
yield row | ||||
|
r2356 | |||
|
r8371 | self.refresh() | ||
|
r5601 | sortable = ["name", "description", "contact", "lastchange"] | ||
|
r8346 | sortcolumn, descending = sortdefault | ||
|
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 | |||
|
r8371 | self.refresh() | ||
|
r10673 | self.updatereqenv(req.env) | ||
|
r6221 | |||
|
r5965 | return tmpl("index", entries=entries, subdir=subdir, | ||
|
r18515 | pathdef=makebreadcrumb('/' + subdir, self.prefix), | ||
|
r5965 | sortcolumn=sortcolumn, descending=descending, | ||
**dict(sort)) | ||||
|
r5602 | |||
def templater(self, req): | ||||
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): | ||||
|
r8191 | return self.ui.config(section, name, default, untrusted) | ||
|
r5602 | |||
|
r10673 | self.updatereqenv(req.env) | ||
|
r6221 | |||
|
r5602 | url = req.env.get('SCRIPT_NAME', '') | ||
if not url.endswith('/'): | ||||
url += '/' | ||||
|
r8216 | vars = {} | ||
|
r9842 | styles = ( | ||
req.form.get('style', [None])[0], | ||||
config('web', 'style'), | ||||
'paper' | ||||
) | ||||
|
r11649 | style, mapfile = templater.stylemap(styles, self.templatepath) | ||
|
r9842 | if style == styles[0]: | ||
vars['style'] = style | ||||
|
r10282 | |||
|
r8216 | start = url[-1] == '?' and '&' or '?' | ||
sessionvars = webutil.sessionvars(vars, start) | ||||
|
r13964 | logourl = config('web', 'logourl', 'http://mercurial.selenic.com/') | ||
|
r14913 | logoimg = config('web', 'logoimg', 'hglogo.png') | ||
|
r5602 | staticurl = config('web', 'staticurl') or url + 'static/' | ||
if not staticurl.endswith('/'): | ||||
staticurl += '/' | ||||
|
r8360 | tmpl = templater.templater(mapfile, | ||
|
r19906 | defaults={"encoding": encoding.encoding, | ||
|
r5602 | "motd": motd, | ||
"url": url, | ||||
|
r13964 | "logourl": logourl, | ||
|
r14913 | "logoimg": logoimg, | ||
|
r8216 | "staticurl": staticurl, | ||
|
r20253 | "sessionvars": sessionvars, | ||
"style": style, | ||||
}) | ||||
|
r5602 | return tmpl | ||
|
r10673 | |||
def updatereqenv(self, env): | ||||
if self._baseurl is not None: | ||||
|
r15003 | name, port, path = geturlcgivars(self._baseurl, env['SERVER_PORT']) | ||
env['SERVER_NAME'] = name | ||||
env['SERVER_PORT'] = port | ||||
|
r15001 | env['SCRIPT_NAME'] = path | ||