1
0

bytebeat_compiler.py: do not use sys.exit

This commit is contained in:
Intel A80486DX2-66 2024-08-26 19:26:00 +03:00
parent c6558c8c46
commit ee404d1ad2
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -8,7 +8,7 @@ from os import environ, makedirs, name as os_name
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
from sys import stdin, stdout, exit
from sys import stdin, stdout
from typing import Dict, Union
import subprocess
@ -71,8 +71,7 @@ def read_from_file_or_stdin(path: str) -> str:
elif exists(path):
return read_file(path)
else:
print("The specified file doesn't exist")
raise SystemExit
raise SystemExit(f"The specified file {path} doesn't exist")
def substitute_vars(replacements: Dict[str, Union[bool, str]], text: str,
verbose: bool) -> str:
@ -92,7 +91,7 @@ def substitute_vars(replacements: Dict[str, Union[bool, str]], text: str,
def run_command(*command: list[str]) -> None:
print(command_line_join(command), flush=True)
if subprocess.run(command).returncode != EXIT_SUCCESS:
exit(EXIT_FAILURE)
raise SystemExit(EXIT_FAILURE)
preprocessor_bool = lambda value: "1" if value else "0"
C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"'
@ -177,8 +176,7 @@ if __name__ == "__main__":
bytebeat_contents = read_from_file_or_stdin(args.file).strip()
if not bytebeat_contents:
print("No valid contents")
raise SystemExit
raise SystemExit("No valid contents")
# - Compilation
makedirs(PATHS["bin_dir"], exist_ok=True)
@ -211,8 +209,7 @@ if __name__ == "__main__":
samples_specified = not no_samples
if seconds_specified and args.seconds < 0:
print("CLI: Count of seconds can't be less than zero.")
raise SystemExit
raise SystemExit("CLI: Count of seconds can't be less than zero.")
if no_seconds and samples_specified:
samples = args.samples
@ -224,17 +221,14 @@ if __name__ == "__main__":
args.seconds = 30 # default
continue
else:
print("CLI: Incorrect seconds/samples length format.")
raise SystemExit
raise SystemExit("CLI: Incorrect seconds/samples length format.")
break
if samples <= 0:
print("CLI: Count of samples should be greater than zero.")
raise SystemExit
raise SystemExit("CLI: Count of samples should be greater than zero.")
if args.mode != "sequential" and args.mode != "instant":
print("Invalid mode '%s'" % args.mode)
raise SystemExit
raise SystemExit(f"Invalid mode '{args.mode}'")
gen_length = args.channels * samples
@ -280,9 +274,9 @@ if __name__ == "__main__":
print()
if still_unavailable:
print("Could not find an available compiler. Please specify it by "
"setting\nenvironmental variable CC.")
exit(2)
raise SystemExit("Could not find an available compiler. Please "
"specify it by setting\nenvironmental variable "
"CC.")
# Compile
print("Compiling")
@ -296,5 +290,3 @@ if __name__ == "__main__":
"-I" + PATHS["include_directory"]
)
run_command(OUTPUT_FILE)
exit(EXIT_SUCCESS)