[util/cli] Add option to ignore return codes

Add an option that ignores the return code of an execute, instead of
throwing an error (which sometimes causes ugly try/except blocks in
client code)
This commit is contained in:
Tobias Witek 2020-02-23 13:44:49 +01:00
parent 7a1cd4b613
commit 30c539f1f2
2 changed files with 8 additions and 2 deletions

View file

@ -2,7 +2,7 @@ import shlex
import subprocess
import logging
def execute(cmd, wait=True):
def execute(cmd, wait=True, ignore_errors=False):
args = shlex.split(cmd)
logging.debug(cmd)
try:
@ -12,7 +12,7 @@ def execute(cmd, wait=True):
if wait:
out, _ = proc.communicate()
if proc.returncode != 0:
if proc.returncode != 0 and not ignore_errors:
raise RuntimeError('{} exited with {}'.format(cmd, proc.returncode))
return out.decode('utf-8')
return ''