aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Finucane <stephen@that.guru>2020-04-18 11:49:38 +0100
committerStephen Finucane <stephen@that.guru>2020-04-18 11:57:28 +0100
commit167079a5335b36a60b6cf02e57e69c24216103f3 (patch)
tree72d42ebd2c426439f743320d79427b9dd2edd150
parent67e37e3f914be429f8c72c665525ba21e73cf7a9 (diff)
downloadpatchwork-167079a5335b36a60b6cf02e57e69c24216103f3.tar
patchwork-167079a5335b36a60b6cf02e57e69c24216103f3.tar.gz
docs: Add schema validation to 'generate-schemas' tool
Just to make sure we're not generating garbage. Signed-off-by: Stephen Finucane <stephen@that.guru>
-rwxr-xr-xdocs/api/schemas/generate-schemas.py56
1 files changed, 54 insertions, 2 deletions
diff --git a/docs/api/schemas/generate-schemas.py b/docs/api/schemas/generate-schemas.py
index 09e6c44..a0c1e45 100755
--- a/docs/api/schemas/generate-schemas.py
+++ b/docs/api/schemas/generate-schemas.py
@@ -1,15 +1,24 @@
#!/usr/bin/env python3
+import argparse
import os
+import sys
import jinja2
+try:
+ import openapi_spec_validator
+ import yaml
+except ImportError:
+ openapi_spec_validator = None
+ yaml = None
+
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
VERSIONS = [(1, 0), (1, 1), (1, 2), None]
LATEST_VERSION = (1, 2)
-def generate_schema():
+def generate_schemas():
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(ROOT_DIR),
trim_blocks=True,
@@ -32,6 +41,49 @@ def generate_schema():
version_url=version_url).dump(fh, encoding='utf-8')
fh.write(b'\n')
+ print(f'Schemas written to {ROOT_DIR}.')
+
+
+def validate_schemas():
+ for version in VERSIONS:
+ schema = os.path.join(
+ ROOT_DIR,
+ 'v%d.%d' % version if version else 'latest',
+ 'patchwork.yaml',
+ )
+
+ with open(schema) as fh:
+ spec = yaml.safe_load(fh.read())
+ openapi_spec_validator.validate_spec(spec)
+
+ print('Validation successful.')
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description='Generate schemas from the schema template.',
+ )
+ parser.add_argument(
+ '--validate',
+ action='store_true',
+ help='validate the generated schemas. Requires the openapi-validator '
+ 'package.',
+ )
+ args = parser.parse_args()
+
+ if args.validate and openapi_spec_validator is None:
+ print(
+ '\033[1m\033[91mERROR:\033[0m Validation requires the '
+ 'openapi-validator and yaml packages',
+ file=sys.stderr,
+ )
+ sys.exit(1)
+
+ generate_schemas()
+
+ if args.validate:
+ validate_schemas()
+
if __name__ == '__main__':
- generate_schema()
+ main()