mirror of
https://github.com/beetbox/beets.git
synced 2026-01-02 22:12:53 +01:00
Commit #4036
This commit is contained in:
parent
6cd7998c3d
commit
5859d31405
3 changed files with 46 additions and 19 deletions
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
-----------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -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__)
|
||||
|
|
|
|||
Loading…
Reference in a new issue