Skip to content

Commit

Permalink
Merge pull request #960 from cathales/fixes
Browse files Browse the repository at this point in the history
Fix Ctrl-C processing & use as lib with recent python versions
  • Loading branch information
kgugala authored Oct 27, 2023
2 parents f01f628 + 41284d6 commit f0c570d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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

0 comments on commit f0c570d

Please sign in to comment.