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