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