Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(run-tests): fix data JSON checker error reporting #3722

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data_dois() {
}

data_json() {
find . -name "*.json" -exec ./scripts/clean_json_file.py --check {} \;
find . -name "*.json" -exec ./scripts/clean_json_file.py --check {} \+
}

data_licenses() {
Expand Down
68 changes: 36 additions & 32 deletions scripts/clean_json_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3

"""Clean JSON file.
"""
Clean JSON files.

Cleans JSON file so as to fix indentation, order fields, and remove trailing
whitespace.
Clean one or more JSON files so as to fix indentation, order fields, and remove
trailing whitespace.
"""

import json
Expand All @@ -13,46 +14,49 @@


@click.command()
@click.argument("filename", type=click.Path(exists=True))
@click.argument(
"filenames", metavar="[FILENAME] ...", type=click.Path(exists=True), nargs=-1
)
@click.option(
"--check",
is_flag=True,
default=False,
help="Don't write the file back, only check whether the file is well formatted.",
)
def clean_json_file(filename, check): # noqa: D301
"""Clean JSON file.

Clean JSON file so as to fix indentation, order fields, and remove trailing
whitespace.
def clean_json_file(filenames, check): # noqa: D301
"""
Clean JSON files.

\b
:param filename: The file name to be reformatted.
:param check: Do not modify file, only check whether it is well formatted.
:type filename: str
:type check: bool
Clean one or more JSON files so as to fix indentation, order fields, and
remove trailing whitespace.
"""
with open(filename, "r") as fdesc:
old_content = fdesc.read()
records = json.loads(old_content)
new_content = (
json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
problematic_files = []
for filename in list(set(filenames)):
with open(filename, "r") as fdesc:
old_content = fdesc.read()
records = json.loads(old_content)
new_content = (
json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
)
+ "\n"
)
+ "\n"
)

if old_content != new_content:
if check:
if old_content != new_content:
if check:
problematic_files.append(filename)
else:
with open(filename, "w") as fdesc:
fdesc.write(new_content)

if check and problematic_files:
for filename in problematic_files:
print(f"[ERROR] File {filename} is badly formatted.")
sys.exit(1)
else:
with open(filename, "w") as fdesc:
fdesc.write(new_content)
sys.exit(1)


if __name__ == "__main__":
Expand Down