Add execute_handled

This commit is contained in:
erik 2023-07-13 11:07:59 +02:00 committed by Michael Jerger
parent 9c44bebd5d
commit 8b11a4fa61

View file

@ -1,4 +1,4 @@
from subprocess import Popen, PIPE, run from subprocess import Popen, PIPE, run, CalledProcessError
from pathlib import Path from pathlib import Path
from sys import stdout from sys import stdout
from os import chmod, environ from os import chmod, environ
@ -101,7 +101,7 @@ class ExecutionApi:
output = output.rstrip() output = output.rstrip()
return output return output
def execute_live(self, command, dry_run=False, shell=True): def execute_live(self, command: str, dry_run=False, shell=True):
if dry_run: if dry_run:
print(command) print(command)
else: else:
@ -111,6 +111,21 @@ class ExecutionApi:
process.stdout.close() process.stdout.close()
process.wait() process.wait()
def execute_handled(self, command: str, dry_run=False, shell=True, check=True):
if dry_run:
print(command)
else:
try:
run(
command,
shell=shell,
check=check,
stderr=PIPE,
text=True)
except CalledProcessError as exc:
print("Command failed with code: ", exc.returncode, " and message:", exc.stderr)
raise exc
class EnvironmentApi: class EnvironmentApi:
def get(self, key): def get(self, key):