Skip to content
Snippets Groups Projects
Commit 85fceb71 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Changed output management (refs #19907).

parent e20c512d
No related branches found
No related tags found
No related merge requests found
...@@ -6,9 +6,8 @@ import utils ...@@ -6,9 +6,8 @@ import utils
def setup(interactive=True): def setup(interactive=True):
# Get hostname # Get hostname
utils.log('Getting system hostname.') utils.log('Getting system hostname.')
code, out = utils.exec_cmd(['hostname']) code, hostname = utils.exec_cmd('hostname')
if code == 0: if code == 0:
hostname = out
utils.log('Hostname is %s.' % hostname) utils.log('Hostname is %s.' % hostname)
else: else:
raise Exception('Failed to get hostname.') raise Exception('Failed to get hostname.')
......
...@@ -118,9 +118,14 @@ class Tester(): ...@@ -118,9 +118,14 @@ class Tester():
description = self.get_file_description(test_path) description = self.get_file_description(test_path)
# Run test # Run test
try: try:
code, out = utils.exec_cmd(test_path) p = subprocess.Popen([test_path], stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if code != 0: out, err = p.communicate()
raise Exception('Command exited with code %s.' % code) if out:
log(out.decode('utf-8').strip())
if err:
log(err.decode('utf-8').strip())
if p.returncode != 0:
raise Exception('Command exited with code %s.' % p.returncode)
except Exception as e: except Exception as e:
exit_code = 1 exit_code = 1
log(e) log(e)
......
...@@ -21,23 +21,26 @@ def get_dir(file_path): ...@@ -21,23 +21,26 @@ def get_dir(file_path):
return os.path.dirname(os.path.abspath(os.path.expanduser(file_path))) return os.path.dirname(os.path.abspath(os.path.expanduser(file_path)))
def exec_cmd(cmd, log_output=True): def exec_cmd(cmd, log_output=True, get_output=True):
shell = not isinstance(cmd, (tuple, list)) shell = not isinstance(cmd, (tuple, list))
p = subprocess.Popen(cmd, stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell) stdout = subprocess.PIPE if get_output or not log_output else sys.stdout
stderr = subprocess.PIPE if get_output or not log_output else sys.stderr
p = subprocess.Popen(cmd, stdin=sys.stdin, stdout=stdout, stderr=stderr, shell=shell)
out, err = p.communicate() out, err = p.communicate()
out = out.decode('utf-8').strip() if out else '' if get_output:
if err: out = out.decode('utf-8').strip() if out else ''
if out: if err:
out += '\n' if out:
out += err.decode('utf-8').strip() out += '\n'
out = out.strip() out += err.decode('utf-8').strip()
if log_output: out = out.strip()
log(out) if log_output:
log(out)
return p.returncode, out return p.returncode, out
def check_cmd(cmd): def check_cmd(cmd, log_output=False):
code, out = exec_cmd(cmd, log_output=False) code, out = exec_cmd(cmd, log_output, False)
return code return code
...@@ -136,7 +139,7 @@ def run_commands(cmds): ...@@ -136,7 +139,7 @@ def run_commands(cmds):
log('A backup file already exist for:\n%s' % cmd['target']) log('A backup file already exist for:\n%s' % cmd['target'])
else: else:
log('>>> ' + cmd['line']) log('>>> ' + cmd['line'])
code = check_cmd(cmd['line']) code = check_cmd(cmd['line'], log_output=True)
if code != 0: if code != 0:
raise Exception('Command exited with code %s.' % code) raise Exception('Command exited with code %s.' % code)
except Exception as e: except Exception as e:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment