comparison rhodecode/lib/hooks.py @ 547:1e757ac98988

renamed project to rhodecode
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 06 Oct 2010 03:18:16 +0200
parents pylons_app/lib/hooks.py@39203995f2c4
children 5cc96df705b9
comparison
equal deleted inserted replaced
546:7c2f5e4d7bbf 547:1e757ac98988
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # custom hooks for application
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20 """
21 Created on Aug 6, 2010
22
23 @author: marcink
24 """
25
26 import sys
27 import os
28 from rhodecode.lib import helpers as h
29 from rhodecode.model import meta
30 from rhodecode.model.db import UserLog, User
31
32 def repo_size(ui, repo, hooktype=None, **kwargs):
33
34 if hooktype != 'changegroup':
35 return False
36 size_hg, size_root = 0, 0
37 for path, dirs, files in os.walk(repo.root):
38 if path.find('.hg') != -1:
39 for f in files:
40 size_hg += os.path.getsize(os.path.join(path, f))
41 else:
42 for f in files:
43 size_root += os.path.getsize(os.path.join(path, f))
44
45 size_hg_f = h.format_byte_size(size_hg)
46 size_root_f = h.format_byte_size(size_root)
47 size_total_f = h.format_byte_size(size_root + size_hg)
48 sys.stdout.write('Repository size .hg:%s repo:%s total:%s\n' \
49 % (size_hg_f, size_root_f, size_total_f))
50
51 user_action_mapper(ui, repo, hooktype, **kwargs)
52
53 def user_action_mapper(ui, repo, hooktype=None, **kwargs):
54 """
55 Maps user last push action to new changeset id, from mercurial
56 @param ui:
57 @param repo:
58 @param hooktype:
59 """
60
61 try:
62 sa = meta.Session
63 username = kwargs['url'].split(':')[-1]
64 user_log = sa.query(UserLog)\
65 .filter(UserLog.user == sa.query(User)\
66 .filter(User.username == username).one())\
67 .order_by(UserLog.user_log_id.desc()).first()
68
69 if user_log and not user_log.revision:
70 user_log.revision = str(repo['tip'])
71 sa.add(user_log)
72 sa.commit()
73
74 except Exception, e:
75 sa.rollback()
76 raise
77 finally:
78 meta.Session.remove()