diff --git a/dencam/recorder.py b/dencam/recorder.py index 8c714a3..ed11a40 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,36 @@ 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 Exception: + 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