Show More
@@ -1,254 +1,255 b'' | |||||
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, paritygen |
|
13 | from common import get_mtime, staticfile, style_map, paritygen | |
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, parentui=None): |
|
18 | def __init__(self, config, parentui=None): | |
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.parentui = parentui |
|
22 | self.parentui = parentui | |
23 | self.motd = None |
|
23 | self.motd = None | |
24 | self.style = None |
|
24 | self.style = None | |
25 | self.stripecount = None |
|
25 | self.stripecount = None | |
26 | self.repos_sorted = ('name', False) |
|
26 | self.repos_sorted = ('name', False) | |
27 | if isinstance(config, (list, tuple)): |
|
27 | if isinstance(config, (list, tuple)): | |
28 | self.repos = cleannames(config) |
|
28 | self.repos = cleannames(config) | |
29 | self.repos_sorted = ('', False) |
|
29 | self.repos_sorted = ('', False) | |
30 | elif isinstance(config, dict): |
|
30 | elif isinstance(config, dict): | |
31 | self.repos = cleannames(config.items()) |
|
31 | self.repos = cleannames(config.items()) | |
32 | self.repos.sort() |
|
32 | self.repos.sort() | |
33 | else: |
|
33 | else: | |
34 | if isinstance(config, util.configparser): |
|
34 | if isinstance(config, util.configparser): | |
35 | cp = config |
|
35 | cp = config | |
36 | else: |
|
36 | else: | |
37 | cp = util.configparser() |
|
37 | cp = util.configparser() | |
38 | cp.read(config) |
|
38 | cp.read(config) | |
39 | self.repos = [] |
|
39 | self.repos = [] | |
40 | if cp.has_section('web'): |
|
40 | if cp.has_section('web'): | |
41 | if cp.has_option('web', 'motd'): |
|
41 | if cp.has_option('web', 'motd'): | |
42 | self.motd = cp.get('web', 'motd') |
|
42 | self.motd = cp.get('web', 'motd') | |
43 | if cp.has_option('web', 'style'): |
|
43 | if cp.has_option('web', 'style'): | |
44 | self.style = cp.get('web', 'style') |
|
44 | self.style = cp.get('web', 'style') | |
45 | if cp.has_option('web', 'stripes'): |
|
45 | if cp.has_option('web', 'stripes'): | |
46 | self.stripecount = int(cp.get('web', 'stripes')) |
|
46 | self.stripecount = int(cp.get('web', 'stripes')) | |
47 | if cp.has_section('paths'): |
|
47 | if cp.has_section('paths'): | |
48 | self.repos.extend(cleannames(cp.items('paths'))) |
|
48 | self.repos.extend(cleannames(cp.items('paths'))) | |
49 | if cp.has_section('collections'): |
|
49 | if cp.has_section('collections'): | |
50 | for prefix, root in cp.items('collections'): |
|
50 | for prefix, root in cp.items('collections'): | |
51 | for path in util.walkrepos(root): |
|
51 | for path in util.walkrepos(root): | |
52 | repo = os.path.normpath(path) |
|
52 | repo = os.path.normpath(path) | |
53 | name = repo |
|
53 | name = repo | |
54 | if name.startswith(prefix): |
|
54 | if name.startswith(prefix): | |
55 | name = name[len(prefix):] |
|
55 | name = name[len(prefix):] | |
56 | self.repos.append((name.lstrip(os.sep), repo)) |
|
56 | self.repos.append((name.lstrip(os.sep), repo)) | |
57 | self.repos.sort() |
|
57 | self.repos.sort() | |
58 |
|
58 | |||
59 | def run(self): |
|
59 | def run(self): | |
60 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): |
|
60 | if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): | |
61 | raise RuntimeError("This function is only intended to be called while running as a CGI script.") |
|
61 | raise RuntimeError("This function is only intended to be called while running as a CGI script.") | |
62 | import mercurial.hgweb.wsgicgi as wsgicgi |
|
62 | import mercurial.hgweb.wsgicgi as wsgicgi | |
63 | from request import wsgiapplication |
|
63 | from request import wsgiapplication | |
64 | def make_web_app(): |
|
64 | def make_web_app(): | |
65 | return self |
|
65 | return self | |
66 | wsgicgi.launch(wsgiapplication(make_web_app)) |
|
66 | wsgicgi.launch(wsgiapplication(make_web_app)) | |
67 |
|
67 | |||
68 | def run_wsgi(self, req): |
|
68 | def run_wsgi(self, req): | |
69 | def header(**map): |
|
69 | def header(**map): | |
70 | header_file = cStringIO.StringIO( |
|
70 | header_file = cStringIO.StringIO( | |
71 | ''.join(tmpl("header", encoding=util._encoding, **map))) |
|
71 | ''.join(tmpl("header", encoding=util._encoding, **map))) | |
72 | msg = mimetools.Message(header_file, 0) |
|
72 | msg = mimetools.Message(header_file, 0) | |
73 | req.header(msg.items()) |
|
73 | req.header(msg.items()) | |
74 | yield header_file.read() |
|
74 | yield header_file.read() | |
75 |
|
75 | |||
76 | def footer(**map): |
|
76 | def footer(**map): | |
77 | yield tmpl("footer", **map) |
|
77 | yield tmpl("footer", **map) | |
78 |
|
78 | |||
79 | def motd(**map): |
|
79 | def motd(**map): | |
80 | if self.motd is not None: |
|
80 | if self.motd is not None: | |
81 | yield self.motd |
|
81 | yield self.motd | |
82 | else: |
|
82 | else: | |
83 | yield config('web', 'motd', '') |
|
83 | yield config('web', 'motd', '') | |
84 |
|
84 | |||
85 | parentui = self.parentui or ui.ui(report_untrusted=False) |
|
85 | parentui = self.parentui or ui.ui(report_untrusted=False) | |
86 |
|
86 | |||
87 | def config(section, name, default=None, untrusted=True): |
|
87 | def config(section, name, default=None, untrusted=True): | |
88 | return parentui.config(section, name, default, untrusted) |
|
88 | return parentui.config(section, name, default, untrusted) | |
89 |
|
89 | |||
90 | url = req.env['REQUEST_URI'].split('?')[0] |
|
90 | url = req.env['REQUEST_URI'].split('?')[0] | |
91 | if not url.endswith('/'): |
|
91 | if not url.endswith('/'): | |
92 | url += '/' |
|
92 | url += '/' | |
93 | pathinfo = req.env.get('PATH_INFO', '').strip('/') + '/' |
|
93 | pathinfo = req.env.get('PATH_INFO', '').strip('/') + '/' | |
94 | base = url[:len(url) - len(pathinfo)] |
|
94 | base = url[:len(url) - len(pathinfo)] | |
95 | if not base.endswith('/'): |
|
95 | if not base.endswith('/'): | |
96 | base += '/' |
|
96 | base += '/' | |
97 |
|
97 | |||
98 | staticurl = config('web', 'staticurl') or base + 'static/' |
|
98 | staticurl = config('web', 'staticurl') or base + 'static/' | |
99 | if not staticurl.endswith('/'): |
|
99 | if not staticurl.endswith('/'): | |
100 | staticurl += '/' |
|
100 | staticurl += '/' | |
101 |
|
101 | |||
102 | style = self.style |
|
102 | style = self.style | |
103 | if style is None: |
|
103 | if style is None: | |
104 | style = config('web', 'style', '') |
|
104 | style = config('web', 'style', '') | |
105 | if req.form.has_key('style'): |
|
105 | if req.form.has_key('style'): | |
106 | style = req.form['style'][0] |
|
106 | style = req.form['style'][0] | |
107 | if self.stripecount is None: |
|
107 | if self.stripecount is None: | |
108 | self.stripecount = int(config('web', 'stripes', 1)) |
|
108 | self.stripecount = int(config('web', 'stripes', 1)) | |
109 | mapfile = style_map(templater.templatepath(), style) |
|
109 | mapfile = style_map(templater.templatepath(), style) | |
110 | tmpl = templater.templater(mapfile, templater.common_filters, |
|
110 | tmpl = templater.templater(mapfile, templater.common_filters, | |
111 | defaults={"header": header, |
|
111 | defaults={"header": header, | |
112 | "footer": footer, |
|
112 | "footer": footer, | |
113 | "motd": motd, |
|
113 | "motd": motd, | |
114 | "url": url, |
|
114 | "url": url, | |
115 | "staticurl": staticurl}) |
|
115 | "staticurl": staticurl}) | |
116 |
|
116 | |||
117 | def archivelist(ui, nodeid, url): |
|
117 | def archivelist(ui, nodeid, url): | |
118 | allowed = ui.configlist("web", "allow_archive", untrusted=True) |
|
118 | allowed = ui.configlist("web", "allow_archive", untrusted=True) | |
119 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: |
|
119 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: | |
120 | if i[0] in allowed or ui.configbool("web", "allow" + i[0], |
|
120 | if i[0] in allowed or ui.configbool("web", "allow" + i[0], | |
121 | untrusted=True): |
|
121 | untrusted=True): | |
122 | yield {"type" : i[0], "extension": i[1], |
|
122 | yield {"type" : i[0], "extension": i[1], | |
123 | "node": nodeid, "url": url} |
|
123 | "node": nodeid, "url": url} | |
124 |
|
124 | |||
125 | def entries(sortcolumn="", descending=False, subdir="", **map): |
|
125 | def entries(sortcolumn="", descending=False, subdir="", **map): | |
126 | def sessionvars(**map): |
|
126 | def sessionvars(**map): | |
127 | fields = [] |
|
127 | fields = [] | |
128 | if req.form.has_key('style'): |
|
128 | if req.form.has_key('style'): | |
129 | style = req.form['style'][0] |
|
129 | style = req.form['style'][0] | |
130 | if style != get('web', 'style', ''): |
|
130 | if style != get('web', 'style', ''): | |
131 | fields.append(('style', style)) |
|
131 | fields.append(('style', style)) | |
132 |
|
132 | |||
133 | separator = url[-1] == '?' and ';' or '?' |
|
133 | separator = url[-1] == '?' and ';' or '?' | |
134 | for name, value in fields: |
|
134 | for name, value in fields: | |
135 | yield dict(name=name, value=value, separator=separator) |
|
135 | yield dict(name=name, value=value, separator=separator) | |
136 | separator = ';' |
|
136 | separator = ';' | |
137 |
|
137 | |||
138 | rows = [] |
|
138 | rows = [] | |
139 | parity = paritygen(self.stripecount) |
|
139 | parity = paritygen(self.stripecount) | |
140 | for name, path in self.repos: |
|
140 | for name, path in self.repos: | |
141 | if not name.startswith(subdir): |
|
141 | if not name.startswith(subdir): | |
142 | continue |
|
142 | continue | |
|
143 | name = name[len(subdir):] | |||
143 |
|
144 | |||
144 | u = ui.ui(parentui=parentui) |
|
145 | u = ui.ui(parentui=parentui) | |
145 | try: |
|
146 | try: | |
146 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) |
|
147 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) | |
147 | except IOError: |
|
148 | except IOError: | |
148 | pass |
|
149 | pass | |
149 | def get(section, name, default=None): |
|
150 | def get(section, name, default=None): | |
150 | return u.config(section, name, default, untrusted=True) |
|
151 | return u.config(section, name, default, untrusted=True) | |
151 |
|
152 | |||
152 | if u.configbool("web", "hidden", untrusted=True): |
|
153 | if u.configbool("web", "hidden", untrusted=True): | |
153 | continue |
|
154 | continue | |
154 |
|
155 | |||
155 |
url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name |
|
156 | url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name]) | |
156 | .replace("//", "/")) + '/' |
|
157 | .replace("//", "/")) + '/' | |
157 |
|
158 | |||
158 | # update time with local timezone |
|
159 | # update time with local timezone | |
159 | try: |
|
160 | try: | |
160 | d = (get_mtime(path), util.makedate()[1]) |
|
161 | d = (get_mtime(path), util.makedate()[1]) | |
161 | except OSError: |
|
162 | except OSError: | |
162 | continue |
|
163 | continue | |
163 |
|
164 | |||
164 | contact = (get("ui", "username") or # preferred |
|
165 | contact = (get("ui", "username") or # preferred | |
165 | get("web", "contact") or # deprecated |
|
166 | get("web", "contact") or # deprecated | |
166 | get("web", "author", "")) # also |
|
167 | get("web", "author", "")) # also | |
167 | description = get("web", "description", "") |
|
168 | description = get("web", "description", "") | |
168 | name = get("web", "name", name) |
|
169 | name = get("web", "name", name) | |
169 | row = dict(contact=contact or "unknown", |
|
170 | row = dict(contact=contact or "unknown", | |
170 | contact_sort=contact.upper() or "unknown", |
|
171 | contact_sort=contact.upper() or "unknown", | |
171 | name=name, |
|
172 | name=name, | |
172 | name_sort=name, |
|
173 | name_sort=name, | |
173 | url=url, |
|
174 | url=url, | |
174 | description=description or "unknown", |
|
175 | description=description or "unknown", | |
175 | description_sort=description.upper() or "unknown", |
|
176 | description_sort=description.upper() or "unknown", | |
176 | lastchange=d, |
|
177 | lastchange=d, | |
177 | lastchange_sort=d[1]-d[0], |
|
178 | lastchange_sort=d[1]-d[0], | |
178 | sessionvars=sessionvars, |
|
179 | sessionvars=sessionvars, | |
179 | archives=archivelist(u, "tip", url)) |
|
180 | archives=archivelist(u, "tip", url)) | |
180 | if (not sortcolumn |
|
181 | if (not sortcolumn | |
181 | or (sortcolumn, descending) == self.repos_sorted): |
|
182 | or (sortcolumn, descending) == self.repos_sorted): | |
182 | # fast path for unsorted output |
|
183 | # fast path for unsorted output | |
183 | row['parity'] = parity.next() |
|
184 | row['parity'] = parity.next() | |
184 | yield row |
|
185 | yield row | |
185 | else: |
|
186 | else: | |
186 | rows.append((row["%s_sort" % sortcolumn], row)) |
|
187 | rows.append((row["%s_sort" % sortcolumn], row)) | |
187 | if rows: |
|
188 | if rows: | |
188 | rows.sort() |
|
189 | rows.sort() | |
189 | if descending: |
|
190 | if descending: | |
190 | rows.reverse() |
|
191 | rows.reverse() | |
191 | for key, row in rows: |
|
192 | for key, row in rows: | |
192 | row['parity'] = parity.next() |
|
193 | row['parity'] = parity.next() | |
193 | yield row |
|
194 | yield row | |
194 |
|
195 | |||
195 | def makeindex(req, subdir=""): |
|
196 | def makeindex(req, subdir=""): | |
196 | sortable = ["name", "description", "contact", "lastchange"] |
|
197 | sortable = ["name", "description", "contact", "lastchange"] | |
197 | sortcolumn, descending = self.repos_sorted |
|
198 | sortcolumn, descending = self.repos_sorted | |
198 | if req.form.has_key('sort'): |
|
199 | if req.form.has_key('sort'): | |
199 | sortcolumn = req.form['sort'][0] |
|
200 | sortcolumn = req.form['sort'][0] | |
200 | descending = sortcolumn.startswith('-') |
|
201 | descending = sortcolumn.startswith('-') | |
201 | if descending: |
|
202 | if descending: | |
202 | sortcolumn = sortcolumn[1:] |
|
203 | sortcolumn = sortcolumn[1:] | |
203 | if sortcolumn not in sortable: |
|
204 | if sortcolumn not in sortable: | |
204 | sortcolumn = "" |
|
205 | sortcolumn = "" | |
205 |
|
206 | |||
206 | sort = [("sort_%s" % column, |
|
207 | sort = [("sort_%s" % column, | |
207 | "%s%s" % ((not descending and column == sortcolumn) |
|
208 | "%s%s" % ((not descending and column == sortcolumn) | |
208 | and "-" or "", column)) |
|
209 | and "-" or "", column)) | |
209 | for column in sortable] |
|
210 | for column in sortable] | |
210 | req.write(tmpl("index", entries=entries, subdir=subdir, |
|
211 | req.write(tmpl("index", entries=entries, subdir=subdir, | |
211 | sortcolumn=sortcolumn, descending=descending, |
|
212 | sortcolumn=sortcolumn, descending=descending, | |
212 | **dict(sort))) |
|
213 | **dict(sort))) | |
213 |
|
214 | |||
214 | try: |
|
215 | try: | |
215 | virtual = req.env.get("PATH_INFO", "").strip('/') |
|
216 | virtual = req.env.get("PATH_INFO", "").strip('/') | |
216 | if virtual.startswith('static/'): |
|
217 | if virtual.startswith('static/'): | |
217 | static = os.path.join(templater.templatepath(), 'static') |
|
218 | static = os.path.join(templater.templatepath(), 'static') | |
218 | fname = virtual[7:] |
|
219 | fname = virtual[7:] | |
219 | req.write(staticfile(static, fname, req) or |
|
220 | req.write(staticfile(static, fname, req) or | |
220 | tmpl('error', error='%r not found' % fname)) |
|
221 | tmpl('error', error='%r not found' % fname)) | |
221 | elif virtual: |
|
222 | elif virtual: | |
222 | while virtual: |
|
223 | while virtual: | |
223 | real = dict(self.repos).get(virtual) |
|
224 | real = dict(self.repos).get(virtual) | |
224 | if real: |
|
225 | if real: | |
225 | break |
|
226 | break | |
226 | up = virtual.rfind('/') |
|
227 | up = virtual.rfind('/') | |
227 | if up < 0: |
|
228 | if up < 0: | |
228 | break |
|
229 | break | |
229 | virtual = virtual[:up] |
|
230 | virtual = virtual[:up] | |
230 | if real: |
|
231 | if real: | |
231 | req.env['REPO_NAME'] = virtual |
|
232 | req.env['REPO_NAME'] = virtual | |
232 | try: |
|
233 | try: | |
233 | repo = hg.repository(parentui, real) |
|
234 | repo = hg.repository(parentui, real) | |
234 | hgweb(repo).run_wsgi(req) |
|
235 | hgweb(repo).run_wsgi(req) | |
235 | except IOError, inst: |
|
236 | except IOError, inst: | |
236 | req.write(tmpl("error", error=inst.strerror)) |
|
237 | req.write(tmpl("error", error=inst.strerror)) | |
237 | except hg.RepoError, inst: |
|
238 | except hg.RepoError, inst: | |
238 | req.write(tmpl("error", error=str(inst))) |
|
239 | req.write(tmpl("error", error=str(inst))) | |
239 | else: |
|
240 | else: | |
240 | subdir=req.env.get("PATH_INFO", "").strip('/') + '/' |
|
241 | subdir=req.env.get("PATH_INFO", "").strip('/') + '/' | |
241 | if [r for r in self.repos if r[0].startswith(subdir)]: |
|
242 | if [r for r in self.repos if r[0].startswith(subdir)]: | |
242 | makeindex(req, subdir) |
|
243 | makeindex(req, subdir) | |
243 | else: |
|
244 | else: | |
244 | req.write(tmpl("notfound", repo=virtual)) |
|
245 | req.write(tmpl("notfound", repo=virtual)) | |
245 | else: |
|
246 | else: | |
246 | if req.form.has_key('static'): |
|
247 | if req.form.has_key('static'): | |
247 | static = os.path.join(templater.templatepath(), "static") |
|
248 | static = os.path.join(templater.templatepath(), "static") | |
248 | fname = req.form['static'][0] |
|
249 | fname = req.form['static'][0] | |
249 | req.write(staticfile(static, fname, req) |
|
250 | req.write(staticfile(static, fname, req) | |
250 | or tmpl("error", error="%r not found" % fname)) |
|
251 | or tmpl("error", error="%r not found" % fname)) | |
251 | else: |
|
252 | else: | |
252 | makeindex(req) |
|
253 | makeindex(req) | |
253 | finally: |
|
254 | finally: | |
254 | tmpl = None |
|
255 | tmpl = None |
General Comments 0
You need to be logged in to leave comments.
Login now