Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: build/android/pylib/findbugs.py

Issue 11273026: The findbugs_diff and lib. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/python
bulach 2012/10/25 08:30:06 nit: env
michaelbai 2012/10/25 21:13:26 Done.
2 #
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import optparse
8 import os
9 import re
10 import shlex
11 import subprocess
12 import sys
13
bulach 2012/10/25 08:30:06 nit: extra \n
michaelbai 2012/10/25 21:13:26 Done.
14 def _PrintMessage(warnings, title, action, known_bugs_file):
15 if warnings:
16 print
17 print '*' * 80
18 print '%s warnings.' % title
19 print '%s %s' % (action, known_bugs_file)
20 print '-' * 80
21 for warning in warnings:
22 print warning
23 print '-' * 80
24 print
25
26
27 def _StripLineNumbers(current_warnings):
28 re_line = r':\[line.*?\]$'
29 return [re.sub(re_line, '', x) for x in current_warnings]
30
31
32 def _DiffKnownWarnings(current_warnings_set, known_bugs_file):
33 with open(known_bugs_file, 'r') as known_bugs:
34 known_bugs_set = set(known_bugs.read().splitlines())
35
36 new_warnings = current_warnings_set - known_bugs_set
37 _PrintMessage(sorted(new_warnings), 'New', 'Please fix, or perhaps add to',
38 known_bugs_file)
39
40 obsolete_warnings = known_bugs_set - current_warnings_set
41 _PrintMessage(sorted(obsolete_warnings), 'Obsolete', 'Please remove from',
42 known_bugs_file)
43
44 count = len(new_warnings) + len(obsolete_warnings)
45 if count:
46 print '*** %d FindBugs warning%s! ***' % (count, 's' * (count > 1))
47 if len(new_warnings):
48 print '*** %d: new ***' % len(new_warnings)
49 if len(obsolete_warnings):
50 print '*** %d: obsolete ***' % len(obsolete_warnings)
51 print
52 print 'Alternatively, rebaseline with --rebaseline command option'
53 print
54 else:
55 print 'No new FindBugs warnings.'
56 print
57 return count
58
59
60 def _Rebaseline(current_warnings_set, known_bugs_file):
61 with file(known_bugs_file, 'w') as known_bugs:
62 for warning in sorted(current_warnings_set):
63 print >>known_bugs, warning
64 return 0
65
66
67 def _GetChromeClasses(release_version):
68 chrome_src = os.getenv('CHROME_SRC')
69 version = 'Debug'
bulach 2012/10/25 08:30:06 nit: as above, this would probably be better as bu
michaelbai 2012/10/25 21:13:26 As comment's above, it might be better to keep the
70 if release_version:
71 version = 'Release'
72 path = os.path.join(chrome_src, 'out', version)
73 cmd = 'find %s -name "*.class"' % path
74 proc = subprocess.Popen(shlex.split(cmd),
75 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76 out, err = proc.communicate()
77
78 if not out:
79 print 'No classes found in %s' % path
80 return out
81
82
83 def Run(exclude, known_bugs, classes_to_analyze, auxiliary_classes,
84 rebaseline, release_version, findbug_args):
85 """Run the FindBugs.
86
87 Args:
88 exclude: the exclude xml file, refer to FindBugs's -exclude command option.
89 known_bugs: the text file of known bugs. The bugs in it will not be
90 reported.
91 classes_to_analyze: the list of classes need to analyze, refer to FindBug's
92 -onlyAnalyze command line option.
93 auxiliary_classes: the classes help to analyze, refer to FindBug's
94 -auxclasspath command line option.
95 rebaseline: True if the known_bugs file needs rebaseline.
96 release_version: True if the release version needs check, otherwise check
97 debug version.
98 findbug_args: addtional command line options needs pass to Findbugs.
99 """
100
101 chrome_src = os.getenv('CHROME_SRC')
102 sdk_root = os.getenv('ANDROID_SDK_ROOT')
103 sdk_version = os.getenv('ANDROID_SDK_VERSION')
104 if not (chrome_src and sdk_root and sdk_version):
105 print 'Your build environment is not set up correctly.'
106 print 'Please source build/android/envsetup.sh.'
107 return 1
108
109 system_classes = []
110 system_classes.append(os.path.join(sdk_root, 'platforms',
111 'android-%s' % sdk_version, 'android.jar'))
112 if auxiliary_classes:
113 for classes in auxiliary_classes:
114 system_classes.append(os.path.abspath(classes))
115
116 cmd = '%s -textui -sortByClass ' % os.path.join(chrome_src, 'clank',
117 'third_party', 'findbugs',
118 'bin', 'findbugs')
119 cmd = '%s -pluginList %s' % (cmd, os.path.join(chrome_src, 'tools', 'android',
120 'findbugs_plugin', 'lib',
121 'chromiumPlugin.jar'))
122 if len(system_classes):
123 cmd = '%s -auxclasspath %s ' % (cmd, ':'.join(system_classes))
124
125 if classes_to_analyze:
126 cmd = '%s -onlyAnalyze %s ' % (cmd, classes_to_analyze)
127
128 if exclude:
129 cmd = '%s -exclude %s ' % (cmd, os.path.abspath(exclude))
130
131 if findbug_args:
132 cmd = '%s %s ' % (cmd, fingbug_args)
133
134
135 chrome_classes = _GetChromeClasses(release_version)
136 if not chrome_classes:
137 return 1
138 cmd = '%s %s ' % (cmd, chrome_classes)
139
140 proc = subprocess.Popen(shlex.split(cmd),
141 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
142 out, err = proc.communicate()
143 current_warnings_set = set(_StripLineNumbers(filter(None, out.splitlines())))
144
145 if rebaseline:
146 return _Rebaseline(current_warnings_set, known_bugs)
147 else:
148 return _DiffKnownWarnings(current_warnings_set, known_bugs)
149
150
151 def main(argv):
152 parser = optparse.OptionParser()
153 parser.add_option('-r',
154 '--rebaseline',
155 action='store_true',
156 default=False,
bulach 2012/10/25 08:30:06 nit: same nits from above..
michaelbai 2012/10/25 21:13:26 Done.
157 dest='rebaseline',
158 help='Rebaseline known findbugs issues.')
159
160 parser.add_option('-a',
161 '--auxclasspath',
162 action='store',
163 default=None,
164 dest='auxclasspath',
165 help='Set aux classpath for analysis.')
166
167 parser.add_option('-o',
168 '--only-analyze',
169 action='store',
170 default=None,
171 dest='only_analyze',
172 help='Only analyze the given classes and packages.')
173
174 parser.add_option('-e',
175 '--exclude',
176 action='store',
177 default=None,
178 dest='exclude',
179 help='Exclude bugs matching given filter.')
180
181 parser.add_option('-k',
182 '--known-bugs',
183 action='store',
184 default=None,
185 dest='known_bugs',
186 help='No report the bugs in the given file.')
187
188 parser.add_option('-l',
189 '--release',
190 action='store_true',
191 default=False,
192 dest='release_version',
193 help='Whether check release version.')
194
195 parser.add_option('-f',
196 '--findbug-args',
197 action='store',
198 default=None,
199 dest='findbug_args',
200 help='Additoinal findbug arguments.')
201
202 options, _ = parser.parse_args()
203
204 return Run(options.exclude, options.known_bugs, options.only_analyze,
205 options.auxclasspath.split(':'), options.rebaseline,
206 options.findbug_args, options.release_version)
207
208
209 if __name__ == '__main__':
210 sys.exit(main(sys.argv))
OLDNEW
« build/android/findbugs_diff.py ('K') | « build/android/findbugs_filter/findbugs_known_bugs.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698