From 7467f815948666241c5ea304d1af7b016f1bb1ac Mon Sep 17 00:00:00 2001 From: Guy Taylor Date: Thu, 4 Apr 2019 18:06:13 +0100 Subject: [PATCH] Sort JSON files to minimise git diffs Sort and format JSON files to remove/minimise any diffs between update-json runs. This also acts as a JSON validator to error when the downloaded JSON file has been truncated. --- Makefile | 1 + etc/bin/sort_json_file.py | 29 +++++++++++++++++++++++++++++ src/mako/deps.mako | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 etc/bin/sort_json_file.py diff --git a/Makefile b/Makefile index 7796411c20..13ef4776d7 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ CODECOV := $(PYTHON) -m codecov MAKO_RENDER := etc/bin/mako-render API_VERSION_GEN := etc/bin/api_version_to_yaml.py +SORT_JSON_FILE := etc/bin/sort_json_file.py TPL := $(PYTHON) $(MAKO_RENDER) MKDOCS := $(shell pwd)/$(VENV_DIR)/bin/mkdocs diff --git a/etc/bin/sort_json_file.py b/etc/bin/sort_json_file.py new file mode 100755 index 0000000000..c9d9429064 --- /dev/null +++ b/etc/bin/sort_json_file.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# Sort JSON file and print in fixed format +# This lowers git diffs when a file has only changed by whitespace. + + +import argparse +import json +import os + + +def main(json_file_path, skip_missing_file): + if not os.path.isfile(json_file_path) and skip_missing_file: + return + + with open(json_file_path, 'r') as fh: + loaded_json = json.load(fh) + + with open(json_file_path, 'w') as fh: + json.dump(loaded_json, fh, indent=4, sort_keys=True) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Sort and format JSON file.') + parser.add_argument('json_file', metavar='FILE', type=str, help='JSON file to sort in place.') + parser.add_argument('--skip-missing-file', default=False, action='store_true', help='Do not fail on missing file.') + + args = parser.parse_args() + main(args.json_file, args.skip_missing_file) diff --git a/src/mako/deps.mako b/src/mako/deps.mako index 1e63217511..b3d5a6dbec 100644 --- a/src/mako/deps.mako +++ b/src/mako/deps.mako @@ -194,9 +194,10 @@ help${agsuffix}: url = info['discoveryRestUrl'].replace("$", "$$") json_api_targets.append(fake_target) %>\ -${fake_target}: +${fake_target}: $(PYTHON_BIN) @mkdir -p ${target_dir} @-curl --silent --show-error --fail --retry 3 -o '${target}' '${url}' + $(PYTHON) $(SORT_JSON_FILE) --skip-missing-file '${target}' % endfor update-json: ${' '.join(json_api_targets)}