|
1 | | -from os import listdir |
2 | | -from os.path import isfile, join, isdir, exists |
| 1 | +import os |
3 | 2 | import sys |
4 | 3 |
|
5 | 4 | def create_debugger_header(source_path): |
6 | | - |
7 | | - f = open(source_path + "/core/__generated__debugger_ui.h", "w", encoding="utf-8") |
8 | | - f.write("#pragma once\n") |
9 | | - f.write("\n") |
10 | | - f.write("/// This is a generated file by `cpplize_debugger.py`, executed by `SCsub`.\n") |
11 | | - f.write("/// \n") |
12 | | - f.write("/// DO NOT EDIT this header.\n") |
13 | | - f.write("/// If you want to modify this python code, you can simply change `debugger.py`\n") |
14 | | - f.write("/// During the next compilation this header will be updated.\n") |
15 | | - f.write("/// \n") |
16 | | - f.write("/// HOWEVER! The file will not be copied into the `bin` folder unless you remove the\n") |
17 | | - f.write("/// existing `bin/debugger.py` first; this algorithm prevents destroying eventual\n") |
18 | | - f.write("/// changes made on that file.\n") |
19 | | - f.write("\n") |
20 | | - f.write("static const char __debugger_ui_code[] = R\"TheCodeRKS(") |
21 | | - |
22 | | - size = 0 |
23 | | - with open(source_path + '/debugger_ui/debugger.py', encoding="utf-8") as deb_f: |
24 | | - for l in deb_f.readlines(): |
25 | | - l_utf8 = l.encode('utf-8') |
26 | | - size += len(l_utf8) |
27 | | - f.write(l); |
28 | | - |
29 | | - f.write(" )TheCodeRKS\";\n") |
30 | | - f.write("static unsigned int __debugger_ui_code_size = "+str(size)+";\n") |
31 | | - f.close() |
32 | | - |
| 5 | + """ |
| 6 | + Creates the generated header file for the debugger UI. |
33 | 7 |
|
34 | | -if len(sys.argv) < 2: |
35 | | - print("Usage: cpplize_debugger.py <source_path>") |
36 | | - sys.exit(1) |
| 8 | + Args: |
| 9 | + source_path (str): The path to the source directory where the header will be created. |
| 10 | + """ |
| 11 | + # Define the path to the 'core' directory |
| 12 | + core_dir = os.path.join(source_path, "core") |
| 13 | + |
| 14 | + # Create the 'core' directory if it doesn't exist |
| 15 | + os.makedirs(core_dir, exist_ok=True) |
| 16 | + |
| 17 | + # Define the full path to the generated header file |
| 18 | + header_path = os.path.join(core_dir, "__generated__debugger_ui.h") |
| 19 | + |
| 20 | + try: |
| 21 | + with open(header_path, "w", encoding="utf-8") as f: |
| 22 | + f.write("#pragma once\n\n") |
| 23 | + f.write("/// This is a generated file by `cpplize_debugger.py`, executed by `SCsub`.\n") |
| 24 | + f.write("///\n") |
| 25 | + f.write("/// DO NOT EDIT this header.\n") |
| 26 | + f.write("/// If you want to modify this Python code, simply change `debugger.py`\n") |
| 27 | + f.write("/// During the next compilation, this header will be updated.\n") |
| 28 | + f.write("///\n") |
| 29 | + f.write("/// HOWEVER! The file will not be copied into the `bin` folder unless you remove the\n") |
| 30 | + f.write("/// existing `bin/debugger.py` first; this algorithm prevents destroying any\n") |
| 31 | + f.write("/// changes made to that file.\n\n") |
| 32 | + f.write("static const char __debugger_ui_code[] = R\"TheCodeRKS(\n") |
| 33 | + |
| 34 | + size = 0 |
| 35 | + debugger_py_path = os.path.join(source_path, 'debugger_ui', 'debugger.py') |
| 36 | + |
| 37 | + if not os.path.isfile(debugger_py_path): |
| 38 | + print(f"Error: The file '{debugger_py_path}' was not found.") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + with open(debugger_py_path, encoding="utf-8") as deb_f: |
| 42 | + for line in deb_f: |
| 43 | + line_utf8 = line.encode('utf-8') |
| 44 | + size += len(line_utf8) |
| 45 | + f.write(line) |
| 46 | + |
| 47 | + f.write(")TheCodeRKS\";\n") |
| 48 | + f.write(f"static unsigned int __debugger_ui_code_size = {size};\n") |
| 49 | + |
| 50 | + print(f"Header file successfully generated at '{header_path}'.") |
| 51 | + |
| 52 | + except IOError as e: |
| 53 | + print(f"Error writing the header file: {e}") |
| 54 | + sys.exit(1) |
37 | 55 |
|
38 | | -source_path = sys.argv[1] |
| 56 | +# The create_debugger_header function is called directly from SCsub, |
| 57 | +# so there's no need for a main function handling sys.argv. |
39 | 58 |
|
40 | | -create_debugger_header(source_path) |
|
0 commit comments