run liquidsoap from command

This commit is contained in:
bkfox 2016-01-26 16:08:03 +01:00
parent a1fe0b2f93
commit 032bd6c56d
2 changed files with 44 additions and 6 deletions

View File

@ -6,6 +6,8 @@ Main tool to work with liquidsoap. We can:
import os import os
import time import time
import re import re
import subprocess
import atexit
from argparse import RawTextHelpFormatter from argparse import RawTextHelpFormatter
from django.conf import settings as main_settings from django.conf import settings as main_settings
@ -26,6 +28,7 @@ class StationConfig:
Configuration and playlist generator for a station. Configuration and playlist generator for a station.
""" """
controller = None controller = None
process = None
def __init__ (self, station): def __init__ (self, station):
self.controller = utils.Controller(station, False) self.controller = utils.Controller(station, False)
@ -72,6 +75,17 @@ class StationConfig:
with open(stream.path, 'w+') as file: with open(stream.path, 'w+') as file:
file.write('\n'.join(sound.path for sound in sounds)) file.write('\n'.join(sound.path for sound in sounds))
def run (self):
"""
Run subprocess in background, register a terminate handler, and
return process instance.
"""
self.process = \
subprocess.Popen(['liquidsoap', '-v', self.controller.config_path],
stderr=subprocess.STDOUT)
atexit.register(self.process.terminate)
return self.process
class Monitor: class Monitor:
@classmethod @classmethod
@ -218,8 +232,11 @@ class Command (BaseCommand):
group = parser.add_argument_group('configuration') group = parser.add_argument_group('configuration')
group.add_argument( group.add_argument(
'-s', '--station', type=int, '-s', '--station', type=int,
help='generate files for the given station (if not set, do it for' help='generate files for the given station'
' all available stations)' )
group.add_argument(
'-a', '--all', action='store_true',
help='generate files for all stations'
) )
group.add_argument( group.add_argument(
'-c', '--config', action='store_true', '-c', '--config', action='store_true',
@ -229,19 +246,39 @@ class Command (BaseCommand):
'-S', '--streams', action='store_true', '-S', '--streams', action='store_true',
help='generate all stream playlists' help='generate all stream playlists'
) )
group.add_argument(
'-r', '--run', action='store_true',
help='run liquidsoap with the generated configuration'
)
def handle (self, *args, **options): def handle (self, *args, **options):
stations = []
if options.get('station'): if options.get('station'):
station = programs.Station.objects.get(id = options.get('station')) stations = [ StationConfig(
StationConfig(station).handle(options) programs.Station.objects.get(
id = options.get('station')
)) ]
elif options.get('all') or options.get('config') or \ elif options.get('all') or options.get('config') or \
options.get('streams'): options.get('streams'):
for station in programs.Station.objects.filter(active = True): stations = [ StationConfig(station)
StationConfig(station).handle(options) for station in \
programs.Station.objects.filter(active = True)
]
run = options.get('run')
for station in stations:
station.handle(options)
if run:
station.run()
if options.get('on_air') or options.get('monitor'): if options.get('on_air') or options.get('monitor'):
self.handle_monitor(options) self.handle_monitor(options)
if run:
for station in stations:
station.process.wait()
def handle_monitor (self, options): def handle_monitor (self, options):
controllers = [ controllers = [
utils.Controller(station) utils.Controller(station)

View File

@ -14,3 +14,4 @@ import aircox.programs.utils