From 4b5c674d9b87cabb1978fbdf22578be4a15df380 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 18 Sep 2011 16:18:19 -0700 Subject: [PATCH] "--pretend" (dry run) flag for update --- beets/ui/commands.py | 17 ++++++++++++++--- docs/reference/cli.rst | 4 ++++ test/test_ui.py | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 284c2124d..379567091 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -692,7 +692,7 @@ default_commands.append(list_cmd) # update: Update library contents according to on-disk tags. -def update_items(lib, query, album, move, color): +def update_items(lib, query, album, move, color, pretend): """For all the items matched by the query, update the library to reflect the item's embedded tags. """ @@ -704,7 +704,8 @@ def update_items(lib, query, album, move, color): # Item deleted? if not os.path.exists(syspath(item.path)): print_(u'X %s - %s' % (item.artist, item.title)) - lib.remove(item, True) + if not pretend: + lib.remove(item, True) affected_albums.add(item.album_id) continue @@ -731,6 +732,10 @@ def update_items(lib, query, album, move, color): for key, (oldval, newval) in changes.iteritems(): _showdiff(key, oldval, newval, color) + # If we're just pretending, then don't move or save. + if pretend: + continue + # Move the item if it's in the library. if move and lib.directory in ancestry(item.path): lib.move(item) @@ -738,6 +743,10 @@ def update_items(lib, query, album, move, color): lib.store(item) affected_albums.add(item.album_id) + # Skip album changes while pretending. + if pretend: + return + # Modify affected albums to reflect changes in their items. for album_id in affected_albums: if album_id is None: # Singletons. @@ -765,9 +774,11 @@ update_cmd.parser.add_option('-a', '--album', action='store_true', help='show matching albums instead of tracks') update_cmd.parser.add_option('-M', '--nomove', action='store_false', default=True, dest='move', help="don't move files in library") +update_cmd.parser.add_option('-p', '--pretend', action='store_true', + help="show all changes but do nothing") def update_func(lib, config, opts, args): color = ui.config_val(config, 'beets', 'color', DEFAULT_COLOR, bool) - update_items(lib, decargs(args), opts.album, opts.move, color) + update_items(lib, decargs(args), opts.album, opts.move, color, opts.pretend) update_cmd.func = update_func default_commands.append(update_cmd) diff --git a/docs/reference/cli.rst b/docs/reference/cli.rst index b02ce2aa1..ef93af4b4 100644 --- a/docs/reference/cli.rst +++ b/docs/reference/cli.rst @@ -180,6 +180,10 @@ This will scan all the matched files and read their tags, populating the database with the new values. By default, files will be renamed according to their new metadata; disable this with ``-M``. +To perform a "dry run" an update, just use the ``-p`` (for "pretend") flag. This +will show you all the proposed changes but won't actually change anything on +disk. + stats ----- :: diff --git a/test/test_ui.py b/test/test_ui.py index eaef1c1c0..7d8db3782 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -296,7 +296,7 @@ class UpdateTest(unittest.TestCase, _common.ExtraAsserts): def _update(self, query=(), album=False, move=False): self.io.addinput('y') - commands.update_items(self.lib, query, album, move, True) + commands.update_items(self.lib, query, album, move, True, False) def test_delete_removes_item(self): self.assertTrue(list(self.lib.items()))