##// END OF EJS Templates
made repo-size hook more generic
marcink -
r2196:7ccf403b beta
parent child Browse files
Show More
@@ -1,174 +1,184 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.lib.hooks
3 rhodecode.lib.hooks
4 ~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~
5
5
6 Hooks runned by rhodecode
6 Hooks runned by rhodecode
7
7
8 :created_on: Aug 6, 2010
8 :created_on: Aug 6, 2010
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import os
25 import os
26 import sys
26 import sys
27
27
28 from mercurial.scmutil import revrange
28 from mercurial.scmutil import revrange
29 from mercurial.node import nullrev
29 from mercurial.node import nullrev
30 from rhodecode import EXTENSIONS
30 from rhodecode import EXTENSIONS
31 from rhodecode.lib import helpers as h
31 from rhodecode.lib import helpers as h
32 from rhodecode.lib.utils import action_logger
32 from rhodecode.lib.utils import action_logger
33 from inspect import isfunction
33 from inspect import isfunction
34
34
35
35
36 def _get_scm_size(alias, root_path):
37
38 if not alias.startswith('.'):
39 alias += '.'
40
41 size_scm, size_root = 0, 0
42 for path, dirs, files in os.walk(root_path):
43 if path.find(alias) != -1:
44 for f in files:
45 try:
46 size_scm += os.path.getsize(os.path.join(path, f))
47 except OSError:
48 pass
49 else:
50 for f in files:
51 try:
52 size_root += os.path.getsize(os.path.join(path, f))
53 except OSError:
54 pass
55
56 size_scm_f = h.format_byte_size(size_scm)
57 size_root_f = h.format_byte_size(size_root)
58 size_total_f = h.format_byte_size(size_root + size_scm)
59
60 return size_scm_f, size_root_f, size_total_f
61
62
36 def repo_size(ui, repo, hooktype=None, **kwargs):
63 def repo_size(ui, repo, hooktype=None, **kwargs):
37 """
64 """
38 Presents size of repository after push
65 Presents size of repository after push
39
66
40 :param ui:
67 :param ui:
41 :param repo:
68 :param repo:
42 :param hooktype:
69 :param hooktype:
43 """
70 """
44
71
45 size_hg, size_root = 0, 0
72 size_hg_f, size_root_f, size_total_f = _get_scm_size('.hg', repo.root)
46 for path, dirs, files in os.walk(repo.root):
47 if path.find('.hg') != -1:
48 for f in files:
49 try:
50 size_hg += os.path.getsize(os.path.join(path, f))
51 except OSError:
52 pass
53 else:
54 for f in files:
55 try:
56 size_root += os.path.getsize(os.path.join(path, f))
57 except OSError:
58 pass
59
60 size_hg_f = h.format_byte_size(size_hg)
61 size_root_f = h.format_byte_size(size_root)
62 size_total_f = h.format_byte_size(size_root + size_hg)
63
73
64 last_cs = repo[len(repo) - 1]
74 last_cs = repo[len(repo) - 1]
65
75
66 msg = ('Repository size .hg:%s repo:%s total:%s\n'
76 msg = ('Repository size .hg:%s repo:%s total:%s\n'
67 'Last revision is now r%s:%s\n') % (
77 'Last revision is now r%s:%s\n') % (
68 size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12]
78 size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12]
69 )
79 )
70
80
71 sys.stdout.write(msg)
81 sys.stdout.write(msg)
72
82
73
83
74 def log_pull_action(ui, repo, **kwargs):
84 def log_pull_action(ui, repo, **kwargs):
75 """
85 """
76 Logs user last pull action
86 Logs user last pull action
77
87
78 :param ui:
88 :param ui:
79 :param repo:
89 :param repo:
80 """
90 """
81
91
82 extras = dict(repo.ui.configitems('rhodecode_extras'))
92 extras = dict(repo.ui.configitems('rhodecode_extras'))
83 username = extras['username']
93 username = extras['username']
84 repository = extras['repository']
94 repository = extras['repository']
85 action = 'pull'
95 action = 'pull'
86
96
87 action_logger(username, action, repository, extras['ip'], commit=True)
97 action_logger(username, action, repository, extras['ip'], commit=True)
88 # extension hook call
98 # extension hook call
89 callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
99 callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
90
100
91 if isfunction(callback):
101 if isfunction(callback):
92 kw = {}
102 kw = {}
93 kw.update(extras)
103 kw.update(extras)
94 callback(**kw)
104 callback(**kw)
95 return 0
105 return 0
96
106
97
107
98 def log_push_action(ui, repo, **kwargs):
108 def log_push_action(ui, repo, **kwargs):
99 """
109 """
100 Maps user last push action to new changeset id, from mercurial
110 Maps user last push action to new changeset id, from mercurial
101
111
102 :param ui:
112 :param ui:
103 :param repo:
113 :param repo:
104 """
114 """
105
115
106 extras = dict(repo.ui.configitems('rhodecode_extras'))
116 extras = dict(repo.ui.configitems('rhodecode_extras'))
107 username = extras['username']
117 username = extras['username']
108 repository = extras['repository']
118 repository = extras['repository']
109 action = extras['action'] + ':%s'
119 action = extras['action'] + ':%s'
110 node = kwargs['node']
120 node = kwargs['node']
111
121
112 def get_revs(repo, rev_opt):
122 def get_revs(repo, rev_opt):
113 if rev_opt:
123 if rev_opt:
114 revs = revrange(repo, rev_opt)
124 revs = revrange(repo, rev_opt)
115
125
116 if len(revs) == 0:
126 if len(revs) == 0:
117 return (nullrev, nullrev)
127 return (nullrev, nullrev)
118 return (max(revs), min(revs))
128 return (max(revs), min(revs))
119 else:
129 else:
120 return (len(repo) - 1, 0)
130 return (len(repo) - 1, 0)
121
131
122 stop, start = get_revs(repo, [node + ':'])
132 stop, start = get_revs(repo, [node + ':'])
123
133
124 revs = (str(repo[r]) for r in xrange(start, stop + 1))
134 revs = (str(repo[r]) for r in xrange(start, stop + 1))
125
135
126 action = action % ','.join(revs)
136 action = action % ','.join(revs)
127
137
128 action_logger(username, action, repository, extras['ip'], commit=True)
138 action_logger(username, action, repository, extras['ip'], commit=True)
129
139
130 # extension hook call
140 # extension hook call
131 callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
141 callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
132 if isfunction(callback):
142 if isfunction(callback):
133 kw = {'pushed_revs': revs}
143 kw = {'pushed_revs': revs}
134 kw.update(extras)
144 kw.update(extras)
135 callback(**kw)
145 callback(**kw)
136 return 0
146 return 0
137
147
138
148
139 def log_create_repository(repository_dict, created_by, **kwargs):
149 def log_create_repository(repository_dict, created_by, **kwargs):
140 """
150 """
141 Post create repository Hook. This is a dummy function for admins to re-use
151 Post create repository Hook. This is a dummy function for admins to re-use
142 if needed. It's taken from rhodecode-extensions module and executed
152 if needed. It's taken from rhodecode-extensions module and executed
143 if present
153 if present
144
154
145 :param repository: dict dump of repository object
155 :param repository: dict dump of repository object
146 :param created_by: username who created repository
156 :param created_by: username who created repository
147 :param created_date: date of creation
157 :param created_date: date of creation
148
158
149 available keys of repository_dict:
159 available keys of repository_dict:
150
160
151 'repo_type',
161 'repo_type',
152 'description',
162 'description',
153 'private',
163 'private',
154 'created_on',
164 'created_on',
155 'enable_downloads',
165 'enable_downloads',
156 'repo_id',
166 'repo_id',
157 'user_id',
167 'user_id',
158 'enable_statistics',
168 'enable_statistics',
159 'clone_uri',
169 'clone_uri',
160 'fork_id',
170 'fork_id',
161 'group_id',
171 'group_id',
162 'repo_name'
172 'repo_name'
163
173
164 """
174 """
165
175
166 callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None)
176 callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None)
167 if isfunction(callback):
177 if isfunction(callback):
168 kw = {}
178 kw = {}
169 kw.update(repository_dict)
179 kw.update(repository_dict)
170 kw.update({'created_by': created_by})
180 kw.update({'created_by': created_by})
171 kw.update(kwargs)
181 kw.update(kwargs)
172 return callback(**kw)
182 return callback(**kw)
173
183
174 return 0
184 return 0
General Comments 0
You need to be logged in to leave comments. Login now