-
Notifications
You must be signed in to change notification settings - Fork 49
/
validate.py
180 lines (163 loc) · 6.55 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
import argparse
import json
import jsonschema
import os
from PIL import Image
import sys
parser = argparse.ArgumentParser(description="Validates scripts")
parser.add_argument('--scripts_dir',
help="scripts directory (default: working directory)",
default=os.getcwd())
args = parser.parse_args()
scripts_dir = args.scripts_dir
is_valid = True
id_pattern = '^[a-z0-9_.]+$'
# get type directories
type_dirs = []
for type_dir in next(os.walk(scripts_dir))[1]:
if os.path.isfile(scripts_dir + '/' + type_dir + '/type.json'):
type_dirs.append(scripts_dir + '/' + type_dir)
# validate type.json
type_schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'id': {'type': 'string', 'pattern': id_pattern},
},
'required': ['name', 'id']
}
for type_dir in type_dirs:
type_json_file = type_dir + '/type.json'
try:
with open(type_json_file) as f:
type_json = json.loads(f.read())
jsonschema.validate(type_json, type_schema)
except ValueError as value_error:
print("invalid type.json {}: {}".format(type_json_file, value_error))
is_valid = False
except jsonschema.exceptions.ValidationError as validation_error:
print("invalid type.json {}: {}".format(type_json_file, validation_error))
is_valid = False
# get category directories
category_dirs = []
for type_dir in type_dirs:
for category in next(os.walk(type_dir))[1]:
if os.path.isfile(type_dir + '/' + category + '/category.json'):
category_dirs.append(type_dir + '/' + category)
# validate category.json
category_schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'id': {'type': 'string', 'pattern': id_pattern},
'type': {'enum': ['FUNCTIONS', 'INSTALLERS']},
},
'required': ['name', 'id', 'type']
}
for category_dir in category_dirs:
category_json_file = category_dir + '/category.json'
try:
with open(category_json_file) as f:
category_json = json.loads(f.read())
jsonschema.validate(category_json, category_schema)
except ValueError as value_error:
print("invalid category.json {}: {}".format(category_json_file, value_error))
is_valid = False
except jsonschema.exceptions.ValidationError as validation_error:
print("invalid category.json {}: {}".format(category_json_file, validation_error))
is_valid = False
# get application directories
application_dirs = []
for category_dir in category_dirs:
for application in next(os.walk(category_dir))[1]:
if os.path.isfile(category_dir + '/' + application + '/application.json'):
application_dirs.append(category_dir + '/' + application)
# validate application.json
application_schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'id': {'type': 'string', 'pattern': id_pattern},
'description': {'type': 'string'},
},
'required': ['name', 'id', 'description']
}
for application_dir in application_dirs:
application_json_file = application_dir + '/application.json'
try:
with open(application_json_file) as f:
application_json = json.loads(f.read())
jsonschema.validate(application_json, application_schema)
except ValueError as value_error:
print("invalid application.json {}: {}".format(application_json_file, value_error))
is_valid = False
except jsonschema.exceptions.ValidationError as validation_error:
print("invalid application.json {}: {}".format(application_json_file, validation_error))
is_valid = False
# get script directories
script_dirs = []
for application_dir in application_dirs:
script_dirs_for_app = []
for script in next(os.walk(application_dir))[1]:
if os.path.isfile(application_dir + '/' + script + '/script.json') \
and os.path.isfile(application_dir + '/' + script + '/script.js'):
script_dirs_for_app.append(application_dir + '/' + script)
if script_dirs_for_app:
script_dirs.extend(script_dirs_for_app)
else:
print("application {} must contain at least one script directory with 'script.js' and 'script.json'".format(
application_dir))
is_valid = False
# validate script.json
script_schema = {
'type': 'object',
'properties': {
'scriptName': {'type': 'string'},
'id': {'type': 'string', 'pattern': id_pattern},
'compatibleOperatingSystems': {
'type': 'array',
'items': {'enum': ['LINUX', 'MACOSX']},
'maxItems': 2
},
'testingOperatingSystems': {
'type': 'array',
'items': {'enum': ['LINUX', 'MACOSX']},
'maxItems': 2
},
'free': {'type': 'boolean'},
'requiresPatch': {'type': 'boolean'},
},
'required': ['scriptName', 'id', 'compatibleOperatingSystems', 'testingOperatingSystems', 'free', 'requiresPatch']
}
for script_dir in script_dirs:
script_json_file = script_dir + '/script.json'
try:
with open(script_json_file) as f:
script_json = json.loads(f.read())
jsonschema.validate(script_json, script_schema)
# check that testingOperatingSystems is a subset of compatibleOperatingSystems
if not set(script_json['testingOperatingSystems']) <= set(script_json['compatibleOperatingSystems']):
print("{}: testingOperatingSystems must be a subset of compatibleOperatingSystems".format(script_json_file))
is_valid = False
except ValueError as value_error:
print("invalid script.json {}: {}".format(script_json_file, value_error))
is_valid = False
except jsonschema.exceptions.ValidationError as validation_error:
print("invalid script.json {}: {}".format(script_json_file, validation_error))
is_valid = False
# check miniature
for application_dir in application_dirs:
# do not check miniature for verbs etc.
if "Applications" in application_dir:
main_miniature_file = application_dir + '/miniatures/main.png'
if os.path.isfile(main_miniature_file):
image = Image.open(main_miniature_file)
width, height = image.size
if width != 400 or height != 300:
print("main.png {} must be 400x300px".format(main_miniature_file))
is_valid = False
else:
print("missing main.png: {}".format(main_miniature_file))
is_valid = False
sys.exit(0 if is_valid else 1)