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 if not (chrome_src and sdk_root and sdk_version): |
| 106 print 'Your build environment is not set up correctly.' |
| 107 print 'Please source build/android/envsetup.sh.' |
| 108 return 1 |
| 109 |
| 110 system_classes = [] |
| 111 system_classes.append(os.path.join(sdk_root, 'platforms', |
| 112 'android-%s' % sdk_version, 'android.jar')) |
| 113 if auxiliary_classes: |
| 114 for classes in auxiliary_classes: |
| 115 system_classes.append(os.path.abspath(classes)) |
| 116 |
| 117 cmd = '%s -textui -sortByClass ' % os.path.join(chrome_src, 'third_party', |
| 118 'findbugs', '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 def Run(options): |
| 151 exclude_file = None |
| 152 known_bugs_file = None |
| 153 |
| 154 if options.exclude: |
| 155 exclude_file = options.exclude |
| 156 elif options.base_dir: |
| 157 exclude_file = os.path.join(options.base_dir, 'findbugs_exclude.xml') |
| 158 |
| 159 if options.known_bugs: |
| 160 known_bugs_file = options.known_bugs |
| 161 elif options.base_dir: |
| 162 known_bugs_file = os.path.join(options.base_dir, 'findbugs_known_bugs.txt') |
| 163 |
| 164 auxclasspath = None |
| 165 if options.auxclasspath: |
| 166 auxclasspath = options.auxclasspath.split(':') |
| 167 return _Run(exclude_file, known_bugs_file, options.only_analyze, auxclasspath, |
| 168 options.rebaseline, options.release_build, options.findbug_args) |
| 169 |
| 170 |
| 171 def GetCommonParser(): |
| 172 parser = optparse.OptionParser() |
| 173 parser.add_option('-r', |
| 174 '--rebaseline', |
| 175 action='store_true', |
| 176 dest='rebaseline', |
| 177 help='Rebaseline known findbugs issues.') |
| 178 |
| 179 parser.add_option('-a', |
| 180 '--auxclasspath', |
| 181 action='store', |
| 182 default=None, |
| 183 dest='auxclasspath', |
| 184 help='Set aux classpath for analysis.') |
| 185 |
| 186 parser.add_option('-o', |
| 187 '--only-analyze', |
| 188 action='store', |
| 189 default=None, |
| 190 dest='only_analyze', |
| 191 help='Only analyze the given classes and packages.') |
| 192 |
| 193 parser.add_option('-e', |
| 194 '--exclude', |
| 195 action='store', |
| 196 default=None, |
| 197 dest='exclude', |
| 198 help='Exclude bugs matching given filter.') |
| 199 |
| 200 parser.add_option('-k', |
| 201 '--known-bugs', |
| 202 action='store', |
| 203 default=None, |
| 204 dest='known_bugs', |
| 205 help='Not report the bugs in the given file.') |
| 206 |
| 207 parser.add_option('-l', |
| 208 '--release-build', |
| 209 action='store_true', |
| 210 dest='release_build', |
| 211 help='Analyze release build instead of debug.') |
| 212 |
| 213 parser.add_option('-f', |
| 214 '--findbug-args', |
| 215 action='store', |
| 216 default=None, |
| 217 dest='findbug_args', |
| 218 help='Additional findbug arguments.') |
| 219 |
| 220 parser.add_option('-b', |
| 221 '--base-dir', |
| 222 action='store', |
| 223 default=None, |
| 224 dest='base_dir', |
| 225 help='Base directory for configuration file.') |
| 226 |
| 227 return parser |
| 228 |
| 229 def main(argv): |
| 230 parser = GetCommonParser() |
| 231 options, _ = parser.parse_args() |
| 232 |
| 233 return Run(options) |
| 234 |
| 235 if __name__ == '__main__': |
| 236 sys.exit(main(sys.argv)) |
OLD | NEW |