forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.py
More file actions
executable file
·97 lines (77 loc) · 2.36 KB
/
validate_test.py
File metadata and controls
executable file
·97 lines (77 loc) · 2.36 KB
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
#! /usr/bin/python
from jsonschema import Draft4Validator, validators
# Make the validator fill in defaults from the schema
# Fetched from:
# http://python-jsonschema.readthedocs.io/en/latest/faq/
def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
for property, subschema in properties.iteritems():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
for error in validate_properties(
validator, properties, instance, schema,
):
yield error
return validators.extend(
validator_class, {"properties" : set_defaults},
)
import jsonschema
import json
import sys
import os
import glob
vm_schema = None
jsons = []
valid_vms = []
verbose = False
validator = extend_with_default(Draft4Validator)
def load_schema(filename):
global vm_schema
vm_schema = json.loads(open(filename).read());
def validate_vm_spec(filename):
global valid_vms
vm_spec = None
# Load and parse as JSON
try:
vm_spec = json.loads(open(filename).read())
except:
raise Exception("JSON load / parse Error for " + filename)
# Validate JSON according to schema
try:
validator(vm_schema).validate(vm_spec)
except Exception as err:
raise Exception("JSON schema validation failed: " + err.message)
valid_vms.append(vm_spec)
def has_required_stuff(path):
global jsons
# Certain files are mandatory
required_files = [ "Makefile", "test.py", "README.md", "*.cpp" ]
for file in required_files:
if not glob.glob(file):
raise Exception("missing " + file)
# JSON-files must conform to VM-schema
jsons = glob.glob("*.json")
jsons.sort()
for json in jsons:
validate_vm_spec(json)
def validate_path(path, verb = False):
global verbose
verbose = verb
current_dir = os.getcwd()
if not vm_schema:
load_schema("vm.schema.json")
os.chdir(path)
try:
has_required_stuff(path)
if verbose:
print "<validate_test> \tPASS: ",os.getcwd()
return True
except Exception as err:
if verbose:
print "<validate_test> \tFAIL: unmet requirements in " + path, ": " , err.message
finally:
os.chdir(current_dir)
if __name__ == "__main__":
path = sys.argv[1] if len(sys.argv) > 1 else "."
validate_path(path)