From 448eeec0faa9765d0cea4f53394610f959ff6cfd Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 00:48:02 +0100 Subject: [PATCH] refactor: the interactive shell's end-of-output pattern lives in _shell (quality ratchet) 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. --- napalm_linux/_shell.py | 24 ++++++++++++++++++++++++ napalm_linux/linux.py | 27 ++------------------------- tests/quality_baseline.json | 2 +- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/napalm_linux/_shell.py b/napalm_linux/_shell.py index a780953..19fc0bd 100644 --- a/napalm_linux/_shell.py +++ b/napalm_linux/_shell.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +import re from typing import Any logger = logging.getLogger(__name__) @@ -11,6 +12,29 @@ logger = logging.getLogger(__name__) #: like a shell prompt. 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 + ```` 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): #: the shell writes no history file and records nothing more. The leading space #: keeps this line itself out of a history under ``HISTCONTROL=ignorespace``; diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index f339cc7..c8438ec 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -73,8 +73,7 @@ from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutExc from napalm_linux._hostname import HostnameMixin from napalm_linux._local_accounts import LinuxLocalAccountsMixin -from napalm_linux._shell import PROMPT_RE as _PROMPT_RE -from napalm_linux._shell import prepare_shell +from napalm_linux._shell import expect_for, prepare_shell from napalm_linux._sudo_history import LinuxSudoHistoryMixin 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) -#: 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 - ```` 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]: """``(output, exit_status)`` of a command followed by ``echo {_RC_MARKER}$?``. @@ -444,7 +421,7 @@ class LinuxDriver( command, read_timeout=read_timeout, cmd_verify=False, - expect_string=_expect_for(command), + expect_string=expect_for(command), ) return strip_terminal_codes(output).strip() diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 88483be..c558b17 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -22,7 +22,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2286 + "napalm_linux/linux.py": 2265 }, "untyped_public": [] }