mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-02-23 15:49:49 +01:00
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:
committed by
Sebastian Thiel
parent
93dba234dd
commit
7467f81594
1
Makefile
1
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
|
||||
|
||||
|
||||
29
etc/bin/sort_json_file.py
Executable file
29
etc/bin/sort_json_file.py
Executable 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)
|
||||
@@ -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)}
|
||||
|
||||
Reference in New Issue
Block a user