From 4510aaf39363caa82c11eb2ac02f567fa3983aff Mon Sep 17 00:00:00 2001 From: Kishi85 Date: Tue, 2 Apr 2019 10:57:57 +0200 Subject: [PATCH] acertmgr: properly format action output --- acertmgr/__init__.py | 18 +++++++++++++++--- acertmgr/tools.py | 9 +++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/acertmgr/__init__.py b/acertmgr/__init__.py index d5059df..58c83b7 100755 --- a/acertmgr/__init__.py +++ b/acertmgr/__init__.py @@ -185,10 +185,22 @@ def main(): try: # Run actions in a shell environment (to allow shell syntax) as stated in the configuration output = subprocess.check_output(action, shell=True, stderr=subprocess.STDOUT) - log("Executed '{}' successfully: {}".format(action, output)) + logmsg = "Action succeeded: {}".format(action) + if len(output) > 0: + if getattr(output, 'decode', None): + # Decode function available? Use it to get a proper str + output = output.decode('utf-8') + logmsg += os.linesep + tools.indent(output, 18) # 18 = len("Action succeeded: ") + log(logmsg) except subprocess.CalledProcessError as e: - log("Execution of '{}' failed with error '{}': {}".format(e.cmd, e.returncode, e.output), e, - error=True) + output = e.output + logmsg = "Action failed: ({}) {}".format(e.returncode, e.cmd) + if len(output) > 0: + if getattr(output, 'decode', None): + # Decode function available? Use it to get a proper str + output = output.decode('utf-8') + logmsg += os.linesep + tools.indent(output, 15) # 15 = len("Action failed: ") + log(logmsg, error=True) exceptions.append(e) deployment_success = False diff --git a/acertmgr/tools.py b/acertmgr/tools.py index 504bc16..7603bca 100644 --- a/acertmgr/tools.py +++ b/acertmgr/tools.py @@ -31,6 +31,12 @@ class InvalidCertificateError(Exception): pass +# @brief a simple, portable indent function +def indent(text, spaces=0): + ind = ' ' * spaces + return os.linesep.join(ind + line for line in text.splitlines()) + + # @brief wrapper for log output def log(msg, exc=None, error=False, warning=False): if error: @@ -49,8 +55,7 @@ def log(msg, exc=None, error=False, warning=False): else: formatted_exc = traceback.format_exception(type(exc), exc, getattr(exc, '__traceback__', None)) exc_string = ''.join(formatted_exc) if isinstance(formatted_exc, list) else str(formatted_exc) - indent = ' ' * len(prefix) - output += os.linesep + os.linesep.join(indent + line for line in exc_string.splitlines()) + output += os.linesep + indent(exc_string, len(prefix)) if error or warning: sys.stderr.write(output + os.linesep)