-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: propagate signals to remage-cpp
- Loading branch information
Showing
1 changed file
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
import signal | ||
import subprocess | ||
import sys | ||
|
||
from .cpp_config import REMAGE_CPP_EXE_PATH | ||
|
||
|
||
def remage_cli(): | ||
result = subprocess.run([REMAGE_CPP_EXE_PATH] + sys.argv[1:], check=False) | ||
sys.exit(result.returncode) | ||
def _run_remage_cpp() -> int: | ||
"""run the remage-cpp executable and return the exit code as seen in bash.""" | ||
# reuse our own argv[0] to have helpful help messages. | ||
proc = subprocess.Popen(sys.argv, executable=REMAGE_CPP_EXE_PATH) | ||
|
||
# propagate signals to the C++ executable. | ||
def new_signal_handler(sig: int, _): | ||
proc.send_signal(sig) | ||
|
||
signals = [ | ||
signal.SIGHUP, | ||
signal.SIGINT, | ||
signal.SIGQUIT, | ||
signal.SIGTERM, | ||
signal.SIGTSTP, # SIGSTOP cannot be caught, and will do nothing... | ||
signal.SIGCONT, | ||
signal.SIGUSR1, | ||
signal.SIGUSR2, | ||
signal.SIGWINCH, | ||
] | ||
|
||
old_signal_handlers = [signal.signal(sig, new_signal_handler) for sig in signals] | ||
|
||
# wait for C++ executable to finish. | ||
proc.wait() | ||
|
||
# restore signal handlers again, before running more python code. | ||
for sig, handler in zip(signals, old_signal_handlers): | ||
signal.signal(sig, handler) | ||
|
||
return 128 - proc.returncode if proc.returncode < 0 else proc.returncode | ||
|
||
|
||
def remage_cli() -> None: | ||
ec = _run_remage_cpp() | ||
if ec not in [0, 2]: | ||
# remage had an error (::fatal -> ec==134 (SIGABRT); ::error -> ec==1) | ||
# ec==2 is just a warning, continue in the execution flow. | ||
sys.exit(ec) | ||
|
||
# TODO: further post-processing | ||
|
||
sys.exit(ec) |