Created
April 1, 2020 10:33
-
-
Save kofemann/877cb60126b2b8d0ee80d8124c001eef to your computer and use it in GitHub Desktop.
Validate Storage resource record as defined by WLCG Storage Space accounting project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
import jsonschema | |
import urllib.request | |
import urllib.parse | |
import sys | |
import ssl | |
def load(path): | |
url = urllib.parse.urlparse(path) | |
if url.scheme == '': | |
with open(path) as f: | |
return json.loads(f.read()) | |
elif url.scheme == 'http': | |
response = urllib.request.urlopen(path).read() | |
return json.loads(response) | |
elif url.scheme == 'https': | |
context = ssl.create_default_context() | |
response = urllib.request.urlopen(path, context = context).read() | |
return json.loads(response) | |
else: | |
raise Exception("Invalid url: " + path) | |
def main(): | |
if len(sys.argv) != 3: | |
print("Usage: %s <schema> <url>") | |
print("") | |
print(" Example:") | |
print(" $ srr_validate.py srrschema_4.2.json http://localhost:3880/api/v1/srr ") | |
sys.exit(1) | |
schema = load(sys.argv[1]) | |
data = load(sys.argv[2]) | |
try: | |
jsonschema.validate(data, schema) | |
except jsonschema.ValidationError as e: | |
print ("%s: %s" % (' -> '.join( map(lambda x: str(x), e.path)) , e.message), file=sys.stderr) | |
sys.exit(2) | |
except jsonschema.SchemaError as e: | |
print (e, file=sys.stderr) | |
sys.exit(2) | |
if __name__ == "__main__" : | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment