mercurial/hgweb/__init__.py
author Danny Hooper <hooper@google.com>
Wed, 22 May 2019 16:20:34 -0700
changeset 42385 b02f3aa2fab5
parent 42242 91104f10ff65
child 43076 2372284d9457
permissions -rw-r--r--
test: add missing 'cd ..' to test case Differential Revision: https://phab.mercurial-scm.org/D6439
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,
34704
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    17
    pycompat,
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    18
)
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    19
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    20
from ..utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    21
    procutil,
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    22
)
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    23
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    24
from . import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    25
    hgweb_mod,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    26
    hgwebdir_mod,
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    27
    server,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    28
)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    29
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    30
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
    31
    '''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
    32
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    33
    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
    34
    - 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
    35
    - 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
    36
    - 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
    37
    - 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
    38
    - 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
    39
    '''
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    40
42242
91104f10ff65 py3: properly reject non-encoded strings given to hgweb
Ludovic Chabant <ludovic@chabant.com>
parents: 38211
diff changeset
    41
    if isinstance(config, pycompat.unicode):
91104f10ff65 py3: properly reject non-encoded strings given to hgweb
Ludovic Chabant <ludovic@chabant.com>
parents: 38211
diff changeset
    42
        raise error.ProgrammingError(
91104f10ff65 py3: properly reject non-encoded strings given to hgweb
Ludovic Chabant <ludovic@chabant.com>
parents: 38211
diff changeset
    43
            'Mercurial only supports encoded strings: %r' % config)
37745
42567ffa10a2 hgweb: inform hgweb.hgweb() entrypoint that paths should be bytes
Augie Fackler <augie@google.com>
parents: 37123
diff changeset
    44
    if ((isinstance(config, bytes) and not os.path.isdir(config)) or
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    45
        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
    46
        # 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
    47
        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
    48
    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
    49
10992
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    50
def hgwebdir(config, baseui=None):
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    51
    return hgwebdir_mod.hgwebdir(config, baseui=baseui)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    52
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    53
class httpservice(object):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    54
    def __init__(self, ui, app, opts):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    55
        self.ui = ui
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    56
        self.app = app
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    57
        self.opts = opts
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
    def init(self):
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    60
        procutil.setsignalhandler()
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    61
        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
    62
38211
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    63
        if (self.opts['port'] and
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    64
            not self.ui.verbose and
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    65
            not self.opts['print_url']):
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    66
            return
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
        if self.httpd.prefix:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    69
            prefix = self.httpd.prefix.strip('/') + '/'
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    70
        else:
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    71
            prefix = ''
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    72
34704
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    73
        port = r':%d' % self.httpd.port
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    74
        if port == r':80':
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    75
            port = r''
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    76
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    77
        bindaddr = self.httpd.addr
34704
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    78
        if bindaddr == r'0.0.0.0':
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    79
            bindaddr = r'*'
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    80
        elif r':' in bindaddr: # IPv6
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    81
            bindaddr = r'[%s]' % bindaddr
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    82
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    83
        fqaddr = self.httpd.fqaddr
34704
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    84
        if r':' in fqaddr:
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30518
diff changeset
    85
            fqaddr = r'[%s]' % fqaddr
38211
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    86
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    87
        url = 'http://%s%s/%s' % (
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    88
            pycompat.sysbytes(fqaddr), pycompat.sysbytes(port), prefix)
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    89
        if self.opts['print_url']:
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    90
            self.ui.write('%s\n' % url)
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    91
        else:
38211
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    92
            if self.opts['port']:
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    93
                write = self.ui.status
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    94
            else:
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    95
                write = self.ui.write
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    96
            write(_('listening at %s (bound to %s:%d)\n') %
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    97
                  (url, pycompat.sysbytes(bindaddr), self.httpd.port))
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    98
        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
    99
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   100
    def run(self):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   101
        self.httpd.serve_forever()
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   102
30517
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   103
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
   104
    if webconf:
30517
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   105
        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
   106
    else:
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   107
        if not repo:
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   108
            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
   109
                                    " here (.hg not found)"))
30517
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   110
        return hgweb_mod.hgweb(repo, baseui=baseui)