diff options
author | Michael Schwarz <michi.schwarz@gmail.com> | 2015-09-14 15:03:36 +0200 |
---|---|---|
committer | Michael Schwarz <michi.schwarz@gmail.com> | 2015-09-16 01:50:24 +0200 |
commit | a1f09b7f86c0d20941562f5b1254f9c0a2b9a535 (patch) | |
tree | 6b8707b18d893b7948a297879b460fa7cbbc5548 /support | |
parent | 2bd7e1e3d72da6b78f1946d508fa8f50f7e9ef67 (diff) | |
download | pogojig-a1f09b7f86c0d20941562f5b1254f9c0a2b9a535.tar.gz pogojig-a1f09b7f86c0d20941562f5b1254f9c0a2b9a535.tar.bz2 pogojig-a1f09b7f86c0d20941562f5b1254f9c0a2b9a535.zip |
Abstraction which allows a running process to be used as a context manager.
Diffstat (limited to 'support')
-rw-r--r-- | support/lib/util.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/support/lib/util.py b/support/lib/util.py index d14b342..48bfd46 100644 --- a/support/lib/util.py +++ b/support/lib/util.py @@ -56,7 +56,8 @@ def TemporaryDirectory(): shutil.rmtree(dir) -def command(args, remove_env = [], set_env = { }, working_dir = None): +@contextlib.contextmanager +def command_context(args, remove_env = [], set_env = { }, working_dir = None, use_stderr = False): env = dict(os.environ) for i in remove_env: @@ -71,10 +72,24 @@ def command(args, remove_env = [], set_env = { }, working_dir = None): except OSError as e: raise UserError('Error running {}: {}', args[0], e) + try: + yield process + except: + process.kill() + + raise + finally: + process.wait() + if process.returncode: raise UserError('Command failed: {}', ' '.join(args)) +def command(args, remove_env = [], set_env = { }, working_dir = None): + with command_context(args, remove_env, set_env, working_dir) as process: + process.wait() + + def bash_escape_string(string): return "'{}'".format(re.sub("'", "'\"'\"'", string)) |