##// END OF EJS Templates
hgweb: fix tracebacks on both index and repo pages
TK Soh -
r2360:25ec4981 default
parent child Browse files
Show More
@@ -1,154 +1,156 b''
1 # hgweb.py - web interface to a mercurial repository
1 # hgweb.py - web interface to a mercurial repository
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")
11 demandload(globals(), "ConfigParser")
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.request:hgrequest")
14 demandload(globals(), "mercurial.hgweb.request:hgrequest")
15 demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile")
14 from mercurial.i18n import gettext as _
16 from mercurial.i18n import gettext as _
15
17
16 # This is a stopgap
18 # This is a stopgap
17 class hgwebdir(object):
19 class hgwebdir(object):
18 def __init__(self, config):
20 def __init__(self, config):
19 def cleannames(items):
21 def cleannames(items):
20 return [(name.strip(os.sep), path) for name, path in items]
22 return [(name.strip(os.sep), path) for name, path in items]
21
23
22 self.motd = ""
24 self.motd = ""
23 self.repos_sorted = ('name', False)
25 self.repos_sorted = ('name', False)
24 if isinstance(config, (list, tuple)):
26 if isinstance(config, (list, tuple)):
25 self.repos = cleannames(config)
27 self.repos = cleannames(config)
26 self.repos_sorted = ('', False)
28 self.repos_sorted = ('', False)
27 elif isinstance(config, dict):
29 elif isinstance(config, dict):
28 self.repos = cleannames(config.items())
30 self.repos = cleannames(config.items())
29 self.repos.sort()
31 self.repos.sort()
30 else:
32 else:
31 cp = ConfigParser.SafeConfigParser()
33 cp = ConfigParser.SafeConfigParser()
32 cp.read(config)
34 cp.read(config)
33 self.repos = []
35 self.repos = []
34 if cp.has_section('web') and cp.has_option('web', 'motd'):
36 if cp.has_section('web') and cp.has_option('web', 'motd'):
35 self.motd = cp.get('web', 'motd')
37 self.motd = cp.get('web', 'motd')
36 if cp.has_section('paths'):
38 if cp.has_section('paths'):
37 self.repos.extend(cleannames(cp.items('paths')))
39 self.repos.extend(cleannames(cp.items('paths')))
38 if cp.has_section('collections'):
40 if cp.has_section('collections'):
39 for prefix, root in cp.items('collections'):
41 for prefix, root in cp.items('collections'):
40 for path in util.walkrepos(root):
42 for path in util.walkrepos(root):
41 repo = os.path.normpath(path)
43 repo = os.path.normpath(path)
42 name = repo
44 name = repo
43 if name.startswith(prefix):
45 if name.startswith(prefix):
44 name = name[len(prefix):]
46 name = name[len(prefix):]
45 self.repos.append((name.lstrip(os.sep), repo))
47 self.repos.append((name.lstrip(os.sep), repo))
46 self.repos.sort()
48 self.repos.sort()
47
49
48 def run(self, req=hgrequest()):
50 def run(self, req=hgrequest()):
49 def header(**map):
51 def header(**map):
50 yield tmpl("header", **map)
52 yield tmpl("header", **map)
51
53
52 def footer(**map):
54 def footer(**map):
53 yield tmpl("footer", motd=self.motd, **map)
55 yield tmpl("footer", motd=self.motd, **map)
54
56
55 m = os.path.join(templater.templatepath(), "map")
57 m = os.path.join(templater.templatepath(), "map")
56 tmpl = templater.templater(m, templater.common_filters,
58 tmpl = templater.templater(m, templater.common_filters,
57 defaults={"header": header,
59 defaults={"header": header,
58 "footer": footer})
60 "footer": footer})
59
61
60 def archivelist(ui, nodeid, url):
62 def archivelist(ui, nodeid, url):
61 allowed = (ui.config("web", "allow_archive", "")
63 allowed = (ui.config("web", "allow_archive", "")
62 .replace(",", " ").split())
64 .replace(",", " ").split())
63 for i in ['zip', 'gz', 'bz2']:
65 for i in ['zip', 'gz', 'bz2']:
64 if i in allowed or ui.configbool("web", "allow" + i):
66 if i in allowed or ui.configbool("web", "allow" + i):
65 yield {"type" : i, "node": nodeid, "url": url}
67 yield {"type" : i, "node": nodeid, "url": url}
66
68
67 def entries(sortcolumn="", descending=False, **map):
69 def entries(sortcolumn="", descending=False, **map):
68 rows = []
70 rows = []
69 parity = 0
71 parity = 0
70 for name, path in self.repos:
72 for name, path in self.repos:
71 u = ui.ui()
73 u = ui.ui()
72 try:
74 try:
73 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
75 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
74 except IOError:
76 except IOError:
75 pass
77 pass
76 get = u.config
78 get = u.config
77
79
78 url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name])
80 url = ('/'.join([req.env["REQUEST_URI"].split('?')[0], name])
79 .replace("//", "/"))
81 .replace("//", "/"))
80
82
81 # update time with local timezone
83 # update time with local timezone
82 try:
84 try:
83 d = (get_mtime(path), util.makedate()[1])
85 d = (get_mtime(path), util.makedate()[1])
84 except OSError:
86 except OSError:
85 continue
87 continue
86
88
87 contact = (get("ui", "username") or # preferred
89 contact = (get("ui", "username") or # preferred
88 get("web", "contact") or # deprecated
90 get("web", "contact") or # deprecated
89 get("web", "author", "")) # also
91 get("web", "author", "")) # also
90 description = get("web", "description", "")
92 description = get("web", "description", "")
91 name = get("web", "name", name)
93 name = get("web", "name", name)
92 row = dict(contact=contact or "unknown",
94 row = dict(contact=contact or "unknown",
93 contact_sort=contact.upper() or "unknown",
95 contact_sort=contact.upper() or "unknown",
94 name=name,
96 name=name,
95 name_sort=name,
97 name_sort=name,
96 url=url,
98 url=url,
97 description=description or "unknown",
99 description=description or "unknown",
98 description_sort=description.upper() or "unknown",
100 description_sort=description.upper() or "unknown",
99 lastchange=d,
101 lastchange=d,
100 lastchange_sort=d[1]-d[0],
102 lastchange_sort=d[1]-d[0],
101 archives=archivelist(u, "tip", url))
103 archives=archivelist(u, "tip", url))
102 if (not sortcolumn
104 if (not sortcolumn
103 or (sortcolumn, descending) == self.repos_sorted):
105 or (sortcolumn, descending) == self.repos_sorted):
104 # fast path for unsorted output
106 # fast path for unsorted output
105 row['parity'] = parity
107 row['parity'] = parity
106 parity = 1 - parity
108 parity = 1 - parity
107 yield row
109 yield row
108 else:
110 else:
109 rows.append((row["%s_sort" % sortcolumn], row))
111 rows.append((row["%s_sort" % sortcolumn], row))
110 if rows:
112 if rows:
111 rows.sort()
113 rows.sort()
112 if descending:
114 if descending:
113 rows.reverse()
115 rows.reverse()
114 for key, row in rows:
116 for key, row in rows:
115 row['parity'] = parity
117 row['parity'] = parity
116 parity = 1 - parity
118 parity = 1 - parity
117 yield row
119 yield row
118
120
119 virtual = req.env.get("PATH_INFO", "").strip('/')
121 virtual = req.env.get("PATH_INFO", "").strip('/')
120 if virtual:
122 if virtual:
121 real = dict(self.repos).get(virtual)
123 real = dict(self.repos).get(virtual)
122 if real:
124 if real:
123 try:
125 try:
124 hgweb(real).run(req)
126 hgweb(real).run(req)
125 except IOError, inst:
127 except IOError, inst:
126 req.write(tmpl("error", error=inst.strerror))
128 req.write(tmpl("error", error=inst.strerror))
127 except hg.RepoError, inst:
129 except hg.RepoError, inst:
128 req.write(tmpl("error", error=str(inst)))
130 req.write(tmpl("error", error=str(inst)))
129 else:
131 else:
130 req.write(tmpl("notfound", repo=virtual))
132 req.write(tmpl("notfound", repo=virtual))
131 else:
133 else:
132 if req.form.has_key('static'):
134 if req.form.has_key('static'):
133 static = os.path.join(templater.templatepath(), "static")
135 static = os.path.join(templater.templatepath(), "static")
134 fname = req.form['static'][0]
136 fname = req.form['static'][0]
135 req.write(staticfile(static, fname)
137 req.write(staticfile(static, fname)
136 or tmpl("error", error="%r not found" % fname))
138 or tmpl("error", error="%r not found" % fname))
137 else:
139 else:
138 sortable = ["name", "description", "contact", "lastchange"]
140 sortable = ["name", "description", "contact", "lastchange"]
139 sortcolumn, descending = self.repos_sorted
141 sortcolumn, descending = self.repos_sorted
140 if req.form.has_key('sort'):
142 if req.form.has_key('sort'):
141 sortcolumn = req.form['sort'][0]
143 sortcolumn = req.form['sort'][0]
142 descending = sortcolumn.startswith('-')
144 descending = sortcolumn.startswith('-')
143 if descending:
145 if descending:
144 sortcolumn = sortcolumn[1:]
146 sortcolumn = sortcolumn[1:]
145 if sortcolumn not in sortable:
147 if sortcolumn not in sortable:
146 sortcolumn = ""
148 sortcolumn = ""
147
149
148 sort = [("sort_%s" % column,
150 sort = [("sort_%s" % column,
149 "%s%s" % ((not descending and column == sortcolumn)
151 "%s%s" % ((not descending and column == sortcolumn)
150 and "-" or "", column))
152 and "-" or "", column))
151 for column in sortable]
153 for column in sortable]
152 req.write(tmpl("index", entries=entries,
154 req.write(tmpl("index", entries=entries,
153 sortcolumn=sortcolumn, descending=descending,
155 sortcolumn=sortcolumn, descending=descending,
154 **dict(sort)))
156 **dict(sort)))
General Comments 0
You need to be logged in to leave comments. Login now