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 8c7d7ac8f9
commit 16384717a7
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 re import re
import subprocess import subprocess
@ -72,8 +72,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:
@ -93,7 +92,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'\"') + '"'
@ -206,8 +205,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")
exit(1)
# - Parse arguments from file # - Parse arguments from file
used_parameter_line = False used_parameter_line = False
@ -276,8 +274,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.")
exit(1)
if no_seconds and samples_specified: if no_seconds and samples_specified:
samples = args.samples samples = args.samples
@ -289,17 +286,16 @@ 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.")
exit(1)
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.")
exit(1)
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}'")
exit(1)
gen_length = args.channels * samples
ansi_escape_codes_supported = args.color == "auto" and stdout_atty or \ ansi_escape_codes_supported = args.color == "auto" and stdout_atty or \
args.color == "always" args.color == "always"
@ -375,9 +371,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")
@ -391,5 +387,3 @@ if __name__ == "__main__":
"-I" + PATHS["include_directory"] "-I" + PATHS["include_directory"]
) )
run_command(OUTPUT_FILE) run_command(OUTPUT_FILE)
exit(EXIT_SUCCESS)