From fc2879ca8ff0261a27c88415319404320b27fcc8 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 24 Nov 2015 07:17:31 -0800 Subject: [PATCH] refactor dumping of json Summary: public Small refactoring. Reviewed By: jeremydubreil Differential Revision: D2679580 fb-gh-sync-id: b1e6061 --- infer/lib/python/BuckAnalyze | 3 +-- infer/lib/python/inferlib/analyze.py | 4 +--- infer/lib/python/inferlib/capture/buck.py | 3 +-- infer/lib/python/inferlib/issues.py | 6 ++---- infer/lib/python/inferlib/utils.py | 14 ++++++++++++++ 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/infer/lib/python/BuckAnalyze b/infer/lib/python/BuckAnalyze index 950cef9a7..c4dfe1d3f 100755 --- a/infer/lib/python/BuckAnalyze +++ b/infer/lib/python/BuckAnalyze @@ -408,8 +408,7 @@ def collect_results(args, start_time): store_performances_csv(args.infer_out, stats) stats_filename = os.path.join(args.infer_out, config.STATS_FILENAME) - with open(stats_filename, 'w') as stats_out: - json.dump(stats, stats_out, indent=2) + utils.dump_json_to_path(stats, stats_filename) basic_stats = get_basic_stats(stats) diff --git a/infer/lib/python/inferlib/analyze.py b/infer/lib/python/inferlib/analyze.py index ac8ba25bd..d2c9d7ad2 100644 --- a/infer/lib/python/inferlib/analyze.py +++ b/infer/lib/python/inferlib/analyze.py @@ -535,9 +535,7 @@ class Infer: } stats_path = os.path.join(self.args.infer_out, config.STATS_FILENAME) - with codecs.open(stats_path, 'w', - encoding=config.LOCALE) as stats_file: - json.dump(self.stats, stats_file, indent=2) + utils.dump_json_to_path(self.stats, stats_path) def close(self): diff --git a/infer/lib/python/inferlib/capture/buck.py b/infer/lib/python/inferlib/capture/buck.py index 94f6345a4..d0abcbfef 100644 --- a/infer/lib/python/inferlib/capture/buck.py +++ b/infer/lib/python/inferlib/capture/buck.py @@ -139,8 +139,7 @@ class BuckAnalyzer: all_results = utils.merge_json_arrays_from_files(result_files) merged_results_path = os.path.join(self.args.infer_out, config.JSON_REPORT_FILENAME) - with open(merged_results_path, 'w') as file_out: - json.dump(all_results, file_out, indent=2) + utils.dump_json_to_path(all_results, merged_results_path) print('Results saved in {results_path}'.format( results_path=merged_results_path)) return os.EX_OK diff --git a/infer/lib/python/inferlib/issues.py b/infer/lib/python/inferlib/issues.py index 6aeebf9d5..f45c95bcb 100644 --- a/infer/lib/python/inferlib/issues.py +++ b/infer/lib/python/inferlib/issues.py @@ -134,10 +134,8 @@ def clean_json(args, json_report): collected_rows, cmp=_compare_json_rows) temporary_file = tempfile.mktemp() - with open(temporary_file, 'w') as file_out: - json.dump(collected_rows, file_out, indent=2) - file_out.flush() - shutil.move(temporary_file, json_report) + utils.dump_json_to_path(collected_rows, temporary_file) + shutil.move(temporary_file, json_report) def print_and_save_errors(json_report, bugs_out): diff --git a/infer/lib/python/inferlib/utils.py b/infer/lib/python/inferlib/utils.py index 3bbd5de16..f6f332eb8 100644 --- a/infer/lib/python/inferlib/utils.py +++ b/infer/lib/python/inferlib/utils.py @@ -128,6 +128,20 @@ def run_command(cmd, debug_mode, infer_out, message, env=os.environ): raise e +def dump_json_to_path( + data, path, + skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, + cls=None, + indent=2, # customized + separators=None, + encoding=config.LOCALE, # customized + default=None, sort_keys=False, **kw): + with codecs.open(path, 'w', encoding=config.LOCALE) as file_out: + json.dump(data, file_out, + skipkeys, ensure_ascii, check_circular, allow_nan, cls, + indent, separators, encoding, default, sort_keys, **kw) + + def merge_json_arrays_from_files(report_paths): # TODO: use streams instead of loading the entire json in memory json_data = []