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