Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jeremy Johnson | c63d0f6 | 2023-09-14 16:07:00 +0100 | [diff] [blame] | 2 | # Copyright (c) 2021-2023, ARM Limited. |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | """This script converts generated tests into conformance tests. |
| 5 | |
| 6 | It can convert a framework unit test or a reference model unit test. |
| 7 | It expects the tests have been already run on the reference model |
| 8 | so it can capture the result as the expected result. |
| 9 | """ |
| 10 | import argparse |
| 11 | import json |
| 12 | import logging |
| 13 | import os |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 14 | import shutil |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 15 | from pathlib import Path |
| 16 | from typing import Optional |
| 17 | |
| 18 | from json2fbbin.json2fbbin import fbbin_to_json |
| 19 | from json2numpy.json2numpy import npy_to_json |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 20 | from schemavalidation.schemavalidation import TestDescSchemaValidator |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 21 | |
| 22 | logging.basicConfig(level=logging.INFO) |
| 23 | logger = logging.getLogger("convert2conformance") |
| 24 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 25 | |
| 26 | NAME_FLATBUFFER_DIR = ["flatbuffer-", "_FW_"] |
| 27 | NAME_DESC_FILENAME = "desc.json" |
| 28 | NAME_CONFORMANCE_RESULT_PREFIX = "Conformance-" |
| 29 | NAME_REFMODEL_RUN_RESULT_SUFFIX = ".runner.tosa_refmodel_sut_run.npy" |
| 30 | |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 31 | PROFILES_LIST = ["tosa-bi", "tosa-mi"] |
| 32 | |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 33 | OUTPUT_TYPE_JSON = "json" |
| 34 | OUTPUT_TYPE_BINARY = "binary" |
| 35 | OUTPUT_TYPE_BOTH = "both" |
| 36 | OUTPUT_TYPES = (OUTPUT_TYPE_JSON, OUTPUT_TYPE_BINARY, OUTPUT_TYPE_BOTH) |
| 37 | OUTPUT_TYPE_DEFAULT = OUTPUT_TYPE_JSON |
| 38 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 39 | |
| 40 | def parse_args(argv): |
| 41 | """Parse the arguments.""" |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 42 | # Set prog for when we are called via tosa_verif_conformance_generator |
| 43 | parser = argparse.ArgumentParser(prog="convert2conformance") |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 44 | parser.add_argument( |
| 45 | "test_dir", |
| 46 | default=Path.cwd(), |
| 47 | type=Path, |
| 48 | nargs="?", |
| 49 | help="The test directory to convert (default is CWD)", |
| 50 | ) |
| 51 | parser.add_argument( |
Jeremy Johnson | f0348ea | 2023-09-27 16:10:59 +0100 | [diff] [blame] | 52 | "--schema-path", |
| 53 | "--operator-fbs", |
| 54 | dest="schema_path", |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 55 | type=Path, |
| 56 | required=True, |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 57 | help="Path to reference model schema.", |
Jeremy Johnson | f0348ea | 2023-09-27 16:10:59 +0100 | [diff] [blame] | 58 | ) |
| 59 | parser.add_argument( |
| 60 | "--flatc-path", |
| 61 | dest="flatc_path", |
| 62 | type=Path, |
| 63 | required=True, |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 64 | help="Path to flatc executable.", |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 65 | ) |
| 66 | parser.add_argument( |
| 67 | "--output-directory", |
| 68 | dest="output_dir", |
| 69 | type=Path, |
| 70 | default=Path.cwd() / "conformance", |
| 71 | help="Output directory (default is conformance in CWD)", |
| 72 | ) |
| 73 | parser.add_argument( |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 74 | "--output-type", |
| 75 | dest="output_type", |
| 76 | choices=OUTPUT_TYPES, |
| 77 | default=OUTPUT_TYPE_DEFAULT, |
| 78 | help=f"Output file type produced (default is {OUTPUT_TYPE_DEFAULT})", |
| 79 | ) |
| 80 | parser.add_argument( |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 81 | "--framework", |
| 82 | dest="framework", |
| 83 | choices=["tflite"], |
| 84 | default="tflite", |
| 85 | help="Framework to convert (default tflite)", |
| 86 | ) |
| 87 | parser.add_argument( |
| 88 | "--framework-schema", |
| 89 | dest="framework_schema", |
| 90 | type=Path, |
| 91 | help="Framework schema needed to convert framework models", |
| 92 | ) |
| 93 | parser.add_argument( |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 94 | "--profile", |
| 95 | dest="profile", |
| 96 | choices=PROFILES_LIST, |
| 97 | action="append", |
| 98 | required=True, |
| 99 | help="Profiles this test is suitable for. May be repeated", |
| 100 | ) |
| 101 | parser.add_argument( |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 102 | "--tag", |
Jeremy Johnson | 76c6a55 | 2023-09-11 09:30:02 +0100 | [diff] [blame] | 103 | dest="tags", |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 104 | action="append", |
| 105 | type=str, |
Jeremy Johnson | 76c6a55 | 2023-09-11 09:30:02 +0100 | [diff] [blame] | 106 | help="Optional string tag to mark this test with. May be repeated", |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 107 | ) |
| 108 | parser.add_argument( |
Jeremy Johnson | dd8d9c2 | 2022-12-12 14:18:10 +0000 | [diff] [blame] | 109 | "--strict", |
| 110 | dest="strict", |
| 111 | action="store_true", |
| 112 | help="Output directory must not contain the same test directory", |
| 113 | ) |
| 114 | parser.add_argument( |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 115 | "--lazy-data-generation", |
| 116 | action="store_true", |
| 117 | help="Enable lazy data generation (only for tosa-mi)", |
| 118 | ) |
| 119 | parser.add_argument( |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 120 | "-v", "--verbose", dest="verbose", action="store_true", help="Verbose operation" |
| 121 | ) |
| 122 | args = parser.parse_args(argv) |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 123 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 124 | return args |
| 125 | |
| 126 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 127 | def find_framework_artifacts(framework: str, schema_path: Path, desc_file: Path): |
| 128 | """Check that any required schema has been supplied for conversion.""" |
| 129 | if framework == "tflite": |
| 130 | if not schema_path: |
| 131 | raise Exception("the following arguments are required: --framework-schema") |
| 132 | elif not schema_path.is_file(): |
| 133 | raise Exception(f"framework schema not found at {schema_path}") |
| 134 | model = desc_file.parent.parent / "model.tflite" |
| 135 | if not model.is_file(): |
| 136 | raise Exception(f"Model file not found at {model}") |
| 137 | return schema_path, model |
| 138 | return None, None |
| 139 | |
| 140 | |
| 141 | def get_framework_name(name_array: list, framework: str): |
| 142 | """Get the framework conversion directory name.""" |
| 143 | name = "" |
| 144 | for part in name_array: |
| 145 | if part == "_FW_": |
| 146 | part = framework |
| 147 | name = f"{name}{part}" |
| 148 | return name |
| 149 | |
| 150 | |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 151 | def convert_flatbuffer_file( |
| 152 | output_type: str, flatc: Path, schema: Path, model_file: Path, output: Path |
| 153 | ): |
| 154 | """Convert and/or copy the flatbuffer binary.""" |
| 155 | if output_type in (OUTPUT_TYPE_JSON, OUTPUT_TYPE_BOTH): |
| 156 | try: |
| 157 | fbbin_to_json(flatc, schema, model_file, output) |
| 158 | except Exception as e: |
| 159 | logger.error(f"Failed to convert flatbuffer binary:\n{e}") |
| 160 | return None |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 161 | |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 162 | if model_file.name == "model.tflite": |
| 163 | file_name = "model-tflite.json" |
| 164 | os.rename(output / "model.json", output / file_name) |
| 165 | else: |
| 166 | file_name = model_file.stem + ".json" |
| 167 | if output_type in (OUTPUT_TYPE_BINARY, OUTPUT_TYPE_BOTH): |
| 168 | try: |
| 169 | shutil.copy(model_file, output) |
| 170 | except Exception as e: |
| 171 | logger.error(f"Failed to copy flatbuffer binary:\n{e}") |
| 172 | return None |
| 173 | # By default return the binary name (if we have created both) |
| 174 | file_name = model_file.name |
| 175 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 176 | return output / file_name |
| 177 | |
| 178 | |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 179 | def convert_numpy_file( |
| 180 | output_type: str, npy_file: Path, output: Path, outstem: Optional[str] = None |
| 181 | ): |
| 182 | """Convert and/or copy the numpy file.""" |
| 183 | if output_type in (OUTPUT_TYPE_JSON, OUTPUT_TYPE_BOTH): |
| 184 | new_file = output / ((outstem if outstem else npy_file.stem) + ".json") |
| 185 | npy_to_json(npy_file, new_file) |
| 186 | if output_type in (OUTPUT_TYPE_BINARY, OUTPUT_TYPE_BOTH): |
| 187 | new_file = output / ((outstem + ".npy") if outstem else npy_file.name) |
| 188 | shutil.copy(npy_file, new_file) |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 189 | |
| 190 | |
| 191 | def update_desc_json( |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 192 | output_type: str, |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 193 | test_dir: Path, |
| 194 | test_desc, |
| 195 | output_dir: Optional[Path] = None, |
Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 196 | record_result=True, |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 197 | profiles=None, |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 198 | tags=None, |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 199 | ): |
Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 200 | """Update the desc.json format for conformance and optionally record result.""" |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 201 | ofm_files = [] |
| 202 | cfm_files = [] |
| 203 | if not output_dir: |
| 204 | output_dir = test_dir |
| 205 | for index, ofm in enumerate(test_desc["ofm_file"]): |
| 206 | ofm_path = test_dir / ofm |
| 207 | if not test_desc["expected_failure"]: |
| 208 | cfm = NAME_CONFORMANCE_RESULT_PREFIX + test_desc["ofm_name"][index] |
Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 209 | if record_result: |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 210 | if ofm_path.is_file(): |
| 211 | # Use the desc.json name |
| 212 | ofm_refmodel = ofm_path |
| 213 | else: |
| 214 | # Adjust for renaming due to tosa_verif_run_tests |
| 215 | ofm_refmodel = ofm_path.with_suffix(NAME_REFMODEL_RUN_RESULT_SUFFIX) |
| 216 | # Create conformance result |
| 217 | if ofm_refmodel.is_file(): |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 218 | convert_numpy_file( |
| 219 | output_type, ofm_refmodel, output_dir, outstem=cfm |
| 220 | ) |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 221 | else: |
| 222 | logger.error(f"Missing result file {ofm_path}") |
| 223 | return None |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 224 | cfm_files.append(cfm + ".npy") |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 225 | # Remove path and "ref-"/"ref_model_" from output filenames |
| 226 | ofm_files.append(strip_ref_output_name(ofm_path.name)) |
| 227 | |
| 228 | # Rewrite output file names as they can be relative, but keep them npys |
| 229 | test_desc["ofm_file"] = ofm_files |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 230 | if not test_desc["expected_failure"] and cfm_files: |
| 231 | # Output expected result file for conformance if expected pass and we |
| 232 | # have some files! |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 233 | test_desc["expected_result_file"] = cfm_files |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 234 | |
| 235 | # Add supported profiles |
| 236 | if profiles is None: |
| 237 | # Assume base profile |
| 238 | profiles = [PROFILES_LIST[0]] |
| 239 | test_desc["profile"] = profiles |
| 240 | |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 241 | # Add tags (if any) |
| 242 | if tags is not None: |
Jeremy Johnson | c63d0f6 | 2023-09-14 16:07:00 +0100 | [diff] [blame] | 243 | test_desc["tag"] = tags |
Jeremy Johnson | fd05bb3 | 2023-02-07 16:39:24 +0000 | [diff] [blame] | 244 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 245 | return test_desc |
| 246 | |
| 247 | |
| 248 | def strip_ref_output_name(name): |
| 249 | """Remove mentions of reference from output files.""" |
| 250 | if name.startswith("ref-"): |
| 251 | name = name[4:] |
| 252 | if name.startswith("ref_model_"): |
| 253 | name = name[10:] |
| 254 | return name |
| 255 | |
| 256 | |
| 257 | def main(argv=None): |
| 258 | """Convert the given directory to a conformance test.""" |
| 259 | args = parse_args(argv) |
| 260 | # Verbosity |
| 261 | if args.verbose: |
| 262 | logger.setLevel(logging.DEBUG) |
| 263 | |
| 264 | # Check we can get the files we need |
Jeremy Johnson | f0348ea | 2023-09-27 16:10:59 +0100 | [diff] [blame] | 265 | if not args.flatc_path.is_file(): |
| 266 | logger.error("flatc not found at %s", args.flatc_path) |
| 267 | return 2 |
| 268 | if not args.schema_path.is_file(): |
| 269 | logger.error("TOSA schema not found at %s", args.schema_path) |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 270 | return 2 |
| 271 | |
| 272 | # Work out where the desc.json file is |
| 273 | desc_filename = args.test_dir / NAME_DESC_FILENAME |
| 274 | framework_conversion = False |
Jeremy Johnson | 0ecfa37 | 2022-06-30 14:27:56 +0100 | [diff] [blame] | 275 | test_type_desc = "unknown" |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 276 | if desc_filename.is_file(): |
Jeremy Johnson | 0ecfa37 | 2022-06-30 14:27:56 +0100 | [diff] [blame] | 277 | logger.debug("Found TOSA operator unit test") |
| 278 | test_type_desc = "TOSA operator" |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 279 | else: |
| 280 | desc_filename = ( |
| 281 | args.test_dir |
| 282 | / get_framework_name(NAME_FLATBUFFER_DIR, args.framework) |
| 283 | / NAME_DESC_FILENAME |
| 284 | ) |
| 285 | if desc_filename.is_file(): |
Jeremy Johnson | 0ecfa37 | 2022-06-30 14:27:56 +0100 | [diff] [blame] | 286 | logger.debug(f"Found framework unit test for {args.framework}") |
| 287 | test_type_desc = f"{args.framework}" |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 288 | framework_conversion = True |
| 289 | else: |
| 290 | logger.error(f"Could not find {NAME_DESC_FILENAME} in {args.test_dir}") |
| 291 | return 2 |
| 292 | logger.debug(f"desc.json file: {desc_filename}") |
| 293 | |
| 294 | # Check for required files for framework conversion |
| 295 | if framework_conversion: |
| 296 | try: |
| 297 | framework_schema, framework_filename = find_framework_artifacts( |
| 298 | args.framework, args.framework_schema, desc_filename |
| 299 | ) |
| 300 | except Exception as err: |
| 301 | logger.error(err) |
| 302 | return 2 |
| 303 | else: |
| 304 | framework_schema, framework_filename = None, None |
| 305 | |
| 306 | # Open the meta desc.json file |
| 307 | with open(desc_filename, mode="r") as fd: |
| 308 | test_desc = json.load(fd) |
| 309 | |
| 310 | if "tosa_file" not in test_desc: |
| 311 | logger.error(f"Unsupported desc.json file found {desc_filename}") |
| 312 | return 2 |
| 313 | |
| 314 | # Dictionary fix |
| 315 | if "ifm_name" not in test_desc: |
| 316 | logger.warn("Old format desc.json file found - attempting to fix up") |
| 317 | test_desc["ifm_name"] = test_desc["ifm_placeholder"] |
| 318 | del test_desc["ifm_placeholder"] |
| 319 | |
| 320 | # Make the output directory if needed |
| 321 | try: |
Jeremy Johnson | dd8d9c2 | 2022-12-12 14:18:10 +0000 | [diff] [blame] | 322 | args.output_dir.mkdir(parents=True, exist_ok=(not args.strict)) |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 323 | except FileExistsError: |
Jeremy Johnson | dd8d9c2 | 2022-12-12 14:18:10 +0000 | [diff] [blame] | 324 | if args.strict: |
| 325 | logger.error(f"{args.output_dir} already exists") |
| 326 | else: |
| 327 | logger.error(f"{args.output_dir} is not a directory") |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 328 | return 2 |
| 329 | |
| 330 | # Convert the TOSA flatbuffer binary |
| 331 | tosa_filename = desc_filename.parent / test_desc["tosa_file"] |
| 332 | tosa_filename = convert_flatbuffer_file( |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 333 | args.output_type, |
| 334 | args.flatc_path, |
| 335 | args.schema_path, |
| 336 | tosa_filename, |
| 337 | args.output_dir, |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 338 | ) |
| 339 | if not tosa_filename: |
| 340 | # Failed to convert the file, json2fbbin will have printed an error |
| 341 | return 1 |
| 342 | else: |
| 343 | # Replace binary with JSON name |
| 344 | test_desc["tosa_file"] = tosa_filename.name |
| 345 | |
| 346 | if framework_conversion and framework_filename: |
| 347 | # Convert the framework flatbuffer binary |
| 348 | framework_filename = convert_flatbuffer_file( |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 349 | args.output_type, |
| 350 | args.flatc_path, |
| 351 | framework_schema, |
| 352 | framework_filename, |
| 353 | args.output_dir, |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 354 | ) |
| 355 | if not framework_filename: |
| 356 | # Failed to convert the file, json2fbbin will have printed an error |
| 357 | return 1 |
| 358 | |
| 359 | # Convert input files to JSON |
| 360 | ifm_files = [] |
| 361 | for file in test_desc["ifm_file"]: |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 362 | if file: |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 363 | path = desc_filename.parent / file |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 364 | ifm_files.append(path.name) |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 365 | if path.is_file(): |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 366 | convert_numpy_file(args.output_type, path, args.output_dir) |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 367 | else: |
| 368 | if not args.lazy_data_generation: |
| 369 | logger.error(f"Missing input file {path.name}") |
| 370 | return 1 |
| 371 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 372 | # Rewrite input file names to make sure the paths are correct, |
| 373 | # but keep them numpys as the test runner will convert them back |
| 374 | # before giving them to the SUT |
| 375 | test_desc["ifm_file"] = ifm_files |
| 376 | |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 377 | # Check for cpp files for data-generator/verifier |
| 378 | cpp_files = args.test_dir.glob("*.cpp") |
| 379 | for cpp in cpp_files: |
| 380 | shutil.copy(str(cpp), str(args.output_dir)) |
| 381 | |
Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 382 | # Work out if we have a result to record |
| 383 | record_result = not args.lazy_data_generation |
| 384 | if "meta" in test_desc and "compliance" in test_desc["meta"]: |
| 385 | # We don't have pre-generated results for compliance tests |
| 386 | record_result = False |
| 387 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 388 | # Update desc.json and convert result files to JSON |
| 389 | test_desc = update_desc_json( |
Jeremy Johnson | 9c2fe6e | 2023-10-04 16:55:04 +0100 | [diff] [blame] | 390 | args.output_type, |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 391 | desc_filename.parent, |
| 392 | test_desc, |
| 393 | output_dir=args.output_dir, |
Jeremy Johnson | e2b5e87 | 2023-09-14 17:02:09 +0100 | [diff] [blame] | 394 | record_result=record_result, |
Jeremy Johnson | 8858862 | 2022-07-12 16:42:29 +0100 | [diff] [blame] | 395 | profiles=args.profile, |
Jeremy Johnson | 76c6a55 | 2023-09-11 09:30:02 +0100 | [diff] [blame] | 396 | tags=args.tags, |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 397 | ) |
| 398 | if not test_desc: |
| 399 | # Error from conversion/update |
| 400 | return 1 |
| 401 | |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 402 | # Validate the desc.json schema |
Jeremy Johnson | 76c6a55 | 2023-09-11 09:30:02 +0100 | [diff] [blame] | 403 | try: |
| 404 | TestDescSchemaValidator().validate_config(test_desc) |
| 405 | except Exception as e: |
| 406 | logger.error(e) |
| 407 | return 1 |
Jeremy Johnson | 1271c44 | 2023-09-05 11:39:26 +0100 | [diff] [blame] | 408 | |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 409 | # Output new desc.json |
| 410 | new_desc_filename = args.output_dir / NAME_DESC_FILENAME |
| 411 | with open(new_desc_filename, "w") as fd: |
| 412 | json.dump(test_desc, fd, indent=2) |
| 413 | |
Jeremy Johnson | 0ecfa37 | 2022-06-30 14:27:56 +0100 | [diff] [blame] | 414 | logger.info(f"Converted {test_type_desc} test to {args.output_dir}") |
Jeremy Johnson | 6179c21 | 2022-01-13 13:46:35 +0000 | [diff] [blame] | 415 | return 0 |
| 416 | |
| 417 | |
| 418 | if __name__ == "__main__": |
| 419 | exit(main()) |