Module: deluge
Branch: 1.3-stable
Commit: 2354eeca7b49a05cf61fa6628549ccd08b0ac827

Author: Calum Lind <[email protected]>
Date:   Tue Feb  8 19:04:35 2011 +0000

Fix #690 - Renaming folders does not remove old empty folders

---

 deluge/core/torrentmanager.py |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py
index 4ce881c..77d1de9 100644
--- a/deluge/core/torrentmanager.py
+++ b/deluge/core/torrentmanager.py
@@ -730,6 +730,36 @@ class TorrentManager(component.Component):
             fastresume_file.close()
         except IOError:
             log.warning("Error trying to save fastresume file")
+        
+    def remove_empty_folders(self, torrent_id, folder):
+        """
+        Recursively removes folders but only if they are empty.
+        Cleans up after libtorrent folder renames.
+            
+        """
+        if torrent_id not in self.torrents:
+            raise InvalidTorrentError("torrent_id is not in session")    
+          
+        info = self.torrents[torrent_id].get_status(['save_path'])
+        folder_full_path = os.path.join(info['save_path'], folder)
+
+        try:
+            if not os.listdir(folder_full_path):
+                os.removedirs(folder_full_path)
+                log.debug("Removed Empty Folder %s", folder_full_path)
+            else:
+                for root, dirs, files in os.walk(folder_full_path, 
topdown=False):
+                    for name in dirs:
+                        try:
+                            os.removedirs(os.path.join(root, name))
+                            log.debug("Removed Empty Folder %s", 
os.path.join(root, name))
+                        except OSError as (errno, strerror):
+                            if errno == 39:
+                                # Error raised if folder is not empty
+                                log.debug("%s", strerror)
+
+        except OSError as (errno, strerror):
+            log.debug("Cannot Remove Folder: %s (ErrNo %s)", strerror, errno)  
  
 
     def queue_top(self, torrent_id):
         """Queue torrent to top"""
@@ -992,6 +1022,8 @@ class TorrentManager(component.Component):
                 if len(wait_on_folder[2]) == 1:
                     # This is the last alert we were waiting for, time to send 
signal
                     
component.get("EventManager").emit(TorrentFolderRenamedEvent(torrent_id, 
wait_on_folder[0], wait_on_folder[1]))
+                    # Empty folders are removed after libtorrent folder 
renames 
+                    self.remove_empty_folders(torrent_id, wait_on_folder[0])
                     del torrent.waiting_on_folder_rename[i]
                     self.save_resume_data((torrent_id,))
                     break

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to