From e1d9e6bb45505472030f886c8817d6168568e897 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 2 Aug 2011 16:01:11 -0700 Subject: [PATCH] prune directories when moving --- beets/library.py | 8 ++++++++ test/test_files.py | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/beets/library.py b/beets/library.py index 6cda6af79..0ff5d964e 100644 --- a/beets/library.py +++ b/beets/library.py @@ -247,8 +247,13 @@ class Item(object): shutil.move(syspath(self.path), syspath(dest)) # Either copying or moving succeeded, so update the stored path. + old_path = self.path self.path = dest + # Prune vacated directory. + if not copy: + util.prune_dirs(os.path.dirname(old_path), library.directory) + # Library queries. @@ -1169,6 +1174,9 @@ class Album(BaseAlbum): else: shutil.move(syspath(old_art), syspath(new_art)) self.artpath = new_art + if not copy: # Prune old path. + util.prune_dirs(os.path.dirname(old_art), + self._library.directory) # Store new item paths. We do this at the end to avoid # locking the database for too long while files are copied. diff --git a/test/test_files.py b/test/test_files.py index b534721aa..b5a7b42c6 100644 --- a/test/test_files.py +++ b/test/test_files.py @@ -26,7 +26,7 @@ from _common import item, touch import beets.library from beets import util -class MoveTest(unittest.TestCase): +class MoveTest(unittest.TestCase, _common.ExtraAsserts): def setUp(self): # make a temporary file self.path = join(_common.RSRC, 'temp.mp3') @@ -54,19 +54,29 @@ class MoveTest(unittest.TestCase): def test_move_arrives(self): self.i.move(self.lib) - self.assertTrue(os.path.exists(self.dest)) + self.assertExists(self.dest) def test_move_departs(self): self.i.move(self.lib) - self.assertTrue(not os.path.exists(self.path)) + self.assertNotExists(self.path) + + def test_move_in_lib_prunes_empty_dir(self): + self.i.move(self.lib) + old_path = self.i.path + self.assertExists(old_path) + + self.i.artist = 'newArtist' + self.i.move(self.lib) + self.assertNotExists(old_path) + self.assertNotExists(os.path.dirname(old_path)) def test_copy_arrives(self): self.i.move(self.lib, copy=True) - self.assertTrue(os.path.exists(self.dest)) + self.assertExists(self.dest) def test_copy_does_not_depart(self): self.i.move(self.lib, copy=True) - self.assertTrue(os.path.exists(self.path)) + self.assertExists(self.path) def test_move_changes_path(self): self.i.move(self.lib) @@ -257,7 +267,7 @@ class RemoveTest(unittest.TestCase): if os.path.exists(self.libdir): shutil.rmtree(self.libdir) - def test_removing_last_item_removes_empty_dir(self): + def test_removing_last_item_prunes_empty_dir(self): parent = os.path.dirname(self.i.path) self.assertTrue(os.path.exists(parent)) self.lib.remove(self.i, True)