From a7c29e2a95cd0b6a42be8db78e55291b34f1bbbd Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Thu, 8 Feb 2024 19:07:15 +0100 Subject: [PATCH] CI: Only use json5 Python package for validation This simplifies the script and makes the output more readable --- .github/workflows/github.yml | 2 +- CI/linux-qt6/validate_json.py | 53 +++++++---------------------------- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/.github/workflows/github.yml b/.github/workflows/github.yml index 7fa7adadd..c51cb8f7c 100644 --- a/.github/workflows/github.yml +++ b/.github/workflows/github.yml @@ -124,7 +124,7 @@ jobs: # also, running it on multiple presets is redundant and slightly increases already long CI built times if: ${{ startsWith(matrix.preset, 'linux-clang-test') }} run: | - pip3 install json5 jstyleson + pip3 install json5 python3 CI/linux-qt6/validate_json.py - name: Dependencies diff --git a/CI/linux-qt6/validate_json.py b/CI/linux-qt6/validate_json.py index 5fbe0ac76..886eb8e7d 100755 --- a/CI/linux-qt6/validate_json.py +++ b/CI/linux-qt6/validate_json.py @@ -6,55 +6,22 @@ from pathlib import Path from pprint import pprint import json5 -import jstyleson -import yaml -# 'json', 'json5' or 'yaml' -# json: strict, but doesn't preserve line numbers necessarily, since it strips comments before parsing -# json5: strict and preserves line numbers even for files with line comments -# yaml: less strict, allows e.g. leading zeros -VALIDATION_TYPE = "json5" +validation_failed = False -errors = [] -for path in sorted(Path(".").glob("**/*.json")): +for path in sorted(Path(".").glob("**/*.json"), key=lambda path: str(path).lower()): # because path is an object and not a string path_str = str(path) + if path_str.startswith("."): + continue + try: with open(path_str, "r") as file: - if VALIDATION_TYPE == "json": - jstyleson.load(file) - elif VALIDATION_TYPE == "json5": - json5.load(file) - elif VALIDATION_TYPE == "yaml": - file = file.read().replace("\t", " ") - file = file.replace("//", "#") - yaml.safe_load(file) - print(f"Validation of {path_str} succeeded") + json5.load(file) + print(f"✅ {path_str}") except Exception as exc: - print(f"Validation of {path_str} failed") - pprint(exc) + print(f"❌ {str(exc).replace('', path_str)}") + validation_failed = True - error_pos = path_str - - # create error position strings for each type of parser - if hasattr(exc, "pos"): - # 'json' - # https://stackoverflow.com/a/72850269/2278742 - error_pos = f"{path_str}:{exc.lineno}:{exc.colno}" - print(error_pos) - elif VALIDATION_TYPE == "json5": - # 'json5' - pos = re.findall(r"\d+", str(exc)) - error_pos = f"{path_str}:{pos[0]}:{pos[-1]}" - elif hasattr(exc, "problem_mark"): - # 'yaml' - mark = exc.problem_mark - error_pos = f"{path_str}:{mark.line+1}:{mark.column+1}" - print(error_pos) - - errors.append({"error_pos": error_pos, "error_msg": exc}) - -if errors: - print("The following JSON files are invalid:") - pprint(errors) +if validation_failed: sys.exit(1)