FreedomBox/actions/minidlna
Nektarios Katakis ef5f5a21de
feature: minidlna app
Closes #1679

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-12-08 14:49:21 -05:00

107 lines
3.1 KiB
Python
Executable File

#!/usr/bin/python3
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Configuration actions for the minidlna server.
"""
import argparse
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
import augeas
from plinth.utils import grep
config_path = '/etc/minidlna.conf'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('setup', help='Setup SSH server')
subparsers.add_parser('get-media-dir', help='Get media directory')
set_media_dir = subparsers.add_parser('set-media-dir',
help='Set custom media directory')
set_media_dir.add_argument('--dir')
subparsers.required = True
return parser.parse_args()
def subcommand_setup(arguments):
"""
Increase inotify watches per folder to allow minidlna to
monitor changes in large media-dirs.
"""
aug = augeas.Augeas(
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Sysctl/lens', 'Sysctl.lns')
aug.set('/augeas/load/Sysctl/incl[last() + 1]', '/etc/sysctl.conf')
aug.load()
aug.set('/files/etc/sysctl.conf/fs.inotify.max_user_watches', '100000')
aug.save()
def subcommand_get_media_dir(arguments):
"""Retrieve media directory from minidlna.conf"""
line = grep('^media_dir=', config_path)
print(line[0].split("=")[1])
def subcommand_set_media_dir(arguments):
"""Set media directory in minidlna.conf"""
line = grep('^media_dir=', config_path)[0]
new_line = 'media_dir=%s\n' % arguments.dir
replace_in_config_file(config_path, line, new_line)
def replace_in_config_file(file_path, pattern, subst):
"""
Create a temporary minidlna.conf file,
replace the media dir config,
remove original one and move the temporary file.
"""
temp_file, temp_file_path = mkstemp()
with fdopen(temp_file, 'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
remove(file_path)
move(temp_file_path, file_path)
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()