fixed media files cleanup issues (closes #152)
authorCalin Crisan <ccrisan@gmail.com>
Sun, 31 May 2015 13:23:59 +0000 (16:23 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sun, 31 May 2015 13:24:43 +0000 (16:24 +0300)
src/cleanup.py
src/mediafiles.py

index 3355af256260142a0c2a75130d581b5aa35a92da..a032ef16bbc4444376e1d0f9596fcc6ac70eeb6d 100644 (file)
@@ -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' % {
index e6bc5a83d90079e09ddd179eb95b24ce69c45595..f7c5fc1a412f6c4d7466a7d87169d298ccfb8dca 100644 (file)
@@ -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():