From df6863a3f50591d2fe3057d1739c6c96a706300e Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Mon, 26 Aug 2024 21:40:33 +0300 Subject: [PATCH] bytebeat_compiler.py: remove generated files --- bytebeat_compiler.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index 1f045ef..170367a 100644 --- a/bytebeat_compiler.py +++ b/bytebeat_compiler.py @@ -4,7 +4,8 @@ if __name__ == "__main__": print(":: C bytebeat generator: compiler unit") 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 shlex import join as command_line_join, split as command_line_split from shutil import which @@ -94,6 +95,10 @@ def run_command(*command: list[str]) -> None: if subprocess.run(command).returncode != EXIT_SUCCESS: 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" C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"' @@ -200,6 +205,9 @@ if __name__ == "__main__": parser.add_argument("--color", default="auto", type=str, help="ANSI escape codes. Set to 'always' to enable them, 'none' to " "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() bytebeat_contents = read_from_file_or_stdin(args.file).strip() @@ -387,3 +395,8 @@ if __name__ == "__main__": "-I" + PATHS["include_directory"] ) run_command(OUTPUT_FILE) + + if not args.keep_files: + delete_file(PATHS["substitute"]) + delete_file(OUTPUT_FILE) + delete_empty_dir(PATHS["bin_dir"])