Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated permissions plugin to change directory permissions too. #1324

Merged
merged 5 commits into from
Feb 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 39 additions & 8 deletions beetsplug/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

permissions:
file: 644
dir: 755
"""
import os
from beets import config, util
from beets.plugins import BeetsPlugin
from beets.util import ancestry


def convert_perm(perm):
Expand All @@ -28,13 +30,22 @@ def check_permissions(path, permission):
return oct(os.stat(path).st_mode & 0o777) == oct(permission)


def dirs_in_library(library, item):
"""Creates a list of ancestor directories in the beets library path.
"""
return [ancestor
for ancestor in ancestry(item)
if ancestor.startswith(library)][1:]


class Permissions(BeetsPlugin):
def __init__(self):
super(Permissions, self).__init__()

# Adding defaults.
self.config.add({
u'file': 644
u'file': 644,
u'dir': 755
})


Expand All @@ -45,25 +56,45 @@ def permissions(lib, item=None, album=None):
"""
# Getting the config.
file_perm = config['permissions']['file'].get()
dir_perm = config['permissions']['dir'].get()

# Converts file permissions to oct.
# Converts permissions to oct.
file_perm = convert_perm(file_perm)
dir_perm = convert_perm(dir_perm)

# Create chmod_queue.
chmod_queue = []
file_chmod_queue = []
if item:
chmod_queue.append(item.path)
file_chmod_queue.append(item.path)
elif album:
for album_item in album.items():
chmod_queue.append(album_item.path)
file_chmod_queue.append(album_item.path)

# Setting permissions for every path in the queue.
for path in chmod_queue:
# Changing permissions on the destination path.
# A set of directories to change permissions for.
dir_chmod_queue = set()

for path in file_chmod_queue:
# Changing permissions on the destination file.
os.chmod(util.bytestring_path(path), file_perm)

# Checks if the destination path has the permissions configured.
if not check_permissions(util.bytestring_path(path), file_perm):
message = 'There was a problem setting permission on {}'.format(
path)
print(message)

# Adding directories to the directory chmod queue.
dir_chmod_queue.update(
dirs_in_library(config['directory'].get(),
path))

# Change permissions for the directories.
for path in dir_chmod_queue:
# Chaning permissions on the destination directory.
os.chmod(util.bytestring_path(path), dir_perm)

# Checks if the destination path has the permissions configured.
if not check_permissions(util.bytestring_path(path), dir_perm):
message = 'There was a problem setting permission on {}'.format(
path)
print(message)
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Features:
* The number of missing/unmatched tracks is shown during import. :bug:`1088`
* The data source used during import (e.g., MusicBrainz) is now saved as a
flexible attribute `data_source` of an Item/Album. :bug:`1311`
* :doc:`/plugins/permissions`: Now handles also the permissions of the
directories. :bug:`1308` bug:`1324`

Core changes:

Expand Down
5 changes: 3 additions & 2 deletions docs/plugins/permissions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Permissions Plugin
==================

The ``permissions`` plugin allows you to set file permissions for imported
music files.
music files and its directories.

To use the ``permissions`` plugin, enable it in your configuration (see
:ref:`using-plugins`). Permissions will be adjusted automatically on import.
Expand All @@ -12,9 +12,10 @@ Configuration

To configure the plugin, make an ``permissions:`` section in your configuration
file. The ``file`` config value therein uses **octal modes** to specify the
desired permissions. The default flags are octal 644.
desired permissions. The default flags for files are octal 644 and 755 for directories.

Here's an example::

permissions:
file: 644
dir: 755
41 changes: 33 additions & 8 deletions test/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

from test._common import unittest
from test.helper import TestHelper
from beetsplug.permissions import check_permissions, convert_perm
from beetsplug.permissions import (check_permissions,
convert_perm,
dirs_in_library)


class PermissionsPluginTest(unittest.TestCase, TestHelper):
Expand All @@ -14,7 +16,8 @@ def setUp(self):
self.load_plugins('permissions')

self.config['permissions'] = {
'file': 777}
'file': 777,
'dir': 777}

def tearDown(self):
self.teardown_beets()
Expand All @@ -24,23 +27,45 @@ def test_permissions_on_album_imported(self):
self.importer = self.create_importer()
self.importer.run()
item = self.lib.items().get()
config_perm = self.config['permissions']['file'].get()
config_perm = convert_perm(config_perm)

self.assertTrue(check_permissions(item.path, config_perm))
file_perm = self.config['permissions']['file'].get()
file_perm = convert_perm(file_perm)

dir_perm = self.config['permissions']['dir'].get()
dir_perm = convert_perm(dir_perm)

music_dirs = dirs_in_library(self.config['directory'].get(),
item.path)

self.assertTrue(check_permissions(item.path, file_perm))
self.assertFalse(check_permissions(item.path, convert_perm(644)))

for path in music_dirs:
self.assertTrue(check_permissions(path, dir_perm))
self.assertFalse(check_permissions(path, convert_perm(644)))

def test_permissions_on_item_imported(self):
self.config['import']['singletons'] = True
self.importer = self.create_importer()
self.importer.run()
item = self.lib.items().get()
config_perm = self.config['permissions']['file'].get()
config_perm = convert_perm(config_perm)

self.assertTrue(check_permissions(item.path, config_perm))
file_perm = self.config['permissions']['file'].get()
file_perm = convert_perm(file_perm)

dir_perm = self.config['permissions']['dir'].get()
dir_perm = convert_perm(dir_perm)

music_dirs = dirs_in_library(self.config['directory'].get(),
item.path)

self.assertTrue(check_permissions(item.path, file_perm))
self.assertFalse(check_permissions(item.path, convert_perm(644)))

for path in music_dirs:
self.assertTrue(check_permissions(path, dir_perm))
self.assertFalse(check_permissions(path, convert_perm(644)))


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
Expand Down