Add validate-regulators-config.py validation tool.
validate-regulators-config.py is a tool that validates configuration
files for the phosphor-regulators application. See
phosphor-regulators/docs/config_file/README.md for the
command to use the tool.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I41cd4070dbb6ec27e834308a9eb0a819feb77319
diff --git a/phosphor-regulators/tools/validate-regulators-config.py b/phosphor-regulators/tools/validate-regulators-config.py
new file mode 100644
index 0000000..38ed495
--- /dev/null
+++ b/phosphor-regulators/tools/validate-regulators-config.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+import argparse
+import json
+import sys
+import jsonschema
+
+r"""
+Validates the phosphor-regulators configuration file. Checks it against a JSON
+schema as well as doing some extra checks that can't be encoded in the schema.
+"""
+
+def validate_schema(config, schema):
+ r"""
+ Validates the specified config file using the specified
+ schema file.
+
+ config: Path of the file containing the config JSON
+ schema: Path of the file containing the schema JSON
+ """
+
+ with open(config) as config_handle:
+ config_json = json.load(config_handle)
+
+ with open(schema) as schema_handle:
+ schema_json = json.load(schema_handle)
+
+ try:
+ jsonschema.validate(config_json, schema_json)
+ except jsonschema.ValidationError as e:
+ print(e)
+ sys.exit("Validation failed.")
+
+if __name__ == '__main__':
+
+ parser = argparse.ArgumentParser(
+ description='phosphor-regulators configuration file validator')
+
+ parser.add_argument('-s', '--schema-file', dest='schema_file',
+ help='The phosphor-regulators schema file')
+
+ parser.add_argument('-c', '--configuration-file', dest='configuration_file',
+ help='The phosphor-regulators configuration file')
+
+ args = parser.parse_args()
+
+ if not args.schema_file:
+ parser.print_help()
+ sys.exit("Error: Schema file is required.")
+ if not args.configuration_file:
+ parser.print_help()
+ sys.exit("Error: Configuration file is required.")
+
+ validate_schema(args.configuration_file, args.schema_file)