From c71ddd7fe6c7f48de4df7517e41a3a09f9a57656 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 08:13:20 +0100 Subject: [PATCH] refactor(apt_proxy): smaller parts, under the standard's complexity limit The quality ratchet counted _dump, parse_apt_proxy and _https_direct over complexity 10 (NetOrk/netork#1811). Same behaviour, same tests. --- napalm_device_types/apt_proxy.py | 73 ++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/napalm_device_types/apt_proxy.py b/napalm_device_types/apt_proxy.py index e8e933a..daa70c3 100644 --- a/napalm_device_types/apt_proxy.py +++ b/napalm_device_types/apt_proxy.py @@ -226,22 +226,31 @@ def _dump(lines: list[str]) -> _Dump: "https_per_host": {}, } for found in (_DUMP.match(line.strip()) for line in lines): - if not found: - continue - scheme, suffix, value = found.groups() - https = scheme.lower() == "https" - if not suffix and https: - dump["https"] = value or None - elif not suffix: - dump["proxy"] = None if value.lower() in _NO_PROXY else value - elif suffix.lower() == "-auto-detect": - if not https: - dump["auto_detect"] = value or None - elif value: - dump["https_per_host" if https else "per_host"][suffix[2:]] = value + if found: + _take(dump, *found.groups()) return dump +def _take(dump: _Dump, scheme: str, suffix: str | None, value: str) -> None: + """One line of the dump into *dump*; a later one of the same key wins, as in apt.""" + https = scheme.lower() == "https" + if not suffix: + _take_for_all(dump, https, value) + elif suffix.lower() == "-auto-detect": + if not https: + dump["auto_detect"] = value or None + elif value: + dump["https_per_host" if https else "per_host"][suffix[2:]] = value + + +def _take_for_all(dump: _Dump, https: bool, value: str) -> None: + """The proxy for every host: https keeps an empty value as unset, apt's fallback.""" + if https: + dump["https"] = value or None + else: + dump["proxy"] = None if value.lower() in _NO_PROXY else value + + def _https_proxy(dump: _Dump) -> str | None: """apt's https method takes its own setting and falls back to the http one.""" if dump["https"] is None: @@ -278,15 +287,25 @@ def parse_apt_proxy(output: str) -> AptProxyReportDict: "proxy": dump["proxy"], "per_host": dump["per_host"], "auto_detect": dump["auto_detect"], - "settings": [s for s in (_setting(line, main) for main, line in lines) if s], + "settings": _settings(lines, _setting), "https_proxy": _https_proxy(dump), "https_per_host": {**dump["per_host"], **dump["https_per_host"]}, - "https_settings": [s for s in (_https_setting(line, main) for main, line in lines) if s], - "http_sources": [s for s in sources if s.startswith("http://")], - "https_sources": [s for s in sources if s.startswith("https://")], + "https_settings": _settings(lines, _https_setting), + "http_sources": _of_scheme(sources, "http"), + "https_sources": _of_scheme(sources, "https"), } +def _settings( + lines: list[tuple[bool, str]], parse: Callable[[str, bool], AptProxySettingDict | None] +) -> list[AptProxySettingDict]: + return [s for s in (parse(line, main) for main, line in lines) if s] + + +def _of_scheme(sources: list[str], scheme: str) -> list[str]: + return [s for s in sources if s.startswith(f"{scheme}://")] + + _DEFAULT_PORT = {"http": 80, "https": 443} @@ -593,11 +612,7 @@ class AptProxyMixin(PackageProxyMixin): written = self._write_package_proxy(url, https_direct=True) if not written["ok"]: return written - later = [ - s - for s in self._report()["https_settings"] - if s["file"] != self.APT_PROXY_FILE and same_proxy_url(s["value"], url) - ] + later = self._later_https_lines(self._report(), url) for path, numbers in _editable_lines(later).items(): outcome = self._run_apt_root_command( comment_out_command(path, numbers, scheme="https"), f"commented out in {path}" @@ -605,16 +620,20 @@ class AptProxyMixin(PackageProxyMixin): if not outcome["ok"]: return outcome report = self._report() - still = [ - f"{s['file']}:{s['line']}" - for s in report["https_settings"] - if s["file"] != self.APT_PROXY_FILE and same_proxy_url(s["value"], url) - ] if same_proxy_url(report["https_proxy"], url): + still = [f"{s['file']}:{s['line']}" for s in self._later_https_lines(report, url)] where = f", set in {', '.join(still)}" if still else "" return {"ok": False, "output": f"https still goes through {url}{where}"} return {"ok": True, "output": "; ".join(map(_described, later))} + def _later_https_lines(self, report: AptProxyReportDict, url: str) -> list[AptProxySettingDict]: + """The lines outside :attr:`APT_PROXY_FILE` that send https to *url*.""" + return [ + s + for s in report["https_settings"] + if s["file"] != self.APT_PROXY_FILE and same_proxy_url(s["value"], url) + ] + def _https_source( report: AptProxyReportDict, through: Callable[[str | None], bool]