Show More
@@ -1,264 +1,251 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.summary |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Summary controller for Rhodecode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 18, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | |
|
26 | 26 | import traceback |
|
27 | 27 | import calendar |
|
28 | 28 | import logging |
|
29 | 29 | import urllib |
|
30 | 30 | from time import mktime |
|
31 | 31 | from datetime import timedelta, date |
|
32 | 32 | from urlparse import urlparse |
|
33 | 33 | |
|
34 | 34 | from pylons import tmpl_context as c, request, url, config |
|
35 | 35 | from pylons.i18n.translation import _ |
|
36 | 36 | from webob.exc import HTTPBadRequest |
|
37 | 37 | |
|
38 | 38 | from beaker.cache import cache_region, region_invalidate |
|
39 | 39 | |
|
40 | 40 | from rhodecode.lib import helpers as h |
|
41 | 41 | from rhodecode.lib.compat import product |
|
42 | 42 | from rhodecode.lib.vcs.exceptions import ChangesetError, EmptyRepositoryError, \ |
|
43 | 43 | NodeDoesNotExistError |
|
44 | 44 | from rhodecode.config.conf import ALL_READMES, ALL_EXTS, LANGUAGES_EXTENSIONS_MAP |
|
45 | 45 | from rhodecode.model.db import Statistics, CacheInvalidation |
|
46 | 46 | from rhodecode.lib.utils import jsonify |
|
47 | 47 | from rhodecode.lib.utils2 import safe_unicode, safe_str |
|
48 | 48 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator,\ |
|
49 | 49 | NotAnonymous |
|
50 | 50 | from rhodecode.lib.base import BaseRepoController, render |
|
51 | 51 | from rhodecode.lib.vcs.backends.base import EmptyChangeset |
|
52 | 52 | from rhodecode.lib.markup_renderer import MarkupRenderer |
|
53 | 53 | from rhodecode.lib.celerylib import run_task |
|
54 | 54 | from rhodecode.lib.celerylib.tasks import get_commits_stats |
|
55 | 55 | from rhodecode.lib.helpers import RepoPage |
|
56 | 56 | from rhodecode.lib.compat import json, OrderedDict |
|
57 | 57 | from rhodecode.lib.vcs.nodes import FileNode |
|
58 | 58 | |
|
59 | 59 | log = logging.getLogger(__name__) |
|
60 | 60 | |
|
61 | 61 | README_FILES = [''.join([x[0][0], x[1][0]]) for x in |
|
62 | 62 | sorted(list(product(ALL_READMES, ALL_EXTS)), |
|
63 | 63 | key=lambda y:y[0][1] + y[1][1])] |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | class SummaryController(BaseRepoController): |
|
67 | 67 | |
|
68 | 68 | def __before__(self): |
|
69 | 69 | super(SummaryController, self).__before__() |
|
70 | 70 | |
|
71 | 71 | def _get_download_links(self, repo): |
|
72 | 72 | |
|
73 | 73 | download_l = [] |
|
74 | 74 | |
|
75 | 75 | branches_group = ([], _("Branches")) |
|
76 | 76 | tags_group = ([], _("Tags")) |
|
77 | 77 | |
|
78 | 78 | for name, chs in c.rhodecode_repo.branches.items(): |
|
79 | 79 | #chs = chs.split(':')[-1] |
|
80 | 80 | branches_group[0].append((chs, name),) |
|
81 | 81 | download_l.append(branches_group) |
|
82 | 82 | |
|
83 | 83 | for name, chs in c.rhodecode_repo.tags.items(): |
|
84 | 84 | #chs = chs.split(':')[-1] |
|
85 | 85 | tags_group[0].append((chs, name),) |
|
86 | 86 | download_l.append(tags_group) |
|
87 | 87 | |
|
88 | 88 | return download_l |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | def __get_readme_data(self, db_repo): |
|
92 | 92 | repo_name = db_repo.repo_name |
|
93 | 93 | |
|
94 | 94 | @cache_region('long_term') |
|
95 | 95 | def _get_readme_from_cache(key): |
|
96 | 96 | readme_data = None |
|
97 | 97 | readme_file = None |
|
98 | 98 | log.debug('Looking for README file') |
|
99 | 99 | try: |
|
100 | 100 | # get's the landing revision! or tip if fails |
|
101 | 101 | cs = db_repo.get_landing_changeset() |
|
102 | 102 | if isinstance(cs, EmptyChangeset): |
|
103 | 103 | raise EmptyRepositoryError() |
|
104 | 104 | renderer = MarkupRenderer() |
|
105 | 105 | for f in README_FILES: |
|
106 | 106 | try: |
|
107 | 107 | readme = cs.get_node(f) |
|
108 | 108 | if not isinstance(readme, FileNode): |
|
109 | 109 | continue |
|
110 | 110 | readme_file = f |
|
111 | 111 | log.debug('Found README file `%s` rendering...' % |
|
112 | 112 | readme_file) |
|
113 | 113 | readme_data = renderer.render(readme.content, f) |
|
114 | 114 | break |
|
115 | 115 | except NodeDoesNotExistError: |
|
116 | 116 | continue |
|
117 | 117 | except ChangesetError: |
|
118 | 118 | log.error(traceback.format_exc()) |
|
119 | 119 | pass |
|
120 | 120 | except EmptyRepositoryError: |
|
121 | 121 | pass |
|
122 | 122 | except Exception: |
|
123 | 123 | log.error(traceback.format_exc()) |
|
124 | 124 | |
|
125 | 125 | return readme_data, readme_file |
|
126 | 126 | |
|
127 | 127 | key = repo_name + '_README' |
|
128 | 128 | inv = CacheInvalidation.invalidate(key) |
|
129 | 129 | if inv is not None: |
|
130 | 130 | region_invalidate(_get_readme_from_cache, None, key) |
|
131 | 131 | CacheInvalidation.set_valid(inv.cache_key) |
|
132 | 132 | return _get_readme_from_cache(key) |
|
133 | 133 | |
|
134 | 134 | @LoginRequired() |
|
135 | 135 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
136 | 136 | 'repository.admin') |
|
137 | 137 | def index(self, repo_name): |
|
138 | 138 | c.dbrepo = dbrepo = c.rhodecode_db_repo |
|
139 | 139 | |
|
140 | 140 | def url_generator(**kw): |
|
141 | 141 | return url('shortlog_home', repo_name=repo_name, size=10, **kw) |
|
142 | 142 | |
|
143 | 143 | c.repo_changesets = RepoPage(c.rhodecode_repo, page=1, |
|
144 | 144 | items_per_page=10, url=url_generator) |
|
145 | 145 | page_revisions = [x.raw_id for x in list(c.repo_changesets)] |
|
146 | 146 | c.statuses = c.rhodecode_db_repo.statuses(page_revisions) |
|
147 | 147 | |
|
148 | 148 | if self.rhodecode_user.username == 'default': |
|
149 | 149 | # for default(anonymous) user we don't need to pass credentials |
|
150 | 150 | username = '' |
|
151 | 151 | password = '' |
|
152 | 152 | else: |
|
153 | 153 | username = str(self.rhodecode_user.username) |
|
154 | 154 | password = '@' |
|
155 | 155 | |
|
156 | 156 | parsed_url = urlparse(url.current(qualified=True)) |
|
157 | 157 | |
|
158 | 158 | default_clone_uri = '{scheme}://{user}{pass}{netloc}{path}' |
|
159 | 159 | |
|
160 | 160 | uri_tmpl = config.get('clone_uri', default_clone_uri) |
|
161 | 161 | uri_tmpl = uri_tmpl.replace('{', '%(').replace('}', ')s') |
|
162 | 162 | decoded_path = safe_unicode(urllib.unquote(parsed_url.path)) |
|
163 | 163 | uri_dict = { |
|
164 | 164 | 'user': urllib.quote(username), |
|
165 | 165 | 'pass': password, |
|
166 | 166 | 'scheme': parsed_url.scheme, |
|
167 | 167 | 'netloc': parsed_url.netloc, |
|
168 | 168 | 'path': urllib.quote(safe_str(decoded_path)) |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | uri = (uri_tmpl % uri_dict) |
|
172 | 172 | # generate another clone url by id |
|
173 | 173 | uri_dict.update( |
|
174 | 174 | {'path': decoded_path.replace(repo_name, '_%s' % c.dbrepo.repo_id)} |
|
175 | 175 | ) |
|
176 | 176 | uri_id = uri_tmpl % uri_dict |
|
177 | 177 | |
|
178 | 178 | c.clone_repo_url = uri |
|
179 | 179 | c.clone_repo_url_id = uri_id |
|
180 | c.repo_tags = OrderedDict() | |
|
181 | for name, hash_ in c.rhodecode_repo.tags.items()[:10]: | |
|
182 | try: | |
|
183 | c.repo_tags[name] = c.rhodecode_repo.get_changeset(hash_) | |
|
184 | except ChangesetError: | |
|
185 | c.repo_tags[name] = EmptyChangeset(hash_) | |
|
186 | ||
|
187 | c.repo_branches = OrderedDict() | |
|
188 | for name, hash_ in c.rhodecode_repo.branches.items()[:10]: | |
|
189 | try: | |
|
190 | c.repo_branches[name] = c.rhodecode_repo.get_changeset(hash_) | |
|
191 | except ChangesetError: | |
|
192 | c.repo_branches[name] = EmptyChangeset(hash_) | |
|
193 | 180 | |
|
194 | 181 | td = date.today() + timedelta(days=1) |
|
195 | 182 | td_1m = td - timedelta(days=calendar.mdays[td.month]) |
|
196 | 183 | td_1y = td - timedelta(days=365) |
|
197 | 184 | |
|
198 | 185 | ts_min_m = mktime(td_1m.timetuple()) |
|
199 | 186 | ts_min_y = mktime(td_1y.timetuple()) |
|
200 | 187 | ts_max_y = mktime(td.timetuple()) |
|
201 | 188 | |
|
202 | 189 | if dbrepo.enable_statistics: |
|
203 | 190 | c.show_stats = True |
|
204 | 191 | c.no_data_msg = _('No data loaded yet') |
|
205 | 192 | recurse_limit = 500 # don't recurse more than 500 times when parsing |
|
206 | 193 | run_task(get_commits_stats, c.dbrepo.repo_name, ts_min_y, |
|
207 | 194 | ts_max_y, recurse_limit) |
|
208 | 195 | else: |
|
209 | 196 | c.show_stats = False |
|
210 | 197 | c.no_data_msg = _('Statistics are disabled for this repository') |
|
211 | 198 | c.ts_min = ts_min_m |
|
212 | 199 | c.ts_max = ts_max_y |
|
213 | 200 | |
|
214 | 201 | stats = self.sa.query(Statistics)\ |
|
215 | 202 | .filter(Statistics.repository == dbrepo)\ |
|
216 | 203 | .scalar() |
|
217 | 204 | |
|
218 | 205 | c.stats_percentage = 0 |
|
219 | 206 | |
|
220 | 207 | if stats and stats.languages: |
|
221 | 208 | c.no_data = False is dbrepo.enable_statistics |
|
222 | 209 | lang_stats_d = json.loads(stats.languages) |
|
223 | 210 | c.commit_data = stats.commit_activity |
|
224 | 211 | c.overview_data = stats.commit_activity_combined |
|
225 | 212 | |
|
226 | 213 | lang_stats = ((x, {"count": y, |
|
227 | 214 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) |
|
228 | 215 | for x, y in lang_stats_d.items()) |
|
229 | 216 | |
|
230 | 217 | c.trending_languages = json.dumps( |
|
231 | 218 | sorted(lang_stats, reverse=True, key=lambda k: k[1])[:10] |
|
232 | 219 | ) |
|
233 | 220 | last_rev = stats.stat_on_revision + 1 |
|
234 | 221 | c.repo_last_rev = c.rhodecode_repo.count()\ |
|
235 | 222 | if c.rhodecode_repo.revisions else 0 |
|
236 | 223 | if last_rev == 0 or c.repo_last_rev == 0: |
|
237 | 224 | pass |
|
238 | 225 | else: |
|
239 | 226 | c.stats_percentage = '%.2f' % ((float((last_rev)) / |
|
240 | 227 | c.repo_last_rev) * 100) |
|
241 | 228 | else: |
|
242 | 229 | c.commit_data = json.dumps({}) |
|
243 | 230 | c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 10]]) |
|
244 | 231 | c.trending_languages = json.dumps({}) |
|
245 | 232 | c.no_data = True |
|
246 | 233 | |
|
247 | 234 | c.enable_downloads = dbrepo.enable_downloads |
|
248 | 235 | if c.enable_downloads: |
|
249 | 236 | c.download_options = self._get_download_links(c.rhodecode_repo) |
|
250 | 237 | |
|
251 | 238 | c.readme_data, c.readme_file = \ |
|
252 | 239 | self.__get_readme_data(c.rhodecode_db_repo) |
|
253 | 240 | return render('summary/summary.html') |
|
254 | 241 | |
|
255 | 242 | @LoginRequired() |
|
256 | 243 | @NotAnonymous() |
|
257 | 244 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
258 | 245 | 'repository.admin') |
|
259 | 246 | @jsonify |
|
260 | 247 | def repo_size(self, repo_name): |
|
261 | 248 | if request.is_xhr: |
|
262 | 249 | return c.rhodecode_db_repo._repo_size() |
|
263 | 250 | else: |
|
264 | 251 | raise HTTPBadRequest() |
General Comments 0
You need to be logged in to leave comments.
Login now