From 2871b1871086d83210a57002ec94a2d0f953c372 Mon Sep 17 00:00:00 2001 From: iamgarcia <32944795+iamgarcia@users.noreply.github.com> Date: Fri, 11 Feb 2022 16:52:09 -0800 Subject: [PATCH 1/2] Add helper function to clear ghost drives - Add subprocess and sys as imports - Add helper function to clear ghost drives --- dencam/recorder.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dencam/recorder.py b/dencam/recorder.py index 8c714a3..7981b60 100644 --- a/dencam/recorder.py +++ b/dencam/recorder.py @@ -9,6 +9,8 @@ import os import getpass import time +import subprocess +import sys from abc import ABC, abstractmethod import picamera @@ -66,6 +68,7 @@ def __init__(self, configs): self.VID_FILE_SIZE = self.FILE_SIZE * self.SAFETY_FACTOR self.recording = False self.last_known_video_path = None + self._clear_ghost_drives() self.video_path = self._video_path_selector() @abstractmethod @@ -164,6 +167,30 @@ def _check_home_storage_capacity(self, media_path): return media_path + def _clear_ghost_drives(self): + log.info('Clearing ghost drives, if any are present.') + user = getpass.getuser() + media_dir = os.path.join('/media', user) + usb_drive_list = os.listdir(media_dir) + if len(usb_drive_list) > 0: + for usb_drive in usb_drive_list: + usb_drive_dir = media_dir + '/' + usb_drive + try: + result = subprocess.Popen(['sudo', 'rmdir', usb_drive_dir], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + output, error = result.communicate() + if output: + log.info('Ghost drive (subprocess - output): ' + output.decode('ascii')) + if error: + log.info('Ghost drive (subprocess - error): ' + error.decode('ascii').strip()) + except OSError as os_error: + log.info('Ghost drive (OSError - code): ' + os_error.errno) + log.info('Ghost drive (OSError - message): ' + os_error.strerror) + log.info('Ghost drive (OSError - filename): ' + os_error.filename) + except: + log.info('Ghost drive (Error): ' + sys.exc_info()) + def get_free_space(self, media_path=None): """Get the remaining space on SD card in gigabytes From 4dc1b04952e4c741612ce9b90364f16c41168876 Mon Sep 17 00:00:00 2001 From: iamgarcia <32944795+iamgarcia@users.noreply.github.com> Date: Fri, 11 Feb 2022 19:35:45 -0800 Subject: [PATCH 2/2] Lint recorder.py --- dencam/recorder.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/dencam/recorder.py b/dencam/recorder.py index 7981b60..ed11a40 100644 --- a/dencam/recorder.py +++ b/dencam/recorder.py @@ -176,19 +176,25 @@ def _clear_ghost_drives(self): for usb_drive in usb_drive_list: usb_drive_dir = media_dir + '/' + usb_drive try: - result = subprocess.Popen(['sudo', 'rmdir', usb_drive_dir], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + result = subprocess.Popen( + ['sudo', 'rmdir', usb_drive_dir], + stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) output, error = result.communicate() if output: - log.info('Ghost drive (subprocess - output): ' + output.decode('ascii')) + log.info('Ghost drive (subprocess - output): ' + + output.decode('ascii')) if error: - log.info('Ghost drive (subprocess - error): ' + error.decode('ascii').strip()) + log.info('Ghost drive (subprocess - error): ' + + error.decode('ascii').strip()) except OSError as os_error: - log.info('Ghost drive (OSError - code): ' + os_error.errno) - log.info('Ghost drive (OSError - message): ' + os_error.strerror) - log.info('Ghost drive (OSError - filename): ' + os_error.filename) - except: + log.info('Ghost drive (OSError - code): ' + + os_error.errno) + log.info('Ghost drive (OSError - message): ' + + os_error.strerror) + log.info('Ghost drive (OSError - filename): ' + + os_error.filename) + except Exception: log.info('Ghost drive (Error): ' + sys.exc_info()) def get_free_space(self, media_path=None):