Matt Spinler | 446d2b9 | 2020-03-05 16:49:14 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import sys |
| 6 | |
| 7 | r""" |
| 8 | Validates the PEL message registry JSON, which includes checking it against |
| 9 | a JSON schema using the jsonschema module as well as doing some extra checks |
| 10 | that can't be encoded in the schema. |
| 11 | """ |
| 12 | |
| 13 | |
| 14 | def check_duplicate_names(registry_json): |
| 15 | r""" |
| 16 | Check that there aren't any message registry entries with the same |
| 17 | 'Name' field. There may be a use case for this in the future, but there |
| 18 | isn't right now. |
| 19 | |
| 20 | registry_json: The message registry JSON |
| 21 | """ |
| 22 | |
Matt Spinler | 446d2b9 | 2020-03-05 16:49:14 -0600 | [diff] [blame] | 23 | names = [] |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 24 | for entry in registry_json["PELs"]: |
| 25 | if entry["Name"] in names: |
| 26 | sys.exit("Found multiple uses of error {}".format(entry["Name"])) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 27 | else: |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 28 | names.append(entry["Name"]) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 29 | |
| 30 | |
Matt Spinler | 55a6978 | 2019-10-25 13:49:30 -0500 | [diff] [blame] | 31 | def check_duplicate_reason_codes(registry_json): |
| 32 | r""" |
| 33 | Check that there aren't any message registry entries with the same |
| 34 | 'ReasonCode' field. |
| 35 | |
| 36 | registry_json: The message registry JSON |
| 37 | """ |
| 38 | |
Matt Spinler | 446d2b9 | 2020-03-05 16:49:14 -0600 | [diff] [blame] | 39 | reasonCodes = [] |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 40 | for entry in registry_json["PELs"]: |
| 41 | if entry["SRC"]["ReasonCode"] in reasonCodes: |
| 42 | sys.exit( |
| 43 | "Found duplicate SRC reason code {}".format( |
| 44 | entry["SRC"]["ReasonCode"] |
| 45 | ) |
| 46 | ) |
Matt Spinler | 55a6978 | 2019-10-25 13:49:30 -0500 | [diff] [blame] | 47 | else: |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 48 | reasonCodes.append(entry["SRC"]["ReasonCode"]) |
Matt Spinler | 55a6978 | 2019-10-25 13:49:30 -0500 | [diff] [blame] | 49 | |
| 50 | |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 51 | def check_component_id(registry_json): |
| 52 | r""" |
| 53 | Check that the upper byte of the ComponentID field matches the upper byte |
| 54 | of the ReasonCode field, but not on "11" type SRCs where they aren't |
| 55 | supposed to match. |
| 56 | |
| 57 | registry_json: The message registry JSON |
| 58 | """ |
| 59 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 60 | for entry in registry_json["PELs"]: |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 61 | # Don't check on "11" SRCs as those reason codes aren't supposed to |
| 62 | # match the component ID. |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 63 | if entry["SRC"].get("Type", "") == "11": |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 64 | continue |
| 65 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 66 | if "ComponentID" in entry: |
| 67 | id = int(entry["ComponentID"], 16) |
| 68 | reason_code = int(entry["SRC"]["ReasonCode"], 16) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 69 | |
| 70 | if (id & 0xFF00) != (reason_code & 0xFF00): |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 71 | sys.exit( |
| 72 | "Found mismatching component ID {} vs reason " |
| 73 | "code {} for error {}".format( |
| 74 | entry["ComponentID"], |
| 75 | entry["SRC"]["ReasonCode"], |
| 76 | entry["Name"], |
| 77 | ) |
| 78 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def check_message_args(registry_json): |
| 82 | r""" |
| 83 | Check that if the Message field uses the '%' style placeholders that there |
| 84 | are that many entries in the MessageArgSources field. Also checks that |
| 85 | the MessageArgSources field is present but only if there are placeholders. |
| 86 | |
| 87 | registry_json: The message registry JSON |
| 88 | """ |
| 89 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 90 | for entry in registry_json["PELs"]: |
| 91 | num_placeholders = entry["Documentation"]["Message"].count("%") |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 92 | if num_placeholders == 0: |
| 93 | continue |
| 94 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 95 | if "MessageArgSources" not in entry["Documentation"]: |
| 96 | sys.exit( |
| 97 | "Missing MessageArgSources property for error {}".format( |
| 98 | entry["Name"] |
| 99 | ) |
| 100 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 101 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 102 | if num_placeholders != len( |
| 103 | entry["Documentation"]["MessageArgSources"] |
| 104 | ): |
| 105 | sys.exit( |
| 106 | "Different number of placeholders found in " |
| 107 | "Message vs MessageArgSources for error {}".format( |
| 108 | entry["Name"] |
| 109 | ) |
| 110 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def validate_schema(registry, schema): |
| 114 | r""" |
| 115 | Validates the passed in JSON against the passed in schema JSON |
| 116 | |
| 117 | registry: Path of the file containing the registry JSON |
| 118 | schema: Path of the file containing the schema JSON |
| 119 | Use None to skip the pure schema validation |
| 120 | """ |
| 121 | |
| 122 | with open(registry) as registry_handle: |
| 123 | registry_json = json.load(registry_handle) |
| 124 | |
| 125 | if schema: |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 126 | import jsonschema |
| 127 | |
| 128 | with open(schema) as schema_handle: |
| 129 | schema_json = json.load(schema_handle) |
| 130 | |
| 131 | try: |
| 132 | jsonschema.validate(registry_json, schema_json) |
| 133 | except jsonschema.ValidationError as e: |
| 134 | print(e) |
| 135 | sys.exit("Schema validation failed") |
| 136 | |
| 137 | check_duplicate_names(registry_json) |
| 138 | |
Matt Spinler | 55a6978 | 2019-10-25 13:49:30 -0500 | [diff] [blame] | 139 | check_duplicate_reason_codes(registry_json) |
| 140 | |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 141 | check_component_id(registry_json) |
| 142 | |
| 143 | check_message_args(registry_json) |
| 144 | |
| 145 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 146 | if __name__ == "__main__": |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 147 | parser = argparse.ArgumentParser( |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 148 | description="PEL message registry validator" |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 149 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 150 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 151 | parser.add_argument( |
| 152 | "-s", |
| 153 | "--schema-file", |
| 154 | dest="schema_file", |
| 155 | help="The message registry JSON schema file", |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 156 | required=True, |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 157 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 158 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 159 | parser.add_argument( |
| 160 | "-r", |
| 161 | "--registry-file", |
| 162 | dest="registry_file", |
| 163 | help="The message registry JSON file", |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 164 | required=True, |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 165 | ) |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 166 | |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 167 | parser.add_argument( |
| 168 | "-k", |
| 169 | "--skip-schema-validation", |
| 170 | action="store_true", |
| 171 | dest="skip_schema", |
Patrick Williams | d27675d | 2022-12-08 06:18:00 -0600 | [diff] [blame] | 172 | help="Skip running schema validation. Only do the extra checks.", |
Patrick Williams | e6555f5 | 2022-08-04 13:56:17 -0500 | [diff] [blame] | 173 | ) |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 174 | |
| 175 | args = parser.parse_args() |
| 176 | |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 177 | schema = args.schema_file |
| 178 | if args.skip_schema: |
| 179 | schema = None |
Matt Spinler | 5cb5deb | 2019-09-27 13:51:36 -0500 | [diff] [blame] | 180 | |
Matt Spinler | 1ed1067 | 2023-03-08 16:39:15 -0600 | [diff] [blame] | 181 | validate_schema(args.registry_file, schema) |