forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_spotbugs.py
175 lines (163 loc) · 7.74 KB
/
mx_spotbugs.py
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
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
#
from __future__ import print_function
import mx
import os
import tempfile
import zipfile
import shutil
from os.path import join, exists
def defaultFindbugsArgs():
args = ['-textui', '-low', '-maxRank', '15']
if mx.is_interactive():
args.append('-progress')
return args
def _get_spotbugs_attribute(p, suffix, default=None):
spotbugs_attribute_value = None
attributes = [s + suffix for s in ('spotbugs', 'findbugs')]
found = False
for spotbugs_attribute_name in attributes:
if hasattr(p, spotbugs_attribute_name):
if spotbugs_attribute_value is not None:
mx.abort('Spotbugs attribute "{}" is redundant. Please use attribute "{}" instead'.format(spotbugs_attribute_name, attributes[0]), context=p)
spotbugs_attribute_value = getattr(p, spotbugs_attribute_name)
found = True
return spotbugs_attribute_value if found else default
def _should_test_project(p):
spotbugs_attribute_value = _get_spotbugs_attribute(p, '')
if spotbugs_attribute_value is not None:
if p.isJavaProject():
return spotbugs_attribute_value.lower() == 'true' or spotbugs_attribute_value is True
else:
return spotbugs_attribute_value.lower() == 'always'
if not p.isJavaProject():
return False
if p.is_test_project():
return False
if p.javaCompliance >= '9':
# We no longer use p.suite.getMxCompatibility().filterFindbugsProjectsByJavaCompliance()
# as we don't want projects with Java compliance greater than 8 to ever prevent FindBugs
# being run on projects with compliance less than or equal to 8.
return False
return True
def spotbugs(args, fbArgs=None, suite=None, projects=None, jarFileName='spotbugs.jar'):
projectsToTest = [p for p in mx.projects() if _should_test_project(p)]
projectsByVersion = {}
for p in projectsToTest:
compat = p.suite.getMxCompatibility()
spotbugsVersion = compat.spotbugs_version()
if spotbugsVersion not in projectsByVersion:
projectsByVersion[spotbugsVersion] = []
projectsByVersion[spotbugsVersion].append(p)
resultcode = 0
for spotbugsVersion, versionProjects in projectsByVersion.items():
mx.logv('Running spotbugs version {} on projects {}'.format(spotbugsVersion, versionProjects))
resultcode = max(resultcode, _spotbugs(args, fbArgs, suite, versionProjects, spotbugsVersion))
return resultcode
def _spotbugs(args, fbArgs, suite, projectsToTest, spotbugsVersion):
"""run FindBugs against non-test Java projects"""
findBugsHome = mx.get_env('SPOTBUGS_HOME', mx.get_env('FINDBUGS_HOME', None))
if spotbugsVersion == '3.0.0':
jarFileName = 'findbugs.jar'
else:
jarFileName = 'spotbugs.jar'
if suite is None:
suite = mx.primary_suite()
if findBugsHome:
spotbugsJar = join(findBugsHome, 'lib', jarFileName)
else:
spotbugsLib = join(mx._mx_suite.get_output_root(), 'spotbugs-' + spotbugsVersion)
if not exists(spotbugsLib):
tmp = tempfile.mkdtemp(prefix='spotbugs-download-tmp', dir=mx._mx_suite.dir)
try:
spotbugsDist = mx.library('SPOTBUGS_' + spotbugsVersion).get_path(resolve=True)
with zipfile.ZipFile(spotbugsDist) as zf:
candidates = [e for e in zf.namelist() if e.endswith('/lib/' + jarFileName)]
assert len(candidates) == 1, candidates
libDirInZip = os.path.dirname(candidates[0])
zf.extractall(tmp)
shutil.copytree(join(tmp, libDirInZip), spotbugsLib)
finally:
shutil.rmtree(tmp)
spotbugsJar = join(spotbugsLib, jarFileName)
assert exists(spotbugsJar)
if not projectsToTest:
return 0
ignoredClasses = set()
for p in projectsToTest:
ignore = _get_spotbugs_attribute(p, 'IgnoresGenerated', False)
if not isinstance(ignore, bool):
mx.abort('Value of attribute "spotbugsIgnoresGenerated" must be True or False', context=p)
if ignore is True:
sourceDir = p.source_gen_dir()
for root, _, files in os.walk(sourceDir):
for name in files:
if name.endswith('.java') and '-info' not in name:
pkg = root[len(sourceDir) + 1:].replace(os.sep, '.')
cls = pkg + '.' + name[:-len('.java')]
ignoredClasses.add(cls)
with tempfile.NamedTemporaryFile(suffix='.xml', prefix='spotbugs_exclude_filter.', mode='w', delete=False) as fp:
spotbugsExcludeFilterFile = fp.name
xmlDoc = mx.XMLDoc()
xmlDoc.open('FindBugsFilter')
if spotbugsVersion == '3.0.0':
# Javac from JDK11 might have been used to compile the classes so
# disable the check for redundant null tests since FindBugs does
# not (yet) detect and suppress warnings about patterns generated by
# this version of javac. This will be removed once
# https://github.com/spotbugs/spotbugs/issues/600 is resolved.
xmlDoc.open('Match')
xmlDoc.element('Bug', attributes={'pattern' : 'RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE'})
xmlDoc.close('Match')
for cls in ignoredClasses:
xmlDoc.open('Match')
xmlDoc.element('Class', attributes={'name' : '~' + cls + '.*'})
xmlDoc.close('Match')
xmlDoc.close('FindBugsFilter')
xml = xmlDoc.xml(indent=' ', newl='\n')
print(xml, file=fp)
outputDirs = [mx._cygpathU2W(p.output_dir()) for p in projectsToTest]
javaCompliance = max([p.javaCompliance for p in projectsToTest])
jdk = mx.get_jdk(javaCompliance)
if jdk.javaCompliance >= '9':
mx.log('FindBugs does not yet support JDK9 - skipping')
return 0
spotbugsResults = join(suite.dir, 'spotbugs.results')
if fbArgs is None:
fbArgs = defaultFindbugsArgs()
cmd = ['-jar', mx._cygpathU2W(spotbugsJar)] + fbArgs
cmd = cmd + ['-exclude', spotbugsExcludeFilterFile]
cmd = cmd + ['-auxclasspath', mx._separatedCygpathU2W(mx.classpath([p.name for p in projectsToTest], jdk=jdk)), '-output', mx._cygpathU2W(spotbugsResults), '-exitcode'] + args + outputDirs
try:
exitcode = mx.run_java(cmd, nonZeroIsFatal=False, jdk=jdk)
finally:
os.unlink(spotbugsExcludeFilterFile)
if exitcode != 0:
with open(spotbugsResults) as fp:
mx.log(fp.read())
os.unlink(spotbugsResults)
return exitcode