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