Show More
@@ -1,412 +1,412 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.scm |
|
3 | rhodecode.model.scm | |
4 | ~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Scm model for RhodeCode |
|
6 | Scm model for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Apr 9, 2010 |
|
8 | :created_on: Apr 9, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2011 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 | import os |
|
25 | import os | |
26 | import time |
|
26 | import time | |
27 | import traceback |
|
27 | import traceback | |
28 | import logging |
|
28 | import logging | |
29 |
|
29 | |||
30 | from mercurial import ui |
|
30 | from mercurial import ui | |
31 |
|
31 | |||
32 | from sqlalchemy.exc import DatabaseError |
|
32 | from sqlalchemy.exc import DatabaseError | |
33 | from sqlalchemy.orm import make_transient |
|
33 | from sqlalchemy.orm import make_transient | |
34 |
|
34 | |||
35 | from beaker.cache import cache_region, region_invalidate |
|
35 | from beaker.cache import cache_region, region_invalidate | |
36 |
|
36 | |||
37 | from vcs import get_backend |
|
37 | from vcs import get_backend | |
38 | from vcs.utils.helpers import get_scm |
|
38 | from vcs.utils.helpers import get_scm | |
39 | from vcs.exceptions import RepositoryError, VCSError |
|
39 | from vcs.exceptions import RepositoryError, VCSError | |
40 | from vcs.utils.lazy import LazyProperty |
|
40 | from vcs.utils.lazy import LazyProperty | |
41 | from vcs.nodes import FileNode |
|
41 | from vcs.nodes import FileNode | |
42 |
|
42 | |||
43 | from rhodecode import BACKENDS |
|
43 | from rhodecode import BACKENDS | |
44 | from rhodecode.lib import helpers as h |
|
44 | from rhodecode.lib import helpers as h | |
45 | from rhodecode.lib.auth import HasRepoPermissionAny |
|
45 | from rhodecode.lib.auth import HasRepoPermissionAny | |
46 | from rhodecode.lib.utils import get_repos as get_filesystem_repos, make_ui, \ |
|
46 | from rhodecode.lib.utils import get_repos as get_filesystem_repos, make_ui, \ | |
47 | action_logger |
|
47 | action_logger | |
48 | from rhodecode.model import BaseModel |
|
48 | from rhodecode.model import BaseModel | |
49 | from rhodecode.model.user import UserModel |
|
49 | from rhodecode.model.user import UserModel | |
50 | from rhodecode.model.repo import RepoModel |
|
50 | from rhodecode.model.repo import RepoModel | |
51 | from rhodecode.model.db import Repository, RhodeCodeUi, CacheInvalidation, \ |
|
51 | from rhodecode.model.db import Repository, RhodeCodeUi, CacheInvalidation, \ | |
52 | UserFollowing, UserLog |
|
52 | UserFollowing, UserLog | |
53 | from rhodecode.model.caching_query import FromCache |
|
53 | from rhodecode.model.caching_query import FromCache | |
54 |
|
54 | |||
55 | log = logging.getLogger(__name__) |
|
55 | log = logging.getLogger(__name__) | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class UserTemp(object): |
|
58 | class UserTemp(object): | |
59 | def __init__(self, user_id): |
|
59 | def __init__(self, user_id): | |
60 | self.user_id = user_id |
|
60 | self.user_id = user_id | |
61 |
|
61 | |||
62 | def __repr__(self): |
|
62 | def __repr__(self): | |
63 | return "<%s('id:%s')>" % (self.__class__.__name__, self.user_id) |
|
63 | return "<%s('id:%s')>" % (self.__class__.__name__, self.user_id) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | class RepoTemp(object): |
|
66 | class RepoTemp(object): | |
67 | def __init__(self, repo_id): |
|
67 | def __init__(self, repo_id): | |
68 | self.repo_id = repo_id |
|
68 | self.repo_id = repo_id | |
69 |
|
69 | |||
70 | def __repr__(self): |
|
70 | def __repr__(self): | |
71 | return "<%s('id:%s')>" % (self.__class__.__name__, self.repo_id) |
|
71 | return "<%s('id:%s')>" % (self.__class__.__name__, self.repo_id) | |
72 |
|
72 | |||
73 | class CachedRepoList(object): |
|
73 | class CachedRepoList(object): | |
74 |
|
74 | |||
75 | def __init__(self, db_repo_list, invalidation_list, repos_path, |
|
75 | def __init__(self, db_repo_list, invalidation_list, repos_path, | |
76 | order_by=None): |
|
76 | order_by=None): | |
77 | self.db_repo_list = db_repo_list |
|
77 | self.db_repo_list = db_repo_list | |
78 | self.invalidation_list = invalidation_list |
|
78 | self.invalidation_list = invalidation_list | |
79 | self.repos_path = repos_path |
|
79 | self.repos_path = repos_path | |
80 | self.order_by = order_by |
|
80 | self.order_by = order_by | |
81 | self.reversed = (order_by or '').startswith('-') |
|
81 | self.reversed = (order_by or '').startswith('-') | |
82 |
|
82 | |||
83 | def __len__(self): |
|
83 | def __len__(self): | |
84 | return len(self.db_repo_list) |
|
84 | return len(self.db_repo_list) | |
85 |
|
85 | |||
86 | def __repr__(self): |
|
86 | def __repr__(self): | |
87 | return '<%s (%s)>' % (self.__class__.__name__, self.__len__()) |
|
87 | return '<%s (%s)>' % (self.__class__.__name__, self.__len__()) | |
88 |
|
88 | |||
89 | def __iter__(self): |
|
89 | def __iter__(self): | |
90 | for db_repo in self.db_repo_list: |
|
90 | for db_repo in self.db_repo_list: | |
91 | dbr = db_repo |
|
91 | dbr = db_repo | |
92 |
|
92 | |||
93 | # invalidate the repo cache if needed before getting the |
|
93 | # invalidate the repo cache if needed before getting the | |
94 | # scm instance |
|
94 | # scm instance | |
95 |
|
95 | |||
96 | scm_invalidate = False |
|
96 | scm_invalidate = False | |
97 | if self.invalidation_list is not None: |
|
97 | if self.invalidation_list is not None: | |
98 | scm_invalidate = dbr.repo_name in self.invalidation_list |
|
98 | scm_invalidate = dbr.repo_name in self.invalidation_list | |
99 |
|
99 | |||
100 | if scm_invalidate: |
|
100 | if scm_invalidate: | |
101 | log.info('invalidating cache for repository %s', |
|
101 | log.info('invalidating cache for repository %s', | |
102 | dbr.repo_name) |
|
102 | dbr.repo_name) | |
103 | db_repo.set_invalidate |
|
103 | db_repo.set_invalidate | |
104 |
|
104 | |||
105 | scmr = db_repo.scm_instance_cached |
|
105 | scmr = db_repo.scm_instance_cached | |
106 |
|
106 | |||
107 | #check permission at this level |
|
107 | #check permission at this level | |
108 | if not HasRepoPermissionAny('repository.read', |
|
108 | if not HasRepoPermissionAny('repository.read', | |
109 | 'repository.write', |
|
109 | 'repository.write', | |
110 | 'repository.admin')(dbr.repo_name, |
|
110 | 'repository.admin')(dbr.repo_name, | |
111 | 'get repo check'): |
|
111 | 'get repo check'): | |
112 | continue |
|
112 | continue | |
113 |
|
113 | |||
114 |
|
114 | |||
115 |
|
115 | |||
116 |
|
116 | |||
117 |
|
117 | |||
118 | last_change = scmr.last_change |
|
118 | last_change = scmr.last_change | |
119 | tip = h.get_changeset_safe(scmr, 'tip') |
|
119 | tip = h.get_changeset_safe(scmr, 'tip') | |
120 |
|
120 | |||
121 | tmp_d = {} |
|
121 | tmp_d = {} | |
122 | tmp_d['name'] = dbr.repo_name |
|
122 | tmp_d['name'] = dbr.repo_name | |
123 | tmp_d['name_sort'] = tmp_d['name'].lower() |
|
123 | tmp_d['name_sort'] = tmp_d['name'].lower() | |
124 | tmp_d['description'] = dbr.description |
|
124 | tmp_d['description'] = dbr.description | |
125 | tmp_d['description_sort'] = tmp_d['description'] |
|
125 | tmp_d['description_sort'] = tmp_d['description'] | |
126 | tmp_d['last_change'] = last_change |
|
126 | tmp_d['last_change'] = last_change | |
127 | tmp_d['last_change_sort'] = time.mktime(last_change \ |
|
127 | tmp_d['last_change_sort'] = time.mktime(last_change \ | |
128 | .timetuple()) |
|
128 | .timetuple()) | |
129 | tmp_d['tip'] = tip.raw_id |
|
129 | tmp_d['tip'] = tip.raw_id | |
130 | tmp_d['tip_sort'] = tip.revision |
|
130 | tmp_d['tip_sort'] = tip.revision | |
131 | tmp_d['rev'] = tip.revision |
|
131 | tmp_d['rev'] = tip.revision | |
132 | tmp_d['contact'] = dbr.user.full_contact |
|
132 | tmp_d['contact'] = dbr.user.full_contact | |
133 | tmp_d['contact_sort'] = tmp_d['contact'] |
|
133 | tmp_d['contact_sort'] = tmp_d['contact'] | |
134 | tmp_d['owner_sort'] = tmp_d['contact'] |
|
134 | tmp_d['owner_sort'] = tmp_d['contact'] | |
135 | tmp_d['repo_archives'] = list(scmr._get_archives()) |
|
135 | tmp_d['repo_archives'] = list(scmr._get_archives()) | |
136 | tmp_d['last_msg'] = tip.message |
|
136 | tmp_d['last_msg'] = tip.message | |
137 | tmp_d['repo'] = scmr |
|
137 | tmp_d['repo'] = scmr | |
138 | tmp_d['dbrepo'] = dbr.get_dict() |
|
138 | tmp_d['dbrepo'] = dbr.get_dict() | |
139 | tmp_d['dbrepo_fork'] = dbr.fork.get_dict() if dbr.fork \ |
|
139 | tmp_d['dbrepo_fork'] = dbr.fork.get_dict() if dbr.fork \ | |
140 | else {} |
|
140 | else {} | |
141 | yield tmp_d |
|
141 | yield tmp_d | |
142 |
|
142 | |||
143 | class ScmModel(BaseModel): |
|
143 | class ScmModel(BaseModel): | |
144 | """Generic Scm Model |
|
144 | """Generic Scm Model | |
145 | """ |
|
145 | """ | |
146 |
|
146 | |||
147 | @LazyProperty |
|
147 | @LazyProperty | |
148 | def repos_path(self): |
|
148 | def repos_path(self): | |
149 | """Get's the repositories root path from database |
|
149 | """Get's the repositories root path from database | |
150 | """ |
|
150 | """ | |
151 |
|
151 | |||
152 | q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() |
|
152 | q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() | |
153 |
|
153 | |||
154 | return q.ui_value |
|
154 | return q.ui_value | |
155 |
|
155 | |||
156 | def repo_scan(self, repos_path=None): |
|
156 | def repo_scan(self, repos_path=None): | |
157 | """Listing of repositories in given path. This path should not be a |
|
157 | """Listing of repositories in given path. This path should not be a | |
158 | repository itself. Return a dictionary of repository objects |
|
158 | repository itself. Return a dictionary of repository objects | |
159 |
|
159 | |||
160 | :param repos_path: path to directory containing repositories |
|
160 | :param repos_path: path to directory containing repositories | |
161 | """ |
|
161 | """ | |
162 |
|
162 | |||
163 | log.info('scanning for repositories in %s', repos_path) |
|
163 | log.info('scanning for repositories in %s', repos_path) | |
164 |
|
164 | |||
165 | if repos_path is None: |
|
165 | if repos_path is None: | |
166 | repos_path = self.repos_path |
|
166 | repos_path = self.repos_path | |
167 |
|
167 | |||
168 | baseui = make_ui('db') |
|
168 | baseui = make_ui('db') | |
169 | repos_list = {} |
|
169 | repos_list = {} | |
170 |
|
170 | |||
171 | for name, path in get_filesystem_repos(repos_path, recursive=True): |
|
171 | for name, path in get_filesystem_repos(repos_path, recursive=True): | |
172 | try: |
|
172 | try: | |
173 | if name in repos_list: |
|
173 | if name in repos_list: | |
174 | raise RepositoryError('Duplicate repository name %s ' |
|
174 | raise RepositoryError('Duplicate repository name %s ' | |
175 | 'found in %s' % (name, path)) |
|
175 | 'found in %s' % (name, path)) | |
176 | else: |
|
176 | else: | |
177 |
|
177 | |||
178 | klass = get_backend(path[0]) |
|
178 | klass = get_backend(path[0]) | |
179 |
|
179 | |||
180 | if path[0] == 'hg' and path[0] in BACKENDS.keys(): |
|
180 | if path[0] == 'hg' and path[0] in BACKENDS.keys(): | |
181 | repos_list[name] = klass(path[1], baseui=baseui) |
|
181 | repos_list[name] = klass(path[1], baseui=baseui) | |
182 |
|
182 | |||
183 | if path[0] == 'git' and path[0] in BACKENDS.keys(): |
|
183 | if path[0] == 'git' and path[0] in BACKENDS.keys(): | |
184 | repos_list[name] = klass(path[1]) |
|
184 | repos_list[name] = klass(path[1]) | |
185 | except OSError: |
|
185 | except OSError: | |
186 | continue |
|
186 | continue | |
187 |
|
187 | |||
188 | return repos_list |
|
188 | return repos_list | |
189 |
|
189 | |||
190 | def get_repos(self, all_repos=None, sort_key=None): |
|
190 | def get_repos(self, all_repos=None, sort_key=None): | |
191 | """ |
|
191 | """ | |
192 | Get all repos from db and for each repo create it's |
|
192 | Get all repos from db and for each repo create it's | |
193 | backend instance and fill that backed with information from database |
|
193 | backend instance and fill that backed with information from database | |
194 |
|
194 | |||
195 | :param all_repos: list of repository names as strings |
|
195 | :param all_repos: list of repository names as strings | |
196 | give specific repositories list, good for filtering |
|
196 | give specific repositories list, good for filtering | |
197 | """ |
|
197 | """ | |
198 | if all_repos is None: |
|
198 | if all_repos is None: | |
199 | all_repos = self.sa.query(Repository)\ |
|
199 | all_repos = self.sa.query(Repository)\ | |
200 | .filter(Repository.group_id == None)\ |
|
200 | .filter(Repository.group_id == None)\ | |
201 | .order_by(Repository.repo_name).all() |
|
201 | .order_by(Repository.repo_name).all() | |
202 |
|
202 | |||
203 | #get the repositories that should be invalidated |
|
203 | #get the repositories that should be invalidated | |
204 | invalidation_list = [str(x.cache_key) for x in \ |
|
204 | invalidation_list = [str(x.cache_key) for x in \ | |
205 | self.sa.query(CacheInvalidation.cache_key)\ |
|
205 | self.sa.query(CacheInvalidation.cache_key)\ | |
206 | .filter(CacheInvalidation.cache_active == False)\ |
|
206 | .filter(CacheInvalidation.cache_active == False)\ | |
207 | .all()] |
|
207 | .all()] | |
208 |
|
208 | |||
209 | repo_iter = CachedRepoList(all_repos, invalidation_list, |
|
209 | repo_iter = CachedRepoList(all_repos, invalidation_list, | |
210 | repos_path=self.repos_path, |
|
210 | repos_path=self.repos_path, | |
211 | order_by=sort_key) |
|
211 | order_by=sort_key) | |
212 |
|
212 | |||
213 | return repo_iter |
|
213 | return repo_iter | |
214 |
|
214 | |||
215 | def mark_for_invalidation(self, repo_name): |
|
215 | def mark_for_invalidation(self, repo_name): | |
216 | """Puts cache invalidation task into db for |
|
216 | """Puts cache invalidation task into db for | |
217 | further global cache invalidation |
|
217 | further global cache invalidation | |
218 |
|
218 | |||
219 | :param repo_name: this repo that should invalidation take place |
|
219 | :param repo_name: this repo that should invalidation take place | |
220 | """ |
|
220 | """ | |
221 |
|
221 | |||
222 | log.debug('marking %s for invalidation', repo_name) |
|
222 | log.debug('marking %s for invalidation', repo_name) | |
223 | cache = self.sa.query(CacheInvalidation)\ |
|
223 | cache = self.sa.query(CacheInvalidation)\ | |
224 | .filter(CacheInvalidation.cache_key == repo_name).scalar() |
|
224 | .filter(CacheInvalidation.cache_key == repo_name).scalar() | |
225 |
|
225 | |||
226 | if cache: |
|
226 | if cache: | |
227 | #mark this cache as inactive |
|
227 | #mark this cache as inactive | |
228 | cache.cache_active = False |
|
228 | cache.cache_active = False | |
229 | else: |
|
229 | else: | |
230 | log.debug('cache key not found in invalidation db -> creating one') |
|
230 | log.debug('cache key not found in invalidation db -> creating one') | |
231 | cache = CacheInvalidation(repo_name) |
|
231 | cache = CacheInvalidation(repo_name) | |
232 |
|
232 | |||
233 | try: |
|
233 | try: | |
234 | self.sa.add(cache) |
|
234 | self.sa.add(cache) | |
235 | self.sa.commit() |
|
235 | self.sa.commit() | |
236 | except (DatabaseError,): |
|
236 | except (DatabaseError,): | |
237 | log.error(traceback.format_exc()) |
|
237 | log.error(traceback.format_exc()) | |
238 | self.sa.rollback() |
|
238 | self.sa.rollback() | |
239 |
|
239 | |||
240 | def toggle_following_repo(self, follow_repo_id, user_id): |
|
240 | def toggle_following_repo(self, follow_repo_id, user_id): | |
241 |
|
241 | |||
242 | f = self.sa.query(UserFollowing)\ |
|
242 | f = self.sa.query(UserFollowing)\ | |
243 | .filter(UserFollowing.follows_repo_id == follow_repo_id)\ |
|
243 | .filter(UserFollowing.follows_repo_id == follow_repo_id)\ | |
244 | .filter(UserFollowing.user_id == user_id).scalar() |
|
244 | .filter(UserFollowing.user_id == user_id).scalar() | |
245 |
|
245 | |||
246 | if f is not None: |
|
246 | if f is not None: | |
247 |
|
247 | |||
248 | try: |
|
248 | try: | |
249 | self.sa.delete(f) |
|
249 | self.sa.delete(f) | |
250 | self.sa.commit() |
|
250 | self.sa.commit() | |
251 | action_logger(UserTemp(user_id), |
|
251 | action_logger(UserTemp(user_id), | |
252 | 'stopped_following_repo', |
|
252 | 'stopped_following_repo', | |
253 | RepoTemp(follow_repo_id)) |
|
253 | RepoTemp(follow_repo_id)) | |
254 | return |
|
254 | return | |
255 | except: |
|
255 | except: | |
256 | log.error(traceback.format_exc()) |
|
256 | log.error(traceback.format_exc()) | |
257 | self.sa.rollback() |
|
257 | self.sa.rollback() | |
258 | raise |
|
258 | raise | |
259 |
|
259 | |||
260 | try: |
|
260 | try: | |
261 | f = UserFollowing() |
|
261 | f = UserFollowing() | |
262 | f.user_id = user_id |
|
262 | f.user_id = user_id | |
263 | f.follows_repo_id = follow_repo_id |
|
263 | f.follows_repo_id = follow_repo_id | |
264 | self.sa.add(f) |
|
264 | self.sa.add(f) | |
265 | self.sa.commit() |
|
265 | self.sa.commit() | |
266 | action_logger(UserTemp(user_id), |
|
266 | action_logger(UserTemp(user_id), | |
267 | 'started_following_repo', |
|
267 | 'started_following_repo', | |
268 | RepoTemp(follow_repo_id)) |
|
268 | RepoTemp(follow_repo_id)) | |
269 | except: |
|
269 | except: | |
270 | log.error(traceback.format_exc()) |
|
270 | log.error(traceback.format_exc()) | |
271 | self.sa.rollback() |
|
271 | self.sa.rollback() | |
272 | raise |
|
272 | raise | |
273 |
|
273 | |||
274 | def toggle_following_user(self, follow_user_id, user_id): |
|
274 | def toggle_following_user(self, follow_user_id, user_id): | |
275 | f = self.sa.query(UserFollowing)\ |
|
275 | f = self.sa.query(UserFollowing)\ | |
276 | .filter(UserFollowing.follows_user_id == follow_user_id)\ |
|
276 | .filter(UserFollowing.follows_user_id == follow_user_id)\ | |
277 | .filter(UserFollowing.user_id == user_id).scalar() |
|
277 | .filter(UserFollowing.user_id == user_id).scalar() | |
278 |
|
278 | |||
279 | if f is not None: |
|
279 | if f is not None: | |
280 | try: |
|
280 | try: | |
281 | self.sa.delete(f) |
|
281 | self.sa.delete(f) | |
282 | self.sa.commit() |
|
282 | self.sa.commit() | |
283 | return |
|
283 | return | |
284 | except: |
|
284 | except: | |
285 | log.error(traceback.format_exc()) |
|
285 | log.error(traceback.format_exc()) | |
286 | self.sa.rollback() |
|
286 | self.sa.rollback() | |
287 | raise |
|
287 | raise | |
288 |
|
288 | |||
289 | try: |
|
289 | try: | |
290 | f = UserFollowing() |
|
290 | f = UserFollowing() | |
291 | f.user_id = user_id |
|
291 | f.user_id = user_id | |
292 | f.follows_user_id = follow_user_id |
|
292 | f.follows_user_id = follow_user_id | |
293 | self.sa.add(f) |
|
293 | self.sa.add(f) | |
294 | self.sa.commit() |
|
294 | self.sa.commit() | |
295 | except: |
|
295 | except: | |
296 | log.error(traceback.format_exc()) |
|
296 | log.error(traceback.format_exc()) | |
297 | self.sa.rollback() |
|
297 | self.sa.rollback() | |
298 | raise |
|
298 | raise | |
299 |
|
299 | |||
300 | def is_following_repo(self, repo_name, user_id, cache=False): |
|
300 | def is_following_repo(self, repo_name, user_id, cache=False): | |
301 | r = self.sa.query(Repository)\ |
|
301 | r = self.sa.query(Repository)\ | |
302 | .filter(Repository.repo_name == repo_name).scalar() |
|
302 | .filter(Repository.repo_name == repo_name).scalar() | |
303 |
|
303 | |||
304 | f = self.sa.query(UserFollowing)\ |
|
304 | f = self.sa.query(UserFollowing)\ | |
305 | .filter(UserFollowing.follows_repository == r)\ |
|
305 | .filter(UserFollowing.follows_repository == r)\ | |
306 | .filter(UserFollowing.user_id == user_id).scalar() |
|
306 | .filter(UserFollowing.user_id == user_id).scalar() | |
307 |
|
307 | |||
308 | return f is not None |
|
308 | return f is not None | |
309 |
|
309 | |||
310 | def is_following_user(self, username, user_id, cache=False): |
|
310 | def is_following_user(self, username, user_id, cache=False): | |
311 | u = UserModel(self.sa).get_by_username(username) |
|
311 | u = UserModel(self.sa).get_by_username(username) | |
312 |
|
312 | |||
313 | f = self.sa.query(UserFollowing)\ |
|
313 | f = self.sa.query(UserFollowing)\ | |
314 | .filter(UserFollowing.follows_user == u)\ |
|
314 | .filter(UserFollowing.follows_user == u)\ | |
315 | .filter(UserFollowing.user_id == user_id).scalar() |
|
315 | .filter(UserFollowing.user_id == user_id).scalar() | |
316 |
|
316 | |||
317 | return f is not None |
|
317 | return f is not None | |
318 |
|
318 | |||
319 | def get_followers(self, repo_id): |
|
319 | def get_followers(self, repo_id): | |
320 | if not isinstance(repo_id, int): |
|
320 | if not isinstance(repo_id, int): | |
321 | repo_id = getattr(Repository.by_repo_name(repo_id), 'repo_id') |
|
321 | repo_id = getattr(Repository.by_repo_name(repo_id), 'repo_id') | |
322 |
|
322 | |||
323 | return self.sa.query(UserFollowing)\ |
|
323 | return self.sa.query(UserFollowing)\ | |
324 | .filter(UserFollowing.follows_repo_id == repo_id).count() |
|
324 | .filter(UserFollowing.follows_repo_id == repo_id).count() | |
325 |
|
325 | |||
326 | def get_forks(self, repo_id): |
|
326 | def get_forks(self, repo_id): | |
327 | if not isinstance(repo_id, int): |
|
327 | if not isinstance(repo_id, int): | |
328 | repo_id = getattr(Repository.by_repo_name(repo_id), 'repo_id') |
|
328 | repo_id = getattr(Repository.by_repo_name(repo_id), 'repo_id') | |
329 |
|
329 | |||
330 | return self.sa.query(Repository)\ |
|
330 | return self.sa.query(Repository)\ | |
331 | .filter(Repository.fork_id == repo_id).count() |
|
331 | .filter(Repository.fork_id == repo_id).count() | |
332 |
|
332 | |||
333 | def pull_changes(self, repo_name, username): |
|
333 | def pull_changes(self, repo_name, username): | |
334 | repo, dbrepo = self.get(repo_name, retval='all') |
|
334 | dbrepo = Repository.by_repo_name(repo_name) | |
335 |
|
335 | repo = dbrepo.scm_instance | ||
336 | try: |
|
336 | try: | |
337 | extras = {'ip': '', |
|
337 | extras = {'ip': '', | |
338 | 'username': username, |
|
338 | 'username': username, | |
339 | 'action': 'push_remote', |
|
339 | 'action': 'push_remote', | |
340 | 'repository': repo_name} |
|
340 | 'repository': repo_name} | |
341 |
|
341 | |||
342 | #inject ui extra param to log this action via push logger |
|
342 | #inject ui extra param to log this action via push logger | |
343 | for k, v in extras.items(): |
|
343 | for k, v in extras.items(): | |
344 | repo._repo.ui.setconfig('rhodecode_extras', k, v) |
|
344 | repo._repo.ui.setconfig('rhodecode_extras', k, v) | |
345 |
|
345 | |||
346 | repo.pull(dbrepo.clone_uri) |
|
346 | repo.pull(dbrepo.clone_uri) | |
347 | self.mark_for_invalidation(repo_name) |
|
347 | self.mark_for_invalidation(repo_name) | |
348 | except: |
|
348 | except: | |
349 | log.error(traceback.format_exc()) |
|
349 | log.error(traceback.format_exc()) | |
350 | raise |
|
350 | raise | |
351 |
|
351 | |||
352 |
|
352 | |||
353 | def commit_change(self, repo, repo_name, cs, user, author, message, content, |
|
353 | def commit_change(self, repo, repo_name, cs, user, author, message, content, | |
354 | f_path): |
|
354 | f_path): | |
355 |
|
355 | |||
356 | if repo.alias == 'hg': |
|
356 | if repo.alias == 'hg': | |
357 | from vcs.backends.hg import MercurialInMemoryChangeset as IMC |
|
357 | from vcs.backends.hg import MercurialInMemoryChangeset as IMC | |
358 | elif repo.alias == 'git': |
|
358 | elif repo.alias == 'git': | |
359 | from vcs.backends.git import GitInMemoryChangeset as IMC |
|
359 | from vcs.backends.git import GitInMemoryChangeset as IMC | |
360 |
|
360 | |||
361 | # decoding here will force that we have proper encoded values |
|
361 | # decoding here will force that we have proper encoded values | |
362 | # in any other case this will throw exceptions and deny commit |
|
362 | # in any other case this will throw exceptions and deny commit | |
363 | content = content.encode('utf8') |
|
363 | content = content.encode('utf8') | |
364 | message = message.encode('utf8') |
|
364 | message = message.encode('utf8') | |
365 | path = f_path.encode('utf8') |
|
365 | path = f_path.encode('utf8') | |
366 | author = author.encode('utf8') |
|
366 | author = author.encode('utf8') | |
367 | m = IMC(repo) |
|
367 | m = IMC(repo) | |
368 | m.change(FileNode(path, content)) |
|
368 | m.change(FileNode(path, content)) | |
369 | tip = m.commit(message=message, |
|
369 | tip = m.commit(message=message, | |
370 | author=author, |
|
370 | author=author, | |
371 | parents=[cs], branch=cs.branch) |
|
371 | parents=[cs], branch=cs.branch) | |
372 |
|
372 | |||
373 | new_cs = tip.short_id |
|
373 | new_cs = tip.short_id | |
374 | action = 'push_local:%s' % new_cs |
|
374 | action = 'push_local:%s' % new_cs | |
375 |
|
375 | |||
376 | action_logger(user, action, repo_name) |
|
376 | action_logger(user, action, repo_name) | |
377 |
|
377 | |||
378 | self.mark_for_invalidation(repo_name) |
|
378 | self.mark_for_invalidation(repo_name) | |
379 |
|
379 | |||
380 |
|
380 | |||
381 | def get_unread_journal(self): |
|
381 | def get_unread_journal(self): | |
382 | return self.sa.query(UserLog).count() |
|
382 | return self.sa.query(UserLog).count() | |
383 |
|
383 | |||
384 | def _should_invalidate(self, repo_name): |
|
384 | def _should_invalidate(self, repo_name): | |
385 | """Looks up database for invalidation signals for this repo_name |
|
385 | """Looks up database for invalidation signals for this repo_name | |
386 |
|
386 | |||
387 | :param repo_name: |
|
387 | :param repo_name: | |
388 | """ |
|
388 | """ | |
389 |
|
389 | |||
390 | ret = self.sa.query(CacheInvalidation)\ |
|
390 | ret = self.sa.query(CacheInvalidation)\ | |
391 | .filter(CacheInvalidation.cache_key == repo_name)\ |
|
391 | .filter(CacheInvalidation.cache_key == repo_name)\ | |
392 | .filter(CacheInvalidation.cache_active == False)\ |
|
392 | .filter(CacheInvalidation.cache_active == False)\ | |
393 | .scalar() |
|
393 | .scalar() | |
394 |
|
394 | |||
395 | return ret |
|
395 | return ret | |
396 |
|
396 | |||
397 | def _mark_invalidated(self, cache_key): |
|
397 | def _mark_invalidated(self, cache_key): | |
398 | """ Marks all occurrences of cache to invalidation as already |
|
398 | """ Marks all occurrences of cache to invalidation as already | |
399 | invalidated |
|
399 | invalidated | |
400 |
|
400 | |||
401 | :param cache_key: |
|
401 | :param cache_key: | |
402 | """ |
|
402 | """ | |
403 |
|
403 | |||
404 | if cache_key: |
|
404 | if cache_key: | |
405 | log.debug('marking %s as already invalidated', cache_key) |
|
405 | log.debug('marking %s as already invalidated', cache_key) | |
406 | try: |
|
406 | try: | |
407 | cache_key.cache_active = True |
|
407 | cache_key.cache_active = True | |
408 | self.sa.add(cache_key) |
|
408 | self.sa.add(cache_key) | |
409 | self.sa.commit() |
|
409 | self.sa.commit() | |
410 | except (DatabaseError,): |
|
410 | except (DatabaseError,): | |
411 | log.error(traceback.format_exc()) |
|
411 | log.error(traceback.format_exc()) | |
412 | self.sa.rollback() |
|
412 | self.sa.rollback() |
General Comments 0
You need to be logged in to leave comments.
Login now