mercurial/hgweb/__init__.py
author Yuya Nishihara <yuya@tcha.org>
Sat, 15 Oct 2016 14:06:46 +0900
changeset 30508 9195bc4cb816
parent 27184 64187e9a5659
child 30509 add7bcad1d9c
permissions -rw-r--r--
hgweb: extract app factory I'll move createservice() to the server module, but createapp() seems good to remain in the hgweb module because of its dependency on hgweb/hgwebdir_mod.
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: 2356
diff changeset
     1
# hgweb/__init__.py - web interface to a mercurial repository
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>
575
7f5ce4bbdd7b More whitespace cleanups
mpm@selenic.com
parents: 572
diff changeset
     4
# Copyright 2005 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: 3877
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 8225
diff changeset
     7
# GNU General Public License version 2 or any later version.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
     9
from __future__ import absolute_import
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    10
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    11
import os
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    12
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    13
from ..i18n import _
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    14
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    15
from .. import (
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    16
    error,
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    17
    util,
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    18
)
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    19
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    20
from . import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    21
    hgweb_mod,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    22
    hgwebdir_mod,
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    23
    server,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    24
)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    25
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    26
def hgweb(config, name=None, baseui=None):
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    27
    '''create an hgweb wsgi object
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    28
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    29
    config can be one of:
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    30
    - repo object (single repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    31
    - path to repo (single repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    32
    - path to config file (multi-repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    33
    - dict of virtual:real pairs (multi-repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    34
    - list of virtual:real tuples (multi-repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    35
    '''
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    36
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    37
    if ((isinstance(config, str) and not os.path.isdir(config)) or
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    38
        isinstance(config, dict) or isinstance(config, list)):
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    39
        # create a multi-dir interface
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    40
        return hgwebdir_mod.hgwebdir(config, baseui=baseui)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    41
    return hgweb_mod.hgweb(config, name=name, baseui=baseui)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    42
10992
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    43
def hgwebdir(config, baseui=None):
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    44
    return hgwebdir_mod.hgwebdir(config, baseui=baseui)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    45
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    46
class httpservice(object):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    47
    def __init__(self, ui, app, opts):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    48
        self.ui = ui
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    49
        self.app = app
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    50
        self.opts = opts
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    51
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    52
    def init(self):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    53
        util.setsignalhandler()
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    54
        self.httpd = server.create_server(self.ui, self.app)
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    55
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    56
        if self.opts['port'] and not self.ui.verbose:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    57
            return
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    58
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    59
        if self.httpd.prefix:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    60
            prefix = self.httpd.prefix.strip('/') + '/'
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    61
        else:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    62
            prefix = ''
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    63
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    64
        port = ':%d' % self.httpd.port
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    65
        if port == ':80':
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    66
            port = ''
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    67
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    68
        bindaddr = self.httpd.addr
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    69
        if bindaddr == '0.0.0.0':
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    70
            bindaddr = '*'
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    71
        elif ':' in bindaddr: # IPv6
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    72
            bindaddr = '[%s]' % bindaddr
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    73
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    74
        fqaddr = self.httpd.fqaddr
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    75
        if ':' in fqaddr:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    76
            fqaddr = '[%s]' % fqaddr
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    77
        if self.opts['port']:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    78
            write = self.ui.status
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    79
        else:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    80
            write = self.ui.write
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    81
        write(_('listening at http://%s%s/%s (bound to %s:%d)\n') %
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    82
              (fqaddr, port, prefix, bindaddr, self.httpd.port))
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    83
        self.ui.flush()  # avoid buffering of status message
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    84
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    85
    def run(self):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    86
        self.httpd.serve_forever()
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    87
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    88
def createservice(ui, repo, opts):
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    89
    # this way we can check if something was given in the command-line
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    90
    if opts.get('port'):
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    91
        opts['port'] = util.getport(opts.get('port'))
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    92
27182
71aa5a26162d hgweb: make sure command options are set to all ui objects
Yuya Nishihara <yuya@tcha.org>
parents: 27181
diff changeset
    93
    alluis = set([ui])
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    94
    if repo:
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    95
        baseui = repo.baseui
27182
71aa5a26162d hgweb: make sure command options are set to all ui objects
Yuya Nishihara <yuya@tcha.org>
parents: 27181
diff changeset
    96
        alluis.update([repo.baseui, repo.ui])
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    97
    else:
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    98
        baseui = ui
27184
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
    99
    webconf = opts.get('web_conf') or opts.get('webdir_conf')
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   100
    if webconf:
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   101
        # load server settings (e.g. web.port) to "copied" ui, which allows
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   102
        # hgwebdir to reload webconf cleanly
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   103
        servui = ui.copy()
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   104
        servui.readconfig(webconf, sections=['web'])
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   105
        alluis.add(servui)
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   106
    else:
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   107
        servui = ui
64187e9a5659 hgweb: load server settings from --web-conf (issue4699)
Yuya Nishihara <yuya@tcha.org>
parents: 27182
diff changeset
   108
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   109
    optlist = ("name templates style address port prefix ipv6"
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   110
               " accesslog errorlog certificate encoding")
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   111
    for o in optlist.split():
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   112
        val = opts.get(o, '')
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   113
        if val in (None, ''): # should check against default options instead
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   114
            continue
27182
71aa5a26162d hgweb: make sure command options are set to all ui objects
Yuya Nishihara <yuya@tcha.org>
parents: 27181
diff changeset
   115
        for u in alluis:
71aa5a26162d hgweb: make sure command options are set to all ui objects
Yuya Nishihara <yuya@tcha.org>
parents: 27181
diff changeset
   116
            u.setconfig("web", o, val, 'serve')
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   117
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   118
    app = createapp(baseui, repo, webconf)
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   119
    return httpservice(servui, app, opts)
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   120
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   121
def createapp(baseui, repo, webconf):
27181
a9cecc7b68d3 hgweb: eliminate duck-typing to select hgweb or hgwebdir by command option
Yuya Nishihara <yuya@tcha.org>
parents: 27139
diff changeset
   122
    if webconf:
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   123
        return hgwebdir_mod.hgwebdir(webconf, baseui=baseui)
27181
a9cecc7b68d3 hgweb: eliminate duck-typing to select hgweb or hgwebdir by command option
Yuya Nishihara <yuya@tcha.org>
parents: 27139
diff changeset
   124
    else:
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   125
        if not repo:
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   126
            raise error.RepoError(_("there is no Mercurial repository"
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   127
                                    " here (.hg not found)"))
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   128
        return hgweb_mod.hgweb(repo, baseui=baseui)