annotate pylons_app/controllers/hg.py @ 56:bf1b64046c79

Added last change translation to 'time ago', added generation of enabled zip archives
author Marcin Kuzminski <marcin@python-blog.com>
date Fri, 09 Apr 2010 00:30:42 +0200
parents e00dccb6f211
children e96bc5a01490
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 #!/usr/bin/python
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 # -*- coding: utf-8 -*-
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 import logging
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
4 import os
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents: 41
diff changeset
5 from pylons_app.lib.base import BaseController
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
6 from pylons import tmpl_context as c, app_globals as g, session, request, config
22
3142616771cd Removed default contact name
Marcin Kuzminski
parents: 21
diff changeset
7 from pylons_app.lib import helpers as h
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
8 from mako.template import Template
37
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
9 from pylons.controllers.util import abort
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
10 from pylons_app.lib.base import BaseController, render
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
11 try:
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
12 from vcs.backends.hg import get_repositories
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
13 except ImportError:
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
14 print 'You have to import vcs module'
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
15 from mercurial.util import matchdate, Abort, makedate
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
16 from mercurial.hgweb.common import get_contact
56
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
17 from mercurial.templatefilters import age
10
525ed90e4577 major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents: 8
diff changeset
18 log = logging.getLogger(__name__)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
19
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
20 class HgController(BaseController):
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
22 def __before__(self):
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
23 c.repos_prefix = config['repos_name']
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
24 c.staticurl = g.statics
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
25
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
26 def index(self):
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
27 c.repos_list = []
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
28
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
29 def get_mtime(spath):
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
30 cl_path = os.path.join(spath, "00changelog.i")
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
31 if os.path.exists(cl_path):
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
32 return os.stat(cl_path).st_mtime
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
33 else:
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
34 return os.stat(spath).st_mtime
56
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
35
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
36 def archivelist(ui, nodeid, url):
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
37 allowed = g.baseui.configlist("web", "allow_archive", untrusted=True)
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
38 for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
39 if i[0] in allowed or ui.configbool("web", "allow" + i[0],
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
40 untrusted=True):
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
41 yield {"type" : i[0], "extension": i[1],
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
42 "node": nodeid, "url": url}
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
43
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
44 for name, r in get_repositories(g.paths[0][0], g.paths[0][1]).items():
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
45 last_change = (get_mtime(r.spath), makedate()[1])
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
46 tip = r.changectx('tip')
56
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
47 tmp_d = {}
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
48 tmp_d['name'] = name
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
49 tmp_d['desc'] = r.ui.config('web', 'description', 'Unknown', untrusted=True)
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
50 tmp_d['last_change'] = age(last_change)
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
51 tmp_d['tip'] = str(tip)
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
52 tmp_d['rev'] = tip.rev()
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
53 tmp_d['contact'] = get_contact(r.ui.config)
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
54 tmp_d['repo_archives'] = archivelist(r.ui, "tip", 'sa')
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
55
bf1b64046c79 Added last change translation to 'time ago', added generation of enabled zip archives
Marcin Kuzminski <marcin@python-blog.com>
parents: 55
diff changeset
56 c.repos_list.append(tmp_d)
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
57 return render('/index.html')
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
58
8
3092016c6d0c Changed to webapp, removed get from routes,
Marcin Kuzminski
parents: 6
diff changeset
59 def view(self, *args, **kwargs):
55
e00dccb6f211 Implemented index page using vcs
Marcin Kuzminski <marcin@python-blog.com>
parents: 43
diff changeset
60 #TODO: reimplement this not tu use hgwebdir
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
61 response = g.hgapp(request.environ, self.start_response)
37
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
62
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
63 http_accept = request.environ.get('HTTP_ACCEPT', False)
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
64 if not http_accept:
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
65 return abort(status_code=400, detail='no http accept in header')
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
66
31
2963f2894a7a Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents: 22
diff changeset
67 #for mercurial protocols and raw files we can't wrap into mako
37
707dfdb1c7a8 Bugfix when client is using old mercurial version and not setting http accept
marcink
parents: 32
diff changeset
68 if http_accept.find("mercurial") != -1 or \
31
2963f2894a7a Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents: 22
diff changeset
69 request.environ['PATH_INFO'].find('raw-file') != -1:
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
70 return response
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
71 try:
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
72 tmpl = u''.join(response)
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
73 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
74 .config['pylons.app_globals'].mako_lookup)
32
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
75
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
76 except (RuntimeError, UnicodeDecodeError):
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
77 log.info('disabling unicode due to encoding error')
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
78 response = g.hgapp(request.environ, self.start_response)
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
79 tmpl = ''.join(response)
f93b523c0be3 dirty fix for multiple file encodings,
Marcin Kuzminski <marcin@python-blog.com>
parents: 31
diff changeset
80 template = Template(tmpl, lookup=request.environ['pylons.pylons']\
41
71ffa932799d Added app basic auth.
Marcin Kuzminski <marcin@python-blog.com>
parents: 37
diff changeset
81 .config['pylons.app_globals'].mako_lookup, disable_unicode=True)
31
2963f2894a7a Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents: 22
diff changeset
82
21
fac1f62a1d71 Wrapped into mako templates,
Marcin Kuzminski
parents: 20
diff changeset
83
31
2963f2894a7a Tempalting change, bugfix for serving raw files, and diffs. Now raw files are not parsed thruough mako, and diffs are mako safe (not parsed also)
Marcin Kuzminski <marcin@python-blog.com>
parents: 22
diff changeset
84 return template.render(g=g, c=c, session=session, h=h)