scripts: refactor validate_configs.py part 2

Extract function 'validator_from_file' to prepare a validator object
from a given schema file path.

Tested:

An invalid config still results in exit code 1 and error output.
When all configs are valid, exit code is still 0.

Change-Id: Ie3e664c9f5098627c6e3c31e6699cd304ce4ce31
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/scripts/validate_configs.py b/scripts/validate_configs.py
index 393d9a8..ce3cb72 100755
--- a/scripts/validate_configs.py
+++ b/scripts/validate_configs.py
@@ -85,16 +85,6 @@
             )
             sys.exit(2)
 
-    schema = {}
-    try:
-        with open(schema_file) as fd:
-            schema = json.load(fd)
-    except FileNotFoundError:
-        sys.stderr.write(
-            "Could not read schema file '{}'\n".format(schema_file)
-        )
-        sys.exit(2)
-
     config_files = args.config or []
     if len(config_files) == 0:
         try:
@@ -134,13 +124,7 @@
             )
             sys.exit(2)
 
-    spec = jsonschema.Draft202012Validator
-    spec.check_schema(schema)
-    base_uri = "file://{}/".format(
-        os.path.split(os.path.realpath(schema_file))[0]
-    )
-    resolver = jsonschema.RefResolver(base_uri, schema)
-    validator = spec(schema, resolver=resolver)
+    validator = validator_from_file(schema_file)
 
     results = {
         "invalid": [],
@@ -171,6 +155,29 @@
     sys.exit(exit_status)
 
 
+def validator_from_file(schema_file):
+
+    schema = {}
+    try:
+        with open(schema_file) as fd:
+            schema = json.load(fd)
+    except FileNotFoundError:
+        sys.stderr.write(
+            "Could not read schema file '{}'\n".format(schema_file)
+        )
+        sys.exit(2)
+
+    spec = jsonschema.Draft202012Validator
+    spec.check_schema(schema)
+    base_uri = "file://{}/".format(
+        os.path.split(os.path.realpath(schema_file))[0]
+    )
+    resolver = jsonschema.RefResolver(base_uri, schema)
+    validator = spec(schema, resolver=resolver)
+
+    return validator
+
+
 def validate_single_config(
     args, config_file, config, expected_fails, validator, results
 ):