X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=exec_utils.py;h=b52f52f0dc785033fc437a638c734d79cf8d5aa8;hb=2c43e31aae1138f79c30619cde25746e41390152;hp=7e9dae51fe564883db085ecf1505fce9c9ab39e7;hpb=ed47f1a0c31184280a303563237e34c0e53437d7;p=python_utils.git diff --git a/exec_utils.py b/exec_utils.py index 7e9dae5..b52f52f 100644 --- a/exec_utils.py +++ b/exec_utils.py @@ -1,11 +1,29 @@ #!/usr/bin/env python3 import atexit +import logging import shlex import subprocess from typing import List, Optional +logger = logging.getLogger(__file__) + + +def cmd_showing_output(command: str) -> None: + p = subprocess.Popen( + command, + shell=True, + bufsize=0, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + for line in iter(p.stdout.readline, b''): + print(line.decode('utf-8'), end='') + p.stdout.close() + p.wait() + + def cmd_with_timeout(command: str, timeout_seconds: Optional[float]) -> int: """ Run a command but do not let it run for more than timeout seconds. @@ -72,6 +90,7 @@ def cmd_in_background( stderr=subprocess.DEVNULL) else: subproc = subprocess.Popen(args, stdin=subprocess.DEVNULL) + def kill_subproc() -> None: try: if subproc.poll() is None: @@ -79,7 +98,7 @@ def cmd_in_background( subproc.terminate() subproc.wait(timeout=10.0) except BaseException as be: - log.error(be) + logger.exception(be) atexit.register(kill_subproc) return subproc