Show More
@@ -1,179 +1,188 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(), "ConfigParser mimetools cStringIO") |
|
11 | demandload(globals(), "ConfigParser 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") |
|
14 | demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile") | |
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 = ConfigParser.SafeConfigParser() |
|
33 | cp = ConfigParser.SafeConfigParser() | |
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", motd=self.motd, **map) |
|
70 | yield tmpl("footer", motd=self.motd, **map) | |
71 |
|
71 | |||
72 | m = os.path.join(templater.templatepath(), "map") |
|
72 | m = os.path.join(templater.templatepath(), "map") | |
73 | style = self.style |
|
73 | style = self.style | |
74 | if req.form.has_key('style'): |
|
74 | if req.form.has_key('style'): | |
75 | style = req.form['style'][0] |
|
75 | style = req.form['style'][0] | |
76 | if style != "": |
|
76 | if style != "": | |
77 | b = os.path.basename("map-" + style) |
|
77 | b = os.path.basename("map-" + style) | |
78 | p = os.path.join(templater.templatepath(), b) |
|
78 | p = os.path.join(templater.templatepath(), b) | |
79 | if os.path.isfile(p): |
|
79 | if os.path.isfile(p): | |
80 | m = p |
|
80 | m = p | |
81 |
|
81 | |||
82 | tmpl = templater.templater(m, templater.common_filters, |
|
82 | tmpl = templater.templater(m, templater.common_filters, | |
83 | defaults={"header": header, |
|
83 | defaults={"header": header, | |
84 | "footer": footer}) |
|
84 | "footer": footer}) | |
85 |
|
85 | |||
86 | def archivelist(ui, nodeid, url): |
|
86 | def archivelist(ui, nodeid, url): | |
87 | allowed = ui.configlist("web", "allow_archive") |
|
87 | allowed = ui.configlist("web", "allow_archive") | |
88 | for i in ['zip', 'gz', 'bz2']: |
|
88 | for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]: | |
89 | if i in allowed or ui.configbool("web", "allow" + i): |
|
89 | if i[0] in allowed or ui.configbool("web", "allow" + i[0]): | |
90 |
yield {"type" : i, " |
|
90 | yield {"type" : i[0], "extension": i[1], | |
|
91 | "node": nodeid, "url": url} | |||
91 |
|
92 | |||
92 | def entries(sortcolumn="", descending=False, **map): |
|
93 | def entries(sortcolumn="", descending=False, **map): | |
93 | rows = [] |
|
94 | rows = [] | |
94 | parity = 0 |
|
95 | parity = 0 | |
95 | for name, path in self.repos: |
|
96 | for name, path in self.repos: | |
96 | u = ui.ui() |
|
97 | u = ui.ui() | |
97 | try: |
|
98 | try: | |
98 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) |
|
99 | u.readconfig(os.path.join(path, '.hg', 'hgrc')) | |
99 | except IOError: |
|
100 | except IOError: | |
100 | pass |
|
101 | pass | |
101 | get = u.config |
|
102 | get = u.config | |
102 |
|
103 | |||
103 | url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name]) |
|
104 | url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name]) | |
104 | .replace("//", "/")) |
|
105 | .replace("//", "/")) + '/' | |
105 |
|
106 | |||
106 | # update time with local timezone |
|
107 | # update time with local timezone | |
107 | try: |
|
108 | try: | |
108 | d = (get_mtime(path), util.makedate()[1]) |
|
109 | d = (get_mtime(path), util.makedate()[1]) | |
109 | except OSError: |
|
110 | except OSError: | |
110 | continue |
|
111 | continue | |
111 |
|
112 | |||
112 | contact = (get("ui", "username") or # preferred |
|
113 | contact = (get("ui", "username") or # preferred | |
113 | get("web", "contact") or # deprecated |
|
114 | get("web", "contact") or # deprecated | |
114 | get("web", "author", "")) # also |
|
115 | get("web", "author", "")) # also | |
115 | description = get("web", "description", "") |
|
116 | description = get("web", "description", "") | |
116 | name = get("web", "name", name) |
|
117 | name = get("web", "name", name) | |
117 | row = dict(contact=contact or "unknown", |
|
118 | row = dict(contact=contact or "unknown", | |
118 | contact_sort=contact.upper() or "unknown", |
|
119 | contact_sort=contact.upper() or "unknown", | |
119 | name=name, |
|
120 | name=name, | |
120 | name_sort=name, |
|
121 | name_sort=name, | |
121 | url=url, |
|
122 | url=url, | |
122 | description=description or "unknown", |
|
123 | description=description or "unknown", | |
123 | description_sort=description.upper() or "unknown", |
|
124 | description_sort=description.upper() or "unknown", | |
124 | lastchange=d, |
|
125 | lastchange=d, | |
125 | lastchange_sort=d[1]-d[0], |
|
126 | lastchange_sort=d[1]-d[0], | |
126 | archives=archivelist(u, "tip", url)) |
|
127 | archives=archivelist(u, "tip", url)) | |
127 | if (not sortcolumn |
|
128 | if (not sortcolumn | |
128 | or (sortcolumn, descending) == self.repos_sorted): |
|
129 | or (sortcolumn, descending) == self.repos_sorted): | |
129 | # fast path for unsorted output |
|
130 | # fast path for unsorted output | |
130 | row['parity'] = parity |
|
131 | row['parity'] = parity | |
131 | parity = 1 - parity |
|
132 | parity = 1 - parity | |
132 | yield row |
|
133 | yield row | |
133 | else: |
|
134 | else: | |
134 | rows.append((row["%s_sort" % sortcolumn], row)) |
|
135 | rows.append((row["%s_sort" % sortcolumn], row)) | |
135 | if rows: |
|
136 | if rows: | |
136 | rows.sort() |
|
137 | rows.sort() | |
137 | if descending: |
|
138 | if descending: | |
138 | rows.reverse() |
|
139 | rows.reverse() | |
139 | for key, row in rows: |
|
140 | for key, row in rows: | |
140 | row['parity'] = parity |
|
141 | row['parity'] = parity | |
141 | parity = 1 - parity |
|
142 | parity = 1 - parity | |
142 | yield row |
|
143 | yield row | |
143 |
|
144 | |||
144 | virtual = req.env.get("PATH_INFO", "").strip('/') |
|
145 | virtual = req.env.get("PATH_INFO", "").strip('/') | |
145 | if virtual: |
|
146 | if virtual: | |
146 | real = dict(self.repos).get(virtual) |
|
147 | while virtual: | |
|
148 | real = dict(self.repos).get(virtual) | |||
|
149 | if real: | |||
|
150 | break | |||
|
151 | up = virtual.rfind('/') | |||
|
152 | if up < 0: | |||
|
153 | break | |||
|
154 | virtual = virtual[:up] | |||
147 | if real: |
|
155 | if real: | |
|
156 | req.env['REPO_NAME'] = virtual | |||
148 | try: |
|
157 | try: | |
149 | hgweb(real).run_wsgi(req) |
|
158 | hgweb(real).run_wsgi(req) | |
150 | except IOError, inst: |
|
159 | except IOError, inst: | |
151 | req.write(tmpl("error", error=inst.strerror)) |
|
160 | req.write(tmpl("error", error=inst.strerror)) | |
152 | except hg.RepoError, inst: |
|
161 | except hg.RepoError, inst: | |
153 | req.write(tmpl("error", error=str(inst))) |
|
162 | req.write(tmpl("error", error=str(inst))) | |
154 | else: |
|
163 | else: | |
155 | req.write(tmpl("notfound", repo=virtual)) |
|
164 | req.write(tmpl("notfound", repo=virtual)) | |
156 | else: |
|
165 | else: | |
157 | if req.form.has_key('static'): |
|
166 | if req.form.has_key('static'): | |
158 | static = os.path.join(templater.templatepath(), "static") |
|
167 | static = os.path.join(templater.templatepath(), "static") | |
159 | fname = req.form['static'][0] |
|
168 | fname = req.form['static'][0] | |
160 | req.write(staticfile(static, fname, req) |
|
169 | req.write(staticfile(static, fname, req) | |
161 | or tmpl("error", error="%r not found" % fname)) |
|
170 | or tmpl("error", error="%r not found" % fname)) | |
162 | else: |
|
171 | else: | |
163 | sortable = ["name", "description", "contact", "lastchange"] |
|
172 | sortable = ["name", "description", "contact", "lastchange"] | |
164 | sortcolumn, descending = self.repos_sorted |
|
173 | sortcolumn, descending = self.repos_sorted | |
165 | if req.form.has_key('sort'): |
|
174 | if req.form.has_key('sort'): | |
166 | sortcolumn = req.form['sort'][0] |
|
175 | sortcolumn = req.form['sort'][0] | |
167 | descending = sortcolumn.startswith('-') |
|
176 | descending = sortcolumn.startswith('-') | |
168 | if descending: |
|
177 | if descending: | |
169 | sortcolumn = sortcolumn[1:] |
|
178 | sortcolumn = sortcolumn[1:] | |
170 | if sortcolumn not in sortable: |
|
179 | if sortcolumn not in sortable: | |
171 | sortcolumn = "" |
|
180 | sortcolumn = "" | |
172 |
|
181 | |||
173 | sort = [("sort_%s" % column, |
|
182 | sort = [("sort_%s" % column, | |
174 | "%s%s" % ((not descending and column == sortcolumn) |
|
183 | "%s%s" % ((not descending and column == sortcolumn) | |
175 | and "-" or "", column)) |
|
184 | and "-" or "", column)) | |
176 | for column in sortable] |
|
185 | for column in sortable] | |
177 | req.write(tmpl("index", entries=entries, |
|
186 | req.write(tmpl("index", entries=entries, | |
178 | sortcolumn=sortcolumn, descending=descending, |
|
187 | sortcolumn=sortcolumn, descending=descending, | |
179 | **dict(sort))) |
|
188 | **dict(sort))) |
General Comments 0
You need to be logged in to leave comments.
Login now