1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import optparse
import os
import re
import shlex
import subprocess
import sys
from pylib import cmd_helper
from pylib import constants
def _PrintMessage(warnings, title, action, known_bugs_file):
if warnings:
print
print '*' * 80
print '%s warnings.' % title
print '%s %s' % (action, known_bugs_file)
print '-' * 80
for warning in warnings:
print warning
print '-' * 80
print
def _StripLineNumbers(current_warnings):
re_line = r':\[line.*?\]$'
return [re.sub(re_line, '', x) for x in current_warnings]
def _DiffKnownWarnings(current_warnings_set, known_bugs_file):
with open(known_bugs_file, 'r') as known_bugs:
known_bugs_set = set(known_bugs.read().splitlines())
new_warnings = current_warnings_set - known_bugs_set
_PrintMessage(sorted(new_warnings), 'New', 'Please fix, or perhaps add to',
known_bugs_file)
obsolete_warnings = known_bugs_set - current_warnings_set
_PrintMessage(sorted(obsolete_warnings), 'Obsolete', 'Please remove from',
known_bugs_file)
count = len(new_warnings) + len(obsolete_warnings)
if count:
print '*** %d FindBugs warning%s! ***' % (count, 's' * (count > 1))
if len(new_warnings):
print '*** %d: new ***' % len(new_warnings)
if len(obsolete_warnings):
print '*** %d: obsolete ***' % len(obsolete_warnings)
print
print 'Alternatively, rebaseline with --rebaseline command option'
print
else:
print 'No new FindBugs warnings.'
print
return count
def _Rebaseline(current_warnings_set, known_bugs_file):
with file(known_bugs_file, 'w') as known_bugs:
for warning in sorted(current_warnings_set):
print >> known_bugs, warning
return 0
def _GetChromeJars(release_version):
version = 'Debug'
if release_version:
version = 'Release'
path = os.path.join(constants.DIR_SOURCE_ROOT,
os.environ.get('CHROMIUM_OUT_DIR', 'out'),
version,
'lib.java')
cmd = 'find %s -name "*.jar"' % path
out = cmd_helper.GetCmdOutput(shlex.split(cmd))
out = [p for p in out.splitlines() if not p.endswith('.dex.jar')]
if not out:
print 'No classes found in %s' % path
return ' '.join(out)
def _Run(exclude, known_bugs, classes_to_analyze, auxiliary_classes,
rebaseline, release_version, findbug_args):
"""Run the FindBugs.
Args:
exclude: the exclude xml file, refer to FindBugs's -exclude command option.
known_bugs: the text file of known bugs. The bugs in it will not be
reported.
classes_to_analyze: the list of classes need to analyze, refer to FindBug's
-onlyAnalyze command line option.
auxiliary_classes: the classes help to analyze, refer to FindBug's
-auxclasspath command line option.
rebaseline: True if the known_bugs file needs rebaseline.
release_version: True if the release version needs check, otherwise check
debug version.
findbug_args: addtional command line options needs pass to Findbugs.
"""
chrome_src = constants.DIR_SOURCE_ROOT
sdk_root = constants.ANDROID_SDK_ROOT
sdk_version = constants.ANDROID_SDK_VERSION
system_classes = []
system_classes.append(os.path.join(sdk_root, 'platforms',
'android-%s' % sdk_version, 'android.jar'))
if auxiliary_classes:
for classes in auxiliary_classes:
system_classes.append(os.path.abspath(classes))
findbugs_javacmd = 'java'
findbugs_home = os.path.join(chrome_src, 'third_party', 'findbugs')
findbugs_jar = os.path.join(findbugs_home, 'lib', 'findbugs.jar')
findbugs_pathsep = ':'
findbugs_maxheap = '768'
cmd = '%s ' % findbugs_javacmd
cmd = '%s -classpath %s%s' % (cmd, findbugs_jar, findbugs_pathsep)
cmd = '%s -Xmx%sm ' % (cmd, findbugs_maxheap)
cmd = '%s -Dfindbugs.home="%s" ' % (cmd, findbugs_home)
cmd = '%s -jar %s ' % (cmd, findbugs_jar)
cmd = '%s -textui -sortByClass ' % cmd
cmd = '%s -pluginList %s' % (cmd, os.path.join(chrome_src, 'tools', 'android',
'findbugs_plugin', 'lib',
'chromiumPlugin.jar'))
if len(system_classes):
cmd = '%s -auxclasspath %s ' % (cmd, ':'.join(system_classes))
if classes_to_analyze:
cmd = '%s -onlyAnalyze %s ' % (cmd, classes_to_analyze)
if exclude:
cmd = '%s -exclude %s ' % (cmd, os.path.abspath(exclude))
if findbug_args:
cmd = '%s %s ' % (cmd, findbug_args)
chrome_classes = _GetChromeJars(release_version)
if not chrome_classes:
return 1
cmd = '%s %s ' % (cmd, chrome_classes)
print
print '*' * 80
print 'Command used to run findbugs:'
print cmd
print '*' * 80
print
proc = subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _err = proc.communicate()
current_warnings_set = set(_StripLineNumbers(filter(None, out.splitlines())))
if rebaseline:
return _Rebaseline(current_warnings_set, known_bugs)
else:
return _DiffKnownWarnings(current_warnings_set, known_bugs)
def Run(options):
exclude_file = None
known_bugs_file = None
if options.exclude:
exclude_file = options.exclude
elif options.base_dir:
exclude_file = os.path.join(options.base_dir, 'findbugs_exclude.xml')
if options.known_bugs:
known_bugs_file = options.known_bugs
elif options.base_dir:
known_bugs_file = os.path.join(options.base_dir, 'findbugs_known_bugs.txt')
auxclasspath = None
if options.auxclasspath:
auxclasspath = options.auxclasspath.split(':')
return _Run(exclude_file, known_bugs_file, options.only_analyze, auxclasspath,
options.rebaseline, options.release_build, options.findbug_args)
def GetCommonParser():
parser = optparse.OptionParser()
parser.add_option('-r',
'--rebaseline',
action='store_true',
dest='rebaseline',
help='Rebaseline known findbugs issues.')
parser.add_option('-a',
'--auxclasspath',
action='store',
default=None,
dest='auxclasspath',
help='Set aux classpath for analysis.')
parser.add_option('-o',
'--only-analyze',
action='store',
default=None,
dest='only_analyze',
help='Only analyze the given classes and packages.')
parser.add_option('-e',
'--exclude',
action='store',
default=None,
dest='exclude',
help='Exclude bugs matching given filter.')
parser.add_option('-k',
'--known-bugs',
action='store',
default=None,
dest='known_bugs',
help='Not report the bugs in the given file.')
parser.add_option('-l',
'--release-build',
action='store_true',
dest='release_build',
help='Analyze release build instead of debug.')
parser.add_option('-f',
'--findbug-args',
action='store',
default=None,
dest='findbug_args',
help='Additional findbug arguments.')
parser.add_option('-b',
'--base-dir',
action='store',
default=None,
dest='base_dir',
help='Base directory for configuration file.')
return parser
def main():
parser = GetCommonParser()
options, _ = parser.parse_args()
return Run(options)
if __name__ == '__main__':
sys.exit(main())
|