prune directories when moving

This commit is contained in:
Adrian Sampson 2011-08-02 16:01:11 -07:00
parent df30952a28
commit e1d9e6bb45
2 changed files with 24 additions and 6 deletions

View file

@ -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.

View file

@ -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)