comparison pylons_app/model/hg_model.py @ 58:8fb1abd4178a

Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
author Marcin Kuzminski <marcin@python-blog.com>
date Fri, 09 Apr 2010 03:00:20 +0200
parents
children 9f956354807b
comparison
equal deleted inserted replaced
57:e96bc5a01490 58:8fb1abd4178a
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright (c) 2010 marcink. All rights reserved.
5 #
6 '''
7 Created on Apr 9, 2010
8
9 @author: marcink
10 '''
11 import os
12 from pylons_app.lib.base import BaseController
13 from pylons import tmpl_context as c, app_globals as g, session, request, config
14 from pylons_app.lib import helpers as h
15 from mako.template import Template
16 from pylons.controllers.util import abort
17 from pylons_app.lib.base import BaseController, render
18 try:
19 from vcs.backends.hg import get_repositories
20 except ImportError:
21 print 'You have to import vcs module'
22 from mercurial.util import matchdate, Abort, makedate
23 from mercurial.hgweb.common import get_contact
24 from mercurial.templatefilters import age
25 from operator import itemgetter
26
27 class HgModel(object):
28 """
29 Mercurial Model
30 """
31
32
33 def __init__(self):
34 """
35 Constructor
36 """
37
38
39 def get_mtime(self, spath):
40 cl_path = os.path.join(spath, "00changelog.i")
41 if os.path.exists(cl_path):
42 return os.stat(cl_path).st_mtime
43 else:
44 return os.stat(spath).st_mtime
45
46 def archivelist(self, ui, nodeid, url):
47 allowed = g.baseui.configlist("web", "allow_archive", untrusted=True)
48 for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
49 if i[0] in allowed or ui.configbool("web", "allow" + i[0],
50 untrusted=True):
51 yield {"type" : i[0], "extension": i[1],
52 "node": nodeid, "url": url}
53
54 def get_repos(self):
55 for name, r in get_repositories(g.paths[0][0], g.paths[0][1]).items():
56 last_change = (self.get_mtime(r.spath), makedate()[1])
57 tip = r.changectx('tip')
58 tmp_d = {}
59 tmp_d['name'] = name
60 tmp_d['name_sort'] = tmp_d['name']
61 tmp_d['description'] = r.ui.config('web', 'description', 'Unknown', untrusted=True)
62 tmp_d['description_sort'] = tmp_d['description']
63 tmp_d['last_change'] = age(last_change)
64 tmp_d['last_change_sort'] = last_change[1] - last_change[0]
65 tmp_d['tip'] = str(tip)
66 tmp_d['tip_sort'] = tip.rev()
67 tmp_d['rev'] = tip.rev()
68 tmp_d['contact'] = get_contact(r.ui.config)
69 tmp_d['contact_sort'] = get_contact(r.ui.config)
70 tmp_d['repo_archives'] = self.archivelist(r.ui, "tip", 'sa')
71
72 yield tmp_d