-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
validate.py
54 lines (41 loc) · 1.02 KB
/
validate.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
#!/usr/bin/env python
from __future__ import print_function
from jsoncomment import JsonComment
from jsonschema import validate
import json
import os
import pprint
import re
import sys
import traceback
def decode(s):
for encoding in 'utf-8-sig', 'utf-16':
try:
return s.decode(encoding)
except UnicodeDecodeError:
continue
return s.decode('latin-1')
schema_name = 'schema.json'
file = sys.argv[1]
if re.match('^schema', file):
sys.exit(0)
print('Validating', file)
with open(schema_name, 'rb') as f:
schema_data = json.load(f)
with open(file, 'rb') as f:
jstr = f.read(os.path.getsize(file))
jstr_no_bom = decode(jstr)
failed = file + '.failed'
if os.path.exists(failed):
os.remove(failed)
try:
parser = JsonComment(json)
json_data = parser.loads(jstr_no_bom)
validate(json_data, schema_data)
except Exception as e:
trace = traceback.format_exc()
print(trace)
with open(failed, 'ab+') as f:
f.write(trace)
sys.exit(1)
sys.exit(0)