1
0

b/c...py: do not use a temp. dir if keeping files

Do not use a temporary directory if keeping files
This commit is contained in:
Intel A80486DX2-66 2024-08-26 22:58:40 +03:00
parent be5c7186ec
commit 23801c4eaa
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -4,7 +4,7 @@ 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, rename from os import environ, makedirs, name as os_name
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
@ -91,6 +91,24 @@ 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 compile_substituted_file(input_file: str, output_file: str) -> None:
print("Compiling")
run_command(
CC,
*command_line_split(CFLAGS),
input_file,
PATHS["fwrite_le"],
"-o", output_file,
"-I" + PATHS["include_directory"]
)
run_command(output_file)
def main_workflow(input_file: str, output_file: str, \
substitute_contents: Dict[str, str]) -> None:
rewrite_file(input_file, substitute_contents)
compile_substituted_file(input_file, output_file)
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'\"') + '"'
@ -251,52 +269,42 @@ if __name__ == "__main__":
"specify it by setting\nan environmental variable " "specify it by setting\nan environmental variable "
"CC.") "CC.")
with TemporaryDirectory() as tmpdirname: substitute_contents = substitute_vars({
temporary_path = lambda path: path_join(tmpdirname, path) "bytebeat_contents": bytebeat_contents,
"output_file": C_str_repr(args.output),
"sample_rate": \
value if (value := args.final_sample_rate) else args.sample_rate,
"original_sample_rate": original_sample_rate,
"final_sample_rate_code": final_sample_rate_code,
"bit_depth": args.bit_depth,
"is_signed": args.signed,
"precalculated_ratio": args.precalculate_ratio,
"faster_sample_ratio_math": args.precalculate_ratio,
"fp_return_type": args.floating_point,
"channels": args.channels,
"length": samples,
"wav_product": gen_length * (args.bit_depth // BITS_PER_BYTE),
"gen_length": gen_length,
"sequential_mode": args.mode == "sequential",
"block_size": args.block_size,
"silent_mode": args.silent,
"verbose_mode": args.verbose and not args.silent,
"fwrite_le": PATHS["fwrite_le_header"],
"ansi_escape_codes_supported": ansi_escape_codes_supported
}, read_file(PATHS["template"]), args.show_substituted_values)
substitute_temp = temporary_path(PATHS["substitute"]) if args.keep_files:
rewrite_file(substitute_temp, substitute_vars({ makedirs(PATHS["bin_dir"], exist_ok=True)
"bytebeat_contents": bytebeat_contents,
"output_file": C_str_repr(args.output),
"sample_rate": \
value if (value := args.final_sample_rate) else \
args.sample_rate,
"original_sample_rate": original_sample_rate,
"final_sample_rate_code": final_sample_rate_code,
"bit_depth": args.bit_depth,
"is_signed": args.signed,
"precalculated_ratio": args.precalculate_ratio,
"faster_sample_ratio_math": args.precalculate_ratio,
"fp_return_type": args.floating_point,
"channels": args.channels,
"length": samples,
"wav_product": gen_length * (args.bit_depth // BITS_PER_BYTE),
"gen_length": gen_length,
"sequential_mode": args.mode == "sequential",
"block_size": args.block_size,
"silent_mode": args.silent,
"verbose_mode": args.verbose and not args.silent,
"fwrite_le": PATHS["fwrite_le_header"],
"ansi_escape_codes_supported": ansi_escape_codes_supported
}, read_file(PATHS["template"]), args.show_substituted_values))
# Compile substitute_file = PATHS["substitute_kept"]
print("Compiling") output_file = PATHS["output_kept"]
output_file_temp = temporary_path(PATHS["output"]) main_workflow(substitute_file, output_file, substitute_contents)
else:
with TemporaryDirectory() as tmpdirname:
temporary_path = lambda path: path_join(tmpdirname, path)
run_command( substitute_temp = temporary_path(PATHS["substitute"])
CC, output_temp = temporary_path(PATHS["output"])
*command_line_split(CFLAGS),
substitute_temp,
PATHS["fwrite_le"],
"-o", output_file_temp,
"-I" + PATHS["include_directory"]
)
run_command(output_file_temp)
if args.keep_files: main_workflow(substitute_temp, output_temp, substitute_contents)
makedirs(PATHS["bin_dir"], exist_ok=True)
rename(substitute_temp, PATHS["substitute_kept"])
rename(output_file_temp, PATHS["output_kept"])