forked from notepad-plus-plus/userDefinedLanguages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_json.py
103 lines (82 loc) · 2.8 KB
/
validator_json.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
import json
import os
import io
import sys
import requests
from hashlib import sha256
from jsonschema import Draft7Validator, FormatChecker
api_url = os.environ.get('APPVEYOR_API_URL')
has_error = False
def post_error(message):
global has_error
has_error = True
message = {
"message": message,
"category": "error",
"details": ""
}
if api_url:
requests.post(api_url + "api/build/messages", json=message)
else:
from pprint import pprint
pprint(message)
def parse(filename):
try:
schema = json.loads(open("udl.schema").read())
schema = Draft7Validator(schema, format_checker=FormatChecker())
except ValueError as e:
post_error("udl.schema - " + str(e))
return
try:
udlfile = json.loads(open(filename, encoding="utf8").read())
except ValueError as e:
post_error(filename + " - " + str(e))
return
for error in schema.iter_errors(udlfile):
post_error(error.message)
idnames = []
displaynames = []
repositories = []
for udl in udlfile["UDLs"]:
print(udl["display-name"])
try:
if udl["repository"] != "" :
response = requests.get(udl["repository"])
except requests.exceptions.RequestException as e:
post_error(str(e))
continue
if response.status_code != 200:
post_error(f'{udl["display-name"]}: failed to download udl. Returned code {response.status_code}')
continue
# Hash it and make sure its what is expected
#hash = sha256(response.content).hexdigest()
#if udl["id"].lower() != hash.lower():
# post_error(f'{udl["display-name"]}: Invalid hash. Got {hash.lower()} but expected {udl["id"]}')
# continue
#check uniqueness of json id-name, display-name and repository
found = False
for name in displaynames :
if udl["display-name"] == name :
post_error(f'{udl["display-name"]}: non unique display-name entry')
found = True
if found == False:
displaynames.append(udl["display-name"])
found = False
for idname in idnames :
if udl["id-name"] == idname :
post_error(f'{udl["id-name"]}: non unique id-name entry')
found = True
if found == False:
idnames.append(udl["id-name"])
found = False
for repo in repositories :
if udl["repository"] != "" and udl["repository"] == repo :
post_error(f'{udl["repository"]}: non unique repository entry')
found = True
if found == False:
repositories.append(udl["repository"])
parse("udl-list.json")
if has_error:
sys.exit(-2)
else:
sys.exit()