Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """ |
| 4 | A tool for validating entity manager configurations. |
| 5 | """ |
| 6 | import argparse |
| 7 | import json |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 8 | import os |
Potin Lai | 0f3a4d9 | 2023-12-05 00:13:55 +0800 | [diff] [blame] | 9 | import re |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 10 | import sys |
| 11 | |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 12 | import jsonschema.validators |
| 13 | |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 14 | DEFAULT_SCHEMA_FILENAME = "global.json" |
| 15 | |
| 16 | |
Potin Lai | 0f3a4d9 | 2023-12-05 00:13:55 +0800 | [diff] [blame] | 17 | def remove_c_comments(string): |
| 18 | # first group captures quoted strings (double or single) |
| 19 | # second group captures comments (//single-line or /* multi-line */) |
| 20 | pattern = r"(\".*?(?<!\\)\"|\'.*?(?<!\\)\')|(/\*.*?\*/|//[^\r\n]*$)" |
| 21 | regex = re.compile(pattern, re.MULTILINE | re.DOTALL) |
| 22 | |
| 23 | def _replacer(match): |
| 24 | if match.group(2) is not None: |
| 25 | return "" |
| 26 | else: |
| 27 | return match.group(1) |
| 28 | |
| 29 | return regex.sub(_replacer, string) |
| 30 | |
| 31 | |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 32 | def main(): |
| 33 | parser = argparse.ArgumentParser( |
| 34 | description="Entity manager configuration validator", |
| 35 | ) |
| 36 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 37 | "-s", |
| 38 | "--schema", |
| 39 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 40 | "Use the specified schema file instead of the default " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 41 | "(__file__/../../schemas/global.json)" |
| 42 | ), |
| 43 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 44 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 45 | "-c", |
| 46 | "--config", |
| 47 | action="append", |
| 48 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 49 | "Validate the specified configuration files (can be " |
| 50 | "specified more than once) instead of the default " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 51 | "(__file__/../../configurations/**.json)" |
| 52 | ), |
| 53 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 54 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 55 | "-e", |
| 56 | "--expected-fails", |
| 57 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 58 | "A file with a list of configurations to ignore should " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 59 | "they fail to validate" |
| 60 | ), |
| 61 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 62 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 63 | "-k", |
| 64 | "--continue", |
| 65 | action="store_true", |
| 66 | help="keep validating after a failure", |
| 67 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 68 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 69 | "-v", "--verbose", action="store_true", help="be noisy" |
| 70 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 71 | args = parser.parse_args() |
| 72 | |
| 73 | schema_file = args.schema |
| 74 | if schema_file is None: |
| 75 | try: |
| 76 | source_dir = os.path.realpath(__file__).split(os.sep)[:-2] |
| 77 | schema_file = os.sep + os.path.join( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 78 | *source_dir, "schemas", DEFAULT_SCHEMA_FILENAME |
| 79 | ) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 80 | except Exception: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 81 | sys.stderr.write( |
| 82 | "Could not guess location of {}\n".format( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 83 | DEFAULT_SCHEMA_FILENAME |
| 84 | ) |
| 85 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 86 | sys.exit(2) |
| 87 | |
| 88 | schema = {} |
| 89 | try: |
| 90 | with open(schema_file) as fd: |
| 91 | schema = json.load(fd) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 92 | except FileNotFoundError: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 93 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 94 | "Could not read schema file '{}'\n".format(schema_file) |
| 95 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 96 | sys.exit(2) |
| 97 | |
| 98 | config_files = args.config or [] |
| 99 | if len(config_files) == 0: |
| 100 | try: |
| 101 | source_dir = os.path.realpath(__file__).split(os.sep)[:-2] |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 102 | configs_dir = os.sep + os.path.join(*source_dir, "configurations") |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 103 | data = os.walk(configs_dir) |
| 104 | for root, _, files in data: |
| 105 | for f in files: |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 106 | if f.endswith(".json"): |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 107 | config_files.append(os.path.join(root, f)) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 108 | except Exception: |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 109 | sys.stderr.write("Could not guess location of configurations\n") |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 110 | sys.exit(2) |
| 111 | |
| 112 | configs = [] |
| 113 | for config_file in config_files: |
| 114 | try: |
| 115 | with open(config_file) as fd: |
Potin Lai | 0f3a4d9 | 2023-12-05 00:13:55 +0800 | [diff] [blame] | 116 | configs.append(json.loads(remove_c_comments(fd.read()))) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 117 | except FileNotFoundError: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 118 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 119 | "Could not parse config file '{}'\n".format(config_file) |
| 120 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 121 | sys.exit(2) |
| 122 | |
| 123 | expected_fails = [] |
| 124 | if args.expected_fails: |
| 125 | try: |
| 126 | with open(args.expected_fails) as fd: |
| 127 | for line in fd: |
| 128 | expected_fails.append(line.strip()) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 129 | except Exception: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 130 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 131 | "Could not read expected fails file '{}'\n".format( |
| 132 | args.expected_fails |
| 133 | ) |
| 134 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 135 | sys.exit(2) |
| 136 | |
Patrick Williams | 4f93bb8 | 2024-09-04 12:40:25 -0400 | [diff] [blame] | 137 | spec = jsonschema.Draft202012Validator |
Andrew Jeffery | 183fab9 | 2024-03-20 11:52:34 +1030 | [diff] [blame] | 138 | spec.check_schema(schema) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 139 | base_uri = "file://{}/".format( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 140 | os.path.split(os.path.realpath(schema_file))[0] |
| 141 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 142 | resolver = jsonschema.RefResolver(base_uri, schema) |
Andrew Jeffery | 183fab9 | 2024-03-20 11:52:34 +1030 | [diff] [blame] | 143 | validator = spec(schema, resolver=resolver) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 144 | |
| 145 | results = { |
| 146 | "invalid": [], |
| 147 | "unexpected_pass": [], |
| 148 | } |
| 149 | for config_file, config in zip(config_files, configs): |
| 150 | name = os.path.split(config_file)[1] |
| 151 | expect_fail = name in expected_fails |
| 152 | try: |
| 153 | validator.validate(config) |
| 154 | if expect_fail: |
| 155 | results["unexpected_pass"].append(name) |
| 156 | if not getattr(args, "continue"): |
| 157 | break |
| 158 | except jsonschema.exceptions.ValidationError as e: |
| 159 | if not expect_fail: |
| 160 | results["invalid"].append(name) |
| 161 | if args.verbose: |
| 162 | print(e) |
| 163 | if expect_fail or getattr(args, "continue"): |
| 164 | continue |
| 165 | break |
| 166 | |
| 167 | exit_status = 0 |
| 168 | if len(results["invalid"]) + len(results["unexpected_pass"]): |
| 169 | exit_status = 1 |
| 170 | unexpected_pass_suffix = " **" |
| 171 | show_suffix_explanation = False |
| 172 | print("results:") |
| 173 | for f in config_files: |
| 174 | if any([x in f for x in results["unexpected_pass"]]): |
| 175 | show_suffix_explanation = True |
| 176 | print(" '{}' passed!{}".format(f, unexpected_pass_suffix)) |
| 177 | if any([x in f for x in results["invalid"]]): |
| 178 | print(" '{}' failed!".format(f)) |
| 179 | |
| 180 | if show_suffix_explanation: |
| 181 | print("\n** configuration expected to fail") |
| 182 | |
| 183 | sys.exit(exit_status) |
| 184 | |
| 185 | |
| 186 | if __name__ == "__main__": |
| 187 | main() |