Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ctrl-C processing & use as lib with recent python versions #960

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
16 changes: 9 additions & 7 deletions scripts/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ def run_cmd(cmd, timeout_s=999, exit_on_error=1, check_return_code=True,
debug_cmd.write(cmd)
debug_cmd.write("\n\n")
return
def killgroup(ps):
try:
os.killpg(os.getpgid(ps.pid), signal.SIGTERM)
except AttributeError: #killpg not available on windows
ps.kill()
sys.exit(130)
try:
ps = subprocess.Popen("exec " + cmd,
shell=True,
Expand All @@ -112,21 +118,17 @@ def run_cmd(cmd, timeout_s=999, exit_on_error=1, check_return_code=True,
env=os.environ,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output = ps.communicate(timeout = timeout_s)[0]
except subprocess.CalledProcessError:
logging.error(ps.communicate()[0])
sys.exit(RET_FAIL)
except KeyboardInterrupt:
logging.info("\nExited Ctrl-C from user request.")
sys.exit(130)
try:
output = ps.communicate(timeout=timeout_s)[0]
killgroup(ps)
except subprocess.TimeoutExpired:
logging.error("Timeout[{}s]: {}".format(timeout_s, cmd))
output = ""
try:
os.killpg(os.getpgid(ps.pid), signal.SIGTERM)
except AttributeError: #killpg not available on windows
ps.kill()
killgroup(ps)
rc = ps.returncode
if rc and check_return_code and rc > 0:
logging.info(output)
Expand Down