From: Loic Dachary Date: Sun, 14 Jun 2015 07:35:35 +0000 (+0200) Subject: misc.py: sh helper to log command and output X-Git-Tag: 1.1.0~884^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F530%2Fhead;p=teuthology.git 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 --- diff --git a/teuthology/misc.py b/teuthology/misc.py index b27041e70..bf6f709b3 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')