From ebe4291c4457d84cb8425999cfa12371c1c7ce40 Mon Sep 17 00:00:00 2001 From: Cédric Ollivier Date: Sun, 16 Dec 2018 17:50:38 +0100 Subject: Allow printing bash cmd output in console MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It switches to Popen to print real-time console. Console has to be enabled per testcase (testcases.yaml). Change-Id: Id36b42c8409262f7c443e98ae2bcc465984b287f Signed-off-by: Cédric Ollivier --- xtesting/core/feature.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'xtesting/core') diff --git a/xtesting/core/feature.py b/xtesting/core/feature.py index 2cb36bd6..2730179f 100644 --- a/xtesting/core/feature.py +++ b/xtesting/core/feature.py @@ -15,7 +15,9 @@ helpers to run any python method or any bash command. import abc import logging +import os import subprocess +import sys import time import six @@ -86,8 +88,8 @@ class BashFeature(Feature): def __init__(self, **kwargs): super(BashFeature, self).__init__(**kwargs) - dir_results = "/var/lib/xtesting/results" - self.result_file = "{}/{}.log".format(dir_results, self.case_name) + self.res_dir = "/var/lib/xtesting/results/{}".format(self.case_name) + self.result_file = "{}/{}.log".format(self.res_dir, self.case_name) def execute(self, **kwargs): """Execute the cmd passed as arg @@ -101,12 +103,22 @@ class BashFeature(Feature): """ try: cmd = kwargs["cmd"] - with open(self.result_file, 'w+') as f_stdout: - subprocess.check_call( - cmd, shell=True, stdout=f_stdout, stderr=subprocess.STDOUT) - return 0 + console = kwargs["console"] if "console" in kwargs else False + if not os.path.isdir(self.res_dir): + os.makedirs(self.res_dir) + with open(self.result_file, 'w') as f_stdout: + self.__logger.info("Calling %s", cmd) + process = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + for line in iter(process.stdout.readline, ''): + if console: + sys.stdout.write(line) + f_stdout.write(line) + process.wait() + with open(self.result_file, 'r') as f_stdin: + self.__logger.debug("$ %s\n%s", cmd, f_stdin.read().rstrip()) + return process.returncode except KeyError: self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs) - except subprocess.CalledProcessError: - self.__logger.error("Execute command: %s failed", cmd) return -1 -- cgit 1.2.3-korg