diff options
Diffstat (limited to 'xtesting/core/feature.py')
-rw-r--r-- | xtesting/core/feature.py | 28 |
1 files changed, 20 insertions, 8 deletions
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 |