From 3247b63cb99570d195278be99318a3681d84e790 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Mon, 14 Sep 2015 10:58:14 +0100 Subject: [PATCH] shh: handle backspaces in serial output --- wlauto/utils/ssh.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/wlauto/utils/ssh.py b/wlauto/utils/ssh.py index 44a00aac..836a27ee 100644 --- a/wlauto/utils/ssh.py +++ b/wlauto/utils/ssh.py @@ -196,7 +196,9 @@ class SshShell(object): if index == 0: self.conn.sendline(self.password) timed_out = self._wait_for_prompt(timeout) - output = re.sub(r'.*?{}'.format(re.escape(command)), '', self.conn.before, 1).strip() + output = re.sub(r' \r([^\n])', r'\1', self.conn.before) + output = process_backspaces(output) + output = re.sub(r'.*?{}'.format(re.escape(command)), '', output, 1).strip() else: if log: logger.debug(command) @@ -205,6 +207,7 @@ class SshShell(object): # the regex removes line breaks potential introduced when writing # command to shell. output = re.sub(r' \r([^\n])', r'\1', self.conn.before) + output = process_backspaces(output) command_index = output.find(command) output = output[command_index + len(command):].strip() if timed_out: @@ -258,3 +261,13 @@ def _check_env(): if not (ssh and scp): raise HostError('OpenSSH must be installed on the host.') + +def process_backspaces(text): + chars = [] + for c in text: + if c == chr(8) and chars: # backspace + chars.pop() + else: + chars.append(c) + return ''.join(chars) +