@@ -259,7 +259,8 @@ def _get_aexpect_helper(self, helper_cmd, pass_fds, echo, command):
259
259
full_output += output
260
260
sub_status = sub .poll ()
261
261
if sub_status is not None :
262
- raise ExpectProcessTerminatedError (pattern , sub_status , full_output )
262
+ raise ExpectProcessTerminatedError (
263
+ pattern , sub_status , full_output )
263
264
else :
264
265
raise ExpectTimeoutError (pattern , full_output )
265
266
return sub
@@ -1188,7 +1189,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
1188
1189
print_func )[1 ]
1189
1190
1190
1191
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 ):
1192
1193
"""
1193
1194
Send a command and return its output.
1194
1195
@@ -1203,15 +1204,19 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1203
1204
error messages that make read_up_to_prompt to timeout. Let's
1204
1205
try to be a little more robust and send a carriage return, to
1205
1206
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
1207
1211
:return: The output of cmd
1208
1212
:raise ShellTimeoutError: Raised if timeout expires
1209
1213
:raise ShellProcessTerminatedError: Raised if the shell process
1210
1214
terminates while waiting for output
1211
1215
:raise ShellError: Raised if an unknown error occurs
1216
+
1212
1217
"""
1213
1218
if safe :
1214
- return self .cmd_output_safe (cmd , timeout )
1219
+ return self .cmd_output_safe (cmd , timeout , strip_console_codes )
1215
1220
session_tag = f"[{ self .output_prefix } ] " if self .output_prefix else ""
1216
1221
LOG .debug ("%sSending command: %s" , session_tag , cmd )
1217
1222
self .read_nonblocking (0 , timeout )
@@ -1223,16 +1228,22 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1223
1228
raise ShellTimeoutError (cmd , output ) from error
1224
1229
except ExpectProcessTerminatedError as error :
1225
1230
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
1227
1233
except ExpectError as error :
1228
1234
output = self .remove_command_echo (error .output , cmd )
1229
1235
raise ShellError (cmd , output ) from error
1230
1236
1231
1237
# 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 ))
1232
1243
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1233
1244
cmd ))
1234
1245
1235
- def cmd_output_safe (self , cmd , timeout = 60 ):
1246
+ def cmd_output_safe (self , cmd , timeout = 60 , strip_console_codes = False ):
1236
1247
"""
1237
1248
Send a command and return its output (serial sessions).
1238
1249
@@ -1244,6 +1255,10 @@ def cmd_output_safe(self, cmd, timeout=60):
1244
1255
:param cmd: Command to send (must not contain newline characters)
1245
1256
:param timeout: The duration (in seconds) to wait for the prompt to
1246
1257
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
1247
1262
1248
1263
:return: The output of cmd
1249
1264
:raise ShellTimeoutError: Raised if timeout expires
@@ -1278,6 +1293,11 @@ def cmd_output_safe(self, cmd, timeout=60):
1278
1293
raise ShellTimeoutError (cmd , out )
1279
1294
1280
1295
# 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 ))
1281
1301
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1282
1302
cmd ))
1283
1303
0 commit comments