From dfa12b17f1ec6e1677024468d5abf5e9b58c68a8 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Wed, 5 Oct 2016 18:23:25 -0700 Subject: [PATCH] [infer] Apply python lint warnings on the Infer python code Summary: Apply python lint warnings on the Infer python code Reviewed By: sblackshear, jvillard Differential Revision: D3970514 fbshipit-source-id: 962f6ca --- infer/lib/python/infer.py | 22 +++++++++++++------ infer/lib/python/inferlib/analyze.py | 15 +++++-------- infer/lib/python/inferlib/bucklib.py | 17 +++++++------- infer/lib/python/inferlib/capture/ant.py | 2 +- infer/lib/python/inferlib/capture/buck.py | 3 ++- infer/lib/python/inferlib/capture/javac.py | 2 +- infer/lib/python/inferlib/capture/make.py | 1 + .../lib/python/inferlib/capture/ndk-build.py | 1 + infer/lib/python/inferlib/capture/util.py | 7 +++--- .../lib/python/inferlib/capture/xcodebuild.py | 3 ++- infer/lib/python/inferlib/config.py | 3 ++- infer/lib/python/inferlib/issues.py | 1 + infer/lib/python/inferlib/jwlib.py | 21 +++++++++--------- infer/lib/python/inferlib/source.py | 3 ++- infer/lib/python/inferlib/utils.py | 2 -- 15 files changed, 58 insertions(+), 45 deletions(-) diff --git a/infer/lib/python/infer.py b/infer/lib/python/infer.py index fcf90ddcd..fef67e93c 100755 --- a/infer/lib/python/infer.py +++ b/infer/lib/python/infer.py @@ -1,5 +1,12 @@ #!/usr/bin/env python2.7 +# Copyright (c) 2015 - present Facebook, Inc. +# All rights reserved. +# +# This source code is licensed under the BSD style license found in the +# LICENSE file in the root directory of this source tree. An additional grant +# of patent rights can be found in the PATENTS file in the same directory. + from __future__ import absolute_import from __future__ import division from __future__ import print_function @@ -7,7 +14,6 @@ from __future__ import unicode_literals import argparse import imp -import json import locale import logging import os @@ -40,9 +46,10 @@ MODULE_TO_COMMAND = { 'ndk-build': ['ndk-build'], } + def get_commands(): """Return all commands that are supported.""" - #flatten and dedup the list of commands + # flatten and dedup the list of commands return set(sum(MODULE_TO_COMMAND.values(), [])) @@ -110,7 +117,8 @@ def create_argparser(parents=[]): default=None, help=('Command to run the compiler/build-system. ' 'Supported build commands (run `infer --help -- ` for ' - 'extra help, e.g. `infer --help -- javac`): ' + supported_commands), + 'extra help, e.g. `infer --help -- javac`): {}'.format( + supported_commands)), ) return parser @@ -151,10 +159,10 @@ def main(): validate_args(imported_module, args) - remove_infer_out = (imported_module is not None - and not args.reactive - and capture_module_name != 'analyze' - and not args.buck) + remove_infer_out = (imported_module is not None and + not args.reactive and + capture_module_name != 'analyze' and + not args.buck) if remove_infer_out: analyze.remove_infer_out(args.infer_out) diff --git a/infer/lib/python/inferlib/analyze.py b/infer/lib/python/inferlib/analyze.py index 3e1523d5e..e6dda8604 100644 --- a/infer/lib/python/inferlib/analyze.py +++ b/infer/lib/python/inferlib/analyze.py @@ -11,10 +11,7 @@ from __future__ import print_function from __future__ import unicode_literals import argparse -import codecs import csv -import glob -import json import logging import multiprocessing import os @@ -35,7 +32,7 @@ def get_infer_version(): try: return subprocess.check_output([ utils.get_cmd_in_bin_dir(INFER_ANALYZE_BINARY), '-version']) - except: + except subprocess.CalledProcessError: utils.stdout('Failed to run {0} binary, exiting' .format(INFER_ANALYZE_BINARY)) sys.exit(os.EX_UNAVAILABLE) @@ -180,6 +177,7 @@ infer_group.add_argument('--specs-dir-list-file', 'in to the list of directories to be ' 'searched for spec files') + def remove_infer_out(infer_out): # it is safe to ignore errors here because recreating the infer_out # directory will fail later @@ -199,6 +197,7 @@ def reset_start_file(results_dir, touch_if_present=False): # create new empty file - this will update modified timestamp open(start_path, 'w').close() + def clean(infer_out): directories = [ 'multicore', 'classnames', 'sources', @@ -367,7 +366,6 @@ class AnalyzerWrapper(object): elif self.args.project_root: infer_options += ['-project_root', self.args.project_root] - infer_options = map(utils.decode_or_not, infer_options) infer_options_str = ' '.join(infer_options) os.environ['INFER_OPTIONS'] = utils.encode(infer_options_str) @@ -470,8 +468,9 @@ class AnalyzerWrapper(object): infer_print_cmd + infer_print_options ) if exit_status != os.EX_OK: - logging.error('Error with InferPrint with the command: ' - + infer_print_cmd) + logging.error( + 'Error with InferPrint with the command: {}'.format( + infer_print_cmd)) else: issues.clean_csv(self.args, csv_report) issues.clean_json(self.args, json_report) @@ -479,7 +478,6 @@ class AnalyzerWrapper(object): return exit_status - def read_proc_stats(self): proc_stats_path = os.path.join( self.args.infer_out, @@ -529,7 +527,6 @@ class AnalyzerWrapper(object): bugs_out, xml_out) def analyze_and_report(self): - should_print_errors = False if self.args.analyzer not in [config.ANALYZER_COMPILE, config.ANALYZER_CAPTURE]: if self.args.analyzer == config.ANALYZER_LINTERS: diff --git a/infer/lib/python/inferlib/bucklib.py b/infer/lib/python/inferlib/bucklib.py index f5a44aa4a..82d2761ea 100644 --- a/infer/lib/python/inferlib/bucklib.py +++ b/infer/lib/python/inferlib/bucklib.py @@ -30,7 +30,7 @@ import time import traceback import zipfile -from inferlib import analyze, config, issues, utils +from inferlib import config, issues, utils ANALYSIS_SUMMARY_OUTPUT = 'analysis_summary.txt' @@ -76,7 +76,7 @@ def prepare_build(args): # Create a temporary directory as a cache for jar files. infer_cache_dir = os.path.join(args.infer_out, 'cache') if not os.path.isdir(infer_cache_dir): - os.mkdir(infer_cache_dir) + os.mkdir(infer_cache_dir) infer_options += ['--infer_cache', infer_cache_dir] temp_files = [infer_cache_dir] @@ -254,7 +254,7 @@ class NotFoundInJar(Exception): def load_stats(opened_jar): try: return json.loads(opened_jar.read(INFER_STATS).decode()) - except KeyError as e: + except KeyError: raise NotFoundInJar @@ -262,14 +262,14 @@ def load_csv_report(opened_jar): try: sio = io.StringIO(opened_jar.read(INFER_CSV_REPORT).decode()) return list(utils.locale_csv_reader(sio)) - except KeyError as e: + except KeyError: raise NotFoundInJar def load_json_report(opened_jar): try: return json.loads(opened_jar.read(INFER_JSON_REPORT).decode()) - except KeyError as e: + except KeyError: raise NotFoundInJar @@ -407,9 +407,9 @@ def cleanup(temp_files): try: logging.info('Removing %s' % file) if os.path.isdir(file): - shutil.rmtree(file) + shutil.rmtree(file) else: - os.unlink(file) + os.unlink(file) except IOError: logging.error('Could not remove %s' % file) @@ -499,7 +499,8 @@ class Wrapper: sys.exit(0) except subprocess.CalledProcessError as e: if self.buck_args.keep_going: - print('Buck failed, but continuing analysis because --keep-going was passed') + print('Buck failed, but continuing the analysis ' + 'because --keep-going was passed') self._collect_results(start_time) return os.EX_OK raise e diff --git a/infer/lib/python/inferlib/capture/ant.py b/infer/lib/python/inferlib/capture/ant.py index 639fa00e2..790dfd5ee 100644 --- a/infer/lib/python/inferlib/capture/ant.py +++ b/infer/lib/python/inferlib/capture/ant.py @@ -5,7 +5,6 @@ # LICENSE file in the root directory of this source tree. An additional grant # of patent rights can be found in the PATENTS file in the same directory. -import os import logging from . import util @@ -19,6 +18,7 @@ Analysis examples: infer -- ant compile''' LANG = ['java'] + def gen_instance(*args): return AntCapture(*args) diff --git a/infer/lib/python/inferlib/capture/buck.py b/infer/lib/python/inferlib/capture/buck.py index 8992db20d..dc05ae4e0 100644 --- a/infer/lib/python/inferlib/capture/buck.py +++ b/infer/lib/python/inferlib/capture/buck.py @@ -11,7 +11,6 @@ from __future__ import print_function from __future__ import unicode_literals import argparse -import json import logging import os import subprocess @@ -35,6 +34,8 @@ def gen_instance(*args): def string_in_quotes(value): return value.strip('\'') + + # This creates an empty argparser for the module, which provides only # description/usage information and no arguments. def create_argparser(group_name=MODULE_NAME): diff --git a/infer/lib/python/inferlib/capture/javac.py b/infer/lib/python/inferlib/capture/javac.py index ce970aa6a..2abaf3878 100644 --- a/infer/lib/python/inferlib/capture/javac.py +++ b/infer/lib/python/inferlib/capture/javac.py @@ -10,7 +10,7 @@ import subprocess import traceback import util -from inferlib import analyze, jwlib +from inferlib import jwlib MODULE_NAME = __name__ MODULE_DESCRIPTION = '''Run analysis of code built with a command like: diff --git a/infer/lib/python/inferlib/capture/make.py b/infer/lib/python/inferlib/capture/make.py index 8c64bc08b..e728dc937 100644 --- a/infer/lib/python/inferlib/capture/make.py +++ b/infer/lib/python/inferlib/capture/make.py @@ -30,6 +30,7 @@ ALIASED_COMMANDS = ['clang', 'clang++', 'cc', 'gcc', 'g++'] BUILD_COMMANDS = ['cmake', 'configure', 'make', 'waf'] SUPPORTED_COMMANDS = ALIASED_COMMANDS + BUILD_COMMANDS + def gen_instance(*args): return MakeCapture(*args) diff --git a/infer/lib/python/inferlib/capture/ndk-build.py b/infer/lib/python/inferlib/capture/ndk-build.py index 915812f9b..55afc0f37 100644 --- a/infer/lib/python/inferlib/capture/ndk-build.py +++ b/infer/lib/python/inferlib/capture/ndk-build.py @@ -15,6 +15,7 @@ MODULE_DESCRIPTION = '''Run analysis of code built with ndk-build infer -- ndk-build''' LANG = ['clang'] + def gen_instance(*args): return NdkBuildCapture(*args) diff --git a/infer/lib/python/inferlib/capture/util.py b/infer/lib/python/inferlib/capture/util.py index e2614edf1..d3cf2e08d 100644 --- a/infer/lib/python/inferlib/capture/util.py +++ b/infer/lib/python/inferlib/capture/util.py @@ -18,7 +18,8 @@ import logging import subprocess import traceback -from inferlib import analyze, jwlib, utils +from inferlib import utils + def get_build_output(build_cmd): # TODO make it return generator to be able to handle large builds @@ -45,7 +46,7 @@ def run_compilation_commands(cmds, clean_cmd): def run_cmd_ignore_fail(cmd): try: return subprocess.check_output(cmd, stderr=subprocess.STDOUT) - except: + except subprocess.CalledProcessError: return 'calling {cmd} failed\n{trace}'.format( cmd=' '.join(cmd), trace=traceback.format_exc()) @@ -62,7 +63,7 @@ def base_argparser(description, module_name): """This creates an empty argparser for the module, which provides only description/usage information and no arguments.""" parser = argparse.ArgumentParser(add_help=False) - group = parser.add_argument_group( + parser.add_argument_group( '{grp} module'.format(grp=group_name), description=description, ) diff --git a/infer/lib/python/inferlib/capture/xcodebuild.py b/infer/lib/python/inferlib/capture/xcodebuild.py index d9728c0dc..469a64b44 100644 --- a/infer/lib/python/inferlib/capture/xcodebuild.py +++ b/infer/lib/python/inferlib/capture/xcodebuild.py @@ -63,7 +63,8 @@ class XcodebuildCapture: def capture(self): # these settings will instruct xcodebuild on which clang to use self.cmd += ['CC={wrapper}'.format(wrapper=CLANG_WRAPPER)] - self.cmd += ['CPLUSPLUS={wrapper}'.format(wrapper=CLANGPLUSPLUS_WRAPPER)] + self.cmd += [ + 'CPLUSPLUS={wrapper}'.format(wrapper=CLANGPLUSPLUS_WRAPPER)] # skip the ProcessPCH phase to fix the "newer/older" incompatibility # error for the pch files generated by apple's clang and diff --git a/infer/lib/python/inferlib/config.py b/infer/lib/python/inferlib/config.py index c5e32b054..2f5f03137 100644 --- a/infer/lib/python/inferlib/config.py +++ b/infer/lib/python/inferlib/config.py @@ -35,7 +35,8 @@ BIN_DIRECTORY = os.path.join(INFER_INFER_DIRECTORY, 'bin') JAVA_LIB_DIRECTORY = os.path.join(LIB_DIRECTORY, 'java') MODELS_JAR = os.path.join(JAVA_LIB_DIRECTORY, 'models.jar') ANNOT_PROCESSOR_JAR = os.path.join(JAVA_LIB_DIRECTORY, 'processor.jar') -ANNOT_PROCESSOR_NAMES = 'com.facebook.infer.annotprocess.CollectSuppressWarnings' +ANNOT_PROCESSOR_NAMES = \ + 'com.facebook.infer.annotprocess.CollectSuppressWarnings' WRAPPERS_DIRECTORY = os.path.join(LIB_DIRECTORY, 'wrappers') XCODE_WRAPPERS_DIRECTORY = os.path.join(LIB_DIRECTORY, 'xcode_wrappers') diff --git a/infer/lib/python/inferlib/issues.py b/infer/lib/python/inferlib/issues.py index 3f34d59a9..0bbb95602 100644 --- a/infer/lib/python/inferlib/issues.py +++ b/infer/lib/python/inferlib/issues.py @@ -164,6 +164,7 @@ def clean_json(args, json_report): utils.dump_json_to_path(rows, temporary_file) shutil.move(temporary_file, json_report) + def _text_of_infer_loc(loc): return ' ({}:{}:{}-{}:)'.format( loc[JSON_INDEX_ISL_FILE], diff --git a/infer/lib/python/inferlib/jwlib.py b/infer/lib/python/inferlib/jwlib.py index 8e8ef9441..9b379081d 100644 --- a/infer/lib/python/inferlib/jwlib.py +++ b/infer/lib/python/inferlib/jwlib.py @@ -15,7 +15,6 @@ import argparse import codecs import os import subprocess -import sys import tempfile import time @@ -150,9 +149,10 @@ class CompilerCall(object): if _is_empty_classpath(cp_line): cp_line = '\n' else: - cp_line = ':%s\n' % cp_line - args_file_cp.write( - prefix + '-classpath\n' + classpath + cp_line + after_cp) + cp_line = ':{}\n'.format(cp_line) + cp_line = prefix + '-classpath\n' + classpath + cp_line + cp_line += after_cp + args_file_cp.write(cp_line) at_arg_cp = '@' + args_file_cp.name self.remaining_args = [ at_arg_cp if arg == at_arg else arg @@ -170,8 +170,10 @@ class CompilerCall(object): # processors (checking the manifest of the annotation processor # JAR), so we don't want to use it unless the javac command does if self.args.processor is not None: - processor = '%s,%s' % (config.ANNOT_PROCESSOR_NAMES, - self.args.processor) + processor = '{infer_processors},{original_processors}'.format( + infer_processors=config.ANNOT_PROCESSOR_NAMES, + original_processors=self.args.processor + ) javac_args += ['-processor', processor] with tempfile.NamedTemporaryFile( @@ -184,8 +186,8 @@ class CompilerCall(object): annot_out.write('{}') self.suppress_warnings_out = annot_out.name javac_args += ['-A%s=%s' % - (config.SUPRESS_WARNINGS_OUTPUT_FILENAME_OPTION, - self.suppress_warnings_out)] + (config.SUPRESS_WARNINGS_OUTPUT_FILENAME_OPTION, + self.suppress_warnings_out)] def arg_must_go_on_cli(arg): # as mandated by javac, argument files must not contain @@ -241,7 +243,7 @@ class AnalyzerWithFrontendWrapper(analyze.AnalyzerWrapper): self.javac = CompilerCall(javac_cmd, javac_args) if not self.javac.args.version: if javac_args is None: - help_exit('No javac command detected') + raise Exception('No javac command detected') analyze.AnalyzerWrapper.__init__(self, args) @@ -325,7 +327,6 @@ class AnalyzerWithFrontendWrapper(analyze.AnalyzerWrapper): if self.args.android_harness: infer_cmd.append('-harness') - return analyze.run_command( infer_cmd, self.args.debug, diff --git a/infer/lib/python/inferlib/source.py b/infer/lib/python/inferlib/source.py index 9f4546130..c8b9d6301 100644 --- a/infer/lib/python/inferlib/source.py +++ b/infer/lib/python/inferlib/source.py @@ -12,12 +12,13 @@ from __future__ import unicode_literals import codecs -from . import colorize, config, utils +from . import colorize, config BASE_INDENT = 2 # how many lines of context around each report SOURCE_CONTEXT = 2 + class Indenter(str): def __init__(self): super(Indenter, self).__init__() diff --git a/infer/lib/python/inferlib/utils.py b/infer/lib/python/inferlib/utils.py index e381ea182..cc4cf5e07 100644 --- a/infer/lib/python/inferlib/utils.py +++ b/infer/lib/python/inferlib/utils.py @@ -20,7 +20,6 @@ import logging import os import subprocess import sys -import tempfile import time from . import config @@ -218,7 +217,6 @@ class Timer: return self._dt - def interact(): """Start interactive mode. Useful for debugging. """