From de86c12a9a762e31c60a51dd5e5429b0195ebb01 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Thu, 3 Aug 2017 07:33:56 -0700 Subject: [PATCH] [diff] suppress console output in report called from diff analysis Summary: That was too noisy. Propagate `--quiet` to the Python reporting hook so as to still emit bugs.txt and so on. Reviewed By: martinoluca Differential Revision: D5501106 fbshipit-source-id: 63b6451 --- infer/lib/python/inferlib/analyze.py | 2 ++ infer/lib/python/inferlib/bucklib.py | 7 ++++--- infer/lib/python/inferlib/capture/buck.py | 3 ++- infer/lib/python/inferlib/issues.py | 13 +++++++------ infer/lib/python/report.py | 4 +++- infer/src/integration/Diff.ml | 4 ++-- infer/src/integration/Driver.ml | 15 ++++++++------- infer/src/integration/Driver.mli | 3 ++- infer/tests/build_systems/diff/Makefile | 1 - .../build_systems/diff_gen_build_script/Makefile | 1 - 10 files changed, 30 insertions(+), 23 deletions(-) diff --git a/infer/lib/python/inferlib/analyze.py b/infer/lib/python/inferlib/analyze.py index 88b78ef4a..c10c8421d 100644 --- a/infer/lib/python/inferlib/analyze.py +++ b/infer/lib/python/inferlib/analyze.py @@ -49,6 +49,8 @@ base_group.add_argument('-nf', '--no-filtering', action='store_true', base_group.add_argument('--pmd-xml', action='store_true', help='''Output issues in (PMD) XML format.''') +base_group.add_argument('--quiet', action='store_true', + help='Silence console output.') infer_parser = argparse.ArgumentParser(parents=[base_parser]) diff --git a/infer/lib/python/inferlib/bucklib.py b/infer/lib/python/inferlib/bucklib.py index 6c5fefa9e..0829c5730 100644 --- a/infer/lib/python/inferlib/bucklib.py +++ b/infer/lib/python/inferlib/bucklib.py @@ -139,8 +139,8 @@ def get_output_jars(targets): def collect_results(args, start_time, targets): - """Walks through buck-gen, collects results for the different buck targets - and stores them in in args.infer_out/results.csv. + """Walks through buck-out/, collects results for the different buck targets + and stores them in in args.infer_out/results.json. """ all_json_rows = set() @@ -166,7 +166,8 @@ def collect_results(args, start_time, targets): bugs_out = os.path.join(args.infer_out, config.BUGS_FILENAME) issues.print_and_save_errors(args.infer_out, args.project_root, - json_report, bugs_out, args.pmd_xml) + json_report, bugs_out, args.pmd_xml, + console_out=not args.quiet) def cleanup(temp_files): diff --git a/infer/lib/python/inferlib/capture/buck.py b/infer/lib/python/inferlib/capture/buck.py index e5a9941d6..58a9b94fb 100644 --- a/infer/lib/python/inferlib/capture/buck.py +++ b/infer/lib/python/inferlib/capture/buck.py @@ -258,7 +258,8 @@ class BuckAnalyzer: json_report = os.path.join(infer_out, config.JSON_REPORT_FILENAME) bugs_out = os.path.join(infer_out, config.BUGS_FILENAME) issues.print_and_save_errors(infer_out, self.args.project_root, - json_report, bugs_out, self.args.pmd_xml) + json_report, bugs_out, self.args.pmd_xml, + console_out=not self.args.quiet) return os.EX_OK def capture_without_flavors(self): diff --git a/infer/lib/python/inferlib/issues.py b/infer/lib/python/inferlib/issues.py index 508c8681c..3c44be065 100644 --- a/infer/lib/python/inferlib/issues.py +++ b/infer/lib/python/inferlib/issues.py @@ -182,7 +182,7 @@ def _text_of_report_list(project_root, reports, bugs_txt_path, limit=None, return bug_list + summary -def _is_user_visible(project_root, report): +def _is_user_visible(report): kind = report[JSON_INDEX_KIND] return kind in [ ISSUE_KIND_ERROR, @@ -192,12 +192,13 @@ def _is_user_visible(project_root, report): def print_and_save_errors(infer_out, project_root, json_report, bugs_out, - pmd_xml): + pmd_xml, console_out): errors = utils.load_json_from_path(json_report) - errors = [e for e in errors if _is_user_visible(project_root, e)] - utils.stderr('') - _text_of_report_list(project_root, errors, bugs_out, console_out=True, - limit=10) + errors = [e for e in errors if _is_user_visible(e)] + if console_out: + utils.stderr('') + _text_of_report_list(project_root, errors, bugs_out, console_out=True, + limit=10) plain_out = _text_of_report_list(project_root, errors, bugs_out, formatter=colorize.PLAIN_FORMATTER) with codecs.open(bugs_out, 'w', diff --git a/infer/lib/python/report.py b/infer/lib/python/report.py index 0e76cd8f7..9439642d6 100755 --- a/infer/lib/python/report.py +++ b/infer/lib/python/report.py @@ -31,6 +31,8 @@ arg_parser.add_argument('--pmd-xml', action='store_true', help='Output issues in (PMD) XML format.') arg_parser.add_argument('--project-root', metavar='', required=True, help='Location of the project root') +arg_parser.add_argument('--quiet', action='store_true', + help='Silence console output.') arg_parser.add_argument('--results-dir', metavar='', required=True, help='Location of the results directory') @@ -40,7 +42,7 @@ def main(): args = arg_parser.parse_args(sys_argv[1:]) issues.print_and_save_errors(args.results_dir, args.project_root, args.issues_json, args.issues_txt, - args.pmd_xml) + args.pmd_xml, console_out=not args.quiet) if __name__ == '__main__': diff --git a/infer/src/integration/Diff.ml b/infer/src/integration/Diff.ml index edf282ac9..70a62087b 100644 --- a/infer/src/integration/Diff.ml +++ b/infer/src/integration/Diff.ml @@ -67,7 +67,7 @@ let diff driver_mode = Driver.run_prologue driver_mode ; let changed_files = Driver.read_config_changed_files () in Driver.capture driver_mode ~changed_files ; - Driver.analyze_and_report driver_mode ~changed_files ; + Driver.analyze_and_report ~suppress_console_report:true driver_mode ~changed_files ; let current_report = Some (save_report Current) in (* Some files in the current checkout may be deleted in the old checkout. If we kept the results of the previous capture and analysis around, we would report issues on these files again in the previous checkout, which is wrong. Do not do anything too smart for now and just delete all results from the analysis of the current checkout. *) delete_capture_and_analysis_artifacts () ; @@ -79,7 +79,7 @@ let diff driver_mode = Config.gen_previous_build_command_script in Driver.capture previous_driver_mode ~changed_files ; - Driver.analyze_and_report previous_driver_mode ~changed_files ; + Driver.analyze_and_report ~suppress_console_report:true previous_driver_mode ~changed_files ; checkout Current ; let previous_report = Some (save_report Previous) in (* compute differential *) diff --git a/infer/src/integration/Driver.ml b/infer/src/integration/Driver.ml index bb1827560..3f2284c4c 100644 --- a/infer/src/integration/Driver.ml +++ b/infer/src/integration/Driver.ml @@ -309,6 +309,7 @@ let capture ~changed_files = function -> ["-l"; string_of_float l] ) @ (if not Config.pmd_xml then [] else ["--pmd-xml"]) @ ["--project-root"; Config.project_root] + @ (if not Config.quiet then [] else ["--quiet"]) @ (if not Config.reactive_mode then [] else ["--reactive"]) @ "--out" :: Config.results_dir @@ -370,7 +371,7 @@ let execute_analyze ~changed_files = InferAnalyze.main ~changed_files ~makefile:"" else run_parallel_analysis ~changed_files -let report () = +let report ?(suppress_console= false) () = let report_csv = if Config.buck_cache_mode then None else Some (Config.results_dir ^/ "report.csv") in @@ -379,8 +380,8 @@ let report () = (* Post-process the report according to the user config. By default, calls report.py to create a human-readable report. - Do not bother calling the report hook when called from within Buck or in quiet mode. *) - match (Config.quiet || Config.buck_cache_mode, Config.report_hook) with + Do not bother calling the report hook when called from within Buck. *) + match (Config.buck_cache_mode, Config.report_hook) with | true, _ | false, None -> () | false, Some prog @@ -388,8 +389,8 @@ let report () = let if_true key opt args = if not opt then args else key :: args in let bugs_txt = Option.value ~default:(Config.results_dir ^/ "bugs.txt") Config.bugs_txt in let args = - if_some "--issues-csv" report_csv - @@ if_true "--pmd-xml" Config.pmd_xml + if_some "--issues-csv" report_csv @@ if_true "--pmd-xml" Config.pmd_xml + @@ if_true "--quiet" (Config.quiet || suppress_console) [ "--issues-json" ; report_json ; "--issues-txt" @@ -404,7 +405,7 @@ let report () = "** Error running the reporting script:@\n** %s %s@\n** See error above@." prog (String.concat ~sep:" " args) -let analyze_and_report ~changed_files mode = +let analyze_and_report ?suppress_console_report ~changed_files mode = let should_analyze, should_report = match (mode, Config.analyzer) with | PythonCapture (BBuck, _), _ when not Config.flavors @@ -433,7 +434,7 @@ let analyze_and_report ~changed_files mode = && (Sys.file_exists Config.captured_dir <> `Yes || check_captured_empty mode) then L.user_error "There was nothing to analyze.@\n@." else if should_analyze then execute_analyze ~changed_files ; - if should_report && Config.report then report () + if should_report && Config.report then report ?suppress_console:suppress_console_report () (** as the Config.fail_on_bug flag mandates, exit with error when an issue is reported *) let fail_on_issue_epilogue () = diff --git a/infer/src/integration/Driver.mli b/infer/src/integration/Driver.mli index 5b32bc99f..786b31dbb 100644 --- a/infer/src/integration/Driver.mli +++ b/infer/src/integration/Driver.mli @@ -41,7 +41,8 @@ val run_prologue : mode -> unit val capture : changed_files:SourceFile.Set.t option -> mode -> unit (** run the capture for the given mode *) -val analyze_and_report : changed_files:SourceFile.Set.t option -> mode -> unit +val analyze_and_report : + ?suppress_console_report:bool -> changed_files:SourceFile.Set.t option -> mode -> unit (** run the analysis for the given mode *) val run_epilogue : mode -> unit diff --git a/infer/tests/build_systems/diff/Makefile b/infer/tests/build_systems/diff/Makefile index cb20dce22..a8b250d50 100644 --- a/infer/tests/build_systems/diff/Makefile +++ b/infer/tests/build_systems/diff/Makefile @@ -16,7 +16,6 @@ INFER_OPTIONS = \ --previous-to-current-script '$(COPY) $(SRC_DIR)/some_bugs.c src/hello.c' \ --current-to-previous-script '$(COPY) $(SRC_DIR)/some_different_bugs.c src/hello.c' \ --changed-files-index changed_files.txt \ - --report-hook '/bin/true' \ -- clang -c src/hello.c SOURCES = $(SRC_DIR)/some_bugs.c $(SRC_DIR)/some_different_bugs.c diff --git a/infer/tests/build_systems/diff_gen_build_script/Makefile b/infer/tests/build_systems/diff_gen_build_script/Makefile index 73218ea6c..2358113db 100644 --- a/infer/tests/build_systems/diff_gen_build_script/Makefile +++ b/infer/tests/build_systems/diff_gen_build_script/Makefile @@ -18,7 +18,6 @@ INFER_OPTIONS = \ --gen-previous-build-command-script 'echo "clang -c src/some_bugs.c"' \ --changed-files-index changed_files.txt \ --file-renamings file_renamings.json \ - --report-hook '/bin/true' \ -- clang -c src/some_different_bugs.c SOURCES = $(SRC_DIR)/some_bugs.c $(SRC_DIR)/some_different_bugs.c