mercurial/hgweb/hgwebdir_mod.py
author Thomas Arendsen Hein <thomas@intevation.de>
Thu, 12 Oct 2006 17:06:51 +0200
changeset 3365 cf680c9ab1dd
parent 3328 415905fad4fe
child 3425 ec6f400cff4d
permissions -rw-r--r--
Keep session variables when linking from hgwebdir's index to repositories.
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
#
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     6
# This software may be used and distributed according to the terms
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     7
# of the GNU General Public License, incorporated herein by reference.
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
2356
2db831b33e8f Final stage of the hgweb split up.
Eric Hopper <hopper@omnifarious.org>
parents: 2355
diff changeset
     9
import os
2311
b832b6eb65ab Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents: 2275
diff changeset
    10
from mercurial.demandload import demandload
2514
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
    11
demandload(globals(), "ConfigParser mimetools cStringIO")
2356
2db831b33e8f Final stage of the hgweb split up.
Eric Hopper <hopper@omnifarious.org>
parents: 2355
diff changeset
    12
demandload(globals(), "mercurial:ui,hg,util,templater")
2360
25ec4981883e hgweb: fix tracebacks on both index and repo pages
TK Soh <teekaysoh@yahoo.com>
parents: 2359
diff changeset
    13
demandload(globals(), "mercurial.hgweb.hgweb_mod:hgweb")
3276
db9d2a624521 hgweb: Search templates in templatepath/style/map, too, using a common function.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3263
diff changeset
    14
demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile,style_map")
2311
b832b6eb65ab Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents: 2275
diff changeset
    15
from mercurial.i18n import gettext as _
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    16
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    17
# This is a stopgap
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1554
diff changeset
    18
class hgwebdir(object):
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    19
    def __init__(self, config):
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
    20
        def cleannames(items):
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    21
            return [(name.strip(os.sep), path) for name, path in items]
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
    22
2148
c72e618c1204 Add MOTD display to hgweb and hgwebdir.
Colin McMillen <mcmillen@cs.cmu.edu>
parents: 2127
diff changeset
    23
        self.motd = ""
3221
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    24
        self.style = ""
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
    25
        self.repos_sorted = ('name', False)
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    26
        if isinstance(config, (list, tuple)):
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
    27
            self.repos = cleannames(config)
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
    28
            self.repos_sorted = ('', False)
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    29
        elif isinstance(config, dict):
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
            self.repos = cleannames(config.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
    31
            self.repos.sort()
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
    32
        else:
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
    33
            cp = ConfigParser.SafeConfigParser()
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
    34
            cp.read(config)
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    35
            self.repos = []
3221
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    36
            if cp.has_section('web'):
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    37
                if cp.has_option('web', 'motd'):
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    38
                    self.motd = cp.get('web', 'motd')
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    39
                if cp.has_option('web', 'style'):
3223
53e843840349 Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3221
diff changeset
    40
                    self.style = cp.get('web', 'style')
1829
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    41
            if cp.has_section('paths'):
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    42
                self.repos.extend(cleannames(cp.items('paths')))
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    43
            if cp.has_section('collections'):
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    44
                for prefix, root in cp.items('collections'):
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    45
                    for path in util.walkrepos(root):
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    46
                        repo = os.path.normpath(path)
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    47
                        name = repo
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    48
                        if name.startswith(prefix):
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    49
                            name = name[len(prefix):]
b0f6af327fd4 hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1703
diff changeset
    50
                        self.repos.append((name.lstrip(os.sep), repo))
1143
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
    51
            self.repos.sort()
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    52
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    53
    def run(self):
2538
f4b7d71c1c60 Cleanup hgweb and hgwebdir's run method a bit.
Eric Hopper <hopper@omnifarious.org>
parents: 2537
diff changeset
    54
        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
    55
            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
    56
        import mercurial.hgweb.wsgicgi as wsgicgi
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    57
        from request import wsgiapplication
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    58
        def make_web_app():
2538
f4b7d71c1c60 Cleanup hgweb and hgwebdir's run method a bit.
Eric Hopper <hopper@omnifarious.org>
parents: 2537
diff changeset
    59
            return self
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    60
        wsgicgi.launch(wsgiapplication(make_web_app))
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    61
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
    62
    def run_wsgi(self, req):
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    63
        def header(**map):
2514
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
    64
            header_file = cStringIO.StringIO(''.join(tmpl("header", **map)))
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
    65
            msg = mimetools.Message(header_file, 0)
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
    66
            req.header(msg.items())
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
    67
            yield header_file.read()
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    68
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    69
        def footer(**map):
2148
c72e618c1204 Add MOTD display to hgweb and hgwebdir.
Colin McMillen <mcmillen@cs.cmu.edu>
parents: 2127
diff changeset
    70
            yield tmpl("footer", motd=self.motd, **map)
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    71
3328
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    72
        url = req.env['REQUEST_URI'].split('?')[0]
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    73
        if not url.endswith('/'):
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    74
            url += '/'
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    75
3221
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    76
        style = self.style
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    77
        if req.form.has_key('style'):
d7d53e3d9590 Add style support to hgwebdir
Edouard Gomez <ed.gomez@free.fr>
parents: 2859
diff changeset
    78
            style = req.form['style'][0]
3276
db9d2a624521 hgweb: Search templates in templatepath/style/map, too, using a common function.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3263
diff changeset
    79
        mapfile = style_map(templater.templatepath(), style)
db9d2a624521 hgweb: Search templates in templatepath/style/map, too, using a common function.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3263
diff changeset
    80
        tmpl = templater.templater(mapfile, templater.common_filters,
1964
778281d46bb2 fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1920
diff changeset
    81
                                   defaults={"header": header,
3328
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    82
                                             "footer": footer,
415905fad4fe Add base URL to hgwebdir templater (fixes index page when the URL does not have a trailing /)
Brendan Cully <brendan@kublai.com>
parents: 3276
diff changeset
    83
                                             "url": url})
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
    84
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
    85
        def archivelist(ui, nodeid, url):
2500
76ff5efe8181 Fixed [web] allow_archive for comma separated parameters by using ui.configlist.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2391
diff changeset
    86
            allowed = ui.configlist("web", "allow_archive")
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
    87
            for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
    88
                if i[0] in allowed or ui.configbool("web", "allow" + i[0]):
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
    89
                    yield {"type" : i[0], "extension": i[1],
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
    90
                           "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
    91
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
    92
        def entries(sortcolumn="", descending=False, **map):
3365
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    93
            def sessionvars(**map):
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    94
                fields = []
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    95
                if req.form.has_key('style'):
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    96
                    style = req.form['style'][0]
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    97
                    if style != get('web', 'style', ''):
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    98
                        fields.append(('style', style))
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
    99
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   100
                separator = url[-1] == '?' and ';' or '?'
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   101
                for name, value in fields:
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   102
                    yield dict(name=name, value=value, separator=separator)
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   103
                    separator = ';'
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   104
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   105
            rows = []
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   106
            parity = 0
1141
033c968d7c66 Use ConfigParser only in hgwebdir.__init__()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1140
diff changeset
   107
            for name, path in self.repos:
1213
db9639b8594c Clean up hgweb imports
mpm@selenic.com
parents: 1210
diff changeset
   108
                u = ui.ui()
1170
85555540a4e2 Make .hg/hgrc optional for repositories published by hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1165
diff changeset
   109
                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
   110
                    u.readconfig(os.path.join(path, '.hg', 'hgrc'))
1170
85555540a4e2 Make .hg/hgrc optional for repositories published by hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1165
diff changeset
   111
                except IOError:
85555540a4e2 Make .hg/hgrc optional for repositories published by hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1165
diff changeset
   112
                    pass
1140
04d52b446e5e Don't create repo objects in hgwebdir, ui object is enough.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1139
diff changeset
   113
                get = u.config
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   114
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
   115
                url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name])
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   116
                       .replace("//", "/")) + '/'
1022
31dcaf9123ba Minor hgwebdir tweaks
mpm@selenic.com
parents: 987
diff changeset
   117
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   118
                # update time with local timezone
1524
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   119
                try:
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   120
                    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
   121
                except OSError:
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   122
                    continue
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   123
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   124
                contact = (get("ui", "username") or # preferred
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   125
                           get("web", "contact") or # deprecated
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   126
                           get("web", "author", "")) # also
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   127
                description = get("web", "description", "")
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   128
                name = get("web", "name", name)
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   129
                row = dict(contact=contact or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   130
                           contact_sort=contact.upper() or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   131
                           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
   132
                           name_sort=name,
1062
6d5a62a549fa pep-0008 cleanup
benoit.boissinot@ens-lyon.fr
parents: 1023
diff changeset
   133
                           url=url,
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   134
                           description=description or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   135
                           description_sort=description.upper() or "unknown",
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   136
                           lastchange=d,
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   137
                           lastchange_sort=d[1]-d[0],
3365
cf680c9ab1dd Keep session variables when linking from hgwebdir's index to repositories.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3328
diff changeset
   138
                           sessionvars=sessionvars,
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   139
                           archives=archivelist(u, "tip", url))
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   140
                if (not sortcolumn
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   141
                    or (sortcolumn, descending) == self.repos_sorted):
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   142
                    # fast path for unsorted output
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   143
                    row['parity'] = parity
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   144
                    parity = 1 - parity
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   145
                    yield row
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   146
                else:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   147
                    rows.append((row["%s_sort" % sortcolumn], row))
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   148
            if rows:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   149
                rows.sort()
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   150
                if descending:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   151
                    rows.reverse()
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   152
                for key, row in rows:
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   153
                    row['parity'] = parity
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   154
                    parity = 1 - parity
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   155
                    yield row
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   156
1159
b6f5a947e62e Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents: 1143
diff changeset
   157
        virtual = req.env.get("PATH_INFO", "").strip('/')
3263
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   158
        if virtual.startswith('static/'):
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   159
            static = os.path.join(templater.templatepath(), 'static')
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   160
            fname = virtual[7:]
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   161
            req.write(staticfile(static, fname, req) or
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   162
                      tmpl('error', error='%r not found' % fname))
3207e30bf468 hgweb: support for generating and parsing NWI URLs
Brendan Cully <brendan@kublai.com>
parents: 3262
diff changeset
   163
        elif virtual:
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   164
            while virtual:
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   165
                real = dict(self.repos).get(virtual)
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   166
                if real:
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   167
                    break
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   168
                up = virtual.rfind('/')
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   169
                if up < 0:
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   170
                    break
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   171
                virtual = virtual[:up]
1141
033c968d7c66 Use ConfigParser only in hgwebdir.__init__()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1140
diff changeset
   172
            if real:
3262
1e322b44b366 Teach hgwebdir about new interface
Brendan Cully <brendan@kublai.com>
parents: 3223
diff changeset
   173
                req.env['REPO_NAME'] = virtual
1554
68ec7b9e09a4 Catch IOErrors and RepoErrors when serving repositories via hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1545
diff changeset
   174
                try:
2537
b6975008d44f Fix hgwebdir to run hgweb using run_wsgi.
Eric Hopper <hopper@omnifarious.org>
parents: 2535
diff changeset
   175
                    hgweb(real).run_wsgi(req)
1554
68ec7b9e09a4 Catch IOErrors and RepoErrors when serving repositories via hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1545
diff changeset
   176
                except IOError, inst:
68ec7b9e09a4 Catch IOErrors and RepoErrors when serving repositories via hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1545
diff changeset
   177
                    req.write(tmpl("error", error=inst.strerror))
68ec7b9e09a4 Catch IOErrors and RepoErrors when serving repositories via hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1545
diff changeset
   178
                except hg.RepoError, inst:
68ec7b9e09a4 Catch IOErrors and RepoErrors when serving repositories via hgweb.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1545
diff changeset
   179
                    req.write(tmpl("error", error=str(inst)))
1123
457c23af92bd Use a template for the error message.
Ollivier Robert <roberto@keltia.freenix.fr>
parents: 1122
diff changeset
   180
            else:
1159
b6f5a947e62e Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents: 1143
diff changeset
   181
                req.write(tmpl("notfound", repo=virtual))
1142
74d184a40a2e Cleaned up hgweb.hgwebdir.run()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1141
diff changeset
   182
        else:
1793
83c6d8355909 Allow serving static files from hgwebdir to fix CSS and favicon.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1792
diff changeset
   183
            if req.form.has_key('static'):
1897
58b6784cf9f1 move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1896
diff changeset
   184
                static = os.path.join(templater.templatepath(), "static")
1793
83c6d8355909 Allow serving static files from hgwebdir to fix CSS and favicon.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1792
diff changeset
   185
                fname = req.form['static'][0]
2514
419c42223bee Really fix http headers for web UI and issue 254.
Eric Hopper <hopper@omnifarious.org>
parents: 2509
diff changeset
   186
                req.write(staticfile(static, fname, req)
1793
83c6d8355909 Allow serving static files from hgwebdir to fix CSS and favicon.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1792
diff changeset
   187
                          or tmpl("error", error="%r not found" % fname))
83c6d8355909 Allow serving static files from hgwebdir to fix CSS and favicon.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1792
diff changeset
   188
            else:
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   189
                sortable = ["name", "description", "contact", "lastchange"]
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   190
                sortcolumn, descending = self.repos_sorted
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   191
                if req.form.has_key('sort'):
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   192
                    sortcolumn = req.form['sort'][0]
2174
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   193
                    descending = sortcolumn.startswith('-')
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   194
                    if descending:
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   195
                        sortcolumn = sortcolumn[1:]
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   196
                    if sortcolumn not in sortable:
3044a3fdae76 If default sorting is name, offer name-descending with one click.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2173
diff changeset
   197
                        sortcolumn = ""
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   198
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   199
                sort = [("sort_%s" % column,
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   200
                         "%s%s" % ((not descending and column == sortcolumn)
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   201
                                   and "-" or "", column))
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   202
                        for column in sortable]
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   203
                req.write(tmpl("index", entries=entries,
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   204
                               sortcolumn=sortcolumn, descending=descending,
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   205
                               **dict(sort)))