Show More
@@ -1,300 +1,304 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Summary controller for RhodeCode Enterprise |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import logging |
|
26 | 26 | from string import lower |
|
27 | 27 | |
|
28 | 28 | from pylons import tmpl_context as c, request |
|
29 | 29 | from pylons.i18n.translation import _ |
|
30 | 30 | from beaker.cache import cache_region, region_invalidate |
|
31 | 31 | |
|
32 | 32 | from rhodecode.config.conf import (LANGUAGES_EXTENSIONS_MAP) |
|
33 | 33 | from rhodecode.controllers import utils |
|
34 | 34 | from rhodecode.controllers.changelog import _load_changelog_summary |
|
35 | 35 | from rhodecode.lib import caches, helpers as h |
|
36 | 36 | from rhodecode.lib.utils import jsonify |
|
37 | 37 | from rhodecode.lib.utils2 import safe_str |
|
38 | 38 | from rhodecode.lib.auth import ( |
|
39 | 39 | LoginRequired, HasRepoPermissionAnyDecorator, NotAnonymous, XHRRequired) |
|
40 | 40 | from rhodecode.lib.base import BaseRepoController, render |
|
41 | 41 | from rhodecode.lib.markup_renderer import MarkupRenderer |
|
42 | 42 | from rhodecode.lib.ext_json import json |
|
43 | 43 | from rhodecode.lib.vcs.backends.base import EmptyCommit |
|
44 | 44 | from rhodecode.lib.vcs.exceptions import ( |
|
45 | 45 | CommitError, EmptyRepositoryError, NodeDoesNotExistError) |
|
46 | 46 | from rhodecode.model.db import Statistics, CacheKey, User |
|
47 | 47 | |
|
48 | 48 | log = logging.getLogger(__name__) |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | class SummaryController(BaseRepoController): |
|
52 | 52 | |
|
53 | 53 | def __before__(self): |
|
54 | 54 | super(SummaryController, self).__before__() |
|
55 | 55 | |
|
56 | 56 | def __get_readme_data(self, db_repo): |
|
57 | 57 | repo_name = db_repo.repo_name |
|
58 | 58 | log.debug('Looking for README file') |
|
59 | 59 | default_renderer = c.visual.default_renderer |
|
60 | 60 | |
|
61 | 61 | @cache_region('long_term') |
|
62 | 62 | def _generate_readme(cache_key): |
|
63 | 63 | readme_data = None |
|
64 | 64 | readme_file = None |
|
65 | 65 | try: |
|
66 | 66 | # gets the landing revision or tip if fails |
|
67 | 67 | commit = db_repo.get_landing_commit() |
|
68 | 68 | if isinstance(commit, EmptyCommit): |
|
69 | 69 | raise EmptyRepositoryError() |
|
70 | 70 | renderer = MarkupRenderer() |
|
71 | 71 | for f in renderer.pick_readme_order(default_renderer): |
|
72 | 72 | try: |
|
73 | 73 | node = commit.get_node(f) |
|
74 | 74 | except NodeDoesNotExistError: |
|
75 | 75 | continue |
|
76 | 76 | |
|
77 | 77 | if not node.is_file(): |
|
78 | 78 | continue |
|
79 | 79 | |
|
80 | 80 | readme_file = f |
|
81 | 81 | log.debug('Found README file `%s` rendering...', |
|
82 | 82 | readme_file) |
|
83 | 83 | readme_data = renderer.render(node.content, |
|
84 | 84 | filename=f) |
|
85 | 85 | break |
|
86 | 86 | except CommitError: |
|
87 |
log.exception( |
|
|
88 | pass | |
|
87 | log.exception( | |
|
88 | "Problem getting commit when trying to render the README.") | |
|
89 | 89 | except EmptyRepositoryError: |
|
90 | pass | |
|
90 | log.debug("Repository is empty, no README to render.") | |
|
91 | 91 | except Exception: |
|
92 |
log.exception(" |
|
|
92 | log.exception("Exception while trying to render the README") | |
|
93 | 93 | |
|
94 | 94 | return readme_data, readme_file |
|
95 | 95 | |
|
96 | 96 | invalidator_context = CacheKey.repo_context_cache( |
|
97 | 97 | _generate_readme, repo_name, CacheKey.CACHE_TYPE_README) |
|
98 | 98 | |
|
99 | 99 | with invalidator_context as context: |
|
100 | 100 | context.invalidate() |
|
101 | 101 | computed = context.compute() |
|
102 | 102 | |
|
103 | 103 | return computed |
|
104 | 104 | |
|
105 | ||
|
106 | 105 | @LoginRequired() |
|
107 | 106 | @HasRepoPermissionAnyDecorator( |
|
108 | 107 | 'repository.read', 'repository.write', 'repository.admin') |
|
109 | 108 | def index(self, repo_name): |
|
109 | ||
|
110 | # Prepare the clone URL | |
|
111 | ||
|
110 | 112 | username = '' |
|
111 | 113 | if c.rhodecode_user.username != User.DEFAULT_USER: |
|
112 | 114 | username = safe_str(c.rhodecode_user.username) |
|
113 | 115 | |
|
114 | 116 | _def_clone_uri = _def_clone_uri_by_id = c.clone_uri_tmpl |
|
115 | 117 | if '{repo}' in _def_clone_uri: |
|
116 | 118 | _def_clone_uri_by_id = _def_clone_uri.replace( |
|
117 | 119 | '{repo}', '_{repoid}') |
|
118 | 120 | elif '{repoid}' in _def_clone_uri: |
|
119 | 121 | _def_clone_uri_by_id = _def_clone_uri.replace( |
|
120 | 122 | '_{repoid}', '{repo}') |
|
121 | 123 | |
|
122 | 124 | c.clone_repo_url = c.rhodecode_db_repo.clone_url( |
|
123 | 125 | user=username, uri_tmpl=_def_clone_uri) |
|
124 | 126 | c.clone_repo_url_id = c.rhodecode_db_repo.clone_url( |
|
125 | 127 | user=username, uri_tmpl=_def_clone_uri_by_id) |
|
126 | 128 | |
|
129 | # If enabled, get statistics data | |
|
130 | ||
|
127 | 131 | c.show_stats = bool(c.rhodecode_db_repo.enable_statistics) |
|
128 | 132 | |
|
129 | 133 | stats = self.sa.query(Statistics)\ |
|
130 | 134 | .filter(Statistics.repository == c.rhodecode_db_repo)\ |
|
131 | 135 | .scalar() |
|
132 | 136 | |
|
133 | 137 | c.stats_percentage = 0 |
|
134 | 138 | |
|
135 | 139 | if stats and stats.languages: |
|
136 | 140 | c.no_data = False is c.rhodecode_db_repo.enable_statistics |
|
137 | 141 | lang_stats_d = json.loads(stats.languages) |
|
138 | 142 | |
|
139 | 143 | # Sort first by decreasing count and second by the file extension, |
|
140 | 144 | # so we have a consistent output. |
|
141 | 145 | lang_stats_items = sorted(lang_stats_d.iteritems(), |
|
142 | 146 | key=lambda k: (-k[1], k[0]))[:10] |
|
143 | 147 | lang_stats = [(x, {"count": y, |
|
144 | 148 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) |
|
145 | 149 | for x, y in lang_stats_items] |
|
146 | 150 | |
|
147 | 151 | c.trending_languages = json.dumps(lang_stats) |
|
148 | 152 | else: |
|
149 | 153 | c.no_data = True |
|
150 | 154 | c.trending_languages = json.dumps({}) |
|
151 | 155 | |
|
152 | 156 | c.enable_downloads = c.rhodecode_db_repo.enable_downloads |
|
153 | 157 | c.repository_followers = self.scm_model.get_followers( |
|
154 | 158 | c.rhodecode_db_repo) |
|
155 | 159 | c.repository_forks = self.scm_model.get_forks(c.rhodecode_db_repo) |
|
156 | 160 | c.repository_is_user_following = self.scm_model.is_following_repo( |
|
157 | 161 | c.repo_name, c.rhodecode_user.user_id) |
|
158 | 162 | |
|
159 | 163 | if c.repository_requirements_missing: |
|
160 | 164 | return render('summary/missing_requirements.html') |
|
161 | 165 | |
|
162 | 166 | c.readme_data, c.readme_file = \ |
|
163 | 167 | self.__get_readme_data(c.rhodecode_db_repo) |
|
164 | 168 | |
|
165 | 169 | _load_changelog_summary() |
|
166 | 170 | |
|
167 | 171 | if request.is_xhr: |
|
168 | 172 | return render('changelog/changelog_summary_data.html') |
|
169 | 173 | |
|
170 | 174 | return render('summary/summary.html') |
|
171 | 175 | |
|
172 | 176 | @LoginRequired() |
|
173 | 177 | @XHRRequired() |
|
174 | 178 | @HasRepoPermissionAnyDecorator( |
|
175 | 179 | 'repository.read', 'repository.write', 'repository.admin') |
|
176 | 180 | @jsonify |
|
177 | 181 | def repo_stats(self, repo_name, commit_id): |
|
178 | 182 | _namespace = caches.get_repo_namespace_key( |
|
179 | 183 | caches.SUMMARY_STATS, repo_name) |
|
180 | 184 | show_stats = bool(c.rhodecode_db_repo.enable_statistics) |
|
181 | 185 | cache_manager = caches.get_cache_manager('repo_cache_long', _namespace) |
|
182 | 186 | _cache_key = caches.compute_key_from_params( |
|
183 | 187 | repo_name, commit_id, show_stats) |
|
184 | 188 | |
|
185 | 189 | def compute_stats(): |
|
186 | 190 | code_stats = {} |
|
187 | 191 | size = 0 |
|
188 | 192 | try: |
|
189 | 193 | scm_instance = c.rhodecode_db_repo.scm_instance() |
|
190 | 194 | commit = scm_instance.get_commit(commit_id) |
|
191 | 195 | |
|
192 | 196 | for node in commit.get_filenodes_generator(): |
|
193 | 197 | size += node.size |
|
194 | 198 | if not show_stats: |
|
195 | 199 | continue |
|
196 | 200 | ext = lower(node.extension) |
|
197 | 201 | ext_info = LANGUAGES_EXTENSIONS_MAP.get(ext) |
|
198 | 202 | if ext_info: |
|
199 | 203 | if ext in code_stats: |
|
200 | 204 | code_stats[ext]['count'] += 1 |
|
201 | 205 | else: |
|
202 | 206 | code_stats[ext] = {"count": 1, "desc": ext_info} |
|
203 | 207 | except EmptyRepositoryError: |
|
204 | 208 | pass |
|
205 | 209 | return {'size': h.format_byte_size_binary(size), |
|
206 | 210 | 'code_stats': code_stats} |
|
207 | 211 | |
|
208 | 212 | stats = cache_manager.get(_cache_key, createfunc=compute_stats) |
|
209 | 213 | return stats |
|
210 | 214 | |
|
211 | 215 | def _switcher_reference_data(self, repo_name, references, is_svn): |
|
212 | 216 | """Prepare reference data for given `references`""" |
|
213 | 217 | items = [] |
|
214 | 218 | for name, commit_id in references.items(): |
|
215 | 219 | use_commit_id = '/' in name or is_svn |
|
216 | 220 | items.append({ |
|
217 | 221 | 'name': name, |
|
218 | 222 | 'commit_id': commit_id, |
|
219 | 223 | 'files_url': h.url( |
|
220 | 224 | 'files_home', |
|
221 | 225 | repo_name=repo_name, |
|
222 | 226 | f_path=name if is_svn else '', |
|
223 | 227 | revision=commit_id if use_commit_id else name, |
|
224 | 228 | at=name) |
|
225 | 229 | }) |
|
226 | 230 | return items |
|
227 | 231 | |
|
228 | 232 | @LoginRequired() |
|
229 | 233 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
230 | 234 | 'repository.admin') |
|
231 | 235 | @jsonify |
|
232 | 236 | def repo_refs_data(self, repo_name): |
|
233 | 237 | repo = c.rhodecode_repo |
|
234 | 238 | refs_to_create = [ |
|
235 | 239 | (_("Branch"), repo.branches, 'branch'), |
|
236 | 240 | (_("Tag"), repo.tags, 'tag'), |
|
237 | 241 | (_("Bookmark"), repo.bookmarks, 'book'), |
|
238 | 242 | ] |
|
239 | 243 | res = self._create_reference_data(repo, repo_name, refs_to_create) |
|
240 | 244 | data = { |
|
241 | 245 | 'more': False, |
|
242 | 246 | 'results': res |
|
243 | 247 | } |
|
244 | 248 | return data |
|
245 | 249 | |
|
246 | 250 | @jsonify |
|
247 | 251 | def repo_refs_changelog_data(self, repo_name): |
|
248 | 252 | repo = c.rhodecode_repo |
|
249 | 253 | |
|
250 | 254 | refs_to_create = [ |
|
251 | 255 | (_("Branches"), repo.branches, 'branch'), |
|
252 | 256 | (_("Closed branches"), repo.branches_closed, 'branch_closed'), |
|
253 | 257 | # TODO: enable when vcs can handle bookmarks filters |
|
254 | 258 | # (_("Bookmarks"), repo.bookmarks, "book"), |
|
255 | 259 | ] |
|
256 | 260 | res = self._create_reference_data(repo, repo_name, refs_to_create) |
|
257 | 261 | data = { |
|
258 | 262 | 'more': False, |
|
259 | 263 | 'results': res |
|
260 | 264 | } |
|
261 | 265 | return data |
|
262 | 266 | |
|
263 | 267 | def _create_reference_data(self, repo, full_repo_name, refs_to_create): |
|
264 | 268 | format_ref_id = utils.get_format_ref_id(repo) |
|
265 | 269 | |
|
266 | 270 | result = [] |
|
267 | 271 | for title, refs, ref_type in refs_to_create: |
|
268 | 272 | if refs: |
|
269 | 273 | result.append({ |
|
270 | 274 | 'text': title, |
|
271 | 275 | 'children': self._create_reference_items( |
|
272 | 276 | repo, full_repo_name, refs, ref_type, format_ref_id), |
|
273 | 277 | }) |
|
274 | 278 | return result |
|
275 | 279 | |
|
276 | 280 | def _create_reference_items(self, repo, full_repo_name, refs, ref_type, |
|
277 | 281 | format_ref_id): |
|
278 | 282 | result = [] |
|
279 | 283 | is_svn = h.is_svn(repo) |
|
280 | 284 | for ref_name, raw_id in refs.iteritems(): |
|
281 | 285 | files_url = self._create_files_url( |
|
282 | 286 | repo, full_repo_name, ref_name, raw_id, is_svn) |
|
283 | 287 | result.append({ |
|
284 | 288 | 'text': ref_name, |
|
285 | 289 | 'id': format_ref_id(ref_name, raw_id), |
|
286 | 290 | 'raw_id': raw_id, |
|
287 | 291 | 'type': ref_type, |
|
288 | 292 | 'files_url': files_url, |
|
289 | 293 | }) |
|
290 | 294 | return result |
|
291 | 295 | |
|
292 | 296 | def _create_files_url(self, repo, full_repo_name, ref_name, raw_id, |
|
293 | 297 | is_svn): |
|
294 | 298 | use_commit_id = '/' in ref_name or is_svn |
|
295 | 299 | return h.url( |
|
296 | 300 | 'files_home', |
|
297 | 301 | repo_name=full_repo_name, |
|
298 | 302 | f_path=ref_name if is_svn else '', |
|
299 | 303 | revision=raw_id if use_commit_id else ref_name, |
|
300 | 304 | at=ref_name) |
General Comments 0
You need to be logged in to leave comments.
Login now