| 1 |
"""Fixes file permissions after the file gets written on import. Put something |
| 2 |
like the following in your config.yaml to configure: |
| 3 |
|
| 4 |
permissions: |
| 5 |
file: 644 |
| 6 |
dir: 755 |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import os |
| 12 |
import stat |
| 13 |
from typing import TYPE_CHECKING |
| 14 |
|
| 15 |
from beets import config |
| 16 |
from beets.plugins import BeetsPlugin |
| 17 |
from beets.util import ancestry, displayable_path, syspath |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from collections.abc import Iterable |
| 21 |
|
| 22 |
from beets.library import Album, Item, Library |
| 23 |
from beets.logging import BeetsLogger as Logger |
| 24 |
|
| 25 |
|
| 26 |
def convert_perm(perm: str | int) -> int: |
| 27 |
"""Convert a string to an integer, interpreting the text as octal. |
| 28 |
Or, if `perm` is an integer, reinterpret it as an octal number that |
| 29 |
has been "misinterpreted" as decimal. |
| 30 |
""" |
| 31 |
if isinstance(perm, int): |
| 32 |
perm = str(perm) |
| 33 |
return int(perm, 8) |
| 34 |
|
| 35 |
|
| 36 |
def check_permissions(path: bytes, permission: int) -> bool: |
| 37 |
"""Check whether the file's permissions equal the given vector. |
| 38 |
Return a boolean. |
| 39 |
""" |
| 40 |
return oct(stat.S_IMODE(os.stat(syspath(path)).st_mode)) == oct(permission) |
| 41 |
|
| 42 |
|
| 43 |
def assert_permissions(path: bytes, permission: int, log: Logger) -> None: |
| 44 |
"""Check whether the file's permissions are as expected, otherwise, |
| 45 |
log a warning message. Return a boolean indicating the match, like |
| 46 |
`check_permissions`. |
| 47 |
""" |
| 48 |
if not check_permissions(path, permission): |
| 49 |
log.warning("could not set permissions on {}", displayable_path(path)) |
| 50 |
log.debug( |
| 51 |
"set permissions to {}, but permissions are now {}", |
| 52 |
permission, |
| 53 |
os.stat(syspath(path)).st_mode & 0o777, |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def dirs_in_library(library: bytes, path: bytes) -> list[bytes]: |
| 58 |
"""Creates a list of ancestor directories in the beets library path.""" |
| 59 |
return [ |
| 60 |
ancestor for ancestor in ancestry(path) if ancestor.startswith(library) |
| 61 |
][1:] |
| 62 |
|
| 63 |
|
| 64 |
class Permissions(BeetsPlugin): |
| 65 |
def __init__(self) -> None: |
| 66 |
super().__init__() |
| 67 |
|
| 68 |
# Adding defaults. |
| 69 |
self.config.add({"file": "644", "dir": "755"}) |
| 70 |
|
| 71 |
self.register_listener("item_imported", self.fix_item) |
| 72 |
self.register_listener("album_imported", self.fix_album) |
| 73 |
self.register_listener("art_set", self.fix_art) |
| 74 |
|
| 75 |
def fix_item(self, lib: Library, item: Item) -> None: |
| 76 |
self.set_permissions( |
| 77 |
files=[item.path], dirs=dirs_in_library(lib.directory, item.path) |
| 78 |
) |
| 79 |
|
| 80 |
def fix_album(self, lib: Library, album: Album) -> None: |
| 81 |
files: list[bytes] = [] |
| 82 |
dirs: set[bytes] = set() |
| 83 |
for item in album.items(): |
| 84 |
files.append(item.path) |
| 85 |
dirs.update(dirs_in_library(lib.directory, item.path)) |
| 86 |
self.set_permissions(files=files, dirs=dirs) |
| 87 |
|
| 88 |
def fix_art(self, album: Album) -> None: |
| 89 |
"""Fix the permission for Album art file.""" |
| 90 |
if album.artpath: |
| 91 |
self.set_permissions(files=[album.artpath]) |
| 92 |
|
| 93 |
def set_permissions( |
| 94 |
self, |
| 95 |
files: Iterable[bytes] | None = None, |
| 96 |
dirs: Iterable[bytes] | None = None, |
| 97 |
) -> None: |
| 98 |
# Get the configured permissions. The user can specify this either a |
| 99 |
# string (in YAML quotes) or, for convenience, as an integer so the |
| 100 |
# quotes can be omitted. In the latter case, we need to reinterpret the |
| 101 |
# integer as octal, not decimal. |
| 102 |
file_perm = config["permissions"]["file"].get() |
| 103 |
dir_perm = config["permissions"]["dir"].get() |
| 104 |
file_perm = convert_perm(file_perm) |
| 105 |
dir_perm = convert_perm(dir_perm) |
| 106 |
|
| 107 |
for path in files or []: |
| 108 |
# Changing permissions on the destination file. |
| 109 |
self._log.debug( |
| 110 |
"setting file permissions on {}", displayable_path(path) |
| 111 |
) |
| 112 |
if not check_permissions(path, file_perm): |
| 113 |
os.chmod(syspath(path), file_perm) |
| 114 |
|
| 115 |
# Checks if the destination path has the permissions configured. |
| 116 |
assert_permissions(path, file_perm, self._log) |
| 117 |
|
| 118 |
# Change permissions for the directories. |
| 119 |
for path in dirs or []: |
| 120 |
# Changing permissions on the destination directory. |
| 121 |
self._log.debug( |
| 122 |
"setting directory permissions on {}", displayable_path(path) |
| 123 |
) |
| 124 |
if not check_permissions(path, dir_perm): |
| 125 |
os.chmod(syspath(path), dir_perm) |
| 126 |
|
| 127 |
# Checks if the destination path has the permissions configured. |
| 128 |
assert_permissions(path, dir_perm, self._log) |