Skip to content

Commit 79d4aa4

Browse files
Removing escape sequence from output in cmd_output function!
In console outputs we are getting escape sequences because of which most of the testcases are failing to use the output of few commands. This PR removes the escape sequence first then use the output.This PR fixes multiple testcases. Signed-off-by: Anushree Mathur <[email protected]>
1 parent 72d2e67 commit 79d4aa4

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

aexpect/client.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def _get_aexpect_helper(self, helper_cmd, pass_fds, echo, command):
259259
full_output += output
260260
sub_status = sub.poll()
261261
if sub_status is not None:
262-
raise ExpectProcessTerminatedError(pattern, sub_status, full_output)
262+
raise ExpectProcessTerminatedError(
263+
pattern, sub_status, full_output)
263264
else:
264265
raise ExpectTimeoutError(pattern, full_output)
265266
return sub
@@ -1188,7 +1189,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
11881189
print_func)[1]
11891190

11901191
def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1191-
print_func=None, safe=False):
1192+
print_func=None, safe=False, strip_console_codes=False):
11921193
"""
11931194
Send a command and return its output.
11941195
@@ -1203,15 +1204,19 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12031204
error messages that make read_up_to_prompt to timeout. Let's
12041205
try to be a little more robust and send a carriage return, to
12051206
see if we can get to the prompt when safe=True.
1206-
1207+
:param strip_console_codes: Whether remove the escape sequence from the output
1208+
In serial session there are escape sequences present,if it is not
1209+
expected while reading the output remove it by passing
1210+
serial_console_codes = True
12071211
:return: The output of cmd
12081212
:raise ShellTimeoutError: Raised if timeout expires
12091213
:raise ShellProcessTerminatedError: Raised if the shell process
12101214
terminates while waiting for output
12111215
:raise ShellError: Raised if an unknown error occurs
1216+
12121217
"""
12131218
if safe:
1214-
return self.cmd_output_safe(cmd, timeout)
1219+
return self.cmd_output_safe(cmd, timeout, strip_console_codes)
12151220
session_tag = f"[{self.output_prefix}] " if self.output_prefix else ""
12161221
LOG.debug("%sSending command: %s", session_tag, cmd)
12171222
self.read_nonblocking(0, timeout)
@@ -1223,16 +1228,22 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12231228
raise ShellTimeoutError(cmd, output) from error
12241229
except ExpectProcessTerminatedError as error:
12251230
output = self.remove_command_echo(error.output, cmd)
1226-
raise ShellProcessTerminatedError(cmd, error.status, output) from error
1231+
raise ShellProcessTerminatedError(
1232+
cmd, error.status, output) from error
12271233
except ExpectError as error:
12281234
output = self.remove_command_echo(error.output, cmd)
12291235
raise ShellError(cmd, output) from error
12301236

12311237
# Remove the echoed command and the final shell prompt
1238+
if strip_console_codes:
1239+
# Removing the escape sequence
1240+
output_no_escape = astring.strip_console_codes(out)
1241+
return self.remove_last_nonempty_line(self.remove_command_echo(output_no_escape,
1242+
cmd))
12321243
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12331244
cmd))
12341245

1235-
def cmd_output_safe(self, cmd, timeout=60):
1246+
def cmd_output_safe(self, cmd, timeout=60, strip_console_codes=False):
12361247
"""
12371248
Send a command and return its output (serial sessions).
12381249
@@ -1244,6 +1255,10 @@ def cmd_output_safe(self, cmd, timeout=60):
12441255
:param cmd: Command to send (must not contain newline characters)
12451256
:param timeout: The duration (in seconds) to wait for the prompt to
12461257
return
1258+
:param strip_console_codes: Whether remove the escape sequence from the output
1259+
In serial session there are escape sequences present,if it is not
1260+
expected while reading the output remove it by passing
1261+
serial_console_codes = True
12471262
12481263
:return: The output of cmd
12491264
:raise ShellTimeoutError: Raised if timeout expires
@@ -1278,6 +1293,11 @@ def cmd_output_safe(self, cmd, timeout=60):
12781293
raise ShellTimeoutError(cmd, out)
12791294

12801295
# Remove the echoed command and the final shell prompt
1296+
if strip_console_codes:
1297+
# Removing the escape sequence
1298+
output_no_escape = astring.strip_console_codes(out)
1299+
return self.remove_last_nonempty_line(self.remove_command_echo(output_no_escape,
1300+
cmd))
12811301
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12821302
cmd))
12831303

0 commit comments

Comments
 (0)