X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=yaml-verify-schema.py;fp=yaml-verify-schema.py;h=3a0d946e5e6b469a624bed6d8db0f964e0c135fd;hb=b83660faf15e9b6983d5d6baa031ecec710140b8;hp=0000000000000000000000000000000000000000;hpb=27090106c7bb23ff93bda6bb9798fe3b94686124;p=releng%2Fglobal-jjb.git diff --git a/yaml-verify-schema.py b/yaml-verify-schema.py new file mode 100644 index 00000000..3a0d946e --- /dev/null +++ b/yaml-verify-schema.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +""" +Verify YAML Schema +""" +import argparse +import logging +import jsonschema +import yaml + +LOADER = yaml.CSafeLoader if yaml.__with_libyaml__ else yaml.SafeLoader + + +def main(): + """Parse arguments and verify YAML""" + logging.basicConfig(level=logging.INFO) + + parser = argparse.ArgumentParser() + parser.add_argument('--yaml', '-y', type=str, required=True) + parser.add_argument('--schema', '-s', type=str, required=True) + + args = parser.parse_args() + + with open(args.yaml) as _: + yaml_file = yaml.load(_, Loader=LOADER) + + with open(args.schema) as _: + schema_file = yaml.load(_, Loader=LOADER) + + validation = jsonschema.Draft4Validator( + schema_file, + format_checker=jsonschema.FormatChecker() + ) + + errors = 0 + for error in validation.iter_errors(yaml_file): + errors += 1 + logging.error(error) + if errors > 0: + raise RuntimeError("%d issues invalidate the schema" % errors) + + +if __name__ == "__main__": + main()