From 52640eb2053e155c7f33468c0e6c74512b062b0b Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 21 Sep 2015 13:02:37 +0100 Subject: [PATCH 1/3] Add a python script for checking that the examples match the event schema. Does the same checks as check.sh, but is a *lot* faster making it suitable for using as a pre-commit hook. I don't suggest replacing check.sh since it's good to check that the schema works with multiple implementations of jsonschema. --- event-schemas/check_examples.py | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 event-schemas/check_examples.py diff --git a/event-schemas/check_examples.py b/event-schemas/check_examples.py new file mode 100755 index 00000000..5fc08f4a --- /dev/null +++ b/event-schemas/check_examples.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python + +import sys +import json +import os + + +def import_error(module, package, debian, error): + sys.stderr.write(( + "Error importing %(module)s: %(error)r\n" + "To install %(module)s run:\n" + " pip install %(package)s\n" + "or on Debian run:\n" + " sudo apt-get install python-%(debian)s\n" + ) % locals()) + if __name__ == '__main__': + sys.exit(1) + +try: + import jsonschema +except ImportError as e: + import_error("jsonschema", "jsonschema", "jsonschema", e) + raise + +try: + import yaml +except ImportError as e: + import_error("yaml", "PyYAML", "yaml", e) + raise + + +def check_example_file(examplepath, schemapath): + with open(examplepath) as f: + example = yaml.load(f) + + with open(schemapath) as f: + schema = yaml.load(f) + + fileurl = "file://" + os.path.abspath(schemapath) + + print ("Checking schema for: %r %r" % (examplepath, schemapath)) + # Setting the 'id' tells jsonschema where the file is so that it + # can correctly resolve relative $ref references in the schema + schema['id'] = fileurl + try: + jsonschema.validate(example, schema) + except: + raise ValueError("Error validating JSON schema for %r %r" % ( + examplepath, schemapath + ), e) + + +def check_example_dir(exampledir, schemadir): + for root, dirs, files in os.walk(exampledir): + for filename in files: + examplepath = os.path.join(root, filename) + schemapath = examplepath.replace(exampledir, schemadir) + check_example_file(examplepath, schemapath) + + +if __name__ == '__main__': + check_example_dir("examples", "schema") From 6ba9b29b3b4ffe9d19c30302e36813b1835eb117 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 21 Sep 2015 15:04:03 +0100 Subject: [PATCH 2/3] Report all the errors in schemas/check_examples, not just the first error. --- event-schemas/check_examples.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/event-schemas/check_examples.py b/event-schemas/check_examples.py index 5fc08f4a..8675396e 100755 --- a/event-schemas/check_examples.py +++ b/event-schemas/check_examples.py @@ -3,6 +3,7 @@ import sys import json import os +import traceback def import_error(module, package, debian, error): @@ -44,19 +45,29 @@ def check_example_file(examplepath, schemapath): schema['id'] = fileurl try: jsonschema.validate(example, schema) - except: + except Exception as e: raise ValueError("Error validating JSON schema for %r %r" % ( examplepath, schemapath ), e) def check_example_dir(exampledir, schemadir): + errors = [] for root, dirs, files in os.walk(exampledir): for filename in files: examplepath = os.path.join(root, filename) schemapath = examplepath.replace(exampledir, schemadir) - check_example_file(examplepath, schemapath) - + try: + check_example_file(examplepath, schemapath) + except Exception as e: + errors.append(sys.exc_info()) + for (exc_type, exc_value, exc_trace) in errors: + traceback.print_exception(exc_type, exc_value, exc_trace) + if errors: + raise ValueError("Error validating examples") if __name__ == '__main__': - check_example_dir("examples", "schema") + try: + check_example_dir("examples", "schema") + except: + sys.exit(1) From 8974b2b67bbc2acf0c482c646be6b07b230a1a15 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 21 Sep 2015 15:05:10 +0100 Subject: [PATCH 3/3] Skip files that start with ".", e.g. vim swp files. --- event-schemas/check_examples.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/event-schemas/check_examples.py b/event-schemas/check_examples.py index 8675396e..e54d3a1c 100755 --- a/event-schemas/check_examples.py +++ b/event-schemas/check_examples.py @@ -55,6 +55,9 @@ def check_example_dir(exampledir, schemadir): errors = [] for root, dirs, files in os.walk(exampledir): for filename in files: + if filename.startswith("."): + # Skip over any vim .swp files. + continue examplepath = os.path.join(root, filename) schemapath = examplepath.replace(exampledir, schemadir) try: