Show More
@@ -1,247 +1,264 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | package.rhodecode.model.repo |
|
3 | package.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) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2010 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 |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
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, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 | import os |
|
27 | import os | |
28 | import shutil |
|
28 | import shutil | |
29 | import logging |
|
29 | import logging | |
30 | import traceback |
|
30 | import traceback | |
31 | from datetime import datetime |
|
31 | from datetime import datetime | |
32 |
|
32 | |||
33 | from pylons import app_globals as g |
|
33 | from pylons import app_globals as g | |
34 |
|
34 | |||
35 | from rhodecode.model import BaseModel |
|
35 | from rhodecode.model import BaseModel | |
36 | from rhodecode.model.caching_query import FromCache |
|
36 | from rhodecode.model.caching_query import FromCache | |
37 | from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ |
|
37 | from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ | |
38 | Statistics |
|
38 | Statistics | |
39 | from rhodecode.model.user import UserModel |
|
39 | from rhodecode.model.user import UserModel | |
40 |
|
40 | |||
41 | from vcs.backends import get_backend |
|
41 | from vcs.backends import get_backend | |
42 |
|
42 | |||
43 | log = logging.getLogger(__name__) |
|
43 | log = logging.getLogger(__name__) | |
44 |
|
44 | |||
45 | class RepoModel(BaseModel): |
|
45 | class RepoModel(BaseModel): | |
46 |
|
46 | |||
47 | def get(self, repo_id, cache=False): |
|
47 | def get(self, repo_id, cache=False): | |
48 | repo = self.sa.query(Repository)\ |
|
48 | repo = self.sa.query(Repository)\ | |
49 | .filter(Repository.repo_id == repo_id) |
|
49 | .filter(Repository.repo_id == repo_id) | |
50 |
|
50 | |||
51 | if cache: |
|
51 | if cache: | |
52 | repo = repo.options(FromCache("sql_cache_short", |
|
52 | repo = repo.options(FromCache("sql_cache_short", | |
53 | "get_repo_%s" % repo_id)) |
|
53 | "get_repo_%s" % repo_id)) | |
54 | return repo.scalar() |
|
54 | return repo.scalar() | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def get_by_repo_name(self, repo_name, cache=False): |
|
57 | def get_by_repo_name(self, repo_name, cache=False): | |
58 | repo = self.sa.query(Repository)\ |
|
58 | repo = self.sa.query(Repository)\ | |
59 | .filter(Repository.repo_name == repo_name) |
|
59 | .filter(Repository.repo_name == repo_name) | |
60 |
|
60 | |||
61 | if cache: |
|
61 | if cache: | |
62 | repo = repo.options(FromCache("sql_cache_short", |
|
62 | repo = repo.options(FromCache("sql_cache_short", | |
63 | "get_repo_%s" % repo_name)) |
|
63 | "get_repo_%s" % repo_name)) | |
64 | return repo.scalar() |
|
64 | return repo.scalar() | |
65 |
|
65 | |||
66 | def get_users_js(self): |
|
66 | def get_users_js(self): | |
67 |
|
67 | |||
68 | users = self.sa.query(User).filter(User.active == True).all() |
|
68 | users = self.sa.query(User).filter(User.active == True).all() | |
69 | u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},''' |
|
69 | u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},''' | |
70 | users_array = '[%s];' % '\n'.join([u_tmpl % (u.user_id, u.name, |
|
70 | users_array = '[%s];' % '\n'.join([u_tmpl % (u.user_id, u.name, | |
71 | u.lastname, u.username) |
|
71 | u.lastname, u.username) | |
72 | for u in users]) |
|
72 | for u in users]) | |
73 | return users_array |
|
73 | return users_array | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def update(self, repo_name, form_data): |
|
76 | def update(self, repo_name, form_data): | |
77 | try: |
|
77 | try: | |
78 |
|
78 | |||
79 | #update permissions |
|
79 | #update permissions | |
80 | for username, perm in form_data['perms_updates']: |
|
80 | for username, perm in form_data['perms_updates']: | |
81 | r2p = self.sa.query(RepoToPerm)\ |
|
81 | r2p = self.sa.query(RepoToPerm)\ | |
82 | .filter(RepoToPerm.user == UserModel(self.sa)\ |
|
82 | .filter(RepoToPerm.user == UserModel(self.sa)\ | |
83 | .get_by_username(username, cache=False))\ |
|
83 | .get_by_username(username, cache=False))\ | |
84 | .filter(RepoToPerm.repository == \ |
|
84 | .filter(RepoToPerm.repository == \ | |
85 | self.get_by_repo_name(repo_name))\ |
|
85 | self.get_by_repo_name(repo_name))\ | |
86 | .one() |
|
86 | .one() | |
87 |
|
87 | |||
88 | r2p.permission_id = self.sa.query(Permission).filter( |
|
88 | r2p.permission_id = self.sa.query(Permission).filter( | |
89 | Permission.permission_name == |
|
89 | Permission.permission_name == | |
90 | perm).one().permission_id |
|
90 | perm).one().permission_id | |
91 | self.sa.add(r2p) |
|
91 | self.sa.add(r2p) | |
92 |
|
92 | |||
93 | #set new permissions |
|
93 | #set new permissions | |
94 | for username, perm in form_data['perms_new']: |
|
94 | for username, perm in form_data['perms_new']: | |
95 | r2p = RepoToPerm() |
|
95 | r2p = RepoToPerm() | |
96 | r2p.repository = self.get_by_repo_name(repo_name) |
|
96 | r2p.repository = self.get_by_repo_name(repo_name) | |
97 | r2p.user = UserModel(self.sa).get_by_username(username, cache=False) |
|
97 | r2p.user = UserModel(self.sa).get_by_username(username, cache=False) | |
98 |
|
98 | |||
99 | r2p.permission_id = self.sa.query(Permission).filter( |
|
99 | r2p.permission_id = self.sa.query(Permission).filter( | |
100 | Permission.permission_name == perm)\ |
|
100 | Permission.permission_name == perm)\ | |
101 | .one().permission_id |
|
101 | .one().permission_id | |
102 | self.sa.add(r2p) |
|
102 | self.sa.add(r2p) | |
103 |
|
103 | |||
104 | #update current repo |
|
104 | #update current repo | |
105 | cur_repo = self.get_by_repo_name(repo_name, cache=False) |
|
105 | cur_repo = self.get_by_repo_name(repo_name, cache=False) | |
106 |
|
106 | |||
107 | for k, v in form_data.items(): |
|
107 | for k, v in form_data.items(): | |
108 | if k == 'user': |
|
108 | if k == 'user': | |
109 | cur_repo.user_id = v |
|
109 | cur_repo.user_id = v | |
110 | else: |
|
110 | else: | |
111 | setattr(cur_repo, k, v) |
|
111 | setattr(cur_repo, k, v) | |
112 |
|
112 | |||
113 | self.sa.add(cur_repo) |
|
113 | self.sa.add(cur_repo) | |
114 |
|
114 | |||
115 | if repo_name != form_data['repo_name']: |
|
115 | if repo_name != form_data['repo_name']: | |
116 | #rename our data |
|
116 | #rename our data | |
117 | self.__rename_repo(repo_name, form_data['repo_name']) |
|
117 | self.__rename_repo(repo_name, form_data['repo_name']) | |
118 |
|
118 | |||
119 | self.sa.commit() |
|
119 | self.sa.commit() | |
120 | except: |
|
120 | except: | |
121 | log.error(traceback.format_exc()) |
|
121 | log.error(traceback.format_exc()) | |
122 | self.sa.rollback() |
|
122 | self.sa.rollback() | |
123 | raise |
|
123 | raise | |
124 |
|
124 | |||
125 | def create(self, form_data, cur_user, just_db=False, fork=False): |
|
125 | def create(self, form_data, cur_user, just_db=False, fork=False): | |
126 | try: |
|
126 | try: | |
127 | if fork: |
|
127 | if fork: | |
128 | #force str since hg doesn't go with unicode |
|
128 | #force str since hg doesn't go with unicode | |
129 | repo_name = str(form_data['fork_name']) |
|
129 | repo_name = str(form_data['fork_name']) | |
130 | org_name = str(form_data['repo_name']) |
|
130 | org_name = str(form_data['repo_name']) | |
131 |
|
131 | |||
132 | else: |
|
132 | else: | |
133 | org_name = repo_name = str(form_data['repo_name']) |
|
133 | org_name = repo_name = str(form_data['repo_name']) | |
134 | new_repo = Repository() |
|
134 | new_repo = Repository() | |
135 | for k, v in form_data.items(): |
|
135 | for k, v in form_data.items(): | |
136 | if k == 'repo_name': |
|
136 | if k == 'repo_name': | |
137 | v = repo_name |
|
137 | v = repo_name | |
138 | setattr(new_repo, k, v) |
|
138 | setattr(new_repo, k, v) | |
139 |
|
139 | |||
140 | if fork: |
|
140 | if fork: | |
141 | parent_repo = self.sa.query(Repository)\ |
|
141 | parent_repo = self.sa.query(Repository)\ | |
142 | .filter(Repository.repo_name == org_name).scalar() |
|
142 | .filter(Repository.repo_name == org_name).scalar() | |
143 | new_repo.fork = parent_repo |
|
143 | new_repo.fork = parent_repo | |
144 |
|
144 | |||
145 | new_repo.user_id = cur_user.user_id |
|
145 | new_repo.user_id = cur_user.user_id | |
146 | self.sa.add(new_repo) |
|
146 | self.sa.add(new_repo) | |
147 |
|
147 | |||
148 | #create default permission |
|
148 | #create default permission | |
149 | repo_to_perm = RepoToPerm() |
|
149 | repo_to_perm = RepoToPerm() | |
150 | default = 'repository.read' |
|
150 | default = 'repository.read' | |
151 | for p in UserModel(self.sa).get_by_username('default', cache=False).user_perms: |
|
151 | for p in UserModel(self.sa).get_by_username('default', cache=False).user_perms: | |
152 | if p.permission.permission_name.startswith('repository.'): |
|
152 | if p.permission.permission_name.startswith('repository.'): | |
153 | default = p.permission.permission_name |
|
153 | default = p.permission.permission_name | |
154 | break |
|
154 | break | |
155 |
|
155 | |||
156 | default_perm = 'repository.none' if form_data['private'] else default |
|
156 | default_perm = 'repository.none' if form_data['private'] else default | |
157 |
|
157 | |||
158 | repo_to_perm.permission_id = self.sa.query(Permission)\ |
|
158 | repo_to_perm.permission_id = self.sa.query(Permission)\ | |
159 | .filter(Permission.permission_name == default_perm)\ |
|
159 | .filter(Permission.permission_name == default_perm)\ | |
160 | .one().permission_id |
|
160 | .one().permission_id | |
161 |
|
161 | |||
162 | repo_to_perm.repository_id = new_repo.repo_id |
|
162 | repo_to_perm.repository_id = new_repo.repo_id | |
163 | repo_to_perm.user_id = UserModel(self.sa)\ |
|
163 | repo_to_perm.user_id = UserModel(self.sa)\ | |
164 | .get_by_username('default', cache=False).user_id |
|
164 | .get_by_username('default', cache=False).user_id | |
165 |
|
165 | |||
166 | self.sa.add(repo_to_perm) |
|
166 | self.sa.add(repo_to_perm) | |
167 | self.sa.commit() |
|
167 | self.sa.commit() | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | #now automatically start following this repository as owner |
|
170 | #now automatically start following this repository as owner | |
171 | from rhodecode.model.scm import ScmModel |
|
171 | from rhodecode.model.scm import ScmModel | |
172 | ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, |
|
172 | ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, | |
173 | cur_user.user_id) |
|
173 | cur_user.user_id) | |
174 |
|
174 | |||
175 | if not just_db: |
|
175 | if not just_db: | |
176 | self.__create_repo(repo_name, form_data['repo_type']) |
|
176 | self.__create_repo(repo_name, form_data['repo_type']) | |
177 | except: |
|
177 | except: | |
178 | log.error(traceback.format_exc()) |
|
178 | log.error(traceback.format_exc()) | |
179 | self.sa.rollback() |
|
179 | self.sa.rollback() | |
180 | raise |
|
180 | raise | |
181 |
|
181 | |||
182 | def create_fork(self, form_data, cur_user): |
|
182 | def create_fork(self, form_data, cur_user): | |
183 | from rhodecode.lib.celerylib import tasks, run_task |
|
183 | from rhodecode.lib.celerylib import tasks, run_task | |
184 | run_task(tasks.create_repo_fork, form_data, cur_user) |
|
184 | run_task(tasks.create_repo_fork, form_data, cur_user) | |
185 |
|
185 | |||
186 | def delete(self, repo): |
|
186 | def delete(self, repo): | |
187 | try: |
|
187 | try: | |
188 | self.sa.delete(repo) |
|
188 | self.sa.delete(repo) | |
189 | self.__delete_repo(repo) |
|
189 | self.__delete_repo(repo) | |
190 | self.sa.commit() |
|
190 | self.sa.commit() | |
191 | except: |
|
191 | except: | |
192 | log.error(traceback.format_exc()) |
|
192 | log.error(traceback.format_exc()) | |
193 | self.sa.rollback() |
|
193 | self.sa.rollback() | |
194 | raise |
|
194 | raise | |
195 |
|
195 | |||
196 | def delete_perm_user(self, form_data, repo_name): |
|
196 | def delete_perm_user(self, form_data, repo_name): | |
197 | try: |
|
197 | try: | |
198 | self.sa.query(RepoToPerm)\ |
|
198 | self.sa.query(RepoToPerm)\ | |
199 | .filter(RepoToPerm.repository \ |
|
199 | .filter(RepoToPerm.repository \ | |
200 | == self.get_by_repo_name(repo_name))\ |
|
200 | == self.get_by_repo_name(repo_name))\ | |
201 | .filter(RepoToPerm.user_id == form_data['user_id']).delete() |
|
201 | .filter(RepoToPerm.user_id == form_data['user_id']).delete() | |
202 | self.sa.commit() |
|
202 | self.sa.commit() | |
203 | except: |
|
203 | except: | |
204 | log.error(traceback.format_exc()) |
|
204 | log.error(traceback.format_exc()) | |
205 | self.sa.rollback() |
|
205 | self.sa.rollback() | |
206 | raise |
|
206 | raise | |
207 |
|
207 | |||
208 | def delete_stats(self, repo_name): |
|
208 | def delete_stats(self, repo_name): | |
209 | try: |
|
209 | try: | |
210 | self.sa.query(Statistics)\ |
|
210 | self.sa.query(Statistics)\ | |
211 | .filter(Statistics.repository == \ |
|
211 | .filter(Statistics.repository == \ | |
212 | self.get_by_repo_name(repo_name)).delete() |
|
212 | self.get_by_repo_name(repo_name)).delete() | |
213 | self.sa.commit() |
|
213 | self.sa.commit() | |
214 | except: |
|
214 | except: | |
215 | log.error(traceback.format_exc()) |
|
215 | log.error(traceback.format_exc()) | |
216 | self.sa.rollback() |
|
216 | self.sa.rollback() | |
217 | raise |
|
217 | raise | |
218 |
|
218 | |||
219 |
|
219 | |||
220 | def __create_repo(self, repo_name, alias): |
|
220 | def __create_repo(self, repo_name, alias): | |
|
221 | """ | |||
|
222 | makes repository on filesystem | |||
|
223 | :param repo_name: | |||
|
224 | :param alias: | |||
|
225 | """ | |||
221 | from rhodecode.lib.utils import check_repo |
|
226 | from rhodecode.lib.utils import check_repo | |
222 | repo_path = os.path.join(g.base_path, repo_name) |
|
227 | repo_path = os.path.join(g.base_path, repo_name) | |
223 | if check_repo(repo_name, g.base_path): |
|
228 | if check_repo(repo_name, g.base_path): | |
224 | log.info('creating repo %s in %s', repo_name, repo_path) |
|
229 | log.info('creating repo %s in %s', repo_name, repo_path) | |
225 | backend = get_backend(alias) |
|
230 | backend = get_backend(alias) | |
226 | backend(repo_path, create=True) |
|
231 | backend(repo_path, create=True) | |
227 |
|
232 | |||
228 | def __rename_repo(self, old, new): |
|
233 | def __rename_repo(self, old, new): | |
|
234 | """ | |||
|
235 | renames repository on filesystem | |||
|
236 | :param old: old name | |||
|
237 | :param new: new name | |||
|
238 | """ | |||
229 | log.info('renaming repo from %s to %s', old, new) |
|
239 | log.info('renaming repo from %s to %s', old, new) | |
230 |
|
240 | |||
231 | old_path = os.path.join(g.base_path, old) |
|
241 | old_path = os.path.join(g.base_path, old) | |
232 | new_path = os.path.join(g.base_path, new) |
|
242 | new_path = os.path.join(g.base_path, new) | |
233 | if os.path.isdir(new_path): |
|
243 | if os.path.isdir(new_path): | |
234 | raise Exception('Was trying to rename to already existing dir %s', |
|
244 | raise Exception('Was trying to rename to already existing dir %s', | |
235 | new_path) |
|
245 | new_path) | |
236 | shutil.move(old_path, new_path) |
|
246 | shutil.move(old_path, new_path) | |
237 |
|
247 | |||
238 | def __delete_repo(self, repo): |
|
248 | def __delete_repo(self, repo): | |
|
249 | """ | |||
|
250 | removes repo from filesystem, the removal is acctually made by | |||
|
251 | added rm__ prefix into dir, and rename internat .hg/.git dirs so this | |||
|
252 | repository is no longer valid for rhodecode, can be undeleted later on | |||
|
253 | by reverting the renames on this repository | |||
|
254 | :param repo: repo object | |||
|
255 | """ | |||
239 | rm_path = os.path.join(g.base_path, repo.repo_name) |
|
256 | rm_path = os.path.join(g.base_path, repo.repo_name) | |
240 | log.info("Removing %s", rm_path) |
|
257 | log.info("Removing %s", rm_path) | |
241 | #disable hg/git |
|
258 | #disable hg/git | |
242 | alias = repo.repo_type |
|
259 | alias = repo.repo_type | |
243 | shutil.move(os.path.join(rm_path, '.%s' % alias), |
|
260 | shutil.move(os.path.join(rm_path, '.%s' % alias), | |
244 | os.path.join(rm_path, 'rm__.%s' % alias)) |
|
261 | os.path.join(rm_path, 'rm__.%s' % alias)) | |
245 | #disable repo |
|
262 | #disable repo | |
246 | shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s__%s' \ |
|
263 | shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s__%s' \ | |
247 | % (datetime.today(), repo.repo_name))) |
|
264 | % (datetime.today(), repo.repo_name))) |
General Comments 0
You need to be logged in to leave comments.
Login now