1
0

bytebeat_compiler.py: remove generated files

This commit is contained in:
Intel A80486DX2-66 2024-08-26 21:40:33 +03:00
parent 1e4f9ac26f
commit 429d43f8c1
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -4,7 +4,8 @@ if __name__ == "__main__":
print(":: C bytebeat generator: compiler unit") print(":: C bytebeat generator: compiler unit")
from argparse import ArgumentParser from argparse import ArgumentParser
from os import environ, makedirs, name as os_name from os import environ, listdir, makedirs, name as os_name, \
remove as delete_file, rmdir
from os.path import exists, join as path_join from os.path import exists, join as path_join
from shlex import join as command_line_join, split as command_line_split from shlex import join as command_line_join, split as command_line_split
from shutil import which from shutil import which
@ -93,6 +94,10 @@ def run_command(*command: list[str]) -> None:
if subprocess.run(command).returncode != EXIT_SUCCESS: if subprocess.run(command).returncode != EXIT_SUCCESS:
raise SystemExit(EXIT_FAILURE) raise SystemExit(EXIT_FAILURE)
def delete_empty_dir(path: str) -> None:
if exists(path) and len(listdir(path)) == 0:
rmdir(path)
preprocessor_bool = lambda value: "1" if value else "0" preprocessor_bool = lambda value: "1" if value else "0"
C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"' C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"'
@ -171,6 +176,9 @@ if __name__ == "__main__":
parser.add_argument("--color", default="auto", type=str, parser.add_argument("--color", default="auto", type=str,
help="ANSI escape codes. Set to 'always' to enable them, 'none' to " help="ANSI escape codes. Set to 'always' to enable them, 'none' to "
"disable. Default: 'auto'.") "disable. Default: 'auto'.")
parser.add_argument("--keep-files", default=False, action="store_true",
help="keep generated files: substituted source code of runtime unit "
"and the executable it will be compiled to.")
args = parser.parse_args() args = parser.parse_args()
bytebeat_contents = read_from_file_or_stdin(args.file).strip() bytebeat_contents = read_from_file_or_stdin(args.file).strip()
@ -290,3 +298,8 @@ if __name__ == "__main__":
"-I" + PATHS["include_directory"] "-I" + PATHS["include_directory"]
) )
run_command(OUTPUT_FILE) run_command(OUTPUT_FILE)
if not args.keep_files:
delete_file(PATHS["substitute"])
delete_file(OUTPUT_FILE)
delete_empty_dir(PATHS["bin_dir"])