From 43988291c6507eec20bf8ebaa7e93d392c3256b1 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 18 Jul 2023 11:10:09 +0200 Subject: [PATCH] Handle errors in execute --- .../infrastructure/infrastructure.py | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 14cc14c..e79129d 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -1,4 +1,4 @@ -from subprocess import Popen, PIPE, run, CalledProcessError +from subprocess import Popen, STDOUT, PIPE, run, CalledProcessError from pathlib import Path from sys import stdout from os import chmod, environ @@ -87,11 +87,18 @@ class ExecutionApi: if dry_run: print(command) else: - # output = check_output(command, encoding="UTF-8", shell=shell) - output = run( - command, encoding="UTF-8", shell=shell, stdout=PIPE, check=check - ).stdout - output = output.rstrip() + try: + output = run( + command, + shell=shell, + 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 # TODO: check for exception handling @@ -107,22 +114,6 @@ class ExecutionApi: process.stdout.close() 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: def get(self, key):