##// END OF EJS Templates
fill miliseconds with 0 since it can return <6 digit number
marcink -
r2592:f6800c5e beta
parent child Browse files
Show More
@@ -1,539 +1,539 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.model.repo
3 rhodecode.model.repo
4 ~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~
5
5
6 Repository model for rhodecode
6 Repository model for rhodecode
7
7
8 :created_on: Jun 5, 2010
8 :created_on: Jun 5, 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 from __future__ import with_statement
25 from __future__ import with_statement
26 import os
26 import os
27 import shutil
27 import shutil
28 import logging
28 import logging
29 import traceback
29 import traceback
30 import pkg_resources
30 import pkg_resources
31 from os.path import dirname as dn, join as jn
31 from os.path import dirname as dn, join as jn
32 from datetime import datetime
32 from datetime import datetime
33
33
34 from rhodecode.lib.vcs.backends import get_backend
34 from rhodecode.lib.vcs.backends import get_backend
35 from rhodecode.lib.compat import json
35 from rhodecode.lib.compat import json
36 from rhodecode.lib.utils2 import LazyProperty, safe_str, safe_unicode
36 from rhodecode.lib.utils2 import LazyProperty, safe_str, safe_unicode
37 from rhodecode.lib.caching_query import FromCache
37 from rhodecode.lib.caching_query import FromCache
38 from rhodecode.lib.hooks import log_create_repository
38 from rhodecode.lib.hooks import log_create_repository
39
39
40 from rhodecode.model import BaseModel
40 from rhodecode.model import BaseModel
41 from rhodecode.model.db import Repository, UserRepoToPerm, User, Permission, \
41 from rhodecode.model.db import Repository, UserRepoToPerm, User, Permission, \
42 Statistics, UsersGroup, UsersGroupRepoToPerm, RhodeCodeUi, RepoGroup
42 Statistics, UsersGroup, UsersGroupRepoToPerm, RhodeCodeUi, RepoGroup
43 from rhodecode.lib import helpers as h
43 from rhodecode.lib import helpers as h
44
44
45
45
46 log = logging.getLogger(__name__)
46 log = logging.getLogger(__name__)
47
47
48
48
49 class RepoModel(BaseModel):
49 class RepoModel(BaseModel):
50
50
51 cls = Repository
51 cls = Repository
52 URL_SEPARATOR = Repository.url_sep()
52 URL_SEPARATOR = Repository.url_sep()
53
53
54 def __get_users_group(self, users_group):
54 def __get_users_group(self, users_group):
55 return self._get_instance(UsersGroup, users_group,
55 return self._get_instance(UsersGroup, users_group,
56 callback=UsersGroup.get_by_group_name)
56 callback=UsersGroup.get_by_group_name)
57
57
58 def __get_repos_group(self, repos_group):
58 def __get_repos_group(self, repos_group):
59 return self._get_instance(RepoGroup, repos_group,
59 return self._get_instance(RepoGroup, repos_group,
60 callback=RepoGroup.get_by_group_name)
60 callback=RepoGroup.get_by_group_name)
61
61
62 @LazyProperty
62 @LazyProperty
63 def repos_path(self):
63 def repos_path(self):
64 """
64 """
65 Get's the repositories root path from database
65 Get's the repositories root path from database
66 """
66 """
67
67
68 q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
68 q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
69 return q.ui_value
69 return q.ui_value
70
70
71 def get(self, repo_id, cache=False):
71 def get(self, repo_id, cache=False):
72 repo = self.sa.query(Repository)\
72 repo = self.sa.query(Repository)\
73 .filter(Repository.repo_id == repo_id)
73 .filter(Repository.repo_id == repo_id)
74
74
75 if cache:
75 if cache:
76 repo = repo.options(FromCache("sql_cache_short",
76 repo = repo.options(FromCache("sql_cache_short",
77 "get_repo_%s" % repo_id))
77 "get_repo_%s" % repo_id))
78 return repo.scalar()
78 return repo.scalar()
79
79
80 def get_repo(self, repository):
80 def get_repo(self, repository):
81 return self._get_repo(repository)
81 return self._get_repo(repository)
82
82
83 def get_by_repo_name(self, repo_name, cache=False):
83 def get_by_repo_name(self, repo_name, cache=False):
84 repo = self.sa.query(Repository)\
84 repo = self.sa.query(Repository)\
85 .filter(Repository.repo_name == repo_name)
85 .filter(Repository.repo_name == repo_name)
86
86
87 if cache:
87 if cache:
88 repo = repo.options(FromCache("sql_cache_short",
88 repo = repo.options(FromCache("sql_cache_short",
89 "get_repo_%s" % repo_name))
89 "get_repo_%s" % repo_name))
90 return repo.scalar()
90 return repo.scalar()
91
91
92 def get_users_js(self):
92 def get_users_js(self):
93 users = self.sa.query(User).filter(User.active == True).all()
93 users = self.sa.query(User).filter(User.active == True).all()
94 return json.dumps([
94 return json.dumps([
95 {
95 {
96 'id': u.user_id,
96 'id': u.user_id,
97 'fname': u.name,
97 'fname': u.name,
98 'lname': u.lastname,
98 'lname': u.lastname,
99 'nname': u.username,
99 'nname': u.username,
100 'gravatar_lnk': h.gravatar_url(u.email, 14)
100 'gravatar_lnk': h.gravatar_url(u.email, 14)
101 } for u in users]
101 } for u in users]
102 )
102 )
103
103
104 def get_users_groups_js(self):
104 def get_users_groups_js(self):
105 users_groups = self.sa.query(UsersGroup)\
105 users_groups = self.sa.query(UsersGroup)\
106 .filter(UsersGroup.users_group_active == True).all()
106 .filter(UsersGroup.users_group_active == True).all()
107
107
108 return json.dumps([
108 return json.dumps([
109 {
109 {
110 'id': gr.users_group_id,
110 'id': gr.users_group_id,
111 'grname': gr.users_group_name,
111 'grname': gr.users_group_name,
112 'grmembers': len(gr.members),
112 'grmembers': len(gr.members),
113 } for gr in users_groups]
113 } for gr in users_groups]
114 )
114 )
115
115
116 def _get_defaults(self, repo_name):
116 def _get_defaults(self, repo_name):
117 """
117 """
118 Get's information about repository, and returns a dict for
118 Get's information about repository, and returns a dict for
119 usage in forms
119 usage in forms
120
120
121 :param repo_name:
121 :param repo_name:
122 """
122 """
123
123
124 repo_info = Repository.get_by_repo_name(repo_name)
124 repo_info = Repository.get_by_repo_name(repo_name)
125
125
126 if repo_info is None:
126 if repo_info is None:
127 return None
127 return None
128
128
129 defaults = repo_info.get_dict()
129 defaults = repo_info.get_dict()
130 group, repo_name = repo_info.groups_and_repo
130 group, repo_name = repo_info.groups_and_repo
131 defaults['repo_name'] = repo_name
131 defaults['repo_name'] = repo_name
132 defaults['repo_group'] = getattr(group[-1] if group else None,
132 defaults['repo_group'] = getattr(group[-1] if group else None,
133 'group_id', None)
133 'group_id', None)
134
134
135 # fill owner
135 # fill owner
136 if repo_info.user:
136 if repo_info.user:
137 defaults.update({'user': repo_info.user.username})
137 defaults.update({'user': repo_info.user.username})
138 else:
138 else:
139 replacement_user = User.query().filter(User.admin ==
139 replacement_user = User.query().filter(User.admin ==
140 True).first().username
140 True).first().username
141 defaults.update({'user': replacement_user})
141 defaults.update({'user': replacement_user})
142
142
143 # fill repository users
143 # fill repository users
144 for p in repo_info.repo_to_perm:
144 for p in repo_info.repo_to_perm:
145 defaults.update({'u_perm_%s' % p.user.username:
145 defaults.update({'u_perm_%s' % p.user.username:
146 p.permission.permission_name})
146 p.permission.permission_name})
147
147
148 # fill repository groups
148 # fill repository groups
149 for p in repo_info.users_group_to_perm:
149 for p in repo_info.users_group_to_perm:
150 defaults.update({'g_perm_%s' % p.users_group.users_group_name:
150 defaults.update({'g_perm_%s' % p.users_group.users_group_name:
151 p.permission.permission_name})
151 p.permission.permission_name})
152
152
153 return defaults
153 return defaults
154
154
155 def update(self, repo_name, form_data):
155 def update(self, repo_name, form_data):
156 try:
156 try:
157 cur_repo = self.get_by_repo_name(repo_name, cache=False)
157 cur_repo = self.get_by_repo_name(repo_name, cache=False)
158
158
159 # update permissions
159 # update permissions
160 for member, perm, member_type in form_data['perms_updates']:
160 for member, perm, member_type in form_data['perms_updates']:
161 if member_type == 'user':
161 if member_type == 'user':
162 # this updates existing one
162 # this updates existing one
163 RepoModel().grant_user_permission(
163 RepoModel().grant_user_permission(
164 repo=cur_repo, user=member, perm=perm
164 repo=cur_repo, user=member, perm=perm
165 )
165 )
166 else:
166 else:
167 RepoModel().grant_users_group_permission(
167 RepoModel().grant_users_group_permission(
168 repo=cur_repo, group_name=member, perm=perm
168 repo=cur_repo, group_name=member, perm=perm
169 )
169 )
170 # set new permissions
170 # set new permissions
171 for member, perm, member_type in form_data['perms_new']:
171 for member, perm, member_type in form_data['perms_new']:
172 if member_type == 'user':
172 if member_type == 'user':
173 RepoModel().grant_user_permission(
173 RepoModel().grant_user_permission(
174 repo=cur_repo, user=member, perm=perm
174 repo=cur_repo, user=member, perm=perm
175 )
175 )
176 else:
176 else:
177 RepoModel().grant_users_group_permission(
177 RepoModel().grant_users_group_permission(
178 repo=cur_repo, group_name=member, perm=perm
178 repo=cur_repo, group_name=member, perm=perm
179 )
179 )
180
180
181 # update current repo
181 # update current repo
182 for k, v in form_data.items():
182 for k, v in form_data.items():
183 if k == 'user':
183 if k == 'user':
184 cur_repo.user = User.get_by_username(v)
184 cur_repo.user = User.get_by_username(v)
185 elif k == 'repo_name':
185 elif k == 'repo_name':
186 pass
186 pass
187 elif k == 'repo_group':
187 elif k == 'repo_group':
188 cur_repo.group = RepoGroup.get(v)
188 cur_repo.group = RepoGroup.get(v)
189
189
190 else:
190 else:
191 setattr(cur_repo, k, v)
191 setattr(cur_repo, k, v)
192
192
193 new_name = cur_repo.get_new_name(form_data['repo_name'])
193 new_name = cur_repo.get_new_name(form_data['repo_name'])
194 cur_repo.repo_name = new_name
194 cur_repo.repo_name = new_name
195
195
196 self.sa.add(cur_repo)
196 self.sa.add(cur_repo)
197
197
198 if repo_name != new_name:
198 if repo_name != new_name:
199 # rename repository
199 # rename repository
200 self.__rename_repo(old=repo_name, new=new_name)
200 self.__rename_repo(old=repo_name, new=new_name)
201
201
202 return cur_repo
202 return cur_repo
203 except:
203 except:
204 log.error(traceback.format_exc())
204 log.error(traceback.format_exc())
205 raise
205 raise
206
206
207 def create_repo(self, repo_name, repo_type, description, owner,
207 def create_repo(self, repo_name, repo_type, description, owner,
208 private=False, clone_uri=None, repos_group=None,
208 private=False, clone_uri=None, repos_group=None,
209 landing_rev='tip', just_db=False, fork_of=None,
209 landing_rev='tip', just_db=False, fork_of=None,
210 copy_fork_permissions=False):
210 copy_fork_permissions=False):
211 from rhodecode.model.scm import ScmModel
211 from rhodecode.model.scm import ScmModel
212
212
213 owner = self._get_user(owner)
213 owner = self._get_user(owner)
214 fork_of = self._get_repo(fork_of)
214 fork_of = self._get_repo(fork_of)
215 repos_group = self.__get_repos_group(repos_group)
215 repos_group = self.__get_repos_group(repos_group)
216 try:
216 try:
217
217
218 # repo name is just a name of repository
218 # repo name is just a name of repository
219 # while repo_name_full is a full qualified name that is combined
219 # while repo_name_full is a full qualified name that is combined
220 # with name and path of group
220 # with name and path of group
221 repo_name_full = repo_name
221 repo_name_full = repo_name
222 repo_name = repo_name.split(self.URL_SEPARATOR)[-1]
222 repo_name = repo_name.split(self.URL_SEPARATOR)[-1]
223
223
224 new_repo = Repository()
224 new_repo = Repository()
225 new_repo.enable_statistics = False
225 new_repo.enable_statistics = False
226 new_repo.repo_name = repo_name_full
226 new_repo.repo_name = repo_name_full
227 new_repo.repo_type = repo_type
227 new_repo.repo_type = repo_type
228 new_repo.user = owner
228 new_repo.user = owner
229 new_repo.group = repos_group
229 new_repo.group = repos_group
230 new_repo.description = description or repo_name
230 new_repo.description = description or repo_name
231 new_repo.private = private
231 new_repo.private = private
232 new_repo.clone_uri = clone_uri
232 new_repo.clone_uri = clone_uri
233 new_repo.landing_rev = landing_rev
233 new_repo.landing_rev = landing_rev
234
234
235 if fork_of:
235 if fork_of:
236 parent_repo = fork_of
236 parent_repo = fork_of
237 new_repo.fork = parent_repo
237 new_repo.fork = parent_repo
238
238
239 self.sa.add(new_repo)
239 self.sa.add(new_repo)
240
240
241 def _create_default_perms():
241 def _create_default_perms():
242 # create default permission
242 # create default permission
243 repo_to_perm = UserRepoToPerm()
243 repo_to_perm = UserRepoToPerm()
244 default = 'repository.read'
244 default = 'repository.read'
245 for p in User.get_by_username('default').user_perms:
245 for p in User.get_by_username('default').user_perms:
246 if p.permission.permission_name.startswith('repository.'):
246 if p.permission.permission_name.startswith('repository.'):
247 default = p.permission.permission_name
247 default = p.permission.permission_name
248 break
248 break
249
249
250 default_perm = 'repository.none' if private else default
250 default_perm = 'repository.none' if private else default
251
251
252 repo_to_perm.permission_id = self.sa.query(Permission)\
252 repo_to_perm.permission_id = self.sa.query(Permission)\
253 .filter(Permission.permission_name == default_perm)\
253 .filter(Permission.permission_name == default_perm)\
254 .one().permission_id
254 .one().permission_id
255
255
256 repo_to_perm.repository = new_repo
256 repo_to_perm.repository = new_repo
257 repo_to_perm.user_id = User.get_by_username('default').user_id
257 repo_to_perm.user_id = User.get_by_username('default').user_id
258
258
259 self.sa.add(repo_to_perm)
259 self.sa.add(repo_to_perm)
260
260
261 if fork_of:
261 if fork_of:
262 if copy_fork_permissions:
262 if copy_fork_permissions:
263 repo = fork_of
263 repo = fork_of
264 user_perms = UserRepoToPerm.query()\
264 user_perms = UserRepoToPerm.query()\
265 .filter(UserRepoToPerm.repository == repo).all()
265 .filter(UserRepoToPerm.repository == repo).all()
266 group_perms = UsersGroupRepoToPerm.query()\
266 group_perms = UsersGroupRepoToPerm.query()\
267 .filter(UsersGroupRepoToPerm.repository == repo).all()
267 .filter(UsersGroupRepoToPerm.repository == repo).all()
268
268
269 for perm in user_perms:
269 for perm in user_perms:
270 UserRepoToPerm.create(perm.user, new_repo,
270 UserRepoToPerm.create(perm.user, new_repo,
271 perm.permission)
271 perm.permission)
272
272
273 for perm in group_perms:
273 for perm in group_perms:
274 UsersGroupRepoToPerm.create(perm.users_group, new_repo,
274 UsersGroupRepoToPerm.create(perm.users_group, new_repo,
275 perm.permission)
275 perm.permission)
276 else:
276 else:
277 _create_default_perms()
277 _create_default_perms()
278 else:
278 else:
279 _create_default_perms()
279 _create_default_perms()
280
280
281 if not just_db:
281 if not just_db:
282 self.__create_repo(repo_name, repo_type,
282 self.__create_repo(repo_name, repo_type,
283 repos_group,
283 repos_group,
284 clone_uri)
284 clone_uri)
285 log_create_repository(new_repo.get_dict(),
285 log_create_repository(new_repo.get_dict(),
286 created_by=owner.username)
286 created_by=owner.username)
287
287
288 # now automatically start following this repository as owner
288 # now automatically start following this repository as owner
289 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
289 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
290 owner.user_id)
290 owner.user_id)
291 return new_repo
291 return new_repo
292 except:
292 except:
293 print traceback.format_exc()
293 print traceback.format_exc()
294 log.error(traceback.format_exc())
294 log.error(traceback.format_exc())
295 raise
295 raise
296
296
297 def create(self, form_data, cur_user, just_db=False, fork=None):
297 def create(self, form_data, cur_user, just_db=False, fork=None):
298
298
299 repo_name = form_data['repo_name_full']
299 repo_name = form_data['repo_name_full']
300 repo_type = form_data['repo_type']
300 repo_type = form_data['repo_type']
301 description = form_data['description']
301 description = form_data['description']
302 owner = cur_user
302 owner = cur_user
303 private = form_data['private']
303 private = form_data['private']
304 clone_uri = form_data.get('clone_uri')
304 clone_uri = form_data.get('clone_uri')
305 repos_group = form_data['repo_group']
305 repos_group = form_data['repo_group']
306 landing_rev = form_data['landing_rev']
306 landing_rev = form_data['landing_rev']
307 copy_fork_permissions = form_data.get('copy_permissions')
307 copy_fork_permissions = form_data.get('copy_permissions')
308 fork_of = form_data.get('fork_parent_id')
308 fork_of = form_data.get('fork_parent_id')
309 return self.create_repo(
309 return self.create_repo(
310 repo_name, repo_type, description, owner, private, clone_uri,
310 repo_name, repo_type, description, owner, private, clone_uri,
311 repos_group, landing_rev, just_db, fork_of, copy_fork_permissions
311 repos_group, landing_rev, just_db, fork_of, copy_fork_permissions
312 )
312 )
313
313
314 def create_fork(self, form_data, cur_user):
314 def create_fork(self, form_data, cur_user):
315 """
315 """
316 Simple wrapper into executing celery task for fork creation
316 Simple wrapper into executing celery task for fork creation
317
317
318 :param form_data:
318 :param form_data:
319 :param cur_user:
319 :param cur_user:
320 """
320 """
321 from rhodecode.lib.celerylib import tasks, run_task
321 from rhodecode.lib.celerylib import tasks, run_task
322 run_task(tasks.create_repo_fork, form_data, cur_user)
322 run_task(tasks.create_repo_fork, form_data, cur_user)
323
323
324 def delete(self, repo):
324 def delete(self, repo):
325 repo = self._get_repo(repo)
325 repo = self._get_repo(repo)
326 if repo:
326 if repo:
327 try:
327 try:
328 self.sa.delete(repo)
328 self.sa.delete(repo)
329 self.__delete_repo(repo)
329 self.__delete_repo(repo)
330 except:
330 except:
331 log.error(traceback.format_exc())
331 log.error(traceback.format_exc())
332 raise
332 raise
333
333
334 def grant_user_permission(self, repo, user, perm):
334 def grant_user_permission(self, repo, user, perm):
335 """
335 """
336 Grant permission for user on given repository, or update existing one
336 Grant permission for user on given repository, or update existing one
337 if found
337 if found
338
338
339 :param repo: Instance of Repository, repository_id, or repository name
339 :param repo: Instance of Repository, repository_id, or repository name
340 :param user: Instance of User, user_id or username
340 :param user: Instance of User, user_id or username
341 :param perm: Instance of Permission, or permission_name
341 :param perm: Instance of Permission, or permission_name
342 """
342 """
343 user = self._get_user(user)
343 user = self._get_user(user)
344 repo = self._get_repo(repo)
344 repo = self._get_repo(repo)
345 permission = self._get_perm(perm)
345 permission = self._get_perm(perm)
346
346
347 # check if we have that permission already
347 # check if we have that permission already
348 obj = self.sa.query(UserRepoToPerm)\
348 obj = self.sa.query(UserRepoToPerm)\
349 .filter(UserRepoToPerm.user == user)\
349 .filter(UserRepoToPerm.user == user)\
350 .filter(UserRepoToPerm.repository == repo)\
350 .filter(UserRepoToPerm.repository == repo)\
351 .scalar()
351 .scalar()
352 if obj is None:
352 if obj is None:
353 # create new !
353 # create new !
354 obj = UserRepoToPerm()
354 obj = UserRepoToPerm()
355 obj.repository = repo
355 obj.repository = repo
356 obj.user = user
356 obj.user = user
357 obj.permission = permission
357 obj.permission = permission
358 self.sa.add(obj)
358 self.sa.add(obj)
359
359
360 def revoke_user_permission(self, repo, user):
360 def revoke_user_permission(self, repo, user):
361 """
361 """
362 Revoke permission for user on given repository
362 Revoke permission for user on given repository
363
363
364 :param repo: Instance of Repository, repository_id, or repository name
364 :param repo: Instance of Repository, repository_id, or repository name
365 :param user: Instance of User, user_id or username
365 :param user: Instance of User, user_id or username
366 """
366 """
367
367
368 user = self._get_user(user)
368 user = self._get_user(user)
369 repo = self._get_repo(repo)
369 repo = self._get_repo(repo)
370
370
371 obj = self.sa.query(UserRepoToPerm)\
371 obj = self.sa.query(UserRepoToPerm)\
372 .filter(UserRepoToPerm.repository == repo)\
372 .filter(UserRepoToPerm.repository == repo)\
373 .filter(UserRepoToPerm.user == user)\
373 .filter(UserRepoToPerm.user == user)\
374 .one()
374 .one()
375 self.sa.delete(obj)
375 self.sa.delete(obj)
376
376
377 def grant_users_group_permission(self, repo, group_name, perm):
377 def grant_users_group_permission(self, repo, group_name, perm):
378 """
378 """
379 Grant permission for users group on given repository, or update
379 Grant permission for users group on given repository, or update
380 existing one if found
380 existing one if found
381
381
382 :param repo: Instance of Repository, repository_id, or repository name
382 :param repo: Instance of Repository, repository_id, or repository name
383 :param group_name: Instance of UserGroup, users_group_id,
383 :param group_name: Instance of UserGroup, users_group_id,
384 or users group name
384 or users group name
385 :param perm: Instance of Permission, or permission_name
385 :param perm: Instance of Permission, or permission_name
386 """
386 """
387 repo = self._get_repo(repo)
387 repo = self._get_repo(repo)
388 group_name = self.__get_users_group(group_name)
388 group_name = self.__get_users_group(group_name)
389 permission = self._get_perm(perm)
389 permission = self._get_perm(perm)
390
390
391 # check if we have that permission already
391 # check if we have that permission already
392 obj = self.sa.query(UsersGroupRepoToPerm)\
392 obj = self.sa.query(UsersGroupRepoToPerm)\
393 .filter(UsersGroupRepoToPerm.users_group == group_name)\
393 .filter(UsersGroupRepoToPerm.users_group == group_name)\
394 .filter(UsersGroupRepoToPerm.repository == repo)\
394 .filter(UsersGroupRepoToPerm.repository == repo)\
395 .scalar()
395 .scalar()
396
396
397 if obj is None:
397 if obj is None:
398 # create new
398 # create new
399 obj = UsersGroupRepoToPerm()
399 obj = UsersGroupRepoToPerm()
400
400
401 obj.repository = repo
401 obj.repository = repo
402 obj.users_group = group_name
402 obj.users_group = group_name
403 obj.permission = permission
403 obj.permission = permission
404 self.sa.add(obj)
404 self.sa.add(obj)
405
405
406 def revoke_users_group_permission(self, repo, group_name):
406 def revoke_users_group_permission(self, repo, group_name):
407 """
407 """
408 Revoke permission for users group on given repository
408 Revoke permission for users group on given repository
409
409
410 :param repo: Instance of Repository, repository_id, or repository name
410 :param repo: Instance of Repository, repository_id, or repository name
411 :param group_name: Instance of UserGroup, users_group_id,
411 :param group_name: Instance of UserGroup, users_group_id,
412 or users group name
412 or users group name
413 """
413 """
414 repo = self._get_repo(repo)
414 repo = self._get_repo(repo)
415 group_name = self.__get_users_group(group_name)
415 group_name = self.__get_users_group(group_name)
416
416
417 obj = self.sa.query(UsersGroupRepoToPerm)\
417 obj = self.sa.query(UsersGroupRepoToPerm)\
418 .filter(UsersGroupRepoToPerm.repository == repo)\
418 .filter(UsersGroupRepoToPerm.repository == repo)\
419 .filter(UsersGroupRepoToPerm.users_group == group_name)\
419 .filter(UsersGroupRepoToPerm.users_group == group_name)\
420 .one()
420 .one()
421 self.sa.delete(obj)
421 self.sa.delete(obj)
422
422
423 def delete_stats(self, repo_name):
423 def delete_stats(self, repo_name):
424 """
424 """
425 removes stats for given repo
425 removes stats for given repo
426
426
427 :param repo_name:
427 :param repo_name:
428 """
428 """
429 try:
429 try:
430 obj = self.sa.query(Statistics)\
430 obj = self.sa.query(Statistics)\
431 .filter(Statistics.repository ==
431 .filter(Statistics.repository ==
432 self.get_by_repo_name(repo_name))\
432 self.get_by_repo_name(repo_name))\
433 .one()
433 .one()
434 self.sa.delete(obj)
434 self.sa.delete(obj)
435 except:
435 except:
436 log.error(traceback.format_exc())
436 log.error(traceback.format_exc())
437 raise
437 raise
438
438
439 def __create_repo(self, repo_name, alias, parent, clone_uri=False):
439 def __create_repo(self, repo_name, alias, parent, clone_uri=False):
440 """
440 """
441 makes repository on filesystem. It's group aware means it'll create
441 makes repository on filesystem. It's group aware means it'll create
442 a repository within a group, and alter the paths accordingly of
442 a repository within a group, and alter the paths accordingly of
443 group location
443 group location
444
444
445 :param repo_name:
445 :param repo_name:
446 :param alias:
446 :param alias:
447 :param parent_id:
447 :param parent_id:
448 :param clone_uri:
448 :param clone_uri:
449 """
449 """
450 from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group
450 from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group
451
451
452 if parent:
452 if parent:
453 new_parent_path = os.sep.join(parent.full_path_splitted)
453 new_parent_path = os.sep.join(parent.full_path_splitted)
454 else:
454 else:
455 new_parent_path = ''
455 new_parent_path = ''
456
456
457 # we need to make it str for mercurial
457 # we need to make it str for mercurial
458 repo_path = os.path.join(*map(lambda x: safe_str(x),
458 repo_path = os.path.join(*map(lambda x: safe_str(x),
459 [self.repos_path, new_parent_path, repo_name]))
459 [self.repos_path, new_parent_path, repo_name]))
460
460
461 # check if this path is not a repository
461 # check if this path is not a repository
462 if is_valid_repo(repo_path, self.repos_path):
462 if is_valid_repo(repo_path, self.repos_path):
463 raise Exception('This path %s is a valid repository' % repo_path)
463 raise Exception('This path %s is a valid repository' % repo_path)
464
464
465 # check if this path is a group
465 # check if this path is a group
466 if is_valid_repos_group(repo_path, self.repos_path):
466 if is_valid_repos_group(repo_path, self.repos_path):
467 raise Exception('This path %s is a valid group' % repo_path)
467 raise Exception('This path %s is a valid group' % repo_path)
468
468
469 log.info('creating repo %s in %s @ %s' % (
469 log.info('creating repo %s in %s @ %s' % (
470 repo_name, safe_unicode(repo_path), clone_uri
470 repo_name, safe_unicode(repo_path), clone_uri
471 )
471 )
472 )
472 )
473 backend = get_backend(alias)
473 backend = get_backend(alias)
474 if alias == 'hg':
474 if alias == 'hg':
475 backend(repo_path, create=True, src_url=clone_uri)
475 backend(repo_path, create=True, src_url=clone_uri)
476 elif alias == 'git':
476 elif alias == 'git':
477 r = backend(repo_path, create=True, src_url=clone_uri, bare=True)
477 r = backend(repo_path, create=True, src_url=clone_uri, bare=True)
478 # add rhodecode hook into this repo
478 # add rhodecode hook into this repo
479
479
480 loc = jn(r.path, 'hooks')
480 loc = jn(r.path, 'hooks')
481 if not r.bare:
481 if not r.bare:
482 loc = jn(r.path, '.git', 'hooks')
482 loc = jn(r.path, '.git', 'hooks')
483 if not os.path.isdir(loc):
483 if not os.path.isdir(loc):
484 os.makedirs(loc)
484 os.makedirs(loc)
485
485
486 tmpl = pkg_resources.resource_string(
486 tmpl = pkg_resources.resource_string(
487 'rhodecode', jn('config', 'post_receive_tmpl.py')
487 'rhodecode', jn('config', 'post_receive_tmpl.py')
488 )
488 )
489 _hook_file = jn(loc, 'post-receive')
489 _hook_file = jn(loc, 'post-receive')
490 with open(_hook_file, 'wb') as f:
490 with open(_hook_file, 'wb') as f:
491 f.write(tmpl)
491 f.write(tmpl)
492 os.chmod(_hook_file, 0755)
492 os.chmod(_hook_file, 0755)
493
493
494 else:
494 else:
495 raise Exception('Undefined alias %s' % alias)
495 raise Exception('Undefined alias %s' % alias)
496
496
497 def __rename_repo(self, old, new):
497 def __rename_repo(self, old, new):
498 """
498 """
499 renames repository on filesystem
499 renames repository on filesystem
500
500
501 :param old: old name
501 :param old: old name
502 :param new: new name
502 :param new: new name
503 """
503 """
504 log.info('renaming repo from %s to %s' % (old, new))
504 log.info('renaming repo from %s to %s' % (old, new))
505
505
506 old_path = os.path.join(self.repos_path, old)
506 old_path = os.path.join(self.repos_path, old)
507 new_path = os.path.join(self.repos_path, new)
507 new_path = os.path.join(self.repos_path, new)
508 if os.path.isdir(new_path):
508 if os.path.isdir(new_path):
509 raise Exception(
509 raise Exception(
510 'Was trying to rename to already existing dir %s' % new_path
510 'Was trying to rename to already existing dir %s' % new_path
511 )
511 )
512 shutil.move(old_path, new_path)
512 shutil.move(old_path, new_path)
513
513
514 def __delete_repo(self, repo):
514 def __delete_repo(self, repo):
515 """
515 """
516 removes repo from filesystem, the removal is acctually made by
516 removes repo from filesystem, the removal is acctually made by
517 added rm__ prefix into dir, and rename internat .hg/.git dirs so this
517 added rm__ prefix into dir, and rename internat .hg/.git dirs so this
518 repository is no longer valid for rhodecode, can be undeleted later on
518 repository is no longer valid for rhodecode, can be undeleted later on
519 by reverting the renames on this repository
519 by reverting the renames on this repository
520
520
521 :param repo: repo object
521 :param repo: repo object
522 """
522 """
523 rm_path = os.path.join(self.repos_path, repo.repo_name)
523 rm_path = os.path.join(self.repos_path, repo.repo_name)
524 log.info("Removing %s" % (rm_path))
524 log.info("Removing %s" % (rm_path))
525 # disable hg/git internal that it doesn't get detected as repo
525 # disable hg/git internal that it doesn't get detected as repo
526 alias = repo.repo_type
526 alias = repo.repo_type
527
527
528 bare = getattr(repo.scm_instance, 'bare', False)
528 bare = getattr(repo.scm_instance, 'bare', False)
529
529
530 if not bare:
530 if not bare:
531 # skip this for bare git repos
531 # skip this for bare git repos
532 shutil.move(os.path.join(rm_path, '.%s' % alias),
532 shutil.move(os.path.join(rm_path, '.%s' % alias),
533 os.path.join(rm_path, 'rm__.%s' % alias))
533 os.path.join(rm_path, 'rm__.%s' % alias))
534 # disable repo
534 # disable repo
535 _now = datetime.now()
535 _now = datetime.now()
536 _ms = str(_now.microsecond)
536 _ms = str(_now.microsecond).rjust(6, '0')
537 _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms),
537 _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms),
538 repo.repo_name)
538 repo.repo_name)
539 shutil.move(rm_path, os.path.join(self.repos_path, _d))
539 shutil.move(rm_path, os.path.join(self.repos_path, _d))
General Comments 0
You need to be logged in to leave comments. Login now