[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
master
Jeremy Dubreil 8 years ago committed by Facebook Github Bot
parent dab5786789
commit dfa12b17f1

@ -1,5 +1,12 @@
#!/usr/bin/env python2.7 #!/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 absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
@ -7,7 +14,6 @@ from __future__ import unicode_literals
import argparse import argparse
import imp import imp
import json
import locale import locale
import logging import logging
import os import os
@ -40,9 +46,10 @@ MODULE_TO_COMMAND = {
'ndk-build': ['ndk-build'], 'ndk-build': ['ndk-build'],
} }
def get_commands(): def get_commands():
"""Return all commands that are supported.""" """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(), [])) return set(sum(MODULE_TO_COMMAND.values(), []))
@ -110,7 +117,8 @@ def create_argparser(parents=[]):
default=None, default=None,
help=('Command to run the compiler/build-system. ' help=('Command to run the compiler/build-system. '
'Supported build commands (run `infer --help -- <cmd_name>` for ' 'Supported build commands (run `infer --help -- <cmd_name>` for '
'extra help, e.g. `infer --help -- javac`): ' + supported_commands), 'extra help, e.g. `infer --help -- javac`): {}'.format(
supported_commands)),
) )
return parser return parser
@ -151,10 +159,10 @@ def main():
validate_args(imported_module, args) validate_args(imported_module, args)
remove_infer_out = (imported_module is not None remove_infer_out = (imported_module is not None and
and not args.reactive not args.reactive and
and capture_module_name != 'analyze' capture_module_name != 'analyze' and
and not args.buck) not args.buck)
if remove_infer_out: if remove_infer_out:
analyze.remove_infer_out(args.infer_out) analyze.remove_infer_out(args.infer_out)

@ -11,10 +11,7 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import codecs
import csv import csv
import glob
import json
import logging import logging
import multiprocessing import multiprocessing
import os import os
@ -35,7 +32,7 @@ def get_infer_version():
try: try:
return subprocess.check_output([ return subprocess.check_output([
utils.get_cmd_in_bin_dir(INFER_ANALYZE_BINARY), '-version']) utils.get_cmd_in_bin_dir(INFER_ANALYZE_BINARY), '-version'])
except: except subprocess.CalledProcessError:
utils.stdout('Failed to run {0} binary, exiting' utils.stdout('Failed to run {0} binary, exiting'
.format(INFER_ANALYZE_BINARY)) .format(INFER_ANALYZE_BINARY))
sys.exit(os.EX_UNAVAILABLE) sys.exit(os.EX_UNAVAILABLE)
@ -180,6 +177,7 @@ infer_group.add_argument('--specs-dir-list-file',
'in <file> to the list of directories to be ' 'in <file> to the list of directories to be '
'searched for spec files') 'searched for spec files')
def remove_infer_out(infer_out): def remove_infer_out(infer_out):
# it is safe to ignore errors here because recreating the infer_out # it is safe to ignore errors here because recreating the infer_out
# directory will fail later # 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 # create new empty file - this will update modified timestamp
open(start_path, 'w').close() open(start_path, 'w').close()
def clean(infer_out): def clean(infer_out):
directories = [ directories = [
'multicore', 'classnames', 'sources', 'multicore', 'classnames', 'sources',
@ -367,7 +366,6 @@ class AnalyzerWrapper(object):
elif self.args.project_root: elif self.args.project_root:
infer_options += ['-project_root', self.args.project_root] infer_options += ['-project_root', self.args.project_root]
infer_options = map(utils.decode_or_not, infer_options) infer_options = map(utils.decode_or_not, infer_options)
infer_options_str = ' '.join(infer_options) infer_options_str = ' '.join(infer_options)
os.environ['INFER_OPTIONS'] = utils.encode(infer_options_str) os.environ['INFER_OPTIONS'] = utils.encode(infer_options_str)
@ -470,8 +468,9 @@ class AnalyzerWrapper(object):
infer_print_cmd + infer_print_options infer_print_cmd + infer_print_options
) )
if exit_status != os.EX_OK: if exit_status != os.EX_OK:
logging.error('Error with InferPrint with the command: ' logging.error(
+ infer_print_cmd) 'Error with InferPrint with the command: {}'.format(
infer_print_cmd))
else: else:
issues.clean_csv(self.args, csv_report) issues.clean_csv(self.args, csv_report)
issues.clean_json(self.args, json_report) issues.clean_json(self.args, json_report)
@ -479,7 +478,6 @@ class AnalyzerWrapper(object):
return exit_status return exit_status
def read_proc_stats(self): def read_proc_stats(self):
proc_stats_path = os.path.join( proc_stats_path = os.path.join(
self.args.infer_out, self.args.infer_out,
@ -529,7 +527,6 @@ class AnalyzerWrapper(object):
bugs_out, xml_out) bugs_out, xml_out)
def analyze_and_report(self): def analyze_and_report(self):
should_print_errors = False
if self.args.analyzer not in [config.ANALYZER_COMPILE, if self.args.analyzer not in [config.ANALYZER_COMPILE,
config.ANALYZER_CAPTURE]: config.ANALYZER_CAPTURE]:
if self.args.analyzer == config.ANALYZER_LINTERS: if self.args.analyzer == config.ANALYZER_LINTERS:

@ -30,7 +30,7 @@ import time
import traceback import traceback
import zipfile import zipfile
from inferlib import analyze, config, issues, utils from inferlib import config, issues, utils
ANALYSIS_SUMMARY_OUTPUT = 'analysis_summary.txt' ANALYSIS_SUMMARY_OUTPUT = 'analysis_summary.txt'
@ -76,7 +76,7 @@ def prepare_build(args):
# Create a temporary directory as a cache for jar files. # Create a temporary directory as a cache for jar files.
infer_cache_dir = os.path.join(args.infer_out, 'cache') infer_cache_dir = os.path.join(args.infer_out, 'cache')
if not os.path.isdir(infer_cache_dir): 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] infer_options += ['--infer_cache', infer_cache_dir]
temp_files = [infer_cache_dir] temp_files = [infer_cache_dir]
@ -254,7 +254,7 @@ class NotFoundInJar(Exception):
def load_stats(opened_jar): def load_stats(opened_jar):
try: try:
return json.loads(opened_jar.read(INFER_STATS).decode()) return json.loads(opened_jar.read(INFER_STATS).decode())
except KeyError as e: except KeyError:
raise NotFoundInJar raise NotFoundInJar
@ -262,14 +262,14 @@ def load_csv_report(opened_jar):
try: try:
sio = io.StringIO(opened_jar.read(INFER_CSV_REPORT).decode()) sio = io.StringIO(opened_jar.read(INFER_CSV_REPORT).decode())
return list(utils.locale_csv_reader(sio)) return list(utils.locale_csv_reader(sio))
except KeyError as e: except KeyError:
raise NotFoundInJar raise NotFoundInJar
def load_json_report(opened_jar): def load_json_report(opened_jar):
try: try:
return json.loads(opened_jar.read(INFER_JSON_REPORT).decode()) return json.loads(opened_jar.read(INFER_JSON_REPORT).decode())
except KeyError as e: except KeyError:
raise NotFoundInJar raise NotFoundInJar
@ -407,9 +407,9 @@ def cleanup(temp_files):
try: try:
logging.info('Removing %s' % file) logging.info('Removing %s' % file)
if os.path.isdir(file): if os.path.isdir(file):
shutil.rmtree(file) shutil.rmtree(file)
else: else:
os.unlink(file) os.unlink(file)
except IOError: except IOError:
logging.error('Could not remove %s' % file) logging.error('Could not remove %s' % file)
@ -499,7 +499,8 @@ class Wrapper:
sys.exit(0) sys.exit(0)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if self.buck_args.keep_going: 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) self._collect_results(start_time)
return os.EX_OK return os.EX_OK
raise e raise e

@ -5,7 +5,6 @@
# LICENSE file in the root directory of this source tree. An additional grant # 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. # of patent rights can be found in the PATENTS file in the same directory.
import os
import logging import logging
from . import util from . import util
@ -19,6 +18,7 @@ Analysis examples:
infer -- ant compile''' infer -- ant compile'''
LANG = ['java'] LANG = ['java']
def gen_instance(*args): def gen_instance(*args):
return AntCapture(*args) return AntCapture(*args)

@ -11,7 +11,6 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import json
import logging import logging
import os import os
import subprocess import subprocess
@ -35,6 +34,8 @@ def gen_instance(*args):
def string_in_quotes(value): def string_in_quotes(value):
return value.strip('\'') return value.strip('\'')
# This creates an empty argparser for the module, which provides only # This creates an empty argparser for the module, which provides only
# description/usage information and no arguments. # description/usage information and no arguments.
def create_argparser(group_name=MODULE_NAME): def create_argparser(group_name=MODULE_NAME):

@ -10,7 +10,7 @@ import subprocess
import traceback import traceback
import util import util
from inferlib import analyze, jwlib from inferlib import jwlib
MODULE_NAME = __name__ MODULE_NAME = __name__
MODULE_DESCRIPTION = '''Run analysis of code built with a command like: MODULE_DESCRIPTION = '''Run analysis of code built with a command like:

@ -30,6 +30,7 @@ ALIASED_COMMANDS = ['clang', 'clang++', 'cc', 'gcc', 'g++']
BUILD_COMMANDS = ['cmake', 'configure', 'make', 'waf'] BUILD_COMMANDS = ['cmake', 'configure', 'make', 'waf']
SUPPORTED_COMMANDS = ALIASED_COMMANDS + BUILD_COMMANDS SUPPORTED_COMMANDS = ALIASED_COMMANDS + BUILD_COMMANDS
def gen_instance(*args): def gen_instance(*args):
return MakeCapture(*args) return MakeCapture(*args)

@ -15,6 +15,7 @@ MODULE_DESCRIPTION = '''Run analysis of code built with ndk-build
infer -- ndk-build''' infer -- ndk-build'''
LANG = ['clang'] LANG = ['clang']
def gen_instance(*args): def gen_instance(*args):
return NdkBuildCapture(*args) return NdkBuildCapture(*args)

@ -18,7 +18,8 @@ import logging
import subprocess import subprocess
import traceback import traceback
from inferlib import analyze, jwlib, utils from inferlib import utils
def get_build_output(build_cmd): def get_build_output(build_cmd):
# TODO make it return generator to be able to handle large builds # 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): def run_cmd_ignore_fail(cmd):
try: try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT) return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except: except subprocess.CalledProcessError:
return 'calling {cmd} failed\n{trace}'.format( return 'calling {cmd} failed\n{trace}'.format(
cmd=' '.join(cmd), cmd=' '.join(cmd),
trace=traceback.format_exc()) 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 """This creates an empty argparser for the module, which provides only
description/usage information and no arguments.""" description/usage information and no arguments."""
parser = argparse.ArgumentParser(add_help=False) parser = argparse.ArgumentParser(add_help=False)
group = parser.add_argument_group( parser.add_argument_group(
'{grp} module'.format(grp=group_name), '{grp} module'.format(grp=group_name),
description=description, description=description,
) )

@ -63,7 +63,8 @@ class XcodebuildCapture:
def capture(self): def capture(self):
# these settings will instruct xcodebuild on which clang to use # these settings will instruct xcodebuild on which clang to use
self.cmd += ['CC={wrapper}'.format(wrapper=CLANG_WRAPPER)] 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 # skip the ProcessPCH phase to fix the "newer/older" incompatibility
# error for the pch files generated by apple's clang and # error for the pch files generated by apple's clang and

@ -35,7 +35,8 @@ BIN_DIRECTORY = os.path.join(INFER_INFER_DIRECTORY, 'bin')
JAVA_LIB_DIRECTORY = os.path.join(LIB_DIRECTORY, 'java') JAVA_LIB_DIRECTORY = os.path.join(LIB_DIRECTORY, 'java')
MODELS_JAR = os.path.join(JAVA_LIB_DIRECTORY, 'models.jar') MODELS_JAR = os.path.join(JAVA_LIB_DIRECTORY, 'models.jar')
ANNOT_PROCESSOR_JAR = os.path.join(JAVA_LIB_DIRECTORY, 'processor.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') WRAPPERS_DIRECTORY = os.path.join(LIB_DIRECTORY, 'wrappers')
XCODE_WRAPPERS_DIRECTORY = os.path.join(LIB_DIRECTORY, 'xcode_wrappers') XCODE_WRAPPERS_DIRECTORY = os.path.join(LIB_DIRECTORY, 'xcode_wrappers')

@ -164,6 +164,7 @@ def clean_json(args, json_report):
utils.dump_json_to_path(rows, temporary_file) utils.dump_json_to_path(rows, temporary_file)
shutil.move(temporary_file, json_report) shutil.move(temporary_file, json_report)
def _text_of_infer_loc(loc): def _text_of_infer_loc(loc):
return ' ({}:{}:{}-{}:)'.format( return ' ({}:{}:{}-{}:)'.format(
loc[JSON_INDEX_ISL_FILE], loc[JSON_INDEX_ISL_FILE],

@ -15,7 +15,6 @@ import argparse
import codecs import codecs
import os import os
import subprocess import subprocess
import sys
import tempfile import tempfile
import time import time
@ -150,9 +149,10 @@ class CompilerCall(object):
if _is_empty_classpath(cp_line): if _is_empty_classpath(cp_line):
cp_line = '\n' cp_line = '\n'
else: else:
cp_line = ':%s\n' % cp_line cp_line = ':{}\n'.format(cp_line)
args_file_cp.write( cp_line = prefix + '-classpath\n' + classpath + cp_line
prefix + '-classpath\n' + classpath + cp_line + after_cp) cp_line += after_cp
args_file_cp.write(cp_line)
at_arg_cp = '@' + args_file_cp.name at_arg_cp = '@' + args_file_cp.name
self.remaining_args = [ self.remaining_args = [
at_arg_cp if arg == at_arg else arg at_arg_cp if arg == at_arg else arg
@ -170,8 +170,10 @@ class CompilerCall(object):
# processors (checking the manifest of the annotation processor # processors (checking the manifest of the annotation processor
# JAR), so we don't want to use it unless the javac command does # JAR), so we don't want to use it unless the javac command does
if self.args.processor is not None: if self.args.processor is not None:
processor = '%s,%s' % (config.ANNOT_PROCESSOR_NAMES, processor = '{infer_processors},{original_processors}'.format(
self.args.processor) infer_processors=config.ANNOT_PROCESSOR_NAMES,
original_processors=self.args.processor
)
javac_args += ['-processor', processor] javac_args += ['-processor', processor]
with tempfile.NamedTemporaryFile( with tempfile.NamedTemporaryFile(
@ -184,8 +186,8 @@ class CompilerCall(object):
annot_out.write('{}') annot_out.write('{}')
self.suppress_warnings_out = annot_out.name self.suppress_warnings_out = annot_out.name
javac_args += ['-A%s=%s' % javac_args += ['-A%s=%s' %
(config.SUPRESS_WARNINGS_OUTPUT_FILENAME_OPTION, (config.SUPRESS_WARNINGS_OUTPUT_FILENAME_OPTION,
self.suppress_warnings_out)] self.suppress_warnings_out)]
def arg_must_go_on_cli(arg): def arg_must_go_on_cli(arg):
# as mandated by javac, argument files must not contain # as mandated by javac, argument files must not contain
@ -241,7 +243,7 @@ class AnalyzerWithFrontendWrapper(analyze.AnalyzerWrapper):
self.javac = CompilerCall(javac_cmd, javac_args) self.javac = CompilerCall(javac_cmd, javac_args)
if not self.javac.args.version: if not self.javac.args.version:
if javac_args is None: if javac_args is None:
help_exit('No javac command detected') raise Exception('No javac command detected')
analyze.AnalyzerWrapper.__init__(self, args) analyze.AnalyzerWrapper.__init__(self, args)
@ -325,7 +327,6 @@ class AnalyzerWithFrontendWrapper(analyze.AnalyzerWrapper):
if self.args.android_harness: if self.args.android_harness:
infer_cmd.append('-harness') infer_cmd.append('-harness')
return analyze.run_command( return analyze.run_command(
infer_cmd, infer_cmd,
self.args.debug, self.args.debug,

@ -12,12 +12,13 @@ from __future__ import unicode_literals
import codecs import codecs
from . import colorize, config, utils from . import colorize, config
BASE_INDENT = 2 BASE_INDENT = 2
# how many lines of context around each report # how many lines of context around each report
SOURCE_CONTEXT = 2 SOURCE_CONTEXT = 2
class Indenter(str): class Indenter(str):
def __init__(self): def __init__(self):
super(Indenter, self).__init__() super(Indenter, self).__init__()

@ -20,7 +20,6 @@ import logging
import os import os
import subprocess import subprocess
import sys import sys
import tempfile
import time import time
from . import config from . import config
@ -218,7 +217,6 @@ class Timer:
return self._dt return self._dt
def interact(): def interact():
"""Start interactive mode. Useful for debugging. """Start interactive mode. Useful for debugging.
""" """

Loading…
Cancel
Save