refactor: the interactive shell's end-of-output pattern lives in _shell (quality ratchet)
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m15s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 3m5s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m15s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 3m5s
driver / publish (pull_request) Skipped
expect_for and STATUS_ECHO_RE say when netmiko's interactive shell has finished printing a command's output, next to PROMPT_RE they build on. Moving them out keeps linux.py within its line count now that it mixes in the personal accounts; the baseline goes down to 2265.
This commit is contained in:
1 parent
9ce915a8b9
commit
448eeec0fa
3 files changed
+27
-26
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -11,6 +12,29 @@ logger = logging.getLogger(__name__)
|
|||||||
#: like a shell prompt.
|
#: like a shell prompt.
|
||||||
PROMPT_RE = r"[#$\>]\s*$"
|
PROMPT_RE = r"[#$\>]\s*$"
|
||||||
|
|
||||||
|
#: A command ending in ``echo __NAME=$?`` reports its exit status on a line of its
|
||||||
|
#: own: the driver's ``_RC_MARKER``, ``__APT_RC=`` and ``__SVC_RC=`` in
|
||||||
|
#: napalm-device-types.
|
||||||
|
STATUS_ECHO_RE = re.compile(r"echo\s+(__[A-Z_]+=)\$\?")
|
||||||
|
|
||||||
|
|
||||||
|
def expect_for(command: str) -> str:
|
||||||
|
"""The pattern that ends *command*'s output.
|
||||||
|
|
||||||
|
netmiko stops reading as soon as the pattern matches what it has read so far.
|
||||||
|
A line the command prints can end in ``#``, ``$`` or ``>`` -- apt's
|
||||||
|
``<ftpmaster@ubuntu.com>`` after a bad signature -- and was taken for the
|
||||||
|
prompt: half the output came back, and the rest started the next command's
|
||||||
|
(#615). A command that echoes its exit status is read until that marker, with
|
||||||
|
a number, and the prompt line after it. The echoed command line carries a
|
||||||
|
literal ``$?`` and cannot match.
|
||||||
|
"""
|
||||||
|
markers: list[str] = STATUS_ECHO_RE.findall(command)
|
||||||
|
if not markers:
|
||||||
|
return PROMPT_RE
|
||||||
|
return re.escape(markers[-1]) + r"\d+\s*\n.*" + PROMPT_RE
|
||||||
|
|
||||||
|
|
||||||
#: Sent before anything else in the interactive session (NetOrk/netork#1039):
|
#: Sent before anything else in the interactive session (NetOrk/netork#1039):
|
||||||
#: the shell writes no history file and records nothing more. The leading space
|
#: the shell writes no history file and records nothing more. The leading space
|
||||||
#: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``;
|
#: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``;
|
||||||
|
|||||||
+2
-25
@@ -73,8 +73,7 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc
|
|||||||
|
|
||||||
from napalm_linux._hostname import HostnameMixin
|
from napalm_linux._hostname import HostnameMixin
|
||||||
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
|
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
|
||||||
from napalm_linux._shell import PROMPT_RE as _PROMPT_RE
|
from napalm_linux._shell import expect_for, prepare_shell
|
||||||
from napalm_linux._shell import prepare_shell
|
|
||||||
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
|
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
|
||||||
|
|
||||||
logger = logging.getLogger("napalm_linux")
|
logger = logging.getLogger("napalm_linux")
|
||||||
@@ -89,28 +88,6 @@ _RC_MARKER = "__NETORK_RC="
|
|||||||
_RC_MARKER_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
|
_RC_MARKER_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
#: A command ending in ``echo __NAME=$?`` reports its exit status on a line of its
|
|
||||||
#: own: ``_RC_MARKER`` here, ``__APT_RC=`` and ``__SVC_RC=`` in napalm-device-types.
|
|
||||||
_STATUS_ECHO_RE = re.compile(r"echo\s+(__[A-Z_]+=)\$\?")
|
|
||||||
|
|
||||||
|
|
||||||
def _expect_for(command: str) -> str:
|
|
||||||
"""The pattern that ends *command*'s output.
|
|
||||||
|
|
||||||
netmiko stops reading as soon as the pattern matches what it has read so far.
|
|
||||||
A line the command prints can end in ``#``, ``$`` or ``>`` -- apt's
|
|
||||||
``<ftpmaster@ubuntu.com>`` after a bad signature -- and was taken for the
|
|
||||||
prompt: half the output came back, and the rest started the next command's
|
|
||||||
(#615). A command that echoes its exit status is read until that marker, with
|
|
||||||
a number, and the prompt line after it. The echoed command line carries a
|
|
||||||
literal ``$?`` and cannot match.
|
|
||||||
"""
|
|
||||||
markers = _STATUS_ECHO_RE.findall(command)
|
|
||||||
if not markers:
|
|
||||||
return _PROMPT_RE
|
|
||||||
return re.escape(markers[-1]) + r"\d+\s*\n.*" + _PROMPT_RE
|
|
||||||
|
|
||||||
|
|
||||||
def _split_status(raw: str) -> tuple[str, int | None]:
|
def _split_status(raw: str) -> tuple[str, int | None]:
|
||||||
"""``(output, exit_status)`` of a command followed by ``echo {_RC_MARKER}$?``.
|
"""``(output, exit_status)`` of a command followed by ``echo {_RC_MARKER}$?``.
|
||||||
|
|
||||||
@@ -444,7 +421,7 @@ class LinuxDriver(
|
|||||||
command,
|
command,
|
||||||
read_timeout=read_timeout,
|
read_timeout=read_timeout,
|
||||||
cmd_verify=False,
|
cmd_verify=False,
|
||||||
expect_string=_expect_for(command),
|
expect_string=expect_for(command),
|
||||||
)
|
)
|
||||||
return strip_terminal_codes(output).strip()
|
return strip_terminal_codes(output).strip()
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"napalm_linux.linux"
|
"napalm_linux.linux"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_linux/linux.py": 2286
|
"napalm_linux/linux.py": 2265
|
||||||
},
|
},
|
||||||
"untyped_public": []
|
"untyped_public": []
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user