Skip to content

Commit 7548820

Browse files
authored
Merge pull request #4036 from Duncaen/permissions-art
permissions: set album art permissions
2 parents d319a81 + 38e7fb4 commit 7548820

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

beetsplug/permissions.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,29 @@ def __init__(self):
7070

7171
self.register_listener('item_imported', self.fix)
7272
self.register_listener('album_imported', self.fix)
73+
self.register_listener('art_set', self.fix_art)
7374

7475
def fix(self, lib, item=None, album=None):
7576
"""Fix the permissions for an imported Item or Album.
7677
"""
78+
files = []
79+
dirs = set()
80+
if item:
81+
files.append(item.path)
82+
dirs.update(dirs_in_library(lib.directory, item.path))
83+
elif album:
84+
for album_item in album.items():
85+
files.append(album_item.path)
86+
dirs.update(dirs_in_library(lib.directory, album_item.path))
87+
self.set_permissions(files=files, dirs=dirs)
88+
89+
def fix_art(self, album):
90+
"""Fix the permission for Album art file.
91+
"""
92+
if album.artpath:
93+
self.set_permissions(files=[album.artpath])
94+
95+
def set_permissions(self, files=[], dirs=[]):
7796
# Get the configured permissions. The user can specify this either a
7897
# string (in YAML quotes) or, for convenience, as an integer so the
7998
# quotes can be omitted. In the latter case, we need to reinterpret the
@@ -83,18 +102,7 @@ def fix(self, lib, item=None, album=None):
83102
file_perm = convert_perm(file_perm)
84103
dir_perm = convert_perm(dir_perm)
85104

86-
# Create chmod_queue.
87-
file_chmod_queue = []
88-
if item:
89-
file_chmod_queue.append(item.path)
90-
elif album:
91-
for album_item in album.items():
92-
file_chmod_queue.append(album_item.path)
93-
94-
# A set of directories to change permissions for.
95-
dir_chmod_queue = set()
96-
97-
for path in file_chmod_queue:
105+
for path in files:
98106
# Changing permissions on the destination file.
99107
self._log.debug(
100108
u'setting file permissions on {}',
@@ -105,13 +113,8 @@ def fix(self, lib, item=None, album=None):
105113
# Checks if the destination path has the permissions configured.
106114
assert_permissions(path, file_perm, self._log)
107115

108-
# Adding directories to the directory chmod queue.
109-
dir_chmod_queue.update(
110-
dirs_in_library(lib.directory,
111-
path))
112-
113116
# Change permissions for the directories.
114-
for path in dir_chmod_queue:
117+
for path in dirs:
115118
# Chaning permissions on the destination directory.
116119
self._log.debug(
117120
u'setting directory permissions on {}',

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Major new features:
2727
* :doc:`/plugins/albumtypes`: An accompanying plugin for formatting
2828
``albumtypes``. Thanks to :user:`edgars-supe`.
2929

30+
Other new things:
31+
32+
* Permissions plugin now sets cover art permissions to the file permissions.
3033

3134
1.5.0 (August 19, 2021)
3235
-----------------------

test/test_permissions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from mock import patch, Mock
1111

1212
from test.helper import TestHelper
13+
from test._common import touch
1314
from beets.util import displayable_path
1415
from beetsplug.permissions import (check_permissions,
1516
convert_perm,
@@ -82,6 +83,25 @@ def test_convert_perm_from_string(self):
8283
def test_convert_perm_from_int(self):
8384
self.assertEqual(convert_perm(10), 8)
8485

86+
def test_permissions_on_set_art(self):
87+
self.do_set_art(True)
88+
89+
@patch("os.chmod", Mock())
90+
def test_failing_permissions_on_set_art(self):
91+
self.do_set_art(False)
92+
93+
def do_set_art(self, expect_success):
94+
if platform.system() == 'Windows':
95+
self.skipTest('permissions not available on Windows')
96+
self.importer = self.create_importer()
97+
self.importer.run()
98+
album = self.lib.albums().get()
99+
artpath = os.path.join(self.temp_dir, b'cover.jpg')
100+
touch(artpath)
101+
album.set_art(artpath)
102+
self.assertEqual(expect_success,
103+
check_permissions(album.artpath, 0o777))
104+
85105

86106
def suite():
87107
return unittest.TestLoader().loadTestsFromName(__name__)

0 commit comments

Comments
 (0)