mercurial/hgweb/hgwebdir_mod.py
author Bryan O'Sullivan <bos@serpentine.com>
Wed, 13 May 2009 13:30:28 -0700
changeset 8371 1bd0fdf4c1ec
parent 8360 acc202b71619
child 8389 4b798b100c32
permissions -rw-r--r--
hgwebdir: refresh configuration periodically The old default behaviour of hgwebdir was to maintain a list of repositories permanently. This interacted badly with persistent application hosting software such as WSGI containers. If a new repository was published, it would potentially never appear in the top-level list of repositories. This change causes the hgwebdir configuration and list of repositories served to be refreshed periodically (at most every 20 seconds).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2391
d351a3be3371 Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents: 2360
diff changeset
     1
# hgweb/hgwebdir_mod.py - Web interface for a directory of repositories.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     2
#
238
3b92f8fe47ae hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents: 222
diff changeset
     3
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2538
diff changeset
     4
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8216
diff changeset
     6
# This software may be used and distributed according to the terms of the
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8216
diff changeset
     7
# GNU General Public License version 2, incorporated herein by reference.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
     9
import os, time
7225
59b4ae211584 i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents: 7107
diff changeset
    10
from mercurial.i18n import _
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8359
diff changeset
    11
from mercurial import ui, hg, util, templater
8345
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    12
from mercurial import error, encoding
7966
aa983c3d94a9 templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
    13
from common import ErrorResponse, get_mtime, staticfile, paritygen,\
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
    14
                   get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3557
diff changeset
    15
from hgweb_mod import hgweb
5566
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
    16
from request import wsgirequest
8216
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
    17
import webutil
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    18
8215
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    19
def cleannames(items):
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    20
    return [(util.pconvert(name).strip('/'), path) for name, path in items]
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    21
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1554
diff changeset
    22
class hgwebdir(object):
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    23
    refreshinterval = 20
8215
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    24
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
    25
    def __init__(self, conf, baseui=None):
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    26
        self.conf = conf
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    27
        self.baseui = baseui
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    28
        self.lastrefresh = 0
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    29
        self.refresh()
1181
4f5001f5b4c3 Make sure the repository names don't have slashes at the at or else in some
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents: 1180
diff changeset
    30
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    31
    def refresh(self):
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    32
        if self.lastrefresh + self.refreshinterval > time.time():
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    33
            return
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    34
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    35
        if self.baseui:
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    36
            self.ui = self.baseui.copy()
8136
6b5522cb2ad2 ui: refactor option setting
Matt Mackall <mpm@selenic.com>
parents: 7966
diff changeset
    37
        else:
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
    38
            self.ui = ui.ui()
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
    39
            self.ui.setconfig('ui', 'report_untrusted', 'off')
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
    40
            self.ui.setconfig('ui', 'interactive', 'off')
8136
6b5522cb2ad2 ui: refactor option setting
Matt Mackall <mpm@selenic.com>
parents: 7966
diff changeset
    41
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    42
        if isinstance(self.conf, (list, tuple)):
8180
6fc30fe7f3e7 hgweb: use config.config
Matt Mackall <mpm@selenic.com>
parents: 8136
diff changeset
    43
            self.repos = cleannames(conf)
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    44
        elif isinstance(self.conf, dict):
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    45
            self.repos = sorted(cleannames(self.conf.items()))
1143
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
    46
        else:
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    47
            self.ui.readconfig(self.conf, remap={'paths': 'hgweb-paths'}, trust=True)
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    48
            self.repos = []
8345
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    49
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    50
        self.motd = self.ui.config('web', 'motd')
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    51
        self.style = self.ui.config('web', 'style', 'paper')
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    52
        self.stripecount = self.ui.config('web', 'stripes', 1)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    53
        if self.stripecount:
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    54
            self.stripecount = int(self.stripecount)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    55
        self._baseurl = self.ui.config('web', 'baseurl')
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    56
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    57
        if self.repos:
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    58
            return
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    59
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    60
        for prefix, root in cleannames(self.ui.configitems('hgweb-paths')):
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    61
            roothead, roottail = os.path.split(root)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    62
            # "foo = /bar/*" makes every subrepo of /bar/ to be
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    63
            # mounted as foo/subrepo
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    64
            # and "foo = /bar/**" also recurses into the subdirectories,
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    65
            # remember to use it without working dir.
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    66
            try:
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    67
                recurse = {'*': False, '**': True}[roottail]
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    68
            except KeyError:
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    69
                self.repos.append((prefix, root))
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    70
                continue
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    71
            roothead = os.path.normpath(roothead)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    72
            for path in util.walkrepos(roothead, followsym=True,
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    73
                                       recurse=recurse):
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    74
                path = os.path.normpath(path)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    75
                name = util.pconvert(path[len(roothead):]).strip('/')
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    76
                if prefix:
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    77
                    name = prefix + '/' + name
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    78
                self.repos.append((name, path))
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    79
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    80
        for prefix, root in self.ui.configitems('collections'):
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    81
             for path in util.walkrepos(root, followsym=True):
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    82
                 repo = os.path.normpath(path)
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    83
                 name = repo
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    84
                 if name.startswith(prefix):
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    85
                     name = name[len(prefix):]
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    86
                 self.repos.append((name.lstrip(os.sep), repo))
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    87
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    88
        self.repos.sort()
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
    89
        self.lastrefresh = time.time()
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    90
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    91
    def run(self):
2538
f4b7d71c1c60 Cleanup hgweb and hgwebdir's run method a bit.
Eric Hopper <hopper@omnifarious.org>
parents: 2537
diff changeset
    92
        if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    93
            raise RuntimeError("This function is only intended to be called while running as a CGI script.")
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    94
        import mercurial.hgweb.wsgicgi as wsgicgi
5566
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
    95
        wsgicgi.launch(self)
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
    96
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
    97
    def __call__(self, env, respond):
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
    98
        req = wsgirequest(env, respond)
6797
8909070fd05e merge another backout
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6796
diff changeset
    99
        return self.run_wsgi(req)
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
   100
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   101
    def read_allowed(self, ui, req):
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   102
        """Check allow_read and deny_read config options of a repo's ui object
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   103
        to determine user permissions.  By default, with neither option set (or
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   104
        both empty), allow all users to read the repo.  There are two ways a
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   105
        user can be denied read access:  (1) deny_read is not empty, and the
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   106
        user is unauthenticated or deny_read contains user (or *), and (2)
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   107
        allow_read is not empty and the user is not in allow_read.  Return True
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   108
        if user is allowed to read the repo, else return False."""
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   109
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   110
        user = req.env.get('REMOTE_USER')
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   111
7575
3ccaefc84f45 hgweb: fix long line lengths introduced in 2dc868712dcc
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7560
diff changeset
   112
        deny_read = ui.configlist('web', 'deny_read', untrusted=True)
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   113
        if deny_read and (not user or deny_read == ['*'] or user in deny_read):
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   114
            return False
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   115
7575
3ccaefc84f45 hgweb: fix long line lengths introduced in 2dc868712dcc
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7560
diff changeset
   116
        allow_read = ui.configlist('web', 'allow_read', untrusted=True)
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   117
        # by default, allow reading if no allow_read option has been set
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   118
        if (not allow_read) or (allow_read == ['*']) or (user in allow_read):
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   119
            return True
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   120
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   121
        return False
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   122
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
   123
    def run_wsgi(self, req):
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   124
        try:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   125
            try:
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   126
                self.refresh()
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   127
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   128
                virtual = req.env.get("PATH_INFO", "").strip('/')
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   129
                tmpl = self.templater(req)
7948
de377b1a9a84 move encoding bits from util to encoding
Matt Mackall <mpm@selenic.com>
parents: 7740
diff changeset
   130
                ctype = tmpl('mimetype', encoding=encoding.encoding)
6391
a1007f7b9b7b Backed out changeset d2bb66a8a435 (temporary template compatibility)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6379
diff changeset
   131
                ctype = templater.stringify(ctype)
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5603
diff changeset
   132
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   133
                # a static file
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   134
                if virtual.startswith('static/') or 'static' in req.form:
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   135
                    if virtual.startswith('static/'):
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   136
                        fname = virtual[7:]
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   137
                    else:
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   138
                        fname = req.form['static'][0]
7107
125c8fedcbe0 Allow hgweb to search for templates in more than one path.
Brendan Cully <brendan@kublai.com>
parents: 6945
diff changeset
   139
                    static = templater.templatepath('static')
7560
305efd897a63 hgwebdir_mod: fix a performance issue with static files
Patrick Mezard <pmezard@gmail.com>
parents: 7523
diff changeset
   140
                    return (staticfile(static, fname, req),)
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   141
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   142
                # top-level index
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   143
                elif not virtual:
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   144
                    req.respond(HTTP_OK, ctype)
7396
526c40a74bd0 templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents: 7337
diff changeset
   145
                    return self.makeindex(req, tmpl)
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   146
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   147
                # nested indexes and hgwebs
6210
942287cb1f57 Removed trailing spaces from everything except test output
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6046
diff changeset
   148
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   149
                repos = dict(self.repos)
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   150
                while virtual:
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   151
                    real = repos.get(virtual)
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   152
                    if real:
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   153
                        req.env['REPO_NAME'] = virtual
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   154
                        try:
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   155
                            repo = hg.repository(self.ui, real)
6797
8909070fd05e merge another backout
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6796
diff changeset
   156
                            return hgweb(repo).run_wsgi(req)
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   157
                        except IOError, inst:
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   158
                            msg = inst.strerror
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   159
                            raise ErrorResponse(HTTP_SERVER_ERROR, msg)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7575
diff changeset
   160
                        except error.RepoError, inst:
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   161
                            raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   162
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   163
                    # browse subdirectories
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   164
                    subdir = virtual + '/'
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   165
                    if [r for r in repos if r.startswith(subdir)]:
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   166
                        req.respond(HTTP_OK, ctype)
7396
526c40a74bd0 templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents: 7337
diff changeset
   167
                        return self.makeindex(req, tmpl, subdir)
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   168
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   169
                    up = virtual.rfind('/')
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   170
                    if up < 0:
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   171
                        break
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   172
                    virtual = virtual[:up]
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   173
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   174
                # prefixes not found
5993
948a41e77902 hgweb: explicit response status
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5976
diff changeset
   175
                req.respond(HTTP_NOT_FOUND, ctype)
7396
526c40a74bd0 templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents: 7337
diff changeset
   176
                return tmpl("notfound", repo=virtual)
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5603
diff changeset
   177
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   178
            except ErrorResponse, err:
7740
176d3d681702 hgweb: pass ErrorResponses directly into req.respond()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
   179
                req.respond(err, ctype)
7396
526c40a74bd0 templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents: 7337
diff changeset
   180
                return tmpl('error', error=err.message or '')
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   181
        finally:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   182
            tmpl = None
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   183
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   184
    def makeindex(self, req, tmpl, subdir=""):
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   185
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   186
        def archivelist(ui, nodeid, url):
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   187
            allowed = ui.configlist("web", "allow_archive", untrusted=True)
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   188
            for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   189
                if i[0] in allowed or ui.configbool("web", "allow" + i[0],
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   190
                                                    untrusted=True):
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   191
                    yield {"type" : i[0], "extension": i[1],
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   192
                           "node": nodeid, "url": url}
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   193
8346
b579823cc546 hgweb: get rid of inaccurate hgwebdir.repos_sorted, localize machinery
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8345
diff changeset
   194
        sortdefault = 'name', False
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   195
        def entries(sortcolumn="", descending=False, subdir="", **map):
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   196
            rows = []
4462
12e4d9524951 hgweb: use generator to count parity of horizontal stripes for easier reading.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4250
diff changeset
   197
            parity = paritygen(self.stripecount)
1141
033c968d7c66 Use ConfigParser only in hgwebdir.__init__()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1140
diff changeset
   198
            for name, path in self.repos:
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   199
                if not name.startswith(subdir):
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   200
                    continue
4843
496ac05c6a31 hgwebdir: show only trailing part of path when browsing subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4841
diff changeset
   201
                name = name[len(subdir):]
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   202
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   203
                u = self.ui.copy()
1170
85555540a4e2 Make .hg/hgrc optional for repositories published by hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1165
diff changeset
   204
                try:
1473
7d66ce9895fa make readconfig take a filename instead of a file pointer as argument
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1445
diff changeset
   205
                    u.readconfig(os.path.join(path, '.hg', 'hgrc'))
5332
b0bfe087ad8a hgwebdir: ignore hgrc parse errors while building the index page
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5289
diff changeset
   206
                except Exception, e:
6913
580d5e6bfc1f move % out of translatable strings
Martin Geisler <mg@daimi.au.dk>
parents: 6908
diff changeset
   207
                    u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
5332
b0bfe087ad8a hgwebdir: ignore hgrc parse errors while building the index page
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5289
diff changeset
   208
                    continue
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   209
                def get(section, name, default=None):
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   210
                    return u.config(section, name, default, untrusted=True)
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   211
4709
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   212
                if u.configbool("web", "hidden", untrusted=True):
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   213
                    continue
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   214
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   215
                if not self.read_allowed(u, req):
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   216
                    continue
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   217
6459
8189e03adb44 hgweb: make hgwebdir work in the absence of PATH_INFO
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6379
diff changeset
   218
                parts = [name]
8189e03adb44 hgweb: make hgwebdir work in the absence of PATH_INFO
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6379
diff changeset
   219
                if 'PATH_INFO' in req.env:
8189e03adb44 hgweb: make hgwebdir work in the absence of PATH_INFO
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6379
diff changeset
   220
                    parts.insert(0, req.env['PATH_INFO'].rstrip('/'))
5579
e15f7db0f0ee Use SCRIPT_NAME and PATH_INFO instead of REQUEST_URI. This is required by WSGI (fixes issue846).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5566
diff changeset
   221
                if req.env['SCRIPT_NAME']:
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5603
diff changeset
   222
                    parts.insert(0, req.env['SCRIPT_NAME'])
5579
e15f7db0f0ee Use SCRIPT_NAME and PATH_INFO instead of REQUEST_URI. This is required by WSGI (fixes issue846).
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5566
diff changeset
   223
                url = ('/'.join(parts).replace("//", "/")) + '/'
1022
31dcaf9123ba Minor hgwebdir tweaks
mpm@selenic.com
parents: 987
diff changeset
   224
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   225
                # update time with local timezone
1524
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   226
                try:
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   227
                    d = (get_mtime(path), util.makedate()[1])
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   228
                except OSError:
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   229
                    continue
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   230
5779
e9f68860d5ed Don't let ui.username override web.contact (issue900)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5760
diff changeset
   231
                contact = get_contact(get)
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   232
                description = get("web", "description", "")
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   233
                name = get("web", "name", name)
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   234
                row = dict(contact=contact or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   235
                           contact_sort=contact.upper() or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   236
                           name=name,
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   237
                           name_sort=name,
1062
6d5a62a549fa pep-0008 cleanup
benoit.boissinot@ens-lyon.fr
parents: 1023
diff changeset
   238
                           url=url,
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   239
                           description=description or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   240
                           description_sort=description.upper() or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   241
                           lastchange=d,
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   242
                           lastchange_sort=d[1]-d[0],
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   243
                           archives=archivelist(u, "tip", url))
8346
b579823cc546 hgweb: get rid of inaccurate hgwebdir.repos_sorted, localize machinery
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8345
diff changeset
   244
                if (not sortcolumn or (sortcolumn, descending) == sortdefault):
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   245
                    # fast path for unsorted output
4462
12e4d9524951 hgweb: use generator to count parity of horizontal stripes for easier reading.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4250
diff changeset
   246
                    row['parity'] = parity.next()
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   247
                    yield row
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   248
                else:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   249
                    rows.append((row["%s_sort" % sortcolumn], row))
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   250
            if rows:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   251
                rows.sort()
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   252
                if descending:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   253
                    rows.reverse()
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   254
                for key, row in rows:
4462
12e4d9524951 hgweb: use generator to count parity of horizontal stripes for easier reading.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4250
diff changeset
   255
                    row['parity'] = parity.next()
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   256
                    yield row
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   257
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   258
        self.refresh()
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   259
        sortable = ["name", "description", "contact", "lastchange"]
8346
b579823cc546 hgweb: get rid of inaccurate hgwebdir.repos_sorted, localize machinery
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8345
diff changeset
   260
        sortcolumn, descending = sortdefault
5915
d0576d065993 Prefer i in d over d.has_key(i)
Christian Ebert <blacktrash@gmx.net>
parents: 5779
diff changeset
   261
        if 'sort' in req.form:
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   262
            sortcolumn = req.form['sort'][0]
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   263
            descending = sortcolumn.startswith('-')
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   264
            if descending:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   265
                sortcolumn = sortcolumn[1:]
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   266
            if sortcolumn not in sortable:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   267
                sortcolumn = ""
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   268
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   269
        sort = [("sort_%s" % column,
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   270
                 "%s%s" % ((not descending and column == sortcolumn)
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   271
                            and "-" or "", column))
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   272
                for column in sortable]
5928
3340aa5a64f7 hgweb: move HTTP content types out of header templates
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5915
diff changeset
   273
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   274
        self.refresh()
6221
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   275
        if self._baseurl is not None:
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   276
            req.env['SCRIPT_NAME'] = self._baseurl
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   277
5965
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   278
        return tmpl("index", entries=entries, subdir=subdir,
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   279
                    sortcolumn=sortcolumn, descending=descending,
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   280
                    **dict(sort))
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   281
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   282
    def templater(self, req):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   283
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   284
        def header(**map):
7948
de377b1a9a84 move encoding bits from util to encoding
Matt Mackall <mpm@selenic.com>
parents: 7740
diff changeset
   285
            yield tmpl('header', encoding=encoding.encoding, **map)
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   286
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   287
        def footer(**map):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   288
            yield tmpl("footer", **map)
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   289
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   290
        def motd(**map):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   291
            if self.motd is not None:
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   292
                yield self.motd
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   293
            else:
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   294
                yield config('web', 'motd', '')
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   295
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   296
        def config(section, name, default=None, untrusted=True):
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   297
            return self.ui.config(section, name, default, untrusted)
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   298
6221
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   299
        if self._baseurl is not None:
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   300
            req.env['SCRIPT_NAME'] = self._baseurl
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   301
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   302
        url = req.env.get('SCRIPT_NAME', '')
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   303
        if not url.endswith('/'):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   304
            url += '/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   305
8216
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   306
        vars = {}
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   307
        style = self.style
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   308
        if 'style' in req.form:
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   309
            vars['style'] = style = req.form['style'][0]
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   310
        start = url[-1] == '?' and '&' or '?'
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   311
        sessionvars = webutil.sessionvars(vars, start)
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   312
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   313
        staticurl = config('web', 'staticurl') or url + 'static/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   314
        if not staticurl.endswith('/'):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   315
            staticurl += '/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   316
8215
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
   317
        style = 'style' in req.form and req.form['style'][0] or self.style
7966
aa983c3d94a9 templater: move stylemap function from hgweb to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
   318
        mapfile = templater.stylemap(style)
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8359
diff changeset
   319
        tmpl = templater.templater(mapfile,
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   320
                                   defaults={"header": header,
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   321
                                             "footer": footer,
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   322
                                             "motd": motd,
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   323
                                             "url": url,
8216
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   324
                                             "staticurl": staticurl,
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   325
                                             "sessionvars": sessionvars})
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   326
        return tmpl