From 2adeb5a788b8cbc29efb9d0cdaab1199e93f2a1d Mon Sep 17 00:00:00 2001 From: Loic Dachary Date: Sun, 14 Jun 2015 09:35:35 +0200 Subject: [PATCH] misc.py: sh helper to log command and output It is the same as subprocess.check_output, only it log.debug the command being run as well as its output. Signed-off-by: Loic Dachary --- teuthology/misc.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/teuthology/misc.py b/teuthology/misc.py index b27041e701..bf6f709b3b 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -10,6 +10,7 @@ import logging import configobj import getpass import socket +import subprocess import sys import tarfile import time @@ -1257,3 +1258,21 @@ def is_in_dict(searchkey, searchval, d): return True else: return searchval == val + + +def sh(command): + """ + Run the shell command and return the output in ascii (stderr and + stdout). If the command fails, raise an exception. The command + and its output are logged, on success and on error. + """ + log.debug(command) + output = '' + try: + output = subprocess.check_output(command, stderr=subprocess.STDOUT, + shell=True) + except subprocess.CalledProcessError as e: + log.exception(command + " error " + str(e.output)) + raise e + log.debug(command + " output " + str(output)) + return output.decode('utf-8') -- 2.39.5