mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-devtest
synced 2026-01-15 02:22:56 +01:00
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:
parent
1740f3c5c8
commit
59b0c60aee
1 changed files with 8 additions and 1 deletions
|
|
@ -6,9 +6,13 @@ import traceback
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from enum import IntFlag
|
||||||
|
|
||||||
from jwutils.log import *
|
from jwutils.log import *
|
||||||
|
|
||||||
|
class ExecFlags(IntFlag): # export
|
||||||
|
NoWaitForNewline = 0x01
|
||||||
|
|
||||||
def consume_readbuffer(buf, carry, cmd, log_act=None, caller=None):
|
def consume_readbuffer(buf, carry, cmd, log_act=None, caller=None):
|
||||||
r = []
|
r = []
|
||||||
lines = buf.splitlines() if buf else []
|
lines = buf.splitlines() if buf else []
|
||||||
|
|
@ -38,7 +42,7 @@ def consume_readbuffer(buf, carry, cmd, log_act=None, caller=None):
|
||||||
# None on error
|
# None on error
|
||||||
# [] for an empty response
|
# [] for an empty response
|
||||||
# [first line, second line]
|
# [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 = []
|
r = []
|
||||||
try:
|
try:
|
||||||
if isinstance(cmd, str) and not cmd.endswith('\n'):
|
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:
|
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))
|
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)
|
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)
|
r.extend(ret)
|
||||||
if rest < 0 or not buf:
|
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")
|
d = os.getenv("JW_DEVTEST_RESPONSE_DIR")
|
||||||
if d is not None:
|
if d is not None:
|
||||||
path = d + '/' + re.sub('[^a-zA-Z0-9]+', '_', cmd)
|
path = d + '/' + re.sub('[^a-zA-Z0-9]+', '_', cmd)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue