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.
This commit is contained in:
Guy Taylor
2019-04-04 18:06:13 +01:00
committed by Sebastian Thiel
parent 93dba234dd
commit 7467f81594
3 changed files with 32 additions and 1 deletions

View File

@@ -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

29
etc/bin/sort_json_file.py Executable file
View File

@@ -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)

View File

@@ -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)}