From 34c9b4869a3f6372babf727c4decd995c7816344 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sun, 31 May 2015 16:23:59 +0300 Subject: [PATCH] fixed media files cleanup issues (closes #152) --- src/cleanup.py | 1 + src/mediafiles.py | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/cleanup.py b/src/cleanup.py index 3355af2..a032ef1 100644 --- a/src/cleanup.py +++ b/src/cleanup.py @@ -89,6 +89,7 @@ def _do_cleanup(): try: mediafiles.cleanup_media('picture') mediafiles.cleanup_media('movie') + logging.debug('cleanup done') except Exception as e: logging.error('failed to cleanup media files: %(msg)s' % { diff --git a/src/mediafiles.py b/src/mediafiles.py index e6bc5a8..f7c5fc1 100644 --- a/src/mediafiles.py +++ b/src/mediafiles.py @@ -118,20 +118,41 @@ def _remove_older_files(dir, moment, exts): logging.debug('removing file %(path)s...' % {'path': full_path}) # remove the file itself - os.remove(full_path) + try: + os.remove(full_path) + + except OSError as e: + if e.errno == errno.ENOENT: + pass # the file might have been removed in the meantime + + else: + logging.error('failed to remove %s: %s' % (full_path, e)) - # remove the parent directories if empty or contains only thumb files + # remove the parent directories if empty or contain only thumb files dir_path = os.path.dirname(full_path) + if not os.path.exists(dir_path): + continue + listing = os.listdir(dir_path) thumbs = [l for l in listing if l.endswith('.thumb')] if len(listing) == len(thumbs): # only thumbs for p in thumbs: - os.remove(os.path.join(dir_path, p)) + try: + os.remove(os.path.join(dir_path, p)) + + except: + logging.error('failed to remove %s: %s' % (p, e)) if not listing or len(listing) == len(thumbs): + # this will possibly cause following paths that are in the media files for loop + # to be removed in advance; the os.remove call will raise ENOENT which is silently ignored logging.debug('removing empty directory %(path)s...' % {'path': dir_path}) - os.removedirs(dir_path) + try: + os.removedirs(dir_path) + + except: + logging.error('failed to remove %s: %s' % (dir_path, e)) def find_ffmpeg(): -- 2.39.5