diff --git a/scripts/generate_driver_wrappers.py b/scripts/generate_driver_wrappers.py index f43d4e57aa..6cf8e86d43 100755 --- a/scripts/generate_driver_wrappers.py +++ b/scripts/generate_driver_wrappers.py @@ -28,6 +28,7 @@ import argparse import jsonschema import jinja2 from mbedtls_dev import build_tree +from traceback import format_tb JSONSchema = NewType('JSONSchema', object) # The Driver is an Object, but practically it's indexable and can called a dictionary to @@ -41,6 +42,12 @@ class JsonValidationException(Exception): super().__init__(self.message) +class DriverReaderException(Exception): + def __init__(self, message="Driver Reader Failed"): + self.message = message + super().__init__(self.message) + + def render(template_path: str, driver_jsoncontext: list) -> str: """ Render template from the input file and driver JSON. @@ -67,7 +74,7 @@ def generate_driver_wrapper_file(template_dir: str, \ out_file.write(result) -def validate_json(driverjson_data: Driver, driverschema_list: dict) -> bool: +def validate_json(driverjson_data: Driver, driverschema_list: dict) -> None: """ Validate the Driver JSON against an appropriate schema the schema passed could be that matching an opaque/ transparent driver. @@ -77,16 +84,15 @@ def validate_json(driverjson_data: Driver, driverschema_list: dict) -> bool: try: _schema = driverschema_list[driver_type] jsonschema.validate(instance=driverjson_data, schema=_schema) - except KeyError as err: - # This could happen if the driverjson_data.type does not exist in the passed in schema list + # This could happen if the driverjson_data.type does not exist in the provided schema list # schemas = {'transparent': transparent_driver_schema, 'opaque': opaque_driver_schema} # Print onto stdout and stderr. print("Unknown Driver type " + driver_type + " for driver " + driver_prefix, str(err)) print("Unknown Driver type " + driver_type + " for driver " + driver_prefix, str(err), file=sys.stderr) - return False + raise JsonValidationException() from err except jsonschema.exceptions.ValidationError as err: # Print onto stdout and stderr. @@ -96,51 +102,60 @@ def validate_json(driverjson_data: Driver, driverschema_list: dict) -> bool: print("Error: Failed to validate data file: {} using schema: {}." "\n Exception Message: \"{}\"" " ".format(driverjson_data, _schema, str(err)), file=sys.stderr) - return False - - return True + raise JsonValidationException() from err def load_driver(schemas: Dict[str, Any], driver_file: str) -> Any: with open(driver_file, 'r') as f: json_data = json.load(f) - if not validate_json(json_data, schemas): - raise JsonValidationException() + try: + validate_json(json_data, schemas) + except JsonValidationException as e: + raise DriverReaderException from e return json_data -def read_driver_descriptions(mbedtls_root: str, json_directory: str, \ - jsondriver_list: str) -> Tuple[bool, list]: +def load_schemas(mbedtls_root): + schema_file_paths = { + 'transparent': os.path.join(mbedtls_root, + 'scripts', + 'data_files', + 'driver_jsons', + 'driver_transparent_schema.json'), + 'opaque': os.path.join(mbedtls_root, + 'scripts', + 'data_files', + 'driver_jsons', + 'driver_transparent_schema.json') + } + driver_schema = {} + for key, file_path in schema_file_paths.items(): + with open(file_path, 'r') as file: + driver_schema[key] = json.load(file) + return driver_schema + + +def read_driver_descriptions(mbedtls_root: str, + json_directory: str, + jsondriver_list: str) -> list: """ Merge driver JSON files into a single ordered JSON after validation. """ result = [] - with open(os.path.join(mbedtls_root, - 'scripts', - 'data_files', - 'driver_jsons', - 'driver_transparent_schema.json'), 'r') as file: - transparent_driver_schema = json.load(file) - with open(os.path.join(mbedtls_root, - 'scripts', - 'data_files', - 'driver_jsons', - 'driver_opaque_schema.json'), 'r') as file: - opaque_driver_schema = json.load(file) + driver_schema = load_schemas(mbedtls_root) - driver_schema = {'transparent': transparent_driver_schema, - 'opaque': opaque_driver_schema} - with open(os.path.join(json_directory, jsondriver_list), 'r') as driverlistfile: - driver_list = json.load(driverlistfile) + with open(os.path.join(json_directory, jsondriver_list), 'r') as driver_list_file: + driver_list = json.load(driver_list_file) - try: - result = [load_driver(schemas=driver_schema, - driver_file=os.path.join(json_directory, driver_file_name)) - for driver_file_name in driver_list] - except JsonValidationException as _: - return False, [] + return [load_driver(schemas=driver_schema, + driver_file=os.path.join(json_directory, driver_file_name)) + for driver_file_name in driver_list] - return True, result + +def trace_exception(e, file=sys.stderr): + print("Exception: type: %s, message: %s, trace: %s" % ( + e.__class__, str(e), format_tb(e.__traceback__) + ), file) def main() -> int: @@ -162,30 +177,29 @@ def main() -> int: args = parser.parse_args() mbedtls_root = os.path.abspath(args.mbedtls_root) - if args.template_dir is None: - args.template_dir = os.path.join(mbedtls_root, - 'scripts', - 'data_files', - 'driver_templates') - if args.json_dir is None: - args.json_dir = os.path.join(mbedtls_root, - 'scripts', - 'data_files', - 'driver_jsons') - if args.output_directory is None: - args.output_directory = os.path.join(mbedtls_root, 'library') - output_directory = args.output_directory - template_directory = args.template_dir - json_directory = args.json_dir + output_directory = args.output_directory if args.output_directory is not None else \ + os.path.join(mbedtls_root, 'library') + template_directory = args.template_dir if args.template_dir is not None else \ + os.path.join(mbedtls_root, + 'scripts', + 'data_files', + 'driver_templates') + json_directory = args.json_dir if args.json_dir is not None else \ + os.path.join(mbedtls_root, + 'scripts', + 'data_files', + 'driver_jsons') - # Read and validate list of driver jsons from driverlist.json - ret, merged_driver_json = read_driver_descriptions(mbedtls_root, json_directory, - 'driverlist.json') - if ret is False: + try: + # Read and validate list of driver jsons from driverlist.json + merged_driver_json = read_driver_descriptions(mbedtls_root, + json_directory, + 'driverlist.json') + except DriverReaderException as e: + trace_exception(e) return 1 generate_driver_wrapper_file(template_directory, output_directory, merged_driver_json) - return 0