From 29917ebf635f8d2bc77557b5964d5b543be7acfa Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Wed, 17 Dec 2014 16:42:29 -0500
Subject: [PATCH] More work on switching pypo to use setuptools

---
 installer/install                             |  3 ++
 python_apps/pypo/bin/airtime-liquidsoap       | 48 +++++++++++++++++++
 python_apps/pypo/bin/airtime-playout          | 31 ++++++++++++
 .../pypo/install/airtime-liquidsoap.conf      | 15 ++++++
 python_apps/pypo/install/airtime-playout.conf | 15 ++++++
 python_apps/pypo/setup.py                     | 46 ++++++++++++++++++
 6 files changed, 158 insertions(+)
 create mode 100755 python_apps/pypo/bin/airtime-liquidsoap
 create mode 100755 python_apps/pypo/bin/airtime-playout
 create mode 100644 python_apps/pypo/install/airtime-liquidsoap.conf
 create mode 100644 python_apps/pypo/install/airtime-playout.conf
 create mode 100644 python_apps/pypo/setup.py

diff --git a/installer/install b/installer/install
index b621518c3..88ea57ca1 100755
--- a/installer/install
+++ b/installer/install
@@ -374,6 +374,9 @@ verbose "...Done"
 chmod 755 /etc/init.d/airtime-*
 initctl reload-configuration
 
+verbose "\n * Copying pypo files..."
+python ${AIRTIMEROOT}/python_apps/pypo/setup.py install
+
 if [ ! -d /var/log/airtime ]; then
     loud "\n-----------------------------------------------------"
     loud "              * Installing Log Files *               "
diff --git a/python_apps/pypo/bin/airtime-liquidsoap b/python_apps/pypo/bin/airtime-liquidsoap
new file mode 100755
index 000000000..def0aaae1
--- /dev/null
+++ b/python_apps/pypo/bin/airtime-liquidsoap
@@ -0,0 +1,48 @@
+#!/bin/bash -e
+
+debug="f"
+
+showhelp () {
+    echo "Usage: airtime-liquidsoap [options]
+--help|-h                         Displays usage information.
+--debug|-d                        Print error messages to console" 
+}
+
+set -- $(getopt -l help,debug "hd" "$@")
+while [ $# -gt 0 ]
+do
+    case "$1" in
+    (-h|--help) showhelp; exit 0;;
+    (-d|--debug) debug="t";;
+    
+    (--) shift; break;;
+    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
+    (*)  break;;
+    esac
+    shift
+done
+
+
+virtualenv_bin="/usr/lib/airtime/airtime_virtualenv/bin/"
+. ${virtualenv_bin}activate
+
+export HOME="/var/tmp/airtime/pypo/"
+api_client_path="/usr/lib/airtime/"
+if [ $debug = "t" ]; then
+    ls_path="/usr/bin/airtime-liquidsoap --verbose -f"
+else
+    ls_path="/usr/bin/airtime-liquidsoap --verbose -f -d"
+fi
+ls_param="/usr/lib/airtime/pypo/bin/liquidsoap_scripts/ls_script.liq"
+
+export PYTHONPATH=${api_client_path}
+
+SCRIPT=`readlink -f $0`
+# Absolute directory this script is in
+SCRIPTPATH=`dirname $SCRIPT`
+
+cd $SCRIPTPATH/liquidsoap_scripts
+python generate_liquidsoap_cfg.py
+
+exec ${ls_path} ${ls_param} 2>&1
+# EOF
diff --git a/python_apps/pypo/bin/airtime-playout b/python_apps/pypo/bin/airtime-playout
new file mode 100755
index 000000000..fca8a412d
--- /dev/null
+++ b/python_apps/pypo/bin/airtime-playout
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+virtualenv_bin="/usr/lib/airtime/airtime_virtualenv/bin/"
+. ${virtualenv_bin}activate
+
+# Absolute path to this script
+SCRIPT=`readlink -f $0`
+# Absolute directory this script is in
+pypo_path=`dirname $SCRIPT`
+
+api_client_path="/usr/lib/airtime/"
+pypo_script="pypocli.py"
+cd ${pypo_path}
+
+set +e
+cat /etc/default/locale | grep -i "LANG=.*UTF-\?8" > /dev/null
+set -e
+if [ "$?" != "0" ]; then
+    echo "non UTF-8 default locale found in /etc/default/locale." > /var/log/airtime/pypo/error.log
+    exit 1
+fi
+
+export HOME="/var/tmp/airtime/pypo/"
+export PYTHONPATH=${api_client_path}:$PYTHONPATH
+export LC_ALL=`cat /etc/default/locale | grep "LANG=" | cut -d= -f2 | tr -d "\n\""`
+export TERM=xterm
+
+
+exec python ${pypo_path}/${pypo_script} > /var/log/airtime/pypo/py-interpreter.log 2>&1
+
+# EOF
diff --git a/python_apps/pypo/install/airtime-liquidsoap.conf b/python_apps/pypo/install/airtime-liquidsoap.conf
new file mode 100644
index 000000000..e60835dea
--- /dev/null
+++ b/python_apps/pypo/install/airtime-liquidsoap.conf
@@ -0,0 +1,15 @@
+description "Airtime Liquidsoap"
+author "help@sourcefabric.org"
+
+start on runlevel [2345]
+stop on runlevel [!2345]
+
+respawn
+
+setuid www-data
+setgid www-data
+
+env LANG='en_US.UTF-8'
+env LC_ALL='en_US.UTF-8'
+
+exec airtime-liquidsoap
diff --git a/python_apps/pypo/install/airtime-playout.conf b/python_apps/pypo/install/airtime-playout.conf
new file mode 100644
index 000000000..bbd8da1a2
--- /dev/null
+++ b/python_apps/pypo/install/airtime-playout.conf
@@ -0,0 +1,15 @@
+description "Pypo"
+author "help@sourcefabric.org"
+
+start on runlevel [2345]
+stop on runlevel [!2345]
+
+respawn
+
+setuid www-data
+setgid www-data
+
+env LANG='en_US.UTF-8'
+env LC_ALL='en_US.UTF-8'
+
+exec airtime-playout
diff --git a/python_apps/pypo/setup.py b/python_apps/pypo/setup.py
new file mode 100644
index 000000000..9c5934618
--- /dev/null
+++ b/python_apps/pypo/setup.py
@@ -0,0 +1,46 @@
+from setuptools import setup
+from subprocess import call
+import sys
+
+# Allows us to avoid installing the upstart init script when deploying airtime_analyzer
+# on Airtime Pro:
+if '--no-init-script' in sys.argv:
+    data_files = []
+    sys.argv.remove('--no-init-script') # super hax
+else:
+    data_files = [('/etc/init', ['install/airtime-playout.conf', 'install/airtime-liquidsoap.conf'])]
+    print data_files
+
+setup(name='airtime-playout',
+      version='0.1',
+      description='Airtime Analyzer Worker and File Importer',
+      url='http://github.com/sourcefabric/Airtime',
+      author='sourcefabric',
+      license='AGPLv3',
+      packages=['pypo'],
+      scripts=[
+          'bin/airtime-playout',
+          'bin/airtime-liquidsoap'
+      ],
+      install_requires=[
+          'amqplib',
+          'anyjson',
+          'argparse',
+          'configobj',
+          'docopt',
+          'kombu',
+          'mutagen',
+          'poster',
+          'PyDispatcher',
+          'pyinotify',
+          'pytz',
+          'wsgiref'
+      ],
+      zip_safe=False,
+      data_files=data_files)
+
+# Reload the initctl config so that "service start airtime_analyzer" works
+if data_files:
+    print "Reloading initctl configuration"
+    call(['initctl', 'reload-configuration'])
+    print "Run \"sudo service airtime-playout start\" and \"sudo service airtime-liquidsoap start\""