changeset 5601:8279cb841467

hgwebdir: split out makeindex function, facilitate test failure diagnosis
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Mon, 03 Dec 2007 18:40:37 +0100
parents 9d900f7282e6
children d676d0f35bd8
files mercurial/hgweb/hgwebdir_mod.py tests/test-hgwebdir tests/test-hgwebdir.out
diffstat 3 files changed, 80 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/hgwebdir_mod.py	Mon Dec 03 13:30:08 2007 +0100
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon Dec 03 18:40:37 2007 +0100
@@ -20,7 +20,8 @@
             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 @@
             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 @@
                                              "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 @@
                     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 @@
                     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)))
--- a/tests/test-hgwebdir	Mon Dec 03 13:30:08 2007 +0100
+++ b/tests/test-hgwebdir	Mon Dec 03 18:40:37 2007 +0100
@@ -27,7 +27,7 @@
 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 @@
 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 @@
 "$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
--- a/tests/test-hgwebdir.out	Mon Dec 03 13:30:08 2007 +0100
+++ b/tests/test-hgwebdir.out	Mon Dec 03 18:40:37 2007 +0100
@@ -119,3 +119,6 @@
 200 Script output follows
 
 c
+% paths errors 1
+% paths errors 2
+% collections errors