Show More
@@ -1,115 +1,116 b'' | |||
|
1 | 1 | # hgweb/__init__.py - web interface to a mercurial repository |
|
2 | 2 | # |
|
3 | 3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
4 | 4 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | from __future__ import absolute_import |
|
10 | 10 | |
|
11 | 11 | import os |
|
12 | 12 | |
|
13 | 13 | from ..i18n import _ |
|
14 | 14 | |
|
15 | 15 | from .. import ( |
|
16 | 16 | error, |
|
17 | 17 | util, |
|
18 | 18 | ) |
|
19 | 19 | |
|
20 | 20 | from . import ( |
|
21 | 21 | hgweb_mod, |
|
22 | 22 | hgwebdir_mod, |
|
23 | 23 | server, |
|
24 | 24 | ) |
|
25 | 25 | |
|
26 | 26 | def hgweb(config, name=None, baseui=None): |
|
27 | 27 | '''create an hgweb wsgi object |
|
28 | 28 | |
|
29 | 29 | config can be one of: |
|
30 | 30 | - repo object (single repo view) |
|
31 | 31 | - path to repo (single repo view) |
|
32 | 32 | - path to config file (multi-repo view) |
|
33 | 33 | - dict of virtual:real pairs (multi-repo view) |
|
34 | 34 | - list of virtual:real tuples (multi-repo view) |
|
35 | 35 | ''' |
|
36 | 36 | |
|
37 | 37 | if ((isinstance(config, str) and not os.path.isdir(config)) or |
|
38 | 38 | isinstance(config, dict) or isinstance(config, list)): |
|
39 | 39 | # create a multi-dir interface |
|
40 | 40 | return hgwebdir_mod.hgwebdir(config, baseui=baseui) |
|
41 | 41 | return hgweb_mod.hgweb(config, name=name, baseui=baseui) |
|
42 | 42 | |
|
43 | 43 | def hgwebdir(config, baseui=None): |
|
44 | 44 | return hgwebdir_mod.hgwebdir(config, baseui=baseui) |
|
45 | 45 | |
|
46 | 46 | class httpservice(object): |
|
47 | 47 | def __init__(self, ui, app, opts): |
|
48 | 48 | self.ui = ui |
|
49 | 49 | self.app = app |
|
50 | 50 | self.opts = opts |
|
51 | 51 | |
|
52 | 52 | def init(self): |
|
53 | 53 | util.setsignalhandler() |
|
54 | 54 | self.httpd = server.create_server(self.ui, self.app) |
|
55 | 55 | |
|
56 | 56 | if self.opts['port'] and not self.ui.verbose: |
|
57 | 57 | return |
|
58 | 58 | |
|
59 | 59 | if self.httpd.prefix: |
|
60 | 60 | prefix = self.httpd.prefix.strip('/') + '/' |
|
61 | 61 | else: |
|
62 | 62 | prefix = '' |
|
63 | 63 | |
|
64 | 64 | port = ':%d' % self.httpd.port |
|
65 | 65 | if port == ':80': |
|
66 | 66 | port = '' |
|
67 | 67 | |
|
68 | 68 | bindaddr = self.httpd.addr |
|
69 | 69 | if bindaddr == '0.0.0.0': |
|
70 | 70 | bindaddr = '*' |
|
71 | 71 | elif ':' in bindaddr: # IPv6 |
|
72 | 72 | bindaddr = '[%s]' % bindaddr |
|
73 | 73 | |
|
74 | 74 | fqaddr = self.httpd.fqaddr |
|
75 | 75 | if ':' in fqaddr: |
|
76 | 76 | fqaddr = '[%s]' % fqaddr |
|
77 | 77 | if self.opts['port']: |
|
78 | 78 | write = self.ui.status |
|
79 | 79 | else: |
|
80 | 80 | write = self.ui.write |
|
81 | 81 | write(_('listening at http://%s%s/%s (bound to %s:%d)\n') % |
|
82 | 82 | (fqaddr, port, prefix, bindaddr, self.httpd.port)) |
|
83 | 83 | self.ui.flush() # avoid buffering of status message |
|
84 | 84 | |
|
85 | 85 | def run(self): |
|
86 | 86 | self.httpd.serve_forever() |
|
87 | 87 | |
|
88 | 88 | def createservice(ui, repo, opts): |
|
89 | 89 | # this way we can check if something was given in the command-line |
|
90 | 90 | if opts.get('port'): |
|
91 | 91 | opts['port'] = util.getport(opts.get('port')) |
|
92 | 92 | |
|
93 | alluis = set([ui]) | |
|
93 | 94 | if repo: |
|
94 | 95 | baseui = repo.baseui |
|
96 | alluis.update([repo.baseui, repo.ui]) | |
|
95 | 97 | else: |
|
96 | 98 | baseui = ui |
|
97 | 99 | optlist = ("name templates style address port prefix ipv6" |
|
98 | 100 | " accesslog errorlog certificate encoding") |
|
99 | 101 | for o in optlist.split(): |
|
100 | 102 | val = opts.get(o, '') |
|
101 | 103 | if val in (None, ''): # should check against default options instead |
|
102 | 104 | continue |
|
103 | baseui.setconfig("web", o, val, 'serve') | |
|
104 | if repo and repo.ui != baseui: | |
|
105 | repo.ui.setconfig("web", o, val, 'serve') | |
|
105 | for u in alluis: | |
|
106 | u.setconfig("web", o, val, 'serve') | |
|
106 | 107 | |
|
107 | 108 | webconf = opts.get('web_conf') or opts.get('webdir_conf') |
|
108 | 109 | if webconf: |
|
109 | 110 | app = hgwebdir_mod.hgwebdir(webconf, baseui=baseui) |
|
110 | 111 | else: |
|
111 | 112 | if not repo: |
|
112 | 113 | raise error.RepoError(_("there is no Mercurial repository" |
|
113 | 114 | " here (.hg not found)")) |
|
114 | 115 | app = hgweb_mod.hgweb(repo, baseui=baseui) |
|
115 | 116 | return httpservice(ui, app, opts) |
General Comments 0
You need to be logged in to leave comments.
Login now