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"""Runs findbugs, and returns an error code if there are new warnings. 8 9Other options 10 --only-analyze used to only analyze the class you are interested. 11 --relase-build analyze the classes in out/Release directory. 12 --findbugs-args used to passin other findbugs's options. 13 14Run 15 $CHROMIUM_SRC/third_party/findbugs/bin/findbugs -textui for details. 16 17""" 18 19import argparse 20import os 21import sys 22 23import devil_chromium 24from devil.utils import run_tests_helper 25 26from pylib.constants import host_paths 27from pylib.utils import findbugs 28 29_DEFAULT_BASE_DIR = os.path.join( 30 host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'findbugs_filter') 31 32sys.path.append( 33 os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'gyp')) 34from util import build_utils # pylint: disable=import-error 35 36 37def main(): 38 parser = argparse.ArgumentParser() 39 40 parser.add_argument( 41 '-v', '--verbose', action='count', help='Enable verbose logging.') 42 parser.add_argument( 43 '-a', '--auxclasspath', default=None, dest='auxclasspath', 44 help='Set aux classpath for analysis.') 45 parser.add_argument( 46 '--auxclasspath-gyp', dest='auxclasspath_gyp', 47 help='A gyp list containing the aux classpath for analysis') 48 parser.add_argument( 49 '-o', '--only-analyze', default=None, 50 dest='only_analyze', help='Only analyze the given classes and packages.') 51 parser.add_argument( 52 '-e', '--exclude', default=None, dest='exclude', 53 help='Exclude bugs matching given filter.') 54 parser.add_argument( 55 '-l', '--release-build', action='store_true', dest='release_build', 56 help='Analyze release build instead of debug.') 57 parser.add_argument( 58 '-f', '--findbug-args', default=None, dest='findbug_args', 59 help='Additional findbug arguments.') 60 parser.add_argument( 61 '-b', '--base-dir', default=_DEFAULT_BASE_DIR, 62 dest='base_dir', help='Base directory for configuration file.') 63 parser.add_argument( 64 '--output-file', dest='output_file', 65 help='Path to save the output to.') 66 parser.add_argument( 67 '--stamp', help='Path to touch on success.') 68 parser.add_argument( 69 '--depfile', help='Path to the depfile. This must be specified as the ' 70 "action's first output.") 71 72 parser.add_argument( 73 'jar_paths', metavar='JAR_PATH', nargs='+', 74 help='JAR file to analyze') 75 76 args = parser.parse_args(build_utils.ExpandFileArgs(sys.argv[1:])) 77 78 run_tests_helper.SetLogLevel(args.verbose) 79 80 devil_chromium.Initialize() 81 82 if args.auxclasspath: 83 args.auxclasspath = args.auxclasspath.split(':') 84 elif args.auxclasspath_gyp: 85 args.auxclasspath = build_utils.ParseGypList(args.auxclasspath_gyp) 86 87 if args.base_dir: 88 if not args.exclude: 89 args.exclude = os.path.join(args.base_dir, 'findbugs_exclude.xml') 90 91 findbugs_command, findbugs_warnings = findbugs.Run( 92 args.exclude, args.only_analyze, args.auxclasspath, 93 args.output_file, args.findbug_args, args.jar_paths) 94 95 if findbugs_warnings: 96 print 97 print '*' * 80 98 print 'FindBugs run via:' 99 print findbugs_command 100 print 101 print 'FindBugs reported the following issues:' 102 for warning in sorted(findbugs_warnings): 103 print str(warning) 104 print '*' * 80 105 print 106 else: 107 if args.depfile: 108 build_utils.WriteDepfile( 109 args.depfile, 110 build_utils.GetPythonDependencies() + args.auxclasspath 111 + args.jar_paths) 112 if args.stamp: 113 build_utils.Touch(args.stamp) 114 115 return len(findbugs_warnings) 116 117 118if __name__ == '__main__': 119 sys.exit(main()) 120 121