From 59b0c60aee833eae696073f94e4a6ba8953ebab6 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Thu, 18 Jan 2024 12:24:52 +0100 Subject: [PATCH] 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 --- src/python/devtest/os/misc.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/python/devtest/os/misc.py b/src/python/devtest/os/misc.py index de20a93..af63f75 100644 --- a/src/python/devtest/os/misc.py +++ b/src/python/devtest/os/misc.py @@ -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)