Bob King | 5cc0128 | 2019-12-17 18:11:57 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import sys |
| 6 | import jsonschema |
| 7 | |
| 8 | r""" |
| 9 | Validates the phosphor-regulators configuration file. Checks it against a JSON |
| 10 | schema as well as doing some extra checks that can't be encoded in the schema. |
| 11 | """ |
| 12 | |
| 13 | def 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 | |
| 34 | if __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) |