Show More
@@ -1,203 +1,205 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
4 | 4 | # Copyright (C) 2014-2019 RhodeCode GmbH |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or modify |
|
7 | 7 | # it under the terms of the GNU General Public License as published by |
|
8 | 8 | # the Free Software Foundation; either version 3 of the License, or |
|
9 | 9 | # (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software Foundation, |
|
18 | 18 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 | 19 | |
|
20 | 20 | import re |
|
21 | 21 | import os |
|
22 | 22 | import sys |
|
23 | 23 | import datetime |
|
24 | 24 | import logging |
|
25 | 25 | import pkg_resources |
|
26 | 26 | |
|
27 | 27 | import vcsserver |
|
28 | 28 | |
|
29 | 29 | log = logging.getLogger(__name__) |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | def get_git_hooks_path(repo_path, bare): |
|
33 | 33 | hooks_path = os.path.join(repo_path, 'hooks') |
|
34 | 34 | if not bare: |
|
35 | 35 | hooks_path = os.path.join(repo_path, '.git', 'hooks') |
|
36 | 36 | |
|
37 | 37 | return hooks_path |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | def install_git_hooks(repo_path, bare, executable=None, force_create=False): |
|
41 | 41 | """ |
|
42 | 42 | Creates a RhodeCode hook inside a git repository |
|
43 | 43 | |
|
44 | 44 | :param repo_path: path to repository |
|
45 | 45 | :param executable: binary executable to put in the hooks |
|
46 | 46 | :param force_create: Create even if same name hook exists |
|
47 | 47 | """ |
|
48 | 48 | executable = executable or sys.executable |
|
49 | 49 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
50 | 50 | |
|
51 | 51 | if not os.path.isdir(hooks_path): |
|
52 | 52 | os.makedirs(hooks_path, mode=0o777) |
|
53 | 53 | |
|
54 | 54 | tmpl_post = pkg_resources.resource_string( |
|
55 | 55 | 'vcsserver', '/'.join( |
|
56 | 56 | ('hook_utils', 'hook_templates', 'git_post_receive.py.tmpl'))) |
|
57 | 57 | tmpl_pre = pkg_resources.resource_string( |
|
58 | 58 | 'vcsserver', '/'.join( |
|
59 | 59 | ('hook_utils', 'hook_templates', 'git_pre_receive.py.tmpl'))) |
|
60 | 60 | |
|
61 | 61 | path = '' # not used for now |
|
62 | 62 | timestamp = datetime.datetime.utcnow().isoformat() |
|
63 | 63 | |
|
64 | 64 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: |
|
65 | 65 | log.debug('Installing git hook in repo %s', repo_path) |
|
66 | 66 | _hook_file = os.path.join(hooks_path, '%s-receive' % h_type) |
|
67 | 67 | _rhodecode_hook = check_rhodecode_hook(_hook_file) |
|
68 | 68 | |
|
69 | 69 | if _rhodecode_hook or force_create: |
|
70 | 70 | log.debug('writing git %s hook file at %s !', h_type, _hook_file) |
|
71 | 71 | try: |
|
72 | 72 | with open(_hook_file, 'wb') as f: |
|
73 | 73 | template = template.replace( |
|
74 | 74 | '_TMPL_', vcsserver.__version__) |
|
75 | 75 | template = template.replace('_DATE_', timestamp) |
|
76 | 76 | template = template.replace('_ENV_', executable) |
|
77 | 77 | template = template.replace('_PATH_', path) |
|
78 | 78 | f.write(template) |
|
79 | 79 | os.chmod(_hook_file, 0o755) |
|
80 | 80 | except IOError: |
|
81 | 81 | log.exception('error writing hook file %s', _hook_file) |
|
82 | 82 | else: |
|
83 | 83 | log.debug('skipping writing hook file') |
|
84 | 84 | |
|
85 | 85 | return True |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def get_svn_hooks_path(repo_path): |
|
89 | 89 | hooks_path = os.path.join(repo_path, 'hooks') |
|
90 | 90 | |
|
91 | 91 | return hooks_path |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def install_svn_hooks(repo_path, executable=None, force_create=False): |
|
95 | 95 | """ |
|
96 | 96 | Creates RhodeCode hooks inside a svn repository |
|
97 | 97 | |
|
98 | 98 | :param repo_path: path to repository |
|
99 | 99 | :param executable: binary executable to put in the hooks |
|
100 | 100 | :param force_create: Create even if same name hook exists |
|
101 | 101 | """ |
|
102 | 102 | executable = executable or sys.executable |
|
103 | 103 | hooks_path = get_svn_hooks_path(repo_path) |
|
104 | 104 | if not os.path.isdir(hooks_path): |
|
105 | 105 | os.makedirs(hooks_path, mode=0o777) |
|
106 | 106 | |
|
107 | 107 | tmpl_post = pkg_resources.resource_string( |
|
108 | 108 | 'vcsserver', '/'.join( |
|
109 | 109 | ('hook_utils', 'hook_templates', 'svn_post_commit_hook.py.tmpl'))) |
|
110 | 110 | tmpl_pre = pkg_resources.resource_string( |
|
111 | 111 | 'vcsserver', '/'.join( |
|
112 | 112 | ('hook_utils', 'hook_templates', 'svn_pre_commit_hook.py.tmpl'))) |
|
113 | 113 | |
|
114 | 114 | path = '' # not used for now |
|
115 | 115 | timestamp = datetime.datetime.utcnow().isoformat() |
|
116 | 116 | |
|
117 | 117 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: |
|
118 | 118 | log.debug('Installing svn hook in repo %s', repo_path) |
|
119 | 119 | _hook_file = os.path.join(hooks_path, '%s-commit' % h_type) |
|
120 | 120 | _rhodecode_hook = check_rhodecode_hook(_hook_file) |
|
121 | 121 | |
|
122 | 122 | if _rhodecode_hook or force_create: |
|
123 | 123 | log.debug('writing svn %s hook file at %s !', h_type, _hook_file) |
|
124 | 124 | |
|
125 | 125 | try: |
|
126 | 126 | with open(_hook_file, 'wb') as f: |
|
127 | 127 | template = template.replace( |
|
128 | 128 | '_TMPL_', vcsserver.__version__) |
|
129 | 129 | template = template.replace('_DATE_', timestamp) |
|
130 | 130 | template = template.replace('_ENV_', executable) |
|
131 | 131 | template = template.replace('_PATH_', path) |
|
132 | 132 | |
|
133 | 133 | f.write(template) |
|
134 | 134 | os.chmod(_hook_file, 0o755) |
|
135 | 135 | except IOError: |
|
136 | 136 | log.exception('error writing hook file %s', _hook_file) |
|
137 | 137 | else: |
|
138 | 138 | log.debug('skipping writing hook file') |
|
139 | 139 | |
|
140 | 140 | return True |
|
141 | 141 | |
|
142 | 142 | |
|
143 | 143 | def get_version_from_hook(hook_path): |
|
144 | 144 | version = '' |
|
145 | 145 | hook_content = read_hook_content(hook_path) |
|
146 | 146 | matches = re.search(r'(?:RC_HOOK_VER)\s*=\s*(.*)', hook_content) |
|
147 | 147 | if matches: |
|
148 | 148 | try: |
|
149 | 149 | version = matches.groups()[0] |
|
150 | 150 | log.debug('got version %s from hooks.', version) |
|
151 | 151 | except Exception: |
|
152 | 152 | log.exception("Exception while reading the hook version.") |
|
153 | 153 | return version.replace("'", "") |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | def check_rhodecode_hook(hook_path): |
|
157 | 157 | """ |
|
158 | 158 | Check if the hook was created by RhodeCode |
|
159 | 159 | """ |
|
160 | 160 | if not os.path.exists(hook_path): |
|
161 | 161 | return True |
|
162 | 162 | |
|
163 | 163 | log.debug('hook exists, checking if it is from RhodeCode') |
|
164 | 164 | |
|
165 | 165 | version = get_version_from_hook(hook_path) |
|
166 | 166 | if version: |
|
167 | 167 | return True |
|
168 | 168 | |
|
169 | 169 | return False |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | def read_hook_content(hook_path): |
|
173 | content = '' | |
|
174 | if os.path.isfile(hook_path): | |
|
173 | 175 | with open(hook_path, 'rb') as f: |
|
174 | 176 | content = f.read() |
|
175 | 177 | return content |
|
176 | 178 | |
|
177 | 179 | |
|
178 | 180 | def get_git_pre_hook_version(repo_path, bare): |
|
179 | 181 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
180 | 182 | _hook_file = os.path.join(hooks_path, 'pre-receive') |
|
181 | 183 | version = get_version_from_hook(_hook_file) |
|
182 | 184 | return version |
|
183 | 185 | |
|
184 | 186 | |
|
185 | 187 | def get_git_post_hook_version(repo_path, bare): |
|
186 | 188 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
187 | 189 | _hook_file = os.path.join(hooks_path, 'post-receive') |
|
188 | 190 | version = get_version_from_hook(_hook_file) |
|
189 | 191 | return version |
|
190 | 192 | |
|
191 | 193 | |
|
192 | 194 | def get_svn_pre_hook_version(repo_path): |
|
193 | 195 | hooks_path = get_svn_hooks_path(repo_path) |
|
194 | 196 | _hook_file = os.path.join(hooks_path, 'pre-commit') |
|
195 | 197 | version = get_version_from_hook(_hook_file) |
|
196 | 198 | return version |
|
197 | 199 | |
|
198 | 200 | |
|
199 | 201 | def get_svn_post_hook_version(repo_path): |
|
200 | 202 | hooks_path = get_svn_hooks_path(repo_path) |
|
201 | 203 | _hook_file = os.path.join(hooks_path, 'post-commit') |
|
202 | 204 | version = get_version_from_hook(_hook_file) |
|
203 | 205 | return version |
General Comments 0
You need to be logged in to leave comments.
Login now