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