-
Notifications
You must be signed in to change notification settings - Fork 21
/
validate-schema
executable file
·117 lines (107 loc) · 4.53 KB
/
validate-schema
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
#!/usr/bin/env python3
import argparse
import json
import os.path
import urllib
from urlparse import urlparse, urlunparse, ParseResult
import jsonschema
# Adapted from slapos.core.git/slapos/slap/util.py
from lxml import etree
def xml2dict(infile):
result_dict = {}
for element in etree.parse(infile).iter(tag=etree.Element):
if element.tag == 'parameter':
key = element.get('id')
value = result_dict.get(key, None)
if value is not None:
value = value + ' ' + element.text
else:
value = element.text
result_dict[key] = value
return result_dict
def jsonInXML2dict(infile):
wrapped = xml2dict(infile)
if '_' in wrapped:
return json.loads(wrapped['_'])
return wrapped
class ValidatingRefResolver(jsonschema.RefResolver):
def resolve_remote(self, *args, **kw):
result = super(ValidatingRefResolver, self).resolve_remote(*args, **kw)
jsonschema.Draft4Validator.check_schema(result)
return result
DEFAULT_SLAPOS_SCHEMA_PATH = os.path.join(
os.path.dirname(__file__),
'schema.json',
)
def main():
parser = argparse.ArgumentParser(
description='Validates SlapOS Software Release descriptor schemas, '
'and optionally validates specific requests and/or responses with '
'these schemas.',
)
parser.add_argument('--software-type', type=str, help='Software type given request/response is intended for')
parser.add_argument('--request', type=argparse.FileType('r'), help='File containing request parameters')
parser.add_argument('--response', type=argparse.FileType('r'), help='File containing published values')
parser.add_argument('--slapos-schema', type=argparse.FileType('r'), help='SlapOS base schema. Default: %s' % (DEFAULT_SLAPOS_SCHEMA_PATH, ))
parser.add_argument('software_release', nargs=1, help='URL of the software release')
args = parser.parse_args()
if args.software_type is None and (
args.request is not None or
args.response is not None
):
parser.error(
'--software-type is required if --request or --response is provided',
)
slapos_schema_file = args.slapos_schema
if slapos_schema_file is None:
slapos_schema_file = open(DEFAULT_SLAPOS_SCHEMA_PATH)
slapos_schema = json.load(slapos_schema_file)
jsonschema.Draft4Validator.check_schema(slapos_schema)
software_release_url = urlparse(args.software_release[0])
if software_release_url.scheme == software_release_url.netloc == '':
# Assume software_release_url.path is a relative path for "file" scheme
software_release_url = ParseResult(
'file',
'',
os.path.abspath(software_release_url.path),
software_release_url.params,
software_release_url.query,
software_release_url.fragment,
)
software_release_base_path, software_release_file = software_release_url.path.rsplit('/', 1)
def loadurl(relative_url):
url = urlunparse((
software_release_url.scheme,
software_release_url.netloc,
software_release_base_path + '/' + relative_url,
software_release_url.params,
software_release_url.query,
software_release_url.fragment,
))
return url, json.loads(urllib.urlopen(url).read())
_, software_release_descriptor = loadurl(software_release_file + '.json')
jsonschema.Draft4Validator.check_schema(software_release_descriptor)
jsonschema.validate(software_release_descriptor, slapos_schema)
load = {
'xml': xml2dict,
'json-in-xml': jsonInXML2dict,
}[software_release_descriptor['serialisation']]
for software_type_name, software_type in software_release_descriptor['software-type'].iteritems():
for key in ('request', 'response'):
relative_url = software_type[key]
url, software_release_schema = loadurl(relative_url)
jsonschema.Draft4Validator.check_schema(software_release_schema)
validator = jsonschema.Draft4Validator(
schema=software_release_schema,
resolver=ValidatingRefResolver(
base_uri=url,
referrer=software_release_schema,
),
)
infile = getattr(args, key)
if infile is not None and software_type_name == args.software_type:
validator.validate(load(infile))
else:
validator.validate({})
if __name__ == '__main__':
main()