From fa26ae2f8f4cece87c463e6b2315ded63d8f0d92 Mon Sep 17 00:00:00 2001 From: Piclaw Date: Tue, 16 Jun 2026 13:48:20 -0700 Subject: [PATCH] Add Shell.append_if_missing() helper Adds the common grep-or-append idiom (grep -qxF line file || echo line >> file) as a reusable method, complementing reconfig(). The installer scripts repeat this pattern constantly for /etc/modules, blacklist, and config.txt entries. Uses re.escape so lines with regex metacharacters match literally. --- adafruit_shell.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adafruit_shell.py b/adafruit_shell.py index 65f4551..3dbed5f 100644 --- a/adafruit_shell.py +++ b/adafruit_shell.py @@ -558,6 +558,14 @@ def reconfig(self, file, pattern, replacement): # Not found; append (silently) self.write_text_file(file, replacement, append=True) + def append_if_missing(self, file, line): + """ + Append an exact line to a file only if it is not already present. + Equivalent to the shell idiom: grep -qxF "line" file || echo line >> file + """ + if not self.pattern_search(file, "^" + re.escape(line) + "$"): + self.write_text_file(file, line, append=True) + # pylint: disable=too-many-arguments def pattern_search( self, location, pattern, multi_line=False, return_match=False, find_all=False