annotate rhodecode/lib/hooks.py @ 635:fd63782c4426 beta

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