-
Notifications
You must be signed in to change notification settings - Fork 14
/
validate-all-tools.py
executable file
·106 lines (93 loc) · 3.34 KB
/
validate-all-tools.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
import sys
from os import listdir
from os.path import isfile, join
import getopt
import json
import fastjsonschema
HELP = """Usage: validate-all-tools.py -s <SCHEMA_DEFINITION_FILE1> -t <TOOLS_DIRECTORY>"""
SCHEMA_DEFINITION_FILES = []
TOOLS_DIRECTORY = None
def main(argv):
load_arguments(argv)
validate_tools()
def load_arguments(argv):
try:
opts, args = getopt.getopt(argv,"hs:t:")
except getopt.GetoptError:
print(HELP)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(HELP)
sys.exit()
elif opt in ("-s"):
global SCHEMA_DEFINITION_FILES
SCHEMA_DEFINITION_FILES.append(arg)
elif opt in ("-t"):
global TOOLS_DIRECTORY
TOOLS_DIRECTORY = arg
if not SCHEMA_DEFINITION_FILES:
print("Missing SCHEMA_DEFINITION_FILE argument", file=sys.stderr)
print(HELP)
sys.exit(2)
if not TOOLS_DIRECTORY:
print("Missing TOOLS_DIRECTORY argument", file=sys.stderr)
print(HELP)
sys.exit(2)
def check_tool_id(tool_ids, failures, tool):
tool_name = tool.get("name")
id = tool.get("id")
if id is None:
failures.append(f"Null ID in tool: {tool_name}")
elif type(id) != int:
failures.append(f"Non-int tool id `{id}` in `{tool_name}`")
elif id <= 0:
failures.append(f"Negative or 0 id `{id}` in `{tool_name}`")
else:
if id in tool_ids:
failures.append(f"Duplicate id `{id}` in `{tool_name}`")
tool_ids.add(id)
def validate_tools():
validate_fns = []
for schema in SCHEMA_DEFINITION_FILES:
print("Reading schema", schema)
try:
with open(schema) as schema_file:
json_data = json.loads(schema_file.read())
validate = fastjsonschema.compile(json_data)
validate_fns.append(validate)
except json.JSONDecodeError as xc:
print("! Error in json schema, file {}:\n\t{}\n".format(schema, xc))
sys.exit(2)
except fastjsonschema.JsonSchemaDefinitionException as xc:
print("! Error in json schema, file {}:\n\t{}\n".format(schema, xc))
sys.exit(2)
tool_dir_entries = [join(TOOLS_DIRECTORY, f) for f in listdir(TOOLS_DIRECTORY)]
tool_files = [f for f in tool_dir_entries if isfile(f)]
failed = False
tool_ids = set()
for filepath in tool_files:
print("Validating", filepath)
failures = []
with open(filepath) as file:
try:
json_data = json.loads(file.read())
except json.JSONDecodeError as xc:
print("! JSON decoding error: file {}\n\t{}".format(filepath, xc))
sys.exit(2)
check_tool_id(tool_ids, failures, json_data)
for validate in validate_fns:
try:
validate(json_data)
except fastjsonschema.JsonSchemaException as xc:
failures.append(xc)
if len(failures) == len(validate_fns):
print("! Error in json file {}:\n".format(filepath))
for i, xc in enumerate(failures):
print(" --- failed to match schema {}: {}\n".format(i, xc))
failed = True
if failed:
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])