os.misc.cmd_exec(): Support NoWaitForNewline

Remove newlines argument from cmd_exec(), which wasn't honoured
anyway, and replace it with the more generic flags argument in
conjunction with support for the NoWaitForNewline flag, which does
the obvious.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2024-01-18 12:24:52 +01:00
commit 59b0c60aee

View file

@ -6,9 +6,13 @@ import traceback
import re
import os
from datetime import datetime, timedelta
from enum import IntFlag
from jwutils.log import *
class ExecFlags(IntFlag): # export
NoWaitForNewline = 0x01
def consume_readbuffer(buf, carry, cmd, log_act=None, caller=None):
r = []
lines = buf.splitlines() if buf else []
@ -38,7 +42,7 @@ def consume_readbuffer(buf, carry, cmd, log_act=None, caller=None):
# None on error
# [] for an empty response
# [first line, second line]
async def cmd_exec(conn, cmd, act_timeout=0.1, total_timeout=None, newlines=False, log_act=None, caller=None, echo_cmd=True): # export
async def cmd_exec(conn, cmd, act_timeout=0.1, total_timeout=None, log_act=None, caller=None, echo_cmd=True, flags = 0x0): # export
r = []
try:
if isinstance(cmd, str) and not cmd.endswith('\n'):
@ -57,8 +61,11 @@ async def cmd_exec(conn, cmd, act_timeout=0.1, total_timeout=None, newlines=Fals
if buf is not None and end and len(buf) == 0 and datetime.now() < end:
raise Exception("connection {} reset while reading command response".format(conn))
ret, carry = consume_readbuffer(buf, carry, None if echo_cmd else cmd, log_act=log_act, caller=caller)
#slog(DEBUG, 'ret = "{}", carry= "{}"'.format(ret, carry))
r.extend(ret)
if rest < 0 or not buf:
if (flags & ExecFlags.NoWaitForNewline) and carry and len(carry):
r.extend([carry])
d = os.getenv("JW_DEVTEST_RESPONSE_DIR")
if d is not None:
path = d + '/' + re.sub('[^a-zA-Z0-9]+', '_', cmd)