__init__.py
123 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
|
r2391 | # hgweb/__init__.py - web interface to a mercurial repository | ||
|
r2311 | # | ||
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | ||||
|
r47575 | # Copyright 2005 Olivia Mackall <olivia@selenic.com> | ||
|
r2311 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2311 | |||
|
r27046 | |||
|
r10996 | import os | ||
|
r27046 | |||
|
r27138 | from ..i18n import _ | ||
from .. import ( | ||||
|
r27139 | error, | ||
|
r34705 | pycompat, | ||
|
r37138 | ) | ||
|
r43346 | from ..utils import procutil | ||
|
r27138 | |||
|
r27046 | from . import ( | ||
hgweb_mod, | ||||
hgwebdir_mod, | ||||
|
r27138 | server, | ||
|
r27046 | ) | ||
|
r3877 | |||
|
r43346 | |||
|
r10996 | def hgweb(config, name=None, baseui=None): | ||
|
r46554 | """create an hgweb wsgi object | ||
|
r10996 | |||
config can be one of: | ||||
- repo object (single repo view) | ||||
- path to repo (single repo view) | ||||
- path to config file (multi-repo view) | ||||
- dict of virtual:real pairs (multi-repo view) | ||||
- list of virtual:real tuples (multi-repo view) | ||||
|
r46554 | """ | ||
|
r10996 | |||
|
r49789 | if isinstance(config, str): | ||
|
r42395 | raise error.ProgrammingError( | ||
|
r43347 | b'Mercurial only supports encoded strings: %r' % config | ||
|
r43346 | ) | ||
if ( | ||||
(isinstance(config, bytes) and not os.path.isdir(config)) | ||||
or isinstance(config, dict) | ||||
or isinstance(config, list) | ||||
): | ||||
|
r10996 | # create a multi-dir interface | ||
return hgwebdir_mod.hgwebdir(config, baseui=baseui) | ||||
return hgweb_mod.hgweb(config, name=name, baseui=baseui) | ||||
|
r3877 | |||
|
r43346 | |||
|
r10992 | def hgwebdir(config, baseui=None): | ||
return hgwebdir_mod.hgwebdir(config, baseui=baseui) | ||||
|
r3877 | |||
|
r43346 | |||
|
r49801 | class httpservice: | ||
|
r27138 | def __init__(self, ui, app, opts): | ||
self.ui = ui | ||||
self.app = app | ||||
self.opts = opts | ||||
def init(self): | ||||
|
r37138 | procutil.setsignalhandler() | ||
|
r27138 | self.httpd = server.create_server(self.ui, self.app) | ||
|
r43346 | if ( | ||
|
r43347 | self.opts[b'port'] | ||
|
r43346 | and not self.ui.verbose | ||
|
r43347 | and not self.opts[b'print_url'] | ||
|
r43346 | ): | ||
|
r27138 | return | ||
if self.httpd.prefix: | ||||
|
r43347 | prefix = self.httpd.prefix.strip(b'/') + b'/' | ||
|
r27138 | else: | ||
|
r43347 | prefix = b'' | ||
|
r27138 | |||
|
r43906 | port = ':%d' % self.httpd.port | ||
if port == ':80': | ||||
port = '' | ||||
|
r27138 | |||
bindaddr = self.httpd.addr | ||||
|
r43906 | if bindaddr == '0.0.0.0': | ||
bindaddr = '*' | ||||
elif ':' in bindaddr: # IPv6 | ||||
bindaddr = '[%s]' % bindaddr | ||||
|
r27138 | |||
fqaddr = self.httpd.fqaddr | ||||
|
r43906 | if ':' in fqaddr: | ||
fqaddr = '[%s]' % fqaddr | ||||
|
r38196 | |||
|
r43347 | url = b'http://%s%s/%s' % ( | ||
|
r43346 | pycompat.sysbytes(fqaddr), | ||
pycompat.sysbytes(port), | ||||
prefix, | ||||
) | ||||
|
r43347 | if self.opts[b'print_url']: | ||
self.ui.write(b'%s\n' % url) | ||||
|
r27138 | else: | ||
|
r43347 | if self.opts[b'port']: | ||
|
r38196 | write = self.ui.status | ||
else: | ||||
write = self.ui.write | ||||
|
r43346 | write( | ||
|
r43347 | _(b'listening at %s (bound to %s:%d)\n') | ||
|
r43346 | % (url, pycompat.sysbytes(bindaddr), self.httpd.port) | ||
) | ||||
|
r27138 | self.ui.flush() # avoid buffering of status message | ||
def run(self): | ||||
self.httpd.serve_forever() | ||||
|
r27139 | |||
|
r43346 | |||
|
r30508 | def createapp(baseui, repo, webconf): | ||
|
r27181 | if webconf: | ||
|
r30508 | return hgwebdir_mod.hgwebdir(webconf, baseui=baseui) | ||
|
r27181 | else: | ||
|
r27139 | if not repo: | ||
|
r43346 | raise error.RepoError( | ||
|
r43387 | _(b"there is no Mercurial repository here (.hg not found)") | ||
|
r43346 | ) | ||
|
r30508 | return hgweb_mod.hgweb(repo, baseui=baseui) | ||