From eef51a6d2bb4200c1f716cb87a74167041420b47 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 16 Apr 2025 20:11:28 -0500 Subject: [PATCH] [olympic] Improve error handling in file-to-cpp --- util/scripts/file-to-cpp.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/util/scripts/file-to-cpp.py b/util/scripts/file-to-cpp.py index 79e789b7..1a43b13a 100755 --- a/util/scripts/file-to-cpp.py +++ b/util/scripts/file-to-cpp.py @@ -15,6 +15,9 @@ import pathlib import sys +tool_name = 'file-to-cpp' + + def write_txt_file(path: str, txt: str): with open(path, "w") as f: f.write(txt) @@ -22,18 +25,22 @@ def write_txt_file(path: str, txt: str): def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]: - with open(path, 'rb') as f: - out = line_prefix - data = f.read() - i = 1 - for b in data: - out += f"{b:#0{4}x}," - if i % 10 == 0: - out += '\n\t' - else: - out += ' ' - i += 1 - return out[:-1], len(data) + try: + with open(path, 'rb') as f: + out = line_prefix + data = f.read() + i = 1 + for b in data: + out += f"{b:#0{4}x}," + if i % 10 == 0: + out += '\n\t' + else: + out += ' ' + i += 1 + return out[:-1], len(data) + except FileNotFoundError: + print(f'{tool_name}: included file not found: {path}', file=sys.stderr) + sys.exit(1) def file_to_cpp(path: str, cpp_name: str) -> tuple[str, str]: @@ -76,11 +83,11 @@ def proc_rsrc_file(rsrc_path: str):# Open and read the JSON file all_files_func += f'\nox::Vector> {all_files}() noexcept {{\n\treturn {{\n' for f in data['files']: if 'path' not in f: - print('src file path missing', file=sys.stderr) + print(f'{tool_name}: src file path missing', file=sys.stderr) sys.exit(1) path = f['path'] if 'cpp_name' not in f: - print('var name missing', file=sys.stderr) + print(f'{tool_name}: var name missing', file=sys.stderr) sys.exit(1) cpp_name = f['cpp_name'] c, h = file_to_cpp(os.path.join(base_path, path), cpp_name)