1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2007 Jeffrey Kyllo <jkyllo-eatlint@echospiral.com> |
---|
4 | # |
---|
5 | # Based on code from the Bitten project: |
---|
6 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
7 | # Copyright (C) 2007-2010 Edgewall Software |
---|
8 | # All rights reserved. |
---|
9 | # |
---|
10 | # This software is licensed as described in the file COPYING, which |
---|
11 | # you should have received as part of this distribution. The terms |
---|
12 | # are also available at http://echospiral.com/trac/eatlint/wiki/License. |
---|
13 | |
---|
14 | __docformat__ = 'restructuredtext en' |
---|
15 | |
---|
16 | from trac.core import * |
---|
17 | from trac.web.chrome import add_stylesheet, add_script |
---|
18 | from bitten.api import IReportChartGenerator, IReportSummarizer |
---|
19 | |
---|
20 | |
---|
21 | class PyLintChartGenerator(Component): |
---|
22 | implements(IReportChartGenerator) |
---|
23 | |
---|
24 | # IReportChartGenerator methods |
---|
25 | |
---|
26 | def get_supported_categories(self): |
---|
27 | return ['lint'] |
---|
28 | |
---|
29 | def generate_chart_data(self, req, config, category): |
---|
30 | assert category == 'lint' |
---|
31 | |
---|
32 | db = self.env.get_db_cnx() |
---|
33 | cursor = db.cursor() |
---|
34 | |
---|
35 | #self.log.debug('config.name=\'%s\'' % (config.name,)) |
---|
36 | query = """ |
---|
37 | select build.rev, |
---|
38 | (select count(*) from bitten_report_item as item |
---|
39 | where item.report = report.id and item.name='category' and item.value='convention'), |
---|
40 | (select count(*) from bitten_report_item as item |
---|
41 | where item.report = report.id and item.name='category' and item.value='error'), |
---|
42 | (select count(*) from bitten_report_item as item |
---|
43 | where item.report = report.id and item.name='category' and item.value='refactor'), |
---|
44 | (select count(*) from bitten_report_item as item |
---|
45 | where item.report = report.id and item.name='category' and item.value='warning') |
---|
46 | from bitten_report as report |
---|
47 | left outer join bitten_build as build ON (report.build=build.id) |
---|
48 | where build.config='%s' and report.category='lint' |
---|
49 | and build.rev_time >= %s and build.rev_time <= %s |
---|
50 | group by build.rev_time, build.rev, build.platform, report.id |
---|
51 | order by build.rev_time;""" % (config.name, |
---|
52 | config.min_rev_time(self.env), |
---|
53 | config.max_rev_time(self.env)) |
---|
54 | |
---|
55 | #self.log.debug('sql=\'%s\'' % (query,)) |
---|
56 | cursor.execute(query) |
---|
57 | |
---|
58 | lint = [] |
---|
59 | prev_rev = None |
---|
60 | prev_counts = None |
---|
61 | |
---|
62 | for rev, conv, err, ref, warn in cursor: |
---|
63 | total = conv + err + ref + warn |
---|
64 | curr_counts = [rev, total, conv, err, ref, warn] |
---|
65 | if rev != prev_rev: |
---|
66 | lint.append(curr_counts) |
---|
67 | else: |
---|
68 | # cunningly / dubiously set rev = max(rev, rev) along with the counts |
---|
69 | lint[-1] = [max(prev, curr) for prev, curr in zip(curr_counts, lint[-1])] |
---|
70 | # recalculate total |
---|
71 | lint[-1][1] = sum(lint[-1][2:]) |
---|
72 | prev_rev = rev |
---|
73 | |
---|
74 | data = {'title': 'Lint Problems by Type', |
---|
75 | 'data': [ |
---|
76 | {'label': 'Total Problems', 'data': [[item[0], item[1]] for item in lint], 'lines': {'fill': True}}, |
---|
77 | {'label': 'Convention', 'data': [[item[0], item[2]] for item in lint]}, |
---|
78 | {'label': 'Error', 'data': [[item[0], item[3]] for item in lint]}, |
---|
79 | {'label': 'Refactor', 'data': [[item[0], item[4]] for item in lint]}, |
---|
80 | {'label': 'Warning', 'data': [[item[0], item[5]] for item in lint]}, |
---|
81 | ], |
---|
82 | 'options': { |
---|
83 | 'legend': {'position': 'sw', 'backgroundOpacity': 0.7}, |
---|
84 | 'xaxis': {'tickDecimals': 0}, |
---|
85 | 'yaxis': {'tickDecimals': 0}, |
---|
86 | }, |
---|
87 | } |
---|
88 | |
---|
89 | return 'json.txt', {"json": data} |
---|
90 | |
---|
91 | |
---|
92 | class PyLintSummarizer(Component): |
---|
93 | implements(IReportSummarizer) |
---|
94 | |
---|
95 | # IReportSummarizer methods |
---|
96 | |
---|
97 | def get_supported_categories(self): |
---|
98 | return ['lint'] |
---|
99 | |
---|
100 | def render_summary(self, req, config, build, step, category): |
---|
101 | assert category == 'lint' |
---|
102 | add_stylesheet(req, 'bitten/bitten_lint.css') |
---|
103 | add_script(req, 'common/js/folding.js') |
---|
104 | add_script(req, 'bitten/bitten_lint.js') |
---|
105 | |
---|
106 | db = self.env.get_db_cnx() |
---|
107 | cursor = db.cursor() |
---|
108 | cursor.execute(""" |
---|
109 | SELECT item_type.value AS type, item_file.value AS file, |
---|
110 | item_line.value as line, item_category.value as category, |
---|
111 | item_msg.value as msg, |
---|
112 | report.category as report_category |
---|
113 | FROM bitten_report AS report |
---|
114 | LEFT OUTER JOIN bitten_report_item AS item_type |
---|
115 | ON (item_type.report=report.id AND item_type.name='type') |
---|
116 | LEFT OUTER JOIN bitten_report_item AS item_file |
---|
117 | ON (item_file.report=report.id AND |
---|
118 | item_file.item=item_type.item AND |
---|
119 | item_file.name='file') |
---|
120 | LEFT OUTER JOIN bitten_report_item AS item_line |
---|
121 | ON (item_line.report=report.id AND |
---|
122 | item_line.item=item_type.item AND |
---|
123 | item_line.name='line') |
---|
124 | LEFT OUTER JOIN bitten_report_item AS item_category |
---|
125 | ON (item_category.report=report.id AND |
---|
126 | item_category.item=item_type.item AND |
---|
127 | item_category.name='category') |
---|
128 | LEFT OUTER JOIN bitten_report_item AS item_msg |
---|
129 | ON (item_msg.report=report.id AND |
---|
130 | item_msg.item=item_type.item AND |
---|
131 | item_msg.name='msg') |
---|
132 | WHERE report.category='lint' AND build=%s AND step=%s |
---|
133 | ORDER BY item_file.value, item_type.value""", (build.id, step.name)) |
---|
134 | |
---|
135 | file_data = {} |
---|
136 | |
---|
137 | type_total = {} |
---|
138 | category_total = {'convention': 0, 'refactor': 0, 'warning': 0, 'error': 0} |
---|
139 | line_total = 0 |
---|
140 | file_total = 0 |
---|
141 | seen_files = {} |
---|
142 | |
---|
143 | for type, file, line, category, msg, report_category in cursor: |
---|
144 | if not file_data.has_key(file): |
---|
145 | file_data[file] = {'file': file, 'type': {}, 'lines': 0, 'category': {}, 'details': []} |
---|
146 | |
---|
147 | d = file_data[file] |
---|
148 | #d = {'type': type, 'line': line, 'category': category} |
---|
149 | if not d['type'].has_key(type): |
---|
150 | d['type'][type] = 0 |
---|
151 | d['type'][type] += 1 |
---|
152 | |
---|
153 | d['lines'] += 1 |
---|
154 | line_total += 1 |
---|
155 | |
---|
156 | if line is not None and line.isdigit(): |
---|
157 | line = int(line) |
---|
158 | |
---|
159 | d['details'].append(( |
---|
160 | line is None and '??' or line, |
---|
161 | type is None and '??' or type, |
---|
162 | msg is None and '-' or msg)) |
---|
163 | |
---|
164 | if not d['category'].has_key(category): |
---|
165 | d['category'][category] = 0 |
---|
166 | d['category'][category] += 1 |
---|
167 | |
---|
168 | if file: |
---|
169 | d['href'] = req.href.browser(config.path, file) |
---|
170 | |
---|
171 | if not type_total.has_key(type): |
---|
172 | type_total[type] = 0 |
---|
173 | type_total[type] += 1 |
---|
174 | |
---|
175 | if not category_total.has_key(category): |
---|
176 | category_total[category] = 0 |
---|
177 | category_total[category] += 1 |
---|
178 | |
---|
179 | if not seen_files.has_key(file): |
---|
180 | seen_files[file] = 0 |
---|
181 | file_total += 1 |
---|
182 | |
---|
183 | data = [] |
---|
184 | for d in file_data.values(): |
---|
185 | d['catnames'] = d['category'].keys() |
---|
186 | d['details'].sort() |
---|
187 | data.append(d) |
---|
188 | |
---|
189 | template_data = {} |
---|
190 | template_data['data'] = data |
---|
191 | template_data['totals'] = {'type': type_total, 'category': category_total, 'files': file_total, 'lines': line_total} |
---|
192 | |
---|
193 | return 'bitten_summary_lint.html', template_data |
---|