From 5859d31405f2e48c776072345caf47ebea667dc6 Mon Sep 17 00:00:00 2001 From: Andrew Rogl Date: Sun, 26 Sep 2021 16:41:13 +1000 Subject: [PATCH] Commit #4036 --- beetsplug/permissions.py | 41 +++++++++++++++++++++------------------- docs/changelog.rst | 4 ++++ test/test_permissions.py | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/beetsplug/permissions.py b/beetsplug/permissions.py index cd4577e4f..f5aab056c 100644 --- a/beetsplug/permissions.py +++ b/beetsplug/permissions.py @@ -65,10 +65,29 @@ class Permissions(BeetsPlugin): self.register_listener('item_imported', self.fix) self.register_listener('album_imported', self.fix) + self.register_listener('art_set', self.fix_art) def fix(self, lib, item=None, album=None): """Fix the permissions for an imported Item or Album. """ + files = [] + dirs = set() + if item: + files.append(item.path) + dirs.update(dirs_in_library(lib.directory, item.path)) + elif album: + for album_item in album.items(): + files.append(album_item.path) + dirs.update(dirs_in_library(lib.directory, album_item.path)) + self.set_permissions(files=files, dirs=dirs) + + def fix_art(self, album): + """Fix the permission for Album art file. + """ + if album.artpath: + self.set_permissions(files=[album.artpath]) + + def set_permissions(self, files=[], dirs=[]): # Get the configured permissions. The user can specify this either a # string (in YAML quotes) or, for convenience, as an integer so the # quotes can be omitted. In the latter case, we need to reinterpret the @@ -78,18 +97,7 @@ class Permissions(BeetsPlugin): file_perm = convert_perm(file_perm) dir_perm = convert_perm(dir_perm) - # Create chmod_queue. - file_chmod_queue = [] - if item: - file_chmod_queue.append(item.path) - elif album: - for album_item in album.items(): - file_chmod_queue.append(album_item.path) - - # A set of directories to change permissions for. - dir_chmod_queue = set() - - for path in file_chmod_queue: + for path in files: # Changing permissions on the destination file. self._log.debug( 'setting file permissions on {}', @@ -100,14 +108,9 @@ class Permissions(BeetsPlugin): # Checks if the destination path has the permissions configured. assert_permissions(path, file_perm, self._log) - # Adding directories to the directory chmod queue. - dir_chmod_queue.update( - dirs_in_library(lib.directory, - path)) - # Change permissions for the directories. - for path in dir_chmod_queue: - # Chaning permissions on the destination directory. + for path in dirs: + # Changing permissions on the destination directory. self._log.debug( 'setting directory permissions on {}', util.displayable_path(path), diff --git a/docs/changelog.rst b/docs/changelog.rst index 216556ec4..cf99baae2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -27,6 +27,10 @@ Major new features: option is set, and sort them by the number of votes. Thanks to :user:`aereaux`. +Other new things: + +* Permissions plugin now sets cover art permissions to the file permissions. + 1.5.0 (August 19, 2021) ----------------------- diff --git a/test/test_permissions.py b/test/test_permissions.py index a67c219c9..c84de5e97 100644 --- a/test/test_permissions.py +++ b/test/test_permissions.py @@ -7,6 +7,7 @@ import unittest from unittest.mock import patch, Mock from test.helper import TestHelper +from test._common import touch from beets.util import displayable_path from beetsplug.permissions import (check_permissions, convert_perm, @@ -79,6 +80,25 @@ class PermissionsPluginTest(unittest.TestCase, TestHelper): def test_convert_perm_from_int(self): self.assertEqual(convert_perm(10), 8) + def test_permissions_on_set_art(self): + self.do_set_art(True) + + @patch("os.chmod", Mock()) + def test_failing_permissions_on_set_art(self): + self.do_set_art(False) + + def do_set_art(self, expect_success): + if platform.system() == 'Windows': + self.skipTest('permissions not available on Windows') + self.importer = self.create_importer() + self.importer.run() + album = self.lib.albums().get() + artpath = os.path.join(self.temp_dir, b'cover.jpg') + touch(artpath) + album.set_art(artpath) + self.assertEqual(expect_success, + check_permissions(album.artpath, 0o777)) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)