##// END OF EJS Templates
hgweb: do not import hgweb_mod.hgweb and .makebreadcrumb as symbol...
Yuya Nishihara -
r27043:ccdc95c6 default
parent child Browse files
Show More
@@ -1,486 +1,486
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 of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 import os, re, time
10 10 from mercurial.i18n import _
11 11 from mercurial import hg, scmutil, util, templater
12 12 from mercurial import ui as uimod
13 13 from mercurial import error, encoding
14 14 from common import ErrorResponse, get_mtime, staticfile, paritygen, ismember, \
15 15 get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
16 from hgweb_mod import hgweb, makebreadcrumb
16 import hgweb_mod
17 17 from request import wsgirequest
18 18 import webutil
19 19
20 20 def cleannames(items):
21 21 return [(util.pconvert(name).strip('/'), path) for name, path in items]
22 22
23 23 def findrepos(paths):
24 24 repos = []
25 25 for prefix, root in cleannames(paths):
26 26 roothead, roottail = os.path.split(root)
27 27 # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below
28 28 # /bar/ be served as as foo/N .
29 29 # '*' will not search inside dirs with .hg (except .hg/patches),
30 30 # '**' will search inside dirs with .hg (and thus also find subrepos).
31 31 try:
32 32 recurse = {'*': False, '**': True}[roottail]
33 33 except KeyError:
34 34 repos.append((prefix, root))
35 35 continue
36 36 roothead = os.path.normpath(os.path.abspath(roothead))
37 37 paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse)
38 38 repos.extend(urlrepos(prefix, roothead, paths))
39 39 return repos
40 40
41 41 def urlrepos(prefix, roothead, paths):
42 42 """yield url paths and filesystem paths from a list of repo paths
43 43
44 44 >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq]
45 45 >>> conv(urlrepos('hg', '/opt', ['/opt/r', '/opt/r/r', '/opt']))
46 46 [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')]
47 47 >>> conv(urlrepos('', '/opt', ['/opt/r', '/opt/r/r', '/opt']))
48 48 [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')]
49 49 """
50 50 for path in paths:
51 51 path = os.path.normpath(path)
52 52 yield (prefix + '/' +
53 53 util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
54 54
55 55 def geturlcgivars(baseurl, port):
56 56 """
57 57 Extract CGI variables from baseurl
58 58
59 59 >>> geturlcgivars("http://host.org/base", "80")
60 60 ('host.org', '80', '/base')
61 61 >>> geturlcgivars("http://host.org:8000/base", "80")
62 62 ('host.org', '8000', '/base')
63 63 >>> geturlcgivars('/base', 8000)
64 64 ('', '8000', '/base')
65 65 >>> geturlcgivars("base", '8000')
66 66 ('', '8000', '/base')
67 67 >>> geturlcgivars("http://host", '8000')
68 68 ('host', '8000', '/')
69 69 >>> geturlcgivars("http://host/", '8000')
70 70 ('host', '8000', '/')
71 71 """
72 72 u = util.url(baseurl)
73 73 name = u.host or ''
74 74 if u.port:
75 75 port = u.port
76 76 path = u.path or ""
77 77 if not path.startswith('/'):
78 78 path = '/' + path
79 79
80 80 return name, str(port), path
81 81
82 82 class hgwebdir(object):
83 83 """HTTP server for multiple repositories.
84 84
85 85 Given a configuration, different repositories will be served depending
86 86 on the request path.
87 87
88 88 Instances are typically used as WSGI applications.
89 89 """
90 90 def __init__(self, conf, baseui=None):
91 91 self.conf = conf
92 92 self.baseui = baseui
93 93 self.ui = None
94 94 self.lastrefresh = 0
95 95 self.motd = None
96 96 self.refresh()
97 97
98 98 def refresh(self):
99 99 refreshinterval = 20
100 100 if self.ui:
101 101 refreshinterval = self.ui.configint('web', 'refreshinterval',
102 102 refreshinterval)
103 103
104 104 # refreshinterval <= 0 means to always refresh.
105 105 if (refreshinterval > 0 and
106 106 self.lastrefresh + refreshinterval > time.time()):
107 107 return
108 108
109 109 if self.baseui:
110 110 u = self.baseui.copy()
111 111 else:
112 112 u = uimod.ui()
113 113 u.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir')
114 114 u.setconfig('ui', 'nontty', 'true', 'hgwebdir')
115 115 # displaying bundling progress bar while serving feels wrong and may
116 116 # break some wsgi implementations.
117 117 u.setconfig('progress', 'disable', 'true', 'hgweb')
118 118
119 119 if not isinstance(self.conf, (dict, list, tuple)):
120 120 map = {'paths': 'hgweb-paths'}
121 121 if not os.path.exists(self.conf):
122 122 raise error.Abort(_('config file %s not found!') % self.conf)
123 123 u.readconfig(self.conf, remap=map, trust=True)
124 124 paths = []
125 125 for name, ignored in u.configitems('hgweb-paths'):
126 126 for path in u.configlist('hgweb-paths', name):
127 127 paths.append((name, path))
128 128 elif isinstance(self.conf, (list, tuple)):
129 129 paths = self.conf
130 130 elif isinstance(self.conf, dict):
131 131 paths = self.conf.items()
132 132
133 133 repos = findrepos(paths)
134 134 for prefix, root in u.configitems('collections'):
135 135 prefix = util.pconvert(prefix)
136 136 for path in scmutil.walkrepos(root, followsym=True):
137 137 repo = os.path.normpath(path)
138 138 name = util.pconvert(repo)
139 139 if name.startswith(prefix):
140 140 name = name[len(prefix):]
141 141 repos.append((name.lstrip('/'), repo))
142 142
143 143 self.repos = repos
144 144 self.ui = u
145 145 encoding.encoding = self.ui.config('web', 'encoding',
146 146 encoding.encoding)
147 147 self.style = self.ui.config('web', 'style', 'paper')
148 148 self.templatepath = self.ui.config('web', 'templates', None)
149 149 self.stripecount = self.ui.config('web', 'stripes', 1)
150 150 if self.stripecount:
151 151 self.stripecount = int(self.stripecount)
152 152 self._baseurl = self.ui.config('web', 'baseurl')
153 153 prefix = self.ui.config('web', 'prefix', '')
154 154 if prefix.startswith('/'):
155 155 prefix = prefix[1:]
156 156 if prefix.endswith('/'):
157 157 prefix = prefix[:-1]
158 158 self.prefix = prefix
159 159 self.lastrefresh = time.time()
160 160
161 161 def run(self):
162 162 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
163 163 raise RuntimeError("This function is only intended to be "
164 164 "called while running as a CGI script.")
165 165 import mercurial.hgweb.wsgicgi as wsgicgi
166 166 wsgicgi.launch(self)
167 167
168 168 def __call__(self, env, respond):
169 169 req = wsgirequest(env, respond)
170 170 return self.run_wsgi(req)
171 171
172 172 def read_allowed(self, ui, req):
173 173 """Check allow_read and deny_read config options of a repo's ui object
174 174 to determine user permissions. By default, with neither option set (or
175 175 both empty), allow all users to read the repo. There are two ways a
176 176 user can be denied read access: (1) deny_read is not empty, and the
177 177 user is unauthenticated or deny_read contains user (or *), and (2)
178 178 allow_read is not empty and the user is not in allow_read. Return True
179 179 if user is allowed to read the repo, else return False."""
180 180
181 181 user = req.env.get('REMOTE_USER')
182 182
183 183 deny_read = ui.configlist('web', 'deny_read', untrusted=True)
184 184 if deny_read and (not user or ismember(ui, user, deny_read)):
185 185 return False
186 186
187 187 allow_read = ui.configlist('web', 'allow_read', untrusted=True)
188 188 # by default, allow reading if no allow_read option has been set
189 189 if (not allow_read) or ismember(ui, user, allow_read):
190 190 return True
191 191
192 192 return False
193 193
194 194 def run_wsgi(self, req):
195 195 try:
196 196 self.refresh()
197 197
198 198 virtual = req.env.get("PATH_INFO", "").strip('/')
199 199 tmpl = self.templater(req)
200 200 ctype = tmpl('mimetype', encoding=encoding.encoding)
201 201 ctype = templater.stringify(ctype)
202 202
203 203 # a static file
204 204 if virtual.startswith('static/') or 'static' in req.form:
205 205 if virtual.startswith('static/'):
206 206 fname = virtual[7:]
207 207 else:
208 208 fname = req.form['static'][0]
209 209 static = self.ui.config("web", "static", None,
210 210 untrusted=False)
211 211 if not static:
212 212 tp = self.templatepath or templater.templatepaths()
213 213 if isinstance(tp, str):
214 214 tp = [tp]
215 215 static = [os.path.join(p, 'static') for p in tp]
216 216 staticfile(static, fname, req)
217 217 return []
218 218
219 219 # top-level index
220 220 elif not virtual:
221 221 req.respond(HTTP_OK, ctype)
222 222 return self.makeindex(req, tmpl)
223 223
224 224 # nested indexes and hgwebs
225 225
226 226 repos = dict(self.repos)
227 227 virtualrepo = virtual
228 228 while virtualrepo:
229 229 real = repos.get(virtualrepo)
230 230 if real:
231 231 req.env['REPO_NAME'] = virtualrepo
232 232 try:
233 233 # ensure caller gets private copy of ui
234 234 repo = hg.repository(self.ui.copy(), real)
235 return hgweb(repo).run_wsgi(req)
235 return hgweb_mod.hgweb(repo).run_wsgi(req)
236 236 except IOError as inst:
237 237 msg = inst.strerror
238 238 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
239 239 except error.RepoError as inst:
240 240 raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
241 241
242 242 up = virtualrepo.rfind('/')
243 243 if up < 0:
244 244 break
245 245 virtualrepo = virtualrepo[:up]
246 246
247 247 # browse subdirectories
248 248 subdir = virtual + '/'
249 249 if [r for r in repos if r.startswith(subdir)]:
250 250 req.respond(HTTP_OK, ctype)
251 251 return self.makeindex(req, tmpl, subdir)
252 252
253 253 # prefixes not found
254 254 req.respond(HTTP_NOT_FOUND, ctype)
255 255 return tmpl("notfound", repo=virtual)
256 256
257 257 except ErrorResponse as err:
258 258 req.respond(err, ctype)
259 259 return tmpl('error', error=err.message or '')
260 260 finally:
261 261 tmpl = None
262 262
263 263 def makeindex(self, req, tmpl, subdir=""):
264 264
265 265 def archivelist(ui, nodeid, url):
266 266 allowed = ui.configlist("web", "allow_archive", untrusted=True)
267 267 archives = []
268 268 for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
269 269 if i[0] in allowed or ui.configbool("web", "allow" + i[0],
270 270 untrusted=True):
271 271 archives.append({"type" : i[0], "extension": i[1],
272 272 "node": nodeid, "url": url})
273 273 return archives
274 274
275 275 def rawentries(subdir="", **map):
276 276
277 277 descend = self.ui.configbool('web', 'descend', True)
278 278 collapse = self.ui.configbool('web', 'collapse', False)
279 279 seenrepos = set()
280 280 seendirs = set()
281 281 for name, path in self.repos:
282 282
283 283 if not name.startswith(subdir):
284 284 continue
285 285 name = name[len(subdir):]
286 286 directory = False
287 287
288 288 if '/' in name:
289 289 if not descend:
290 290 continue
291 291
292 292 nameparts = name.split('/')
293 293 rootname = nameparts[0]
294 294
295 295 if not collapse:
296 296 pass
297 297 elif rootname in seendirs:
298 298 continue
299 299 elif rootname in seenrepos:
300 300 pass
301 301 else:
302 302 directory = True
303 303 name = rootname
304 304
305 305 # redefine the path to refer to the directory
306 306 discarded = '/'.join(nameparts[1:])
307 307
308 308 # remove name parts plus accompanying slash
309 309 path = path[:-len(discarded) - 1]
310 310
311 311 try:
312 312 r = hg.repository(self.ui, path)
313 313 directory = False
314 314 except (IOError, error.RepoError):
315 315 pass
316 316
317 317 parts = [name]
318 318 if 'PATH_INFO' in req.env:
319 319 parts.insert(0, req.env['PATH_INFO'].rstrip('/'))
320 320 if req.env['SCRIPT_NAME']:
321 321 parts.insert(0, req.env['SCRIPT_NAME'])
322 322 url = re.sub(r'/+', '/', '/'.join(parts) + '/')
323 323
324 324 # show either a directory entry or a repository
325 325 if directory:
326 326 # get the directory's time information
327 327 try:
328 328 d = (get_mtime(path), util.makedate()[1])
329 329 except OSError:
330 330 continue
331 331
332 332 # add '/' to the name to make it obvious that
333 333 # the entry is a directory, not a regular repository
334 334 row = {'contact': "",
335 335 'contact_sort': "",
336 336 'name': name + '/',
337 337 'name_sort': name,
338 338 'url': url,
339 339 'description': "",
340 340 'description_sort': "",
341 341 'lastchange': d,
342 342 'lastchange_sort': d[1]-d[0],
343 343 'archives': [],
344 344 'isdirectory': True}
345 345
346 346 seendirs.add(name)
347 347 yield row
348 348 continue
349 349
350 350 u = self.ui.copy()
351 351 try:
352 352 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
353 353 except Exception as e:
354 354 u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
355 355 continue
356 356 def get(section, name, default=None):
357 357 return u.config(section, name, default, untrusted=True)
358 358
359 359 if u.configbool("web", "hidden", untrusted=True):
360 360 continue
361 361
362 362 if not self.read_allowed(u, req):
363 363 continue
364 364
365 365 # update time with local timezone
366 366 try:
367 367 r = hg.repository(self.ui, path)
368 368 except IOError:
369 369 u.warn(_('error accessing repository at %s\n') % path)
370 370 continue
371 371 except error.RepoError:
372 372 u.warn(_('error accessing repository at %s\n') % path)
373 373 continue
374 374 try:
375 375 d = (get_mtime(r.spath), util.makedate()[1])
376 376 except OSError:
377 377 continue
378 378
379 379 contact = get_contact(get)
380 380 description = get("web", "description", "")
381 381 seenrepos.add(name)
382 382 name = get("web", "name", name)
383 383 row = {'contact': contact or "unknown",
384 384 'contact_sort': contact.upper() or "unknown",
385 385 'name': name,
386 386 'name_sort': name,
387 387 'url': url,
388 388 'description': description or "unknown",
389 389 'description_sort': description.upper() or "unknown",
390 390 'lastchange': d,
391 391 'lastchange_sort': d[1]-d[0],
392 392 'archives': archivelist(u, "tip", url),
393 393 'isdirectory': None,
394 394 }
395 395
396 396 yield row
397 397
398 398 sortdefault = None, False
399 399 def entries(sortcolumn="", descending=False, subdir="", **map):
400 400 rows = rawentries(subdir=subdir, **map)
401 401
402 402 if sortcolumn and sortdefault != (sortcolumn, descending):
403 403 sortkey = '%s_sort' % sortcolumn
404 404 rows = sorted(rows, key=lambda x: x[sortkey],
405 405 reverse=descending)
406 406 for row, parity in zip(rows, paritygen(self.stripecount)):
407 407 row['parity'] = parity
408 408 yield row
409 409
410 410 self.refresh()
411 411 sortable = ["name", "description", "contact", "lastchange"]
412 412 sortcolumn, descending = sortdefault
413 413 if 'sort' in req.form:
414 414 sortcolumn = req.form['sort'][0]
415 415 descending = sortcolumn.startswith('-')
416 416 if descending:
417 417 sortcolumn = sortcolumn[1:]
418 418 if sortcolumn not in sortable:
419 419 sortcolumn = ""
420 420
421 421 sort = [("sort_%s" % column,
422 422 "%s%s" % ((not descending and column == sortcolumn)
423 423 and "-" or "", column))
424 424 for column in sortable]
425 425
426 426 self.refresh()
427 427 self.updatereqenv(req.env)
428 428
429 429 return tmpl("index", entries=entries, subdir=subdir,
430 pathdef=makebreadcrumb('/' + subdir, self.prefix),
430 pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix),
431 431 sortcolumn=sortcolumn, descending=descending,
432 432 **dict(sort))
433 433
434 434 def templater(self, req):
435 435
436 436 def motd(**map):
437 437 if self.motd is not None:
438 438 yield self.motd
439 439 else:
440 440 yield config('web', 'motd', '')
441 441
442 442 def config(section, name, default=None, untrusted=True):
443 443 return self.ui.config(section, name, default, untrusted)
444 444
445 445 self.updatereqenv(req.env)
446 446
447 447 url = req.env.get('SCRIPT_NAME', '')
448 448 if not url.endswith('/'):
449 449 url += '/'
450 450
451 451 vars = {}
452 452 styles = (
453 453 req.form.get('style', [None])[0],
454 454 config('web', 'style'),
455 455 'paper'
456 456 )
457 457 style, mapfile = templater.stylemap(styles, self.templatepath)
458 458 if style == styles[0]:
459 459 vars['style'] = style
460 460
461 461 start = url[-1] == '?' and '&' or '?'
462 462 sessionvars = webutil.sessionvars(vars, start)
463 463 logourl = config('web', 'logourl', 'https://mercurial-scm.org/')
464 464 logoimg = config('web', 'logoimg', 'hglogo.png')
465 465 staticurl = config('web', 'staticurl') or url + 'static/'
466 466 if not staticurl.endswith('/'):
467 467 staticurl += '/'
468 468
469 469 tmpl = templater.templater(mapfile,
470 470 defaults={"encoding": encoding.encoding,
471 471 "motd": motd,
472 472 "url": url,
473 473 "logourl": logourl,
474 474 "logoimg": logoimg,
475 475 "staticurl": staticurl,
476 476 "sessionvars": sessionvars,
477 477 "style": style,
478 478 })
479 479 return tmpl
480 480
481 481 def updatereqenv(self, env):
482 482 if self._baseurl is not None:
483 483 name, port, path = geturlcgivars(self._baseurl, env['SERVER_PORT'])
484 484 env['SERVER_NAME'] = name
485 485 env['SERVER_PORT'] = port
486 486 env['SCRIPT_NAME'] = path
General Comments 0
You need to be logged in to leave comments. Login now