mirror of
https://gitlab.kitware.com/cmake/cmake.git
synced 2026-09-25 04:09:36 +03:00
As the number of areas in CMake for which we've provided JSON schemas has expanded, testing infrastructure has mostly been copy-pasted across the relevant test suites. Unify it to a single reusable validator script and CMake helper.
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
import argparse
|
|
import json
|
|
import pathlib
|
|
import sys
|
|
|
|
import jsonschema
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(description="Validate CMake JSON schemas")
|
|
parser.add_argument(
|
|
"schema",
|
|
type=pathlib.Path,
|
|
help="the path to the schema file",
|
|
)
|
|
parser.add_argument(
|
|
"--input-files",
|
|
nargs="+",
|
|
type=pathlib.Path,
|
|
help="the path to an input file to validate",
|
|
)
|
|
parser.add_argument(
|
|
"-v",
|
|
"--verbose",
|
|
action="store_true",
|
|
help="enable verbose output",
|
|
)
|
|
args = parser.parse_args(argv)
|
|
|
|
with open(args.schema, "r", encoding="utf-8") as f:
|
|
schema = json.load(f)
|
|
|
|
for file in args.input_files:
|
|
try:
|
|
with open(file, "r", encoding="utf-8-sig") as f:
|
|
contents = json.load(f)
|
|
jsonschema.validate(contents, schema)
|
|
except jsonschema.ValidationError as e:
|
|
message = str(e) if args.verbose else str(e.message)
|
|
print(f"Failed to validate file {file}: {message}", file=sys.stderr)
|
|
return 1
|
|
except jsonschema.SchemaError as e:
|
|
message = str(e) if args.verbose else str(e.message)
|
|
print(f"Failed to validate schema: {message}", file=sys.stderr)
|
|
return 2
|
|
except Exception as e:
|
|
print(f"Unknown exception: {e}", file=sys.stderr)
|
|
return 3
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv[1:]))
|