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