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