Skip to content

Commit 3b6984a

Browse files
authored
Merge pull request #17 from makermelissa/main
Added more features
2 parents 0a235e3 + 54b2922 commit 3b6984a

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

adafruit_shell.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def reconfig(self, file, pattern, replacement):
282282
# Not found; append (silently)
283283
self.write_text_file(file, replacement, append=True)
284284

285-
def pattern_search(self, location, pattern, multi_line=False):
285+
def pattern_search(self, location, pattern, multi_line=False, return_match=False):
286286
"""
287287
Similar to grep, but uses pure python
288288
multi_line will search the entire file as a large text glob,
@@ -296,13 +296,17 @@ def pattern_search(self, location, pattern, multi_line=False):
296296
if self.exists(location) and not self.isdir(location):
297297
if multi_line:
298298
with open(location, "r+", encoding="utf-8") as file:
299-
if re.search(pattern, file.read(), flags=re.DOTALL):
299+
match = re.search(pattern, file.read(), flags=re.DOTALL)
300+
if match:
300301
found = True
301302
else:
302303
for line in fileinput.FileInput(location):
303-
if re.search(pattern, line):
304+
match = re.search(pattern, line)
305+
if match:
304306
found = True
305-
307+
break
308+
if return_match:
309+
return match
306310
return found
307311

308312
def pattern_replace(self, location, pattern, replace="", multi_line=False):
@@ -369,6 +373,37 @@ def copy(self, source, destination):
369373
destination += os.sep + os.path.basename(source)
370374
shutil.copy(source, destination)
371375

376+
def chmod(self, location, mode):
377+
"""
378+
Change the permissions of a file or directory
379+
"""
380+
location = self.path(location)
381+
if not 0 <= mode <= 0o777:
382+
raise ValueError("Invalid mode value")
383+
if os.path.exists(location):
384+
os.chmod(location, mode)
385+
386+
def chown(self, location, user, group=None, recursive=False):
387+
"""
388+
Change the owner of a file or directory
389+
"""
390+
if group is None:
391+
group = user
392+
393+
location = self.path(location)
394+
if recursive and os.path.isdir(location):
395+
for root, dirs, files in os.walk(location):
396+
for directory in dirs:
397+
shutil.chown(
398+
os.path.join(root, directory),
399+
user,
400+
group,
401+
)
402+
for file in files:
403+
shutil.chown(os.path.join(root, file), user, group)
404+
else:
405+
shutil.chown(location, user, group)
406+
372407
def remove(self, location):
373408
"""
374409
Remove a file or directory if it exists
@@ -472,6 +507,10 @@ def get_os(self):
472507
with open("/etc/os-release", encoding="utf-8") as f:
473508
if "Raspbian" in f.read():
474509
release = "Raspbian"
510+
if self.exists("/etc/rpi-issue"):
511+
with open("/etc/rpi-issue", encoding="utf-8") as f:
512+
if "Raspberry Pi" in f.read():
513+
release = "Raspbian"
475514
if self.run_command("command -v apt-get", suppress_message=True):
476515
with open("/etc/os-release", encoding="utf-8") as f:
477516
release_file = f.read()
@@ -526,6 +565,12 @@ def check_kernel_update_reboot_required(self):
526565

527566
# pylint: enable=invalid-name
528567

568+
def is_raspberry_pi_os(self):
569+
"""
570+
Check if we are running Raspberry Pi OS or Raspbian
571+
"""
572+
return self.get_os() == "Raspbian"
573+
529574
@staticmethod
530575
def is_raspberry_pi():
531576
"""

0 commit comments

Comments
 (0)