@@ -282,7 +282,7 @@ def reconfig(self, file, pattern, replacement):
282
282
# Not found; append (silently)
283
283
self .write_text_file (file , replacement , append = True )
284
284
285
- def pattern_search (self , location , pattern , multi_line = False ):
285
+ def pattern_search (self , location , pattern , multi_line = False , return_match = False ):
286
286
"""
287
287
Similar to grep, but uses pure python
288
288
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):
296
296
if self .exists (location ) and not self .isdir (location ):
297
297
if multi_line :
298
298
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 :
300
301
found = True
301
302
else :
302
303
for line in fileinput .FileInput (location ):
303
- if re .search (pattern , line ):
304
+ match = re .search (pattern , line )
305
+ if match :
304
306
found = True
305
-
307
+ break
308
+ if return_match :
309
+ return match
306
310
return found
307
311
308
312
def pattern_replace (self , location , pattern , replace = "" , multi_line = False ):
@@ -369,6 +373,37 @@ def copy(self, source, destination):
369
373
destination += os .sep + os .path .basename (source )
370
374
shutil .copy (source , destination )
371
375
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
+
372
407
def remove (self , location ):
373
408
"""
374
409
Remove a file or directory if it exists
@@ -472,6 +507,10 @@ def get_os(self):
472
507
with open ("/etc/os-release" , encoding = "utf-8" ) as f :
473
508
if "Raspbian" in f .read ():
474
509
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"
475
514
if self .run_command ("command -v apt-get" , suppress_message = True ):
476
515
with open ("/etc/os-release" , encoding = "utf-8" ) as f :
477
516
release_file = f .read ()
@@ -526,6 +565,12 @@ def check_kernel_update_reboot_required(self):
526
565
527
566
# pylint: enable=invalid-name
528
567
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
+
529
574
@staticmethod
530
575
def is_raspberry_pi ():
531
576
"""
0 commit comments