-
Notifications
You must be signed in to change notification settings - Fork 838
/
Copy pathvalidate_testplans.py
executable file
·79 lines (66 loc) · 2.33 KB
/
validate_testplans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
"""
This script validates the various opentitan testplan.hjson files.
Usage:
./util/validate_testplans.py --schema hw/lint/sival_testplan_schema.hjson \
--dir hw/top_earlgrey/data/
./util/validate_testplans.py --schema hw/lint/sival_testplan_schema.hjson \
--dir hw/top_earlgrey/data/ip/
"""
import argparse
import glob
import logging
import sys
import pathlib
import hjson
import jsonschema
def main(args: argparse.Namespace) -> int:
files = [f for f in glob.glob(f"{args.dir}/*.hjson")]
assert args.schema
return SchemaValidator(args.schema).check(files)
class SchemaValidator:
def __init__(self, schema_file: str):
self.schema = hjson.load(open(schema_file, "r", encoding="utf-8"))
def check(self, files: str) -> int:
res: int = 0
for f in files:
try:
testplan = hjson.load(open(f, "r", encoding="utf-8"))
jsonschema.validate(testplan, schema=self.schema)
except jsonschema.ValidationError as e:
logging.info("Validation failed on file %s: %s", f, e)
res = -1
except hjson.scanner.HjsonDecodeError as e:
logging.info("Failed to decode file %s: %s", f, e)
res = -1
break
if res == 0:
logging.info("No errors found!")
else:
logging.info("Errors found - run validation locally with %s", " ".join(sys.argv))
return res
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--logging",
default="info",
choices=["debug", "info", "warning", "error", "critical"],
help="Logging level",
)
parser.add_argument(
"--dir",
required=True,
help="A directory containing testplan.hjson files.",
)
parser.add_argument(
"--schema",
required=True,
type=pathlib.Path,
help="A hjson file with the validation rules to be used.",
)
args = parser.parse_args()
logging.basicConfig(level=args.logging.upper())
sys.exit(main(args))