# HG changeset patch # User Dirkjan Ochtman # Date 2007-12-03 17:40:37 # Node ID 8279cb8414676293b09558a50fd1406d3b9c03d2 # Parent 9d900f7282e60a1bdbc9a323e92039e390ed11c7 hgwebdir: split out makeindex function, facilitate test failure diagnosis diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py +++ b/mercurial/hgweb/hgwebdir_mod.py @@ -20,7 +20,8 @@ class hgwebdir(object): return [(util.pconvert(name).strip('/'), path) for name, path in items] - self.parentui = parentui + self.parentui = parentui or ui.ui(report_untrusted=False, + interactive = False) self.motd = None self.style = None self.stripecount = None @@ -85,11 +86,8 @@ class hgwebdir(object): else: yield config('web', 'motd', '') - parentui = self.parentui or ui.ui(report_untrusted=False, - interactive=False) - def config(section, name, default=None, untrusted=True): - return parentui.config(section, name, default, untrusted) + return self.parentui.config(section, name, default, untrusted) url = req.env.get('SCRIPT_NAME', '') if not url.endswith('/'): @@ -114,6 +112,54 @@ class hgwebdir(object): "url": url, "staticurl": staticurl}) + try: + try: + virtual = req.env.get("PATH_INFO", "").strip('/') + if virtual.startswith('static/'): + static = os.path.join(templater.templatepath(), 'static') + fname = virtual[7:] + req.write(staticfile(static, fname, req)) + elif virtual: + 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) + return + except IOError, inst: + raise ErrorResponse(500, inst.strerror) + except hg.RepoError, inst: + raise ErrorResponse(500, str(inst)) + + # browse subdirectories + subdir = virtual + '/' + if [r for r in repos if r.startswith(subdir)]: + self.makeindex(req, tmpl, subdir) + return + + up = virtual.rfind('/') + if up < 0: + break + virtual = virtual[:up] + + req.respond(404, tmpl("notfound", repo=virtual)) + else: + if req.form.has_key('static'): + static = os.path.join(templater.templatepath(), "static") + fname = req.form['static'][0] + req.write(staticfile(static, fname, req)) + else: + self.makeindex(req, tmpl) + except ErrorResponse, err: + req.respond(err.code, tmpl('error', error=err.message or '')) + finally: + tmpl = None + + def makeindex(self, req, tmpl, subdir=""): + def archivelist(ui, nodeid, url): allowed = ui.configlist("web", "allow_archive", untrusted=True) for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: @@ -142,7 +188,7 @@ class hgwebdir(object): continue name = name[len(subdir):] - u = ui.ui(parentui=parentui) + u = ui.ui(parentui=self.parentui) try: u.readconfig(os.path.join(path, '.hg', 'hgrc')) except Exception, e: @@ -196,67 +242,20 @@ class hgwebdir(object): row['parity'] = parity.next() yield row - def makeindex(req, subdir=""): - sortable = ["name", "description", "contact", "lastchange"] - sortcolumn, descending = self.repos_sorted - if req.form.has_key('sort'): - sortcolumn = req.form['sort'][0] - descending = sortcolumn.startswith('-') - if descending: - sortcolumn = sortcolumn[1:] - if sortcolumn not in sortable: - sortcolumn = "" - - sort = [("sort_%s" % column, - "%s%s" % ((not descending and column == sortcolumn) - and "-" or "", column)) - for column in sortable] - req.write(tmpl("index", entries=entries, subdir=subdir, - sortcolumn=sortcolumn, descending=descending, - **dict(sort))) + sortable = ["name", "description", "contact", "lastchange"] + sortcolumn, descending = self.repos_sorted + if req.form.has_key('sort'): + sortcolumn = req.form['sort'][0] + descending = sortcolumn.startswith('-') + if descending: + sortcolumn = sortcolumn[1:] + if sortcolumn not in sortable: + sortcolumn = "" - try: - try: - virtual = req.env.get("PATH_INFO", "").strip('/') - if virtual.startswith('static/'): - static = os.path.join(templater.templatepath(), 'static') - fname = virtual[7:] - req.write(staticfile(static, fname, req)) - elif virtual: - repos = dict(self.repos) - while virtual: - real = repos.get(virtual) - if real: - req.env['REPO_NAME'] = virtual - try: - repo = hg.repository(parentui, real) - hgweb(repo).run_wsgi(req) - return - except IOError, inst: - raise ErrorResponse(500, inst.strerror) - except hg.RepoError, inst: - raise ErrorResponse(500, str(inst)) - - # browse subdirectories - subdir = virtual + '/' - if [r for r in repos if r.startswith(subdir)]: - makeindex(req, subdir) - return - - up = virtual.rfind('/') - if up < 0: - break - virtual = virtual[:up] - - req.respond(404, tmpl("notfound", repo=virtual)) - else: - if req.form.has_key('static'): - static = os.path.join(templater.templatepath(), "static") - fname = req.form['static'][0] - req.write(staticfile(static, fname, req)) - else: - makeindex(req) - except ErrorResponse, err: - req.respond(err.code, tmpl('error', error=err.message or '')) - finally: - tmpl = None + sort = [("sort_%s" % column, + "%s%s" % ((not descending and column == sortcolumn) + and "-" or "", column)) + for column in sortable] + req.write(tmpl("index", entries=entries, subdir=subdir, + sortcolumn=sortcolumn, descending=descending, + **dict(sort))) diff --git a/tests/test-hgwebdir b/tests/test-hgwebdir --- a/tests/test-hgwebdir +++ b/tests/test-hgwebdir @@ -27,7 +27,7 @@ b=$root/b EOF hg serve -p $HGPORT -d --pid-file=hg.pid --webdir-conf paths.conf \ - -A access-paths.log -E error-paths.log + -A access-paths.log -E error-paths-1.log cat hg.pid >> $DAEMON_PIDS echo % should give a 404 - file does not exist @@ -48,7 +48,7 @@ b=$root/b EOF hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ - -A access-paths.log -E error-paths.log + -A access-paths.log -E error-paths-2.log cat hg.pid >> $DAEMON_PIDS echo % should succeed, slashy names @@ -75,3 +75,10 @@ echo % should succeed "$TESTDIR/get-with-headers.py" localhost:$HGPORT2 '/a/file/tip/a?style=raw' "$TESTDIR/get-with-headers.py" localhost:$HGPORT2 '/b/file/tip/b?style=raw' "$TESTDIR/get-with-headers.py" localhost:$HGPORT2 '/c/file/tip/c?style=raw' + +echo % paths errors 1 +cat error-paths-1.log +echo % paths errors 2 +cat error-paths-2.log +echo % collections errors +cat error-collections.log diff --git a/tests/test-hgwebdir.out b/tests/test-hgwebdir.out --- a/tests/test-hgwebdir.out +++ b/tests/test-hgwebdir.out @@ -119,3 +119,6 @@ b 200 Script output follows c +% paths errors 1 +% paths errors 2 +% collections errors