Chore: Upgrade Jenkins-job-builder to 6.2.0
[releng/global-jjb.git] / yaml-verify-schema.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 """
12 Verify YAML Schema
13 """
14 import argparse
15 import logging
16 import jsonschema
17 import yaml
18
19 LOADER = yaml.CSafeLoader if yaml.__with_libyaml__ else yaml.SafeLoader
20
21
22 def main():
23     """Parse arguments and verify YAML"""
24     logging.basicConfig(level=logging.INFO)
25
26     parser = argparse.ArgumentParser()
27     parser.add_argument('--yaml', '-y', type=str, required=True)
28     parser.add_argument('--schema', '-s', type=str, required=True)
29
30     args = parser.parse_args()
31
32     with open(args.yaml) as _:
33         yaml_file = yaml.load(_, Loader=LOADER)
34
35     with open(args.schema) as _:
36         schema_file = yaml.load(_, Loader=LOADER)
37
38     validation = jsonschema.Draft4Validator(
39         schema_file,
40         format_checker=jsonschema.FormatChecker()
41     )
42
43     errors = 0
44     for error in validation.iter_errors(yaml_file):
45         errors += 1
46         logging.error(error)
47     if errors > 0:
48         raise RuntimeError("%d issues invalidate the schema" % errors)
49
50
51 if __name__ == "__main__":
52     main()