diff --git a/python_apps/pypo/pypocli.py b/python_apps/pypo/pypocli.py index de30d65dd..b7b8306da 100644 --- a/python_apps/pypo/pypocli.py +++ b/python_apps/pypo/pypocli.py @@ -33,8 +33,6 @@ from configobj import ConfigObj from api_clients import api_client from std_err_override import LogWriter -PYPO_VERSION = '1.1' - # Set up command-line options parser = OptionParser() @@ -43,11 +41,31 @@ usage = "%prog [options]" + " - python playout system" parser = OptionParser(usage=usage) # Options -parser.add_option("-v", "--compat", help="Check compatibility with server API version", default=False, action="store_true", dest="check_compat") +parser.add_option("-v", "--compat", + help="Check compatibility with server API version", + default=False, + action="store_true", + dest="check_compat") -parser.add_option("-t", "--test", help="Do a test to make sure everything is working properly.", default=False, action="store_true", dest="test") -parser.add_option("-b", "--cleanup", help="Cleanup", default=False, action="store_true", dest="cleanup") -parser.add_option("-c", "--check", help="Check the cached schedule and exit", default=False, action="store_true", dest="check") +parser.add_option("-t", "--test", + help="Do a test to make sure everything is working properly.", + default=False, + action="store_true", + dest="test") + +parser.add_option("-b", + "--cleanup", + help="Cleanup", + default=False, + action="store_true", + dest="cleanup") + +parser.add_option("-c", + "--check", + help="Check the cached schedule and exit", + default=False, + action="store_true", + dest="check") # parse options (options, args) = parser.parse_args() @@ -66,6 +84,13 @@ except Exception, e: sys.exit(1) def configure_locale(): + """ + Silly hacks to force Python 2.x to run in UTF-8 mode. Not portable at all, + however serves our purpose at the moment. + + More information available here: + http://stackoverflow.com/questions/3828723/why-we-need-sys-setdefaultencodingutf-8-in-a-py-script + """ logger.debug("Before %s", locale.nl_langinfo(locale.CODESET)) current_locale = locale.getlocale() @@ -74,20 +99,21 @@ def configure_locale(): default_locale = locale.getdefaultlocale() if default_locale[1] is None: - logger.debug("No default locale exists. Let's try loading from /etc/default/locale") + logger.debug("No default locale exists. Let's try loading from \ + /etc/default/locale") if os.path.exists("/etc/default/locale"): locale_config = ConfigObj('/etc/default/locale') lang = locale_config.get('LANG') new_locale = lang else: - logger.error("/etc/default/locale could not be found! Please run 'sudo update-locale' from command-line.") + logger.error("/etc/default/locale could not be found! Please \ + run 'sudo update-locale' from command-line.") sys.exit(1) else: new_locale = default_locale - logger.info("New locale set to: %s", locale.setlocale(locale.LC_ALL, new_locale)) - - + logger.info("New locale set to: %s", \ + locale.setlocale(locale.LC_ALL, new_locale)) reload(sys) sys.setdefaultencoding("UTF-8") @@ -96,7 +122,8 @@ def configure_locale(): logger.debug("After %s", locale.nl_langinfo(locale.CODESET)) if current_locale_encoding not in ['utf-8', 'utf8']: - logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % current_locale_encoding) + logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % \ + current_locale_encoding) sys.exit(1) @@ -107,7 +134,7 @@ try: config = ConfigObj('/etc/airtime/pypo.cfg') except Exception, e: logger.error('Error loading config file: %s', e) - sys.exit() + sys.exit(1) class Global: def __init__(self): @@ -174,7 +201,7 @@ if __name__ == '__main__': if options.test: g.test_api() - sys.exit() + sys.exit(0) api_client = api_client.AirtimeApiClient() @@ -194,25 +221,23 @@ if __name__ == '__main__': recorder_q = Queue() pypoPush_q = Queue() - - """ This queue is shared between pypo-fetch and pypo-file, where pypo-file - is the receiver. Pypo-fetch will send every schedule it gets to pypo-file + is the consumer. Pypo-fetch will send every schedule it gets to pypo-file and pypo will parse this schedule to determine which file has the highest - priority, and will retrieve it. + priority, and retrieve it. """ media_q = Queue() - pmh = PypoMessageHandler(pypoFetch_q, recorder_q) + pmh = PypoMessageHandler(pypoFetch_q, recorder_q, config) pmh.daemon = True pmh.start() - pfile = PypoFile(media_q) + pfile = PypoFile(media_q, config) pfile.daemon = True pfile.start() - pf = PypoFetch(pypoFetch_q, pypoPush_q, media_q, telnet_lock) + pf = PypoFetch(pypoFetch_q, pypoPush_q, media_q, telnet_lock, config) pf.daemon = True pf.start() @@ -228,12 +253,6 @@ if __name__ == '__main__': stat.daemon = True stat.start() - # all join() are commented out because we want to exit entire pypo - # if pypofetch terminates - #pmh.join() - #recorder.join() - #pp.join() pf.join() - logger.info("pypo fetch exit") - sys.exit() + logger.info("System exit") diff --git a/python_apps/pypo/pypofetch.py b/python_apps/pypo/pypofetch.py index 82f404ce5..8bf145928 100644 --- a/python_apps/pypo/pypofetch.py +++ b/python_apps/pypo/pypofetch.py @@ -15,7 +15,6 @@ import traceback from Queue import Empty from threading import Thread from subprocess import Popen, PIPE -from configobj import ConfigObj from api_clients import api_client from std_err_override import LogWriter @@ -35,25 +34,17 @@ signal.signal(signal.SIGINT, keyboardInterruptHandler) #need to wait for Python 2.7 for this.. #logging.captureWarnings(True) -# loading config file -try: - config = ConfigObj('/etc/airtime/pypo.cfg') - LS_HOST = config['ls_host'] - LS_PORT = config['ls_port'] - #POLL_INTERVAL = int(config['poll_interval']) - POLL_INTERVAL = 1800 -except Exception, e: - logger.error('Error loading config file: %s', e) - sys.exit() +POLL_INTERVAL = 1800 class PypoFetch(Thread): - def __init__(self, pypoFetch_q, pypoPush_q, media_q, telnet_lock): + def __init__(self, pypoFetch_q, pypoPush_q, media_q, telnet_lock, config): Thread.__init__(self) self.api_client = api_client.AirtimeApiClient() self.fetch_queue = pypoFetch_q self.push_queue = pypoPush_q self.media_prepare_queue = media_q self.last_update_schedule_timestamp = time.time() + self.config = config self.listener_timeout = POLL_INTERVAL self.telnet_lock = telnet_lock @@ -141,7 +132,7 @@ class PypoFetch(Thread): try: lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) logger.info(command) tn.write(command) tn.write('exit\n') @@ -156,7 +147,7 @@ class PypoFetch(Thread): try: lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) for i in commands: logger.info(i) tn.write(i) @@ -243,7 +234,7 @@ class PypoFetch(Thread): self.logger.info("Waiting for Liquidsoap to start") while True: try: - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) tn.write("exit\n") tn.read_all() self.logger.info("Liquidsoap is up and running") @@ -368,7 +359,7 @@ class PypoFetch(Thread): try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) # update the boot up time of Liquidsoap. Since Liquidsoap is not restarting, # we are manually adjusting the bootup time variable so the status msg will get # updated. @@ -411,7 +402,7 @@ class PypoFetch(Thread): # TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!! try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) command = ('vars.stream_metadata_type %s\n' % stream_format).encode('utf-8') self.logger.info(command) tn.write(command) @@ -427,7 +418,7 @@ class PypoFetch(Thread): # TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!! try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) command = ('vars.default_dj_fade %s\n' % fade).encode('utf-8') self.logger.info(command) tn.write(command) @@ -442,12 +433,9 @@ class PypoFetch(Thread): # Push stream metadata to liquidsoap # TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!! try: - self.logger.info(LS_HOST) - self.logger.info(LS_PORT) - try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) command = ('vars.station_name %s\n' % station_name).encode('utf-8') self.logger.info(command) tn.write(command) diff --git a/python_apps/pypo/pypofile.py b/python_apps/pypo/pypofile.py index 67e829464..aaee3c34f 100644 --- a/python_apps/pypo/pypofile.py +++ b/python_apps/pypo/pypofile.py @@ -2,7 +2,6 @@ from threading import Thread from Queue import Empty -from configobj import ConfigObj import logging import shutil @@ -20,21 +19,10 @@ LogWriter.override_std_err(logger) #need to wait for Python 2.7 for this.. #logging.captureWarnings(True) -# loading config file -try: - config = ConfigObj('/etc/airtime/pypo.cfg') - LS_HOST = config['ls_host'] - LS_PORT = config['ls_port'] - POLL_INTERVAL = int(config['poll_interval']) - -except Exception, e: - logger.error('Error loading config file: %s', e) - sys.exit(1) - class PypoFile(Thread): - def __init__(self, schedule_queue): + def __init__(self, schedule_queue, config): Thread.__init__(self) self.logger = logging.getLogger() self.media_queue = schedule_queue diff --git a/python_apps/pypo/pypomessagehandler.py b/python_apps/pypo/pypomessagehandler.py index 427772c19..56fa1389e 100644 --- a/python_apps/pypo/pypomessagehandler.py +++ b/python_apps/pypo/pypomessagehandler.py @@ -3,7 +3,6 @@ import logging import traceback import sys -from configobj import ConfigObj from threading import Thread import time # For RabbitMQ @@ -23,30 +22,25 @@ LogWriter.override_std_err(logger) #need to wait for Python 2.7 for this.. #logging.captureWarnings(True) -# loading config file -try: - config = ConfigObj('/etc/airtime/pypo.cfg') - LS_HOST = config['ls_host'] - LS_PORT = config['ls_port'] - POLL_INTERVAL = int(config['poll_interval']) - -except Exception, e: - logger.error('Error loading config file: %s', e) - sys.exit() class PypoMessageHandler(Thread): - def __init__(self, pq, rq): + def __init__(self, pq, rq, config): Thread.__init__(self) self.logger = logging.getLogger('message_h') self.pypo_queue = pq self.recorder_queue = rq + self.config = config def init_rabbit_mq(self): self.logger.info("Initializing RabbitMQ stuff") try: schedule_exchange = Exchange("airtime-pypo", "direct", durable=True, auto_delete=True) schedule_queue = Queue("pypo-fetch", exchange=schedule_exchange, key="foo") - connection = BrokerConnection(config["rabbitmq_host"], config["rabbitmq_user"], config["rabbitmq_password"], config["rabbitmq_vhost"]) + connection = BrokerConnection(self.config["rabbitmq_host"], \ + self.config["rabbitmq_user"], \ + self.config["rabbitmq_password"], \ + self.config["rabbitmq_vhost"]) + channel = connection.channel() self.simple_queue = SimpleQueue(channel, schedule_queue) except Exception, e: diff --git a/python_apps/pypo/pypopush.py b/python_apps/pypo/pypopush.py index 4d6c21964..43537b89a 100644 --- a/python_apps/pypo/pypopush.py +++ b/python_apps/pypo/pypopush.py @@ -34,15 +34,8 @@ LogWriter.override_std_err(logger) #need to wait for Python 2.7 for this.. #logging.captureWarnings(True) -# loading config file -try: - config = ConfigObj('/etc/airtime/pypo.cfg') - LS_HOST = config['ls_host'] - LS_PORT = config['ls_port'] - PUSH_INTERVAL = 2 -except Exception, e: - logger.error('Error loading config file %s', e) - sys.exit() +PUSH_INTERVAL = 2 + def is_stream(media_item): return media_item['type'] == 'stream_output_start' @@ -51,12 +44,13 @@ def is_file(media_item): return media_item['type'] == 'file' class PypoPush(Thread): - def __init__(self, q, telnet_lock): + def __init__(self, q, telnet_lock, config): Thread.__init__(self) self.api_client = api_client.AirtimeApiClient() self.queue = q self.telnet_lock = telnet_lock + self.config = config self.pushed_objects = {} self.logger = logging.getLogger('push') @@ -65,7 +59,7 @@ class PypoPush(Thread): self.future_scheduled_queue = Queue() self.pypo_liquidsoap = PypoLiquidsoap(self.logger, telnet_lock,\ - LS_HOST, LS_PORT) + config['ls_host'], config['ls_port']) self.plq = PypoLiqQueue(self.future_scheduled_queue, \ self.pypo_liquidsoap, \ @@ -126,7 +120,7 @@ class PypoPush(Thread): response = "-1" try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port']) msg = 'dynamic_source.get_id\n' tn.write(msg) @@ -173,7 +167,7 @@ class PypoPush(Thread): def stop_web_stream_all(self): try: self.telnet_lock.acquire() - tn = telnetlib.Telnet(LS_HOST, LS_PORT) + tn = telnetlib.Telnet(self.config['LS_HOST'], self.config['LS_PORT']) #msg = 'dynamic_source.read_stop_all xxx\n' msg = 'http.stop\n'