From 57b7586c0af4431e4504235d6ceb490508b202d5 Mon Sep 17 00:00:00 2001 From: boyska Date: Mon, 18 Jul 2016 12:27:21 +0200 Subject: [PATCH] New audiogen: script closes #9 --- larigira/audioform_script.py | 21 ++++++++++++++++ larigira/audiogen_script.py | 47 ++++++++++++++++++++++++++++++++++++ larigira/config.py | 1 + setup.py | 5 +++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 larigira/audioform_script.py create mode 100644 larigira/audiogen_script.py diff --git a/larigira/audioform_script.py b/larigira/audioform_script.py new file mode 100644 index 0000000..90a6590 --- /dev/null +++ b/larigira/audioform_script.py @@ -0,0 +1,21 @@ +from flask_wtf import Form +from wtforms import StringField, validators, SubmitField + + +class ScriptAudioForm(Form): + nick = StringField(u'Audio nick', validators=[validators.required()], + description='A simple name to recognize this audio') + name = StringField(u'Name', validators=[validators.required()], + description='filename (NOT path) of the script') + args = StringField(u'Arguments', + description='arguments, separated by spaces') + submit = SubmitField(u'Submit') + + +def scriptaudio_receive(form): + return { + 'kind': 'script', + 'nick': form.nick.data, + 'name': form.name.data, + 'args': form.args.data + } diff --git a/larigira/audiogen_script.py b/larigira/audiogen_script.py new file mode 100644 index 0000000..da2be6b --- /dev/null +++ b/larigira/audiogen_script.py @@ -0,0 +1,47 @@ +import logging +import os +import subprocess + +from config import get_conf +log = logging.getLogger('audioscript') + + +def generate(spec): + ''' + Recognized arguments (fields in spec): + - name [mandatory] script name + - args [default=empty] arguments, space-separated + ''' + conf = get_conf() + spec.setdefault('args', '') + args = spec['args'].split() + for attr in ('name', ): + if attr not in spec: + raise ValueError("Malformed audiospec: missing '%s'" % attr) + + scriptpath = os.path.join(conf['SCRIPTS_PATH'], spec['name']) + if not os.path.exists(scriptpath): + raise ValueError("Script %s not found", spec['name']) + if not os.access(scriptpath, os.R_OK | os.X_OK): + raise ValueError("Insufficient privileges for script %s" % scriptpath) + if os.stat(scriptpath).st_uid != os.getuid(): + raise ValueError("Script %s owned by %d, should be owned by %d" + % (spec['name'], os.stat(scriptpath).st_uid, + os.getuid())) + try: + log.info('Going to run {}'.format([scriptpath] + args)) + out = subprocess.check_output([scriptpath] + args, + env=dict( + HOME=os.environ['HOME'], + PATH=os.environ['PATH'], + MPD_HOST=conf['MPD_HOST'], + MPD_PORT=str(conf['MPD_PORT'])), + cwd='/') + except subprocess.CalledProcessError as exc: + log.error("Error %d when running script %s" % + (exc.returncode, spec['name'])) + return [] + + out = [p for p in out.split('\n') if p] + logging.debug('Script %s produced %d files' % (spec['name'], len(out))) + return out diff --git a/larigira/config.py b/larigira/config.py index 287b947..6eed424 100644 --- a/larigira/config.py +++ b/larigira/config.py @@ -18,6 +18,7 @@ def get_conf(prefix='LARIGIRA_'): conf['MPD_PORT'] = int(os.getenv('MPD_PORT', '6600')) conf['CACHING_TIME'] = 10 conf['DB_URI'] = os.path.join(conf_dir, 'db.json') + conf['SCRIPTS_PATH'] = os.path.join(conf_dir, 'scripts') conf['BOOTSTRAP_SERVE_LOCAL'] = True conf['SECRET_KEY'] = 'Please replace me!' conf['MPD_WAIT_START'] = True diff --git a/setup.py b/setup.py index dbf6b6a..c50f065 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,8 @@ setup(name='larigira', 'larigira.audiogenerators': [ 'mpd = larigira.audiogen_mpdrandom:generate_by_artist', 'static = larigira.audiogen_static:generate', - 'randomdir = larigira.audiogen_randomdir:generate' + 'randomdir = larigira.audiogen_randomdir:generate', + 'script = larigira.audiogen_script:generate', ], 'larigira.timegenerators': [ 'frequency = larigira.timegen_every:FrequencyAlarm', @@ -72,9 +73,11 @@ setup(name='larigira', ], 'larigira.audioform_create': [ 'static = larigira.audioform_static:StaticAudioForm', + 'script = larigira.audioform_script:ScriptAudioForm', ], 'larigira.audioform_receive': [ 'static = larigira.audioform_static:staticaudio_receive', + 'script = larigira.audioform_script:scriptaudio_receive', ], } )