Handle errors in execute

This commit is contained in:
erik 2023-07-18 11:10:09 +02:00
parent f04477f137
commit 43988291c6

View file

@ -1,4 +1,4 @@
from subprocess import Popen, PIPE, run, CalledProcessError from subprocess import Popen, STDOUT, 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
@ -87,11 +87,18 @@ class ExecutionApi:
if dry_run: if dry_run:
print(command) print(command)
else: else:
# output = check_output(command, encoding="UTF-8", shell=shell) try:
output = run( output = run(
command, encoding="UTF-8", shell=shell, stdout=PIPE, check=check command,
).stdout shell=shell,
output = output.rstrip() check=check,
stdout=PIPE,
stderr=STDOUT,
text=True).stdout
output = output.rstrip()
except CalledProcessError as exc:
print("Command failed with code: ", exc.returncode, " and message:", exc.stderr)
raise exc
return output return output
# TODO: check for exception handling # TODO: check for exception handling
@ -107,22 +114,6 @@ class ExecutionApi:
process.stdout.close() process.stdout.close()
process.wait() process.wait()
# TODO: move this enhancement to execute
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):