1#!/usr/bin/env python
2
3import argparse
4import json
5import sys
6import jsonschema
7
8r"""
9Validates the phosphor-regulators configuration file. Checks it against a JSON
10schema as well as doing some extra checks that can't be encoded in the schema.
11"""
12
13def validate_schema(config, schema):
14    r"""
15    Validates the specified config file using the specified
16    schema file.
17
18    config:   Path of the file containing the config JSON
19    schema:   Path of the file containing the schema JSON
20    """
21
22    with open(config) as config_handle:
23        config_json = json.load(config_handle)
24
25        with open(schema) as schema_handle:
26            schema_json = json.load(schema_handle)
27
28            try:
29                jsonschema.validate(config_json, schema_json)
30            except jsonschema.ValidationError as e:
31                print(e)
32                sys.exit("Validation failed.")
33
34if __name__ == '__main__':
35
36    parser = argparse.ArgumentParser(
37        description='phosphor-regulators configuration file validator')
38
39    parser.add_argument('-s', '--schema-file', dest='schema_file',
40                        help='The phosphor-regulators schema file')
41
42    parser.add_argument('-c', '--configuration-file', dest='configuration_file',
43                        help='The phosphor-regulators configuration file')
44
45    args = parser.parse_args()
46
47    if not args.schema_file:
48        parser.print_help()
49        sys.exit("Error: Schema file is required.")
50    if not args.configuration_file:
51        parser.print_help()
52        sys.exit("Error: Configuration file is required.")
53
54    validate_schema(args.configuration_file, args.schema_file)
55