From b89a2650cc8728d21a8aaa48b82e9b24564e44b0 Mon Sep 17 00:00:00 2001 From: Samuel Cook Date: Mon, 3 Aug 2020 18:20:20 -0700 Subject: [PATCH] Delete after convert (#3700) * If import move is true, files will be deleted after converting. Fixes #2947 * Removed trailing whitespace to comply with W293, fixing build * Add period to the end of the comment Co-Authored-By: Adrian Sampson * Added changelog entry for this fix. * Added delete_originals option to remove source files after transcode * Added unit test, removed redundant syspath call Co-authored-by: Logan Arens Co-authored-by: Logan Arens Co-authored-by: Adrian Sampson Co-authored-by: Logan Arens --- beetsplug/convert.py | 6 ++++++ docs/changelog.rst | 4 ++++ docs/plugins/convert.rst | 2 ++ test/test_convert.py | 10 ++++++++++ 4 files changed, 22 insertions(+) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index e7ac4f3ac..70363f6eb 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -148,6 +148,7 @@ class ConvertPlugin(BeetsPlugin): u'never_convert_lossy_files': False, u'copy_album_art': False, u'album_art_maxwidth': 0, + u'delete_originals': False, }) self.early_import_stages = [self.auto_convert] @@ -532,11 +533,16 @@ class ConvertPlugin(BeetsPlugin): # Change the newly-imported database entry to point to the # converted file. + source_path = item.path item.path = dest item.write() item.read() # Load new audio information data. item.store() + if self.config['delete_originals']: + self._log.info(u'Removing original file {0}', source_path) + util.remove(source_path, False) + def _cleanup(self, task, session): for path in task.old_paths: if path in _temp_files: diff --git a/docs/changelog.rst b/docs/changelog.rst index 4a87f7f07..0dfa2c5f8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -142,6 +142,10 @@ New features: * :doc:`/plugins/thumbnails`: Fix a bug where pathlib expected a string instead of bytes for a path. :bug:`3360` +* :doc:`/plugins/convert`: If ``delete_originals`` is enabled, then the source files will + be deleted after importing. + Thanks to :user:`logan-arens`. + :bug:`2947` Fixes: diff --git a/docs/plugins/convert.rst b/docs/plugins/convert.rst index 6e9d00a11..9581e24a4 100644 --- a/docs/plugins/convert.rst +++ b/docs/plugins/convert.rst @@ -111,6 +111,8 @@ file. The available options are: This option overrides ``link``. Only works when converting to a directory on the same filesystem as the library. Default: ``false``. +- **delete_originals**: Transcoded files will be copied or moved to their destination, depending on the import configuration. By default, the original files are not modified by the plugin. This option deletes the original files after the transcoding step has completed. + Default: ``false``. You can also configure the format to use for transcoding (see the next section): diff --git a/test/test_convert.py b/test/test_convert.py index 33bdb3b24..b8cd56741 100644 --- a/test/test_convert.py +++ b/test/test_convert.py @@ -15,6 +15,7 @@ from __future__ import division, absolute_import, print_function +import fnmatch import sys import re import os.path @@ -121,6 +122,15 @@ class ImportConvertTest(unittest.TestCase, TestHelper): self.assertIsNotNone(item) self.assertTrue(os.path.isfile(item.path)) + def test_delete_originals(self): + self.config['convert']['delete_originals'] = True + self.importer.run() + for path in self.importer.paths: + for root, dirnames, filenames in os.walk(path): + self.assertTrue(len(fnmatch.filter(filenames, '*.mp3')) == 0, + u'Non-empty import directory {0}' + .format(util.displayable_path(path))) + class ConvertCommand(object): """A mixin providing a utility method to run the `convert`command