Show More
@@ -1,211 +1,214 | |||||
1 | # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories. |
|
1 | # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories. | |
2 | # |
|
2 | # | |
3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | |
4 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
4 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms |
|
6 | # This software may be used and distributed according to the terms | |
7 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | # of the GNU General Public License, incorporated herein by reference. | |
8 |
|
8 | |||
9 | from mercurial import demandimport; demandimport.enable() |
|
9 | from mercurial import demandimport; demandimport.enable() | |
10 | import os, mimetools, cStringIO |
|
10 | import os, mimetools, cStringIO | |
11 | from mercurial.i18n import gettext as _ |
|
11 | from mercurial.i18n import gettext as _ | |
12 | from mercurial import ui, hg, util, templater |
|
12 | from mercurial import ui, hg, util, templater | |
13 | from common import get_mtime, staticfile, style_map |
|
13 | from common import get_mtime, staticfile, style_map | |
14 | from hgweb_mod import hgweb |
|
14 | from hgweb_mod import hgweb | |
15 |
|
15 | |||
16 | # This is a stopgap |
|
16 | # This is a stopgap | |
17 | class hgwebdir(object): |
|
17 | class hgwebdir(object): | |
18 | def __init__(self, config): |
|
18 | def __init__(self, config): | |
19 | def cleannames(items): |
|
19 | def cleannames(items): | |
20 | return [(name.strip(os.sep), path) for name, path in items] |
|
20 | return [(name.strip(os.sep), path) for name, path in items] | |
21 |
|
21 | |||
22 | self.motd = "" |
|
22 | self.motd = "" | |
23 | self.style = "" |
|
23 | self.style = "" | |
24 | self.repos_sorted = ('name', False) |
|
24 | self.repos_sorted = ('name', False) | |
25 | if isinstance(config, (list, tuple)): |
|
25 | if isinstance(config, (list, tuple)): | |
26 | self.repos = cleannames(config) |
|
26 | self.repos = cleannames(config) | |
27 | self.repos_sorted = ('', False) |
|
27 | self.repos_sorted = ('', False) | |
28 | elif isinstance(config, dict): |
|
28 | elif isinstance(config, dict): | |
29 | self.repos = cleannames(config.items()) |
|
29 | self.repos = cleannames(config.items()) | |
30 | self.repos.sort() |
|
30 | self.repos.sort() | |
31 | else: |
|
31 | else: | |
32 |
|
|
32 | if isinstance(config, util.configparser): | |
33 |
cp |
|
33 | cp = config | |
|
34 | else: | |||
|
35 | cp = util.configparser() | |||
|
36 | cp.read(config) | |||
34 | self.repos = [] |
|
37 | self.repos = [] | |
35 | if cp.has_section('web'): |
|
38 | if cp.has_section('web'): | |
36 | if cp.has_option('web', 'motd'): |
|
39 | if cp.has_option('web', 'motd'): | |
37 | self.motd = cp.get('web', 'motd') |
|
40 | self.motd = cp.get('web', 'motd') | |
38 | if cp.has_option('web', 'style'): |
|
41 | if cp.has_option('web', 'style'): | |
39 | self.style = cp.get('web', 'style') |
|
42 | self.style = cp.get('web', 'style') | |
40 | if cp.has_section('paths'): |
|
43 | if cp.has_section('paths'): | |
41 | self.repos.extend(cleannames(cp.items('paths'))) |
|
44 | self.repos.extend(cleannames(cp.items('paths'))) | |
42 | if cp.has_section('collections'): |
|
45 | if cp.has_section('collections'): | |
43 | for prefix, root in cp.items('collections'): |
|
46 | for prefix, root in cp.items('collections'): | |
44 | for path in util.walkrepos(root): |
|
47 | for path in util.walkrepos(root): | |
45 | repo = os.path.normpath(path) |
|
48 | repo = os.path.normpath(path) | |
46 | name = repo |
|
49 | name = repo | |
47 | if name.startswith(prefix): |
|
50 | if name.startswith(prefix): | |
48 | name = name[len(prefix):] |
|
51 | name = name[len(prefix):] | |
49 | self.repos.append((name.lstrip(os.sep), repo)) |
|
52 | self.repos.append((name.lstrip(os.sep), repo)) | |
50 | self.repos.sort() |
|
53 | self.repos.sort() | |
51 |
|
54 | |||
52 | def run(self): |
|
55 | def run(self): | |
53 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): |
|
56 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): | |
54 | raise RuntimeError("This function is only intended to be called while running as a CGI script.") |
|
57 | raise RuntimeError("This function is only intended to be called while running as a CGI script.") | |
55 | import mercurial.hgweb.wsgicgi as wsgicgi |
|
58 | import mercurial.hgweb.wsgicgi as wsgicgi | |
56 | from request import wsgiapplication |
|
59 | from request import wsgiapplication | |
57 | def make_web_app(): |
|
60 | def make_web_app(): | |
58 | return self |
|
61 | return self | |
59 | wsgicgi.launch(wsgiapplication(make_web_app)) |
|
62 | wsgicgi.launch(wsgiapplication(make_web_app)) | |
60 |
|
63 | |||
61 | def run_wsgi(self, req): |
|
64 | def run_wsgi(self, req): | |
62 | def header(**map): |
|
65 | def header(**map): | |
63 | header_file = cStringIO.StringIO( |
|
66 | header_file = cStringIO.StringIO( | |
64 | ''.join(tmpl("header", encoding=util._encoding, **map))) |
|
67 | ''.join(tmpl("header", encoding=util._encoding, **map))) | |
65 | msg = mimetools.Message(header_file, 0) |
|
68 | msg = mimetools.Message(header_file, 0) | |
66 | req.header(msg.items()) |
|
69 | req.header(msg.items()) | |
67 | yield header_file.read() |
|
70 | yield header_file.read() | |
68 |
|
71 | |||
69 | def footer(**map): |
|
72 | def footer(**map): | |
70 | yield tmpl("footer", **map) |
|
73 | yield tmpl("footer", **map) | |
71 |
|
74 | |||
72 | def motd(**map): |
|
75 | def motd(**map): | |
73 | yield self.motd |
|
76 | yield self.motd | |
74 |
|
77 | |||
75 | url = req.env['REQUEST_URI'].split('?')[0] |
|
78 | url = req.env['REQUEST_URI'].split('?')[0] | |
76 | if not url.endswith('/'): |
|
79 | if not url.endswith('/'): | |
77 | url += '/' |
|
80 | url += '/' | |
78 |
|
81 | |||
79 | style = self.style |
|
82 | style = self.style | |
80 | if req.form.has_key('style'): |
|
83 | if req.form.has_key('style'): | |
81 | style = req.form['style'][0] |
|
84 | style = req.form['style'][0] | |
82 | mapfile = style_map(templater.templatepath(), style) |
|
85 | mapfile = style_map(templater.templatepath(), style) | |
83 | tmpl = templater.templater(mapfile, templater.common_filters, |
|
86 | tmpl = templater.templater(mapfile, templater.common_filters, | |
84 | defaults={"header": header, |
|
87 | defaults={"header": header, | |
85 | "footer": footer, |
|
88 | "footer": footer, | |
86 | "motd": motd, |
|
89 | "motd": motd, | |
87 | "url": url}) |
|
90 | "url": url}) | |
88 |
|
91 | |||
89 | def archivelist(ui, nodeid, url): |
|
92 | def archivelist(ui, nodeid, url): | |
90 | allowed = ui.configlist("web", "allow_archive", untrusted=True) |
|
93 | allowed = ui.configlist("web", "allow_archive", untrusted=True) | |
91 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: |
|
94 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: | |
92 | if i[0] in allowed or ui.configbool("web", "allow" + i[0], |
|
95 | if i[0] in allowed or ui.configbool("web", "allow" + i[0], | |
93 | untrusted=True): |
|
96 | untrusted=True): | |
94 | yield {"type" : i[0], "extension": i[1], |
|
97 | yield {"type" : i[0], "extension": i[1], | |
95 | "node": nodeid, "url": url} |
|
98 | "node": nodeid, "url": url} | |
96 |
|
99 | |||
97 | def entries(sortcolumn="", descending=False, **map): |
|
100 | def entries(sortcolumn="", descending=False, **map): | |
98 | def sessionvars(**map): |
|
101 | def sessionvars(**map): | |
99 | fields = [] |
|
102 | fields = [] | |
100 | if req.form.has_key('style'): |
|
103 | if req.form.has_key('style'): | |
101 | style = req.form['style'][0] |
|
104 | style = req.form['style'][0] | |
102 | if style != get('web', 'style', ''): |
|
105 | if style != get('web', 'style', ''): | |
103 | fields.append(('style', style)) |
|
106 | fields.append(('style', style)) | |
104 |
|
107 | |||
105 | separator = url[-1] == '?' and ';' or '?' |
|
108 | separator = url[-1] == '?' and ';' or '?' | |
106 | for name, value in fields: |
|
109 | for name, value in fields: | |
107 | yield dict(name=name, value=value, separator=separator) |
|
110 | yield dict(name=name, value=value, separator=separator) | |
108 | separator = ';' |
|
111 | separator = ';' | |
109 |
|
112 | |||
110 | rows = [] |
|
113 | rows = [] | |
111 | parity = 0 |
|
114 | parity = 0 | |
112 | for name, path in self.repos: |
|
115 | for name, path in self.repos: | |
113 | u = ui.ui(report_untrusted=False) |
|
116 | u = ui.ui(report_untrusted=False) | |
114 | try: |
|
117 | try: | |
115 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) |
|
118 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) | |
116 | except IOError: |
|
119 | except IOError: | |
117 | pass |
|
120 | pass | |
118 | def get(section, name, default=None): |
|
121 | def get(section, name, default=None): | |
119 | return u.config(section, name, default, untrusted=True) |
|
122 | return u.config(section, name, default, untrusted=True) | |
120 |
|
123 | |||
121 | url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name]) |
|
124 | url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name]) | |
122 | .replace("//", "/")) + '/' |
|
125 | .replace("//", "/")) + '/' | |
123 |
|
126 | |||
124 | # update time with local timezone |
|
127 | # update time with local timezone | |
125 | try: |
|
128 | try: | |
126 | d = (get_mtime(path), util.makedate()[1]) |
|
129 | d = (get_mtime(path), util.makedate()[1]) | |
127 | except OSError: |
|
130 | except OSError: | |
128 | continue |
|
131 | continue | |
129 |
|
132 | |||
130 | contact = (get("ui", "username") or # preferred |
|
133 | contact = (get("ui", "username") or # preferred | |
131 | get("web", "contact") or # deprecated |
|
134 | get("web", "contact") or # deprecated | |
132 | get("web", "author", "")) # also |
|
135 | get("web", "author", "")) # also | |
133 | description = get("web", "description", "") |
|
136 | description = get("web", "description", "") | |
134 | name = get("web", "name", name) |
|
137 | name = get("web", "name", name) | |
135 | row = dict(contact=contact or "unknown", |
|
138 | row = dict(contact=contact or "unknown", | |
136 | contact_sort=contact.upper() or "unknown", |
|
139 | contact_sort=contact.upper() or "unknown", | |
137 | name=name, |
|
140 | name=name, | |
138 | name_sort=name, |
|
141 | name_sort=name, | |
139 | url=url, |
|
142 | url=url, | |
140 | description=description or "unknown", |
|
143 | description=description or "unknown", | |
141 | description_sort=description.upper() or "unknown", |
|
144 | description_sort=description.upper() or "unknown", | |
142 | lastchange=d, |
|
145 | lastchange=d, | |
143 | lastchange_sort=d[1]-d[0], |
|
146 | lastchange_sort=d[1]-d[0], | |
144 | sessionvars=sessionvars, |
|
147 | sessionvars=sessionvars, | |
145 | archives=archivelist(u, "tip", url)) |
|
148 | archives=archivelist(u, "tip", url)) | |
146 | if (not sortcolumn |
|
149 | if (not sortcolumn | |
147 | or (sortcolumn, descending) == self.repos_sorted): |
|
150 | or (sortcolumn, descending) == self.repos_sorted): | |
148 | # fast path for unsorted output |
|
151 | # fast path for unsorted output | |
149 | row['parity'] = parity |
|
152 | row['parity'] = parity | |
150 | parity = 1 - parity |
|
153 | parity = 1 - parity | |
151 | yield row |
|
154 | yield row | |
152 | else: |
|
155 | else: | |
153 | rows.append((row["%s_sort" % sortcolumn], row)) |
|
156 | rows.append((row["%s_sort" % sortcolumn], row)) | |
154 | if rows: |
|
157 | if rows: | |
155 | rows.sort() |
|
158 | rows.sort() | |
156 | if descending: |
|
159 | if descending: | |
157 | rows.reverse() |
|
160 | rows.reverse() | |
158 | for key, row in rows: |
|
161 | for key, row in rows: | |
159 | row['parity'] = parity |
|
162 | row['parity'] = parity | |
160 | parity = 1 - parity |
|
163 | parity = 1 - parity | |
161 | yield row |
|
164 | yield row | |
162 |
|
165 | |||
163 | virtual = req.env.get("PATH_INFO", "").strip('/') |
|
166 | virtual = req.env.get("PATH_INFO", "").strip('/') | |
164 | if virtual.startswith('static/'): |
|
167 | if virtual.startswith('static/'): | |
165 | static = os.path.join(templater.templatepath(), 'static') |
|
168 | static = os.path.join(templater.templatepath(), 'static') | |
166 | fname = virtual[7:] |
|
169 | fname = virtual[7:] | |
167 | req.write(staticfile(static, fname, req) or |
|
170 | req.write(staticfile(static, fname, req) or | |
168 | tmpl('error', error='%r not found' % fname)) |
|
171 | tmpl('error', error='%r not found' % fname)) | |
169 | elif virtual: |
|
172 | elif virtual: | |
170 | while virtual: |
|
173 | while virtual: | |
171 | real = dict(self.repos).get(virtual) |
|
174 | real = dict(self.repos).get(virtual) | |
172 | if real: |
|
175 | if real: | |
173 | break |
|
176 | break | |
174 | up = virtual.rfind('/') |
|
177 | up = virtual.rfind('/') | |
175 | if up < 0: |
|
178 | if up < 0: | |
176 | break |
|
179 | break | |
177 | virtual = virtual[:up] |
|
180 | virtual = virtual[:up] | |
178 | if real: |
|
181 | if real: | |
179 | req.env['REPO_NAME'] = virtual |
|
182 | req.env['REPO_NAME'] = virtual | |
180 | try: |
|
183 | try: | |
181 | hgweb(real).run_wsgi(req) |
|
184 | hgweb(real).run_wsgi(req) | |
182 | except IOError, inst: |
|
185 | except IOError, inst: | |
183 | req.write(tmpl("error", error=inst.strerror)) |
|
186 | req.write(tmpl("error", error=inst.strerror)) | |
184 | except hg.RepoError, inst: |
|
187 | except hg.RepoError, inst: | |
185 | req.write(tmpl("error", error=str(inst))) |
|
188 | req.write(tmpl("error", error=str(inst))) | |
186 | else: |
|
189 | else: | |
187 | req.write(tmpl("notfound", repo=virtual)) |
|
190 | req.write(tmpl("notfound", repo=virtual)) | |
188 | else: |
|
191 | else: | |
189 | if req.form.has_key('static'): |
|
192 | if req.form.has_key('static'): | |
190 | static = os.path.join(templater.templatepath(), "static") |
|
193 | static = os.path.join(templater.templatepath(), "static") | |
191 | fname = req.form['static'][0] |
|
194 | fname = req.form['static'][0] | |
192 | req.write(staticfile(static, fname, req) |
|
195 | req.write(staticfile(static, fname, req) | |
193 | or tmpl("error", error="%r not found" % fname)) |
|
196 | or tmpl("error", error="%r not found" % fname)) | |
194 | else: |
|
197 | else: | |
195 | sortable = ["name", "description", "contact", "lastchange"] |
|
198 | sortable = ["name", "description", "contact", "lastchange"] | |
196 | sortcolumn, descending = self.repos_sorted |
|
199 | sortcolumn, descending = self.repos_sorted | |
197 | if req.form.has_key('sort'): |
|
200 | if req.form.has_key('sort'): | |
198 | sortcolumn = req.form['sort'][0] |
|
201 | sortcolumn = req.form['sort'][0] | |
199 | descending = sortcolumn.startswith('-') |
|
202 | descending = sortcolumn.startswith('-') | |
200 | if descending: |
|
203 | if descending: | |
201 | sortcolumn = sortcolumn[1:] |
|
204 | sortcolumn = sortcolumn[1:] | |
202 | if sortcolumn not in sortable: |
|
205 | if sortcolumn not in sortable: | |
203 | sortcolumn = "" |
|
206 | sortcolumn = "" | |
204 |
|
207 | |||
205 | sort = [("sort_%s" % column, |
|
208 | sort = [("sort_%s" % column, | |
206 | "%s%s" % ((not descending and column == sortcolumn) |
|
209 | "%s%s" % ((not descending and column == sortcolumn) | |
207 | and "-" or "", column)) |
|
210 | and "-" or "", column)) | |
208 | for column in sortable] |
|
211 | for column in sortable] | |
209 | req.write(tmpl("index", entries=entries, |
|
212 | req.write(tmpl("index", entries=entries, | |
210 | sortcolumn=sortcolumn, descending=descending, |
|
213 | sortcolumn=sortcolumn, descending=descending, | |
211 | **dict(sort))) |
|
214 | **dict(sort))) |
General Comments 0
You need to be logged in to leave comments.
Login now