Show More
@@ -1,365 +1,365 | |||
|
1 | 1 | from celery.decorators import task |
|
2 | 2 | |
|
3 | 3 | import os |
|
4 | 4 | import traceback |
|
5 | 5 | from time import mktime |
|
6 | 6 | from operator import itemgetter |
|
7 | 7 | |
|
8 | 8 | from pylons import config |
|
9 | 9 | from pylons.i18n.translation import _ |
|
10 | 10 | |
|
11 | 11 | from rhodecode.lib.celerylib import run_task, locked_task, str2bool |
|
12 | 12 | from rhodecode.lib.helpers import person |
|
13 | 13 | from rhodecode.lib.smtp_mailer import SmtpMailer |
|
14 | 14 | from rhodecode.lib.utils import OrderedDict, add_cache |
|
15 | 15 | from rhodecode.model import init_model |
|
16 | 16 | from rhodecode.model import meta |
|
17 | 17 | from rhodecode.model.db import RhodeCodeUi |
|
18 | 18 | |
|
19 | 19 | from vcs.backends import get_repo |
|
20 | 20 | |
|
21 | 21 | from sqlalchemy import engine_from_config |
|
22 | 22 | |
|
23 | 23 | add_cache(config) |
|
24 | 24 | |
|
25 | 25 | try: |
|
26 | 26 | import json |
|
27 | 27 | except ImportError: |
|
28 | 28 | #python 2.5 compatibility |
|
29 | 29 | import simplejson as json |
|
30 | 30 | |
|
31 | 31 | __all__ = ['whoosh_index', 'get_commits_stats', |
|
32 | 32 | 'reset_user_password', 'send_email'] |
|
33 | 33 | |
|
34 | 34 | CELERY_ON = str2bool(config['app_conf'].get('use_celery')) |
|
35 | 35 | |
|
36 | 36 | def get_session(): |
|
37 | 37 | if CELERY_ON: |
|
38 | 38 | engine = engine_from_config(config, 'sqlalchemy.db1.') |
|
39 | 39 | init_model(engine) |
|
40 | 40 | sa = meta.Session() |
|
41 | 41 | return sa |
|
42 | 42 | |
|
43 | 43 | def get_repos_path(): |
|
44 | 44 | sa = get_session() |
|
45 | 45 | q = sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one() |
|
46 | 46 | return q.ui_value |
|
47 | 47 | |
|
48 | 48 | @task |
|
49 | 49 | @locked_task |
|
50 | 50 | def whoosh_index(repo_location, full_index): |
|
51 | 51 | log = whoosh_index.get_logger() |
|
52 | 52 | from rhodecode.lib.indexers.daemon import WhooshIndexingDaemon |
|
53 | 53 | index_location = config['index_dir'] |
|
54 | 54 | WhooshIndexingDaemon(index_location=index_location, |
|
55 | 55 | repo_location=repo_location, sa=get_session())\ |
|
56 | 56 | .run(full_index=full_index) |
|
57 | 57 | |
|
58 | 58 | @task |
|
59 | 59 | @locked_task |
|
60 | 60 | def get_commits_stats(repo_name, ts_min_y, ts_max_y): |
|
61 | 61 | from rhodecode.model.db import Statistics, Repository |
|
62 | 62 | log = get_commits_stats.get_logger() |
|
63 | 63 | |
|
64 | 64 | #for js data compatibilty |
|
65 | 65 | author_key_cleaner = lambda k: person(k).replace('"', "") |
|
66 | 66 | |
|
67 | 67 | commits_by_day_author_aggregate = {} |
|
68 | 68 | commits_by_day_aggregate = {} |
|
69 | 69 | repos_path = get_repos_path() |
|
70 | 70 | p = os.path.join(repos_path, repo_name) |
|
71 | 71 | repo = get_repo(p) |
|
72 | 72 | |
|
73 | 73 | skip_date_limit = True |
|
74 | 74 | parse_limit = 250 #limit for single task changeset parsing optimal for |
|
75 | 75 | last_rev = 0 |
|
76 | 76 | last_cs = None |
|
77 | 77 | timegetter = itemgetter('time') |
|
78 | 78 | |
|
79 | 79 | sa = get_session() |
|
80 | 80 | |
|
81 | 81 | dbrepo = sa.query(Repository)\ |
|
82 | 82 | .filter(Repository.repo_name == repo_name).scalar() |
|
83 | 83 | cur_stats = sa.query(Statistics)\ |
|
84 | 84 | .filter(Statistics.repository == dbrepo).scalar() |
|
85 | 85 | if cur_stats: |
|
86 | 86 | last_rev = cur_stats.stat_on_revision |
|
87 | 87 | if not repo.revisions: |
|
88 | 88 | return True |
|
89 | 89 | |
|
90 | 90 | if last_rev == repo.revisions[-1] and len(repo.revisions) > 1: |
|
91 | 91 | #pass silently without any work if we're not on first revision or |
|
92 | 92 | #current state of parsing revision(from db marker) is the last revision |
|
93 | 93 | return True |
|
94 | 94 | |
|
95 | 95 | if cur_stats: |
|
96 | 96 | commits_by_day_aggregate = OrderedDict( |
|
97 | 97 | json.loads( |
|
98 | 98 | cur_stats.commit_activity_combined)) |
|
99 | 99 | commits_by_day_author_aggregate = json.loads(cur_stats.commit_activity) |
|
100 | 100 | |
|
101 | 101 | log.debug('starting parsing %s', parse_limit) |
|
102 | 102 | lmktime = mktime |
|
103 | 103 | |
|
104 | 104 | for cnt, rev in enumerate(repo.revisions[last_rev:]): |
|
105 | 105 | last_cs = cs = repo.get_changeset(rev) |
|
106 | 106 | k = '%s-%s-%s' % (cs.date.timetuple()[0], cs.date.timetuple()[1], |
|
107 | 107 | cs.date.timetuple()[2]) |
|
108 | 108 | timetupple = [int(x) for x in k.split('-')] |
|
109 | 109 | timetupple.extend([0 for _ in xrange(6)]) |
|
110 | 110 | k = lmktime(timetupple) |
|
111 | 111 | if commits_by_day_author_aggregate.has_key(author_key_cleaner(cs.author)): |
|
112 | 112 | try: |
|
113 | 113 | l = [timegetter(x) for x in commits_by_day_author_aggregate\ |
|
114 | 114 | [author_key_cleaner(cs.author)]['data']] |
|
115 | 115 | time_pos = l.index(k) |
|
116 | 116 | except ValueError: |
|
117 | 117 | time_pos = False |
|
118 | 118 | |
|
119 | 119 | if time_pos >= 0 and time_pos is not False: |
|
120 | 120 | |
|
121 | 121 | datadict = commits_by_day_author_aggregate\ |
|
122 | 122 | [author_key_cleaner(cs.author)]['data'][time_pos] |
|
123 | 123 | |
|
124 | 124 | datadict["commits"] += 1 |
|
125 | 125 | datadict["added"] += len(cs.added) |
|
126 | 126 | datadict["changed"] += len(cs.changed) |
|
127 | 127 | datadict["removed"] += len(cs.removed) |
|
128 | 128 | |
|
129 | 129 | else: |
|
130 | 130 | if k >= ts_min_y and k <= ts_max_y or skip_date_limit: |
|
131 | 131 | |
|
132 | 132 | datadict = {"time":k, |
|
133 | 133 | "commits":1, |
|
134 | 134 | "added":len(cs.added), |
|
135 | 135 | "changed":len(cs.changed), |
|
136 | 136 | "removed":len(cs.removed), |
|
137 | 137 | } |
|
138 | 138 | commits_by_day_author_aggregate\ |
|
139 | 139 | [author_key_cleaner(cs.author)]['data'].append(datadict) |
|
140 | 140 | |
|
141 | 141 | else: |
|
142 | 142 | if k >= ts_min_y and k <= ts_max_y or skip_date_limit: |
|
143 | 143 | commits_by_day_author_aggregate[author_key_cleaner(cs.author)] = { |
|
144 | 144 | "label":author_key_cleaner(cs.author), |
|
145 | 145 | "data":[{"time":k, |
|
146 | 146 | "commits":1, |
|
147 | 147 | "added":len(cs.added), |
|
148 | 148 | "changed":len(cs.changed), |
|
149 | 149 | "removed":len(cs.removed), |
|
150 | 150 | }], |
|
151 | 151 | "schema":["commits"], |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | #gather all data by day |
|
155 | 155 | if commits_by_day_aggregate.has_key(k): |
|
156 | 156 | commits_by_day_aggregate[k] += 1 |
|
157 | 157 | else: |
|
158 | 158 | commits_by_day_aggregate[k] = 1 |
|
159 | 159 | |
|
160 | 160 | if cnt >= parse_limit: |
|
161 | 161 | #don't fetch to much data since we can freeze application |
|
162 | 162 | break |
|
163 | 163 | overview_data = [] |
|
164 | 164 | for k, v in commits_by_day_aggregate.items(): |
|
165 | 165 | overview_data.append([k, v]) |
|
166 | 166 | overview_data = sorted(overview_data, key=itemgetter(0)) |
|
167 | 167 | if not commits_by_day_author_aggregate: |
|
168 | 168 | commits_by_day_author_aggregate[author_key_cleaner(repo.contact)] = { |
|
169 | 169 | "label":author_key_cleaner(repo.contact), |
|
170 | 170 | "data":[0, 1], |
|
171 | 171 | "schema":["commits"], |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | stats = cur_stats if cur_stats else Statistics() |
|
175 | 175 | stats.commit_activity = json.dumps(commits_by_day_author_aggregate) |
|
176 | 176 | stats.commit_activity_combined = json.dumps(overview_data) |
|
177 | 177 | |
|
178 | 178 | log.debug('last revison %s', last_rev) |
|
179 | 179 | leftovers = len(repo.revisions[last_rev:]) |
|
180 | 180 | log.debug('revisions to parse %s', leftovers) |
|
181 | 181 | |
|
182 | 182 | if last_rev == 0 or leftovers < parse_limit: |
|
183 | 183 | stats.languages = json.dumps(__get_codes_stats(repo_name)) |
|
184 | 184 | |
|
185 | 185 | stats.repository = dbrepo |
|
186 | 186 | stats.stat_on_revision = last_cs.revision |
|
187 | 187 | |
|
188 | 188 | try: |
|
189 | 189 | sa.add(stats) |
|
190 | 190 | sa.commit() |
|
191 | 191 | except: |
|
192 | 192 | log.error(traceback.format_exc()) |
|
193 | 193 | sa.rollback() |
|
194 | 194 | return False |
|
195 | 195 | if len(repo.revisions) > 1: |
|
196 | 196 | run_task(get_commits_stats, repo_name, ts_min_y, ts_max_y) |
|
197 | 197 | |
|
198 | 198 | return True |
|
199 | 199 | |
|
200 | 200 | @task |
|
201 | 201 | def reset_user_password(user_email): |
|
202 | 202 | log = reset_user_password.get_logger() |
|
203 | 203 | from rhodecode.lib import auth |
|
204 | 204 | from rhodecode.model.db import User |
|
205 | 205 | |
|
206 | 206 | try: |
|
207 | 207 | try: |
|
208 | 208 | sa = get_session() |
|
209 | 209 | user = sa.query(User).filter(User.email == user_email).scalar() |
|
210 | 210 | new_passwd = auth.PasswordGenerator().gen_password(8, |
|
211 | 211 | auth.PasswordGenerator.ALPHABETS_BIG_SMALL) |
|
212 | 212 | if user: |
|
213 | 213 | user.password = auth.get_crypt_password(new_passwd) |
|
214 | 214 | sa.add(user) |
|
215 | 215 | sa.commit() |
|
216 | 216 | log.info('change password for %s', user_email) |
|
217 | 217 | if new_passwd is None: |
|
218 | 218 | raise Exception('unable to generate new password') |
|
219 | 219 | |
|
220 | 220 | except: |
|
221 | 221 | log.error(traceback.format_exc()) |
|
222 | 222 | sa.rollback() |
|
223 | 223 | |
|
224 | 224 | run_task(send_email, user_email, |
|
225 | 225 | "Your new rhodecode password", |
|
226 | 226 | 'Your new rhodecode password:%s' % (new_passwd)) |
|
227 | 227 | log.info('send new password mail to %s', user_email) |
|
228 | 228 | |
|
229 | 229 | |
|
230 | 230 | except: |
|
231 | 231 | log.error('Failed to update user password') |
|
232 | 232 | log.error(traceback.format_exc()) |
|
233 | 233 | |
|
234 | 234 | return True |
|
235 | 235 | |
|
236 | 236 | @task |
|
237 | 237 | def send_email(recipients, subject, body): |
|
238 | 238 | """ |
|
239 | 239 | Sends an email with defined parameters from the .ini files. |
|
240 | 240 | |
|
241 | 241 | |
|
242 | 242 | :param recipients: list of recipients, it this is empty the defined email |
|
243 | 243 | address from field 'email_to' is used instead |
|
244 | 244 | :param subject: subject of the mail |
|
245 | 245 | :param body: body of the mail |
|
246 | 246 | """ |
|
247 | 247 | log = send_email.get_logger() |
|
248 | 248 | email_config = config |
|
249 | 249 | |
|
250 | 250 | if not recipients: |
|
251 | 251 | recipients = [email_config.get('email_to')] |
|
252 | 252 | |
|
253 | 253 | mail_from = email_config.get('app_email_from') |
|
254 | 254 | user = email_config.get('smtp_username') |
|
255 | 255 | passwd = email_config.get('smtp_password') |
|
256 | 256 | mail_server = email_config.get('smtp_server') |
|
257 | 257 | mail_port = email_config.get('smtp_port') |
|
258 | 258 | tls = str2bool(email_config.get('smtp_use_tls')) |
|
259 | 259 | ssl = str2bool(email_config.get('smtp_use_ssl')) |
|
260 | 260 | |
|
261 | 261 | try: |
|
262 | 262 | m = SmtpMailer(mail_from, user, passwd, mail_server, |
|
263 | 263 | mail_port, ssl, tls) |
|
264 | 264 | m.send(recipients, subject, body) |
|
265 | 265 | except: |
|
266 | 266 | log.error('Mail sending failed') |
|
267 | 267 | log.error(traceback.format_exc()) |
|
268 | 268 | return False |
|
269 | 269 | return True |
|
270 | 270 | |
|
271 | 271 | @task |
|
272 | 272 | def create_repo_fork(form_data, cur_user): |
|
273 | 273 | from rhodecode.model.repo import RepoModel |
|
274 | 274 | from vcs import get_backend |
|
275 | 275 | log = create_repo_fork.get_logger() |
|
276 | 276 | repo_model = RepoModel(get_session()) |
|
277 | 277 | repo_model.create(form_data, cur_user, just_db=True, fork=True) |
|
278 | 278 | repo_name = form_data['repo_name'] |
|
279 | 279 | repos_path = get_repos_path() |
|
280 | 280 | repo_path = os.path.join(repos_path, repo_name) |
|
281 | 281 | repo_fork_path = os.path.join(repos_path, form_data['fork_name']) |
|
282 | 282 | alias = form_data['repo_type'] |
|
283 | 283 | |
|
284 | 284 | log.info('creating repo fork %s as %s', repo_name, repo_path) |
|
285 | 285 | backend = get_backend(alias) |
|
286 | 286 | backend(str(repo_fork_path), create=True, src_url=str(repo_path)) |
|
287 | 287 | |
|
288 | 288 | def __get_codes_stats(repo_name): |
|
289 | 289 | LANGUAGES_EXTENSIONS_MAP = {'scm': 'Scheme', 'asmx': 'VbNetAspx', 'Rout': |
|
290 | 290 | 'RConsole', 'rest': 'Rst', 'abap': 'ABAP', 'go': 'Go', 'phtml': 'HtmlPhp', |
|
291 | 291 | 'ns2': 'Newspeak', 'xml': 'EvoqueXml', 'sh-session': 'BashSession', 'ads': |
|
292 | 292 | 'Ada', 'clj': 'Clojure', 'll': 'Llvm', 'ebuild': 'Bash', 'adb': 'Ada', |
|
293 | 293 | 'ada': 'Ada', 'c++-objdump': 'CppObjdump', 'aspx': |
|
294 | 294 | 'VbNetAspx', 'ksh': 'Bash', 'coffee': 'CoffeeScript', 'vert': 'GLShader', |
|
295 | 295 | 'Makefile.*': 'Makefile', 'di': 'D', 'dpatch': 'DarcsPatch', 'rake': |
|
296 | 296 | 'Ruby', 'moo': 'MOOCode', 'erl-sh': 'ErlangShell', 'geo': 'GLShader', |
|
297 | 297 | 'pov': 'Povray', 'bas': 'VbNet', 'bat': 'Batch', 'd': 'D', 'lisp': |
|
298 | 298 | 'CommonLisp', 'h': 'C', 'rbx': 'Ruby', 'tcl': 'Tcl', 'c++': 'Cpp', 'md': |
|
299 | 299 | 'MiniD', '.vimrc': 'Vim', 'xsd': 'Xml', 'ml': 'Ocaml', 'el': 'CommonLisp', |
|
300 | 300 | 'befunge': 'Befunge', 'xsl': 'Xslt', 'pyx': 'Cython', 'cfm': |
|
301 | 301 | 'ColdfusionHtml', 'evoque': 'Evoque', 'cfg': 'Ini', 'htm': 'Html', |
|
302 | 302 | 'Makefile': 'Makefile', 'cfc': 'ColdfusionHtml', 'tex': 'Tex', 'cs': |
|
303 | 303 | 'CSharp', 'mxml': 'Mxml', 'patch': 'Diff', 'apache.conf': 'ApacheConf', |
|
304 | 304 | 'scala': 'Scala', 'applescript': 'AppleScript', 'GNUmakefile': 'Makefile', |
|
305 | 305 | 'c-objdump': 'CObjdump', 'lua': 'Lua', 'apache2.conf': 'ApacheConf', 'rb': |
|
306 | 306 | 'Ruby', 'gemspec': 'Ruby', 'rl': 'RagelObjectiveC', 'vala': 'Vala', 'tmpl': |
|
307 | 307 | 'Cheetah', 'bf': 'Brainfuck', 'plt': 'Gnuplot', 'G': 'AntlrRuby', 'xslt': |
|
308 | 308 | 'Xslt', 'flxh': 'Felix', 'asax': 'VbNetAspx', 'Rakefile': 'Ruby', 'S': 'S', |
|
309 | 309 | 'wsdl': 'Xml', 'js': 'Javascript', 'autodelegate': 'Myghty', 'properties': |
|
310 | 310 | 'Ini', 'bash': 'Bash', 'c': 'C', 'g': 'AntlrRuby', 'r3': 'Rebol', 's': |
|
311 | 311 | 'Gas', 'ashx': 'VbNetAspx', 'cxx': 'Cpp', 'boo': 'Boo', 'prolog': 'Prolog', |
|
312 | 312 | 'sqlite3-console': 'SqliteConsole', 'cl': 'CommonLisp', 'cc': 'Cpp', 'pot': |
|
313 | 313 | 'Gettext', 'vim': 'Vim', 'pxi': 'Cython', 'yaml': 'Yaml', 'SConstruct': |
|
314 | 314 | 'Python', 'diff': 'Diff', 'txt': 'Text', 'cw': 'Redcode', 'pxd': 'Cython', |
|
315 | 315 | 'plot': 'Gnuplot', 'java': 'Java', 'hrl': 'Erlang', 'py': 'Python', |
|
316 | 316 | 'makefile': 'Makefile', 'squid.conf': 'SquidConf', 'asm': 'Nasm', 'toc': |
|
317 | 317 | 'Tex', 'kid': 'Genshi', 'rhtml': 'Rhtml', 'po': 'Gettext', 'pl': 'Prolog', |
|
318 | 318 | 'pm': 'Perl', 'hx': 'Haxe', 'ascx': 'VbNetAspx', 'ooc': 'Ooc', 'asy': |
|
319 | 319 | 'Asymptote', 'hs': 'Haskell', 'SConscript': 'Python', 'pytb': |
|
320 | 320 | 'PythonTraceback', 'myt': 'Myghty', 'hh': 'Cpp', 'R': 'S', 'aux': 'Tex', |
|
321 | 321 | 'rst': 'Rst', 'cpp-objdump': 'CppObjdump', 'lgt': 'Logtalk', 'rss': 'Xml', |
|
322 | 322 | 'flx': 'Felix', 'b': 'Brainfuck', 'f': 'Fortran', 'rbw': 'Ruby', |
|
323 | 323 | '.htaccess': 'ApacheConf', 'cxx-objdump': 'CppObjdump', 'j': 'ObjectiveJ', |
|
324 | 324 | 'mll': 'Ocaml', 'yml': 'Yaml', 'mu': 'MuPAD', 'r': 'Rebol', 'ASM': 'Nasm', |
|
325 | 325 | 'erl': 'Erlang', 'mly': 'Ocaml', 'mo': 'Modelica', 'def': 'Modula2', 'ini': |
|
326 | 326 | 'Ini', 'control': 'DebianControl', 'vb': 'VbNet', 'vapi': 'Vala', 'pro': |
|
327 | 327 | 'Prolog', 'spt': 'Cheetah', 'mli': 'Ocaml', 'as': 'ActionScript3', 'cmd': |
|
328 | 328 | 'Batch', 'cpp': 'Cpp', 'io': 'Io', 'tac': 'Python', 'haml': 'Haml', 'rkt': |
|
329 | 329 | 'Racket', 'st':'Smalltalk', 'inc': 'Povray', 'pas': 'Delphi', 'cmake': |
|
330 | 330 | 'CMake', 'csh':'Tcsh', 'hpp': 'Cpp', 'feature': 'Gherkin', 'html': 'Html', |
|
331 | 331 | 'php':'Php', 'php3':'Php', 'php4':'Php', 'php5':'Php', 'xhtml': 'Html', |
|
332 | 332 | 'hxx': 'Cpp', 'eclass': 'Bash', 'css': 'Css', |
|
333 | 333 | 'frag': 'GLShader', 'd-objdump': 'DObjdump', 'weechatlog': 'IrcLogs', |
|
334 | 334 | 'tcsh': 'Tcsh', 'objdump': 'Objdump', 'pyw': 'Python', 'h++': 'Cpp', |
|
335 | 335 | 'py3tb': 'Python3Traceback', 'jsp': 'Jsp', 'sql': 'Sql', 'mak': 'Makefile', |
|
336 | 336 | 'php': 'Php', 'mao': 'Mako', 'man': 'Groff', 'dylan': 'Dylan', 'sass': |
|
337 | 337 | 'Sass', 'cfml': 'ColdfusionHtml', 'darcspatch': 'DarcsPatch', 'tpl': |
|
338 | 338 | 'Smarty', 'm': 'ObjectiveC', 'f90': 'Fortran', 'mod': 'Modula2', 'sh': |
|
339 | 339 | 'Bash', 'lhs': 'LiterateHaskell', 'sources.list': 'SourcesList', 'axd': |
|
340 | 340 | 'VbNetAspx', 'sc': 'Python'} |
|
341 | 341 | |
|
342 | 342 | repos_path = get_repos_path() |
|
343 | 343 | p = os.path.join(repos_path, repo_name) |
|
344 | 344 | repo = get_repo(p) |
|
345 | 345 | tip = repo.get_changeset() |
|
346 | 346 | code_stats = {} |
|
347 | 347 | |
|
348 | 348 | def aggregate(cs): |
|
349 | 349 | for f in cs[2]: |
|
350 | 350 | ext = f.extension |
|
351 | 351 | key = LANGUAGES_EXTENSIONS_MAP.get(ext, ext) |
|
352 | 352 | key = key or ext |
|
353 | if ext in LANGUAGES_EXTENSIONS_MAP.keys(): | |
|
353 | if ext in LANGUAGES_EXTENSIONS_MAP.keys() and not f.is_binary: | |
|
354 | 354 | if code_stats.has_key(key): |
|
355 | 355 | code_stats[key] += 1 |
|
356 | 356 | else: |
|
357 | 357 | code_stats[key] = 1 |
|
358 | 358 | |
|
359 | 359 | map(aggregate, tip.walk('/')) |
|
360 | 360 | |
|
361 | 361 | return code_stats or {} |
|
362 | 362 | |
|
363 | 363 | |
|
364 | 364 | |
|
365 | 365 |
General Comments 0
You need to be logged in to leave comments.
Login now