##// END OF EJS Templates
Fix hgwebdir to run hgweb using run_wsgi.
Eric Hopper -
r2537:b6975008 default
parent child Browse files
Show More
@@ -1,167 +1,167 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 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005 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.origconfig = config
23 self.origconfig = config
24 self.motd = ""
24 self.motd = ""
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') and cp.has_option('web', 'motd'):
36 if cp.has_section('web') and cp.has_option('web', 'motd'):
37 self.motd = cp.get('web', 'motd')
37 self.motd = cp.get('web', 'motd')
38 if cp.has_section('paths'):
38 if cp.has_section('paths'):
39 self.repos.extend(cleannames(cp.items('paths')))
39 self.repos.extend(cleannames(cp.items('paths')))
40 if cp.has_section('collections'):
40 if cp.has_section('collections'):
41 for prefix, root in cp.items('collections'):
41 for prefix, root in cp.items('collections'):
42 for path in util.walkrepos(root):
42 for path in util.walkrepos(root):
43 repo = os.path.normpath(path)
43 repo = os.path.normpath(path)
44 name = repo
44 name = repo
45 if name.startswith(prefix):
45 if name.startswith(prefix):
46 name = name[len(prefix):]
46 name = name[len(prefix):]
47 self.repos.append((name.lstrip(os.sep), repo))
47 self.repos.append((name.lstrip(os.sep), repo))
48 self.repos.sort()
48 self.repos.sort()
49
49
50 def run(self):
50 def run(self):
51 if os.environ['GATEWAY_INTERFACE'][0:6] != "CGI/1.":
51 if os.environ['GATEWAY_INTERFACE'][0:6] != "CGI/1.":
52 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
52 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
53 import mercurial.hgweb.wsgicgi as wsgicgi
53 import mercurial.hgweb.wsgicgi as wsgicgi
54 from request import wsgiapplication
54 from request import wsgiapplication
55 def make_web_app():
55 def make_web_app():
56 return self.__class__(self.origconfig)
56 return self.__class__(self.origconfig)
57 wsgicgi.launch(wsgiapplication(make_web_app))
57 wsgicgi.launch(wsgiapplication(make_web_app))
58
58
59 def run_wsgi(self, req):
59 def run_wsgi(self, req):
60 def header(**map):
60 def header(**map):
61 header_file = cStringIO.StringIO(''.join(tmpl("header", **map)))
61 header_file = cStringIO.StringIO(''.join(tmpl("header", **map)))
62 msg = mimetools.Message(header_file, 0)
62 msg = mimetools.Message(header_file, 0)
63 req.header(msg.items())
63 req.header(msg.items())
64 yield header_file.read()
64 yield header_file.read()
65
65
66 def footer(**map):
66 def footer(**map):
67 yield tmpl("footer", motd=self.motd, **map)
67 yield tmpl("footer", motd=self.motd, **map)
68
68
69 m = os.path.join(templater.templatepath(), "map")
69 m = os.path.join(templater.templatepath(), "map")
70 tmpl = templater.templater(m, templater.common_filters,
70 tmpl = templater.templater(m, templater.common_filters,
71 defaults={"header": header,
71 defaults={"header": header,
72 "footer": footer})
72 "footer": footer})
73
73
74 def archivelist(ui, nodeid, url):
74 def archivelist(ui, nodeid, url):
75 allowed = ui.configlist("web", "allow_archive")
75 allowed = ui.configlist("web", "allow_archive")
76 for i in ['zip', 'gz', 'bz2']:
76 for i in ['zip', 'gz', 'bz2']:
77 if i in allowed or ui.configbool("web", "allow" + i):
77 if i in allowed or ui.configbool("web", "allow" + i):
78 yield {"type" : i, "node": nodeid, "url": url}
78 yield {"type" : i, "node": nodeid, "url": url}
79
79
80 def entries(sortcolumn="", descending=False, **map):
80 def entries(sortcolumn="", descending=False, **map):
81 rows = []
81 rows = []
82 parity = 0
82 parity = 0
83 for name, path in self.repos:
83 for name, path in self.repos:
84 u = ui.ui()
84 u = ui.ui()
85 try:
85 try:
86 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
86 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
87 except IOError:
87 except IOError:
88 pass
88 pass
89 get = u.config
89 get = u.config
90
90
91 url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name])
91 url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name])
92 .replace("//", "/"))
92 .replace("//", "/"))
93
93
94 # update time with local timezone
94 # update time with local timezone
95 try:
95 try:
96 d = (get_mtime(path), util.makedate()[1])
96 d = (get_mtime(path), util.makedate()[1])
97 except OSError:
97 except OSError:
98 continue
98 continue
99
99
100 contact = (get("ui", "username") or # preferred
100 contact = (get("ui", "username") or # preferred
101 get("web", "contact") or # deprecated
101 get("web", "contact") or # deprecated
102 get("web", "author", "")) # also
102 get("web", "author", "")) # also
103 description = get("web", "description", "")
103 description = get("web", "description", "")
104 name = get("web", "name", name)
104 name = get("web", "name", name)
105 row = dict(contact=contact or "unknown",
105 row = dict(contact=contact or "unknown",
106 contact_sort=contact.upper() or "unknown",
106 contact_sort=contact.upper() or "unknown",
107 name=name,
107 name=name,
108 name_sort=name,
108 name_sort=name,
109 url=url,
109 url=url,
110 description=description or "unknown",
110 description=description or "unknown",
111 description_sort=description.upper() or "unknown",
111 description_sort=description.upper() or "unknown",
112 lastchange=d,
112 lastchange=d,
113 lastchange_sort=d[1]-d[0],
113 lastchange_sort=d[1]-d[0],
114 archives=archivelist(u, "tip", url))
114 archives=archivelist(u, "tip", url))
115 if (not sortcolumn
115 if (not sortcolumn
116 or (sortcolumn, descending) == self.repos_sorted):
116 or (sortcolumn, descending) == self.repos_sorted):
117 # fast path for unsorted output
117 # fast path for unsorted output
118 row['parity'] = parity
118 row['parity'] = parity
119 parity = 1 - parity
119 parity = 1 - parity
120 yield row
120 yield row
121 else:
121 else:
122 rows.append((row["%s_sort" % sortcolumn], row))
122 rows.append((row["%s_sort" % sortcolumn], row))
123 if rows:
123 if rows:
124 rows.sort()
124 rows.sort()
125 if descending:
125 if descending:
126 rows.reverse()
126 rows.reverse()
127 for key, row in rows:
127 for key, row in rows:
128 row['parity'] = parity
128 row['parity'] = parity
129 parity = 1 - parity
129 parity = 1 - parity
130 yield row
130 yield row
131
131
132 virtual = req.env.get("PATH_INFO", "").strip('/')
132 virtual = req.env.get("PATH_INFO", "").strip('/')
133 if virtual:
133 if virtual:
134 real = dict(self.repos).get(virtual)
134 real = dict(self.repos).get(virtual)
135 if real:
135 if real:
136 try:
136 try:
137 hgweb(real).run(req)
137 hgweb(real).run_wsgi(req)
138 except IOError, inst:
138 except IOError, inst:
139 req.write(tmpl("error", error=inst.strerror))
139 req.write(tmpl("error", error=inst.strerror))
140 except hg.RepoError, inst:
140 except hg.RepoError, inst:
141 req.write(tmpl("error", error=str(inst)))
141 req.write(tmpl("error", error=str(inst)))
142 else:
142 else:
143 req.write(tmpl("notfound", repo=virtual))
143 req.write(tmpl("notfound", repo=virtual))
144 else:
144 else:
145 if req.form.has_key('static'):
145 if req.form.has_key('static'):
146 static = os.path.join(templater.templatepath(), "static")
146 static = os.path.join(templater.templatepath(), "static")
147 fname = req.form['static'][0]
147 fname = req.form['static'][0]
148 req.write(staticfile(static, fname, req)
148 req.write(staticfile(static, fname, req)
149 or tmpl("error", error="%r not found" % fname))
149 or tmpl("error", error="%r not found" % fname))
150 else:
150 else:
151 sortable = ["name", "description", "contact", "lastchange"]
151 sortable = ["name", "description", "contact", "lastchange"]
152 sortcolumn, descending = self.repos_sorted
152 sortcolumn, descending = self.repos_sorted
153 if req.form.has_key('sort'):
153 if req.form.has_key('sort'):
154 sortcolumn = req.form['sort'][0]
154 sortcolumn = req.form['sort'][0]
155 descending = sortcolumn.startswith('-')
155 descending = sortcolumn.startswith('-')
156 if descending:
156 if descending:
157 sortcolumn = sortcolumn[1:]
157 sortcolumn = sortcolumn[1:]
158 if sortcolumn not in sortable:
158 if sortcolumn not in sortable:
159 sortcolumn = ""
159 sortcolumn = ""
160
160
161 sort = [("sort_%s" % column,
161 sort = [("sort_%s" % column,
162 "%s%s" % ((not descending and column == sortcolumn)
162 "%s%s" % ((not descending and column == sortcolumn)
163 and "-" or "", column))
163 and "-" or "", column))
164 for column in sortable]
164 for column in sortable]
165 req.write(tmpl("index", entries=entries,
165 req.write(tmpl("index", entries=entries,
166 sortcolumn=sortcolumn, descending=descending,
166 sortcolumn=sortcolumn, descending=descending,
167 **dict(sort)))
167 **dict(sort)))
General Comments 0
You need to be logged in to leave comments. Login now