2011-04-25 18:49:01 +02:00
|
|
|
import time
|
2011-06-23 17:33:05 +02:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2011-04-25 18:49:01 +02:00
|
|
|
import sys
|
2011-06-29 00:01:30 +02:00
|
|
|
import os
|
2011-06-27 22:56:20 +02:00
|
|
|
import signal
|
2012-04-22 19:42:45 +02:00
|
|
|
import traceback
|
2012-04-24 22:24:54 +02:00
|
|
|
import locale
|
2011-04-25 18:49:01 +02:00
|
|
|
|
2012-04-27 22:08:51 +02:00
|
|
|
from configobj import ConfigObj
|
|
|
|
|
2011-08-05 20:40:03 +02:00
|
|
|
from api_clients import api_client as apc
|
2012-04-21 00:32:10 +02:00
|
|
|
from std_err_override import LogWriter
|
2011-07-05 21:48:50 +02:00
|
|
|
|
2011-06-29 23:26:42 +02:00
|
|
|
from multiprocessing import Process, Queue as mpQueue
|
2011-04-21 23:37:59 +02:00
|
|
|
|
2011-07-16 01:49:35 +02:00
|
|
|
from threading import Thread
|
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
from pyinotify import WatchManager
|
|
|
|
|
2011-06-23 17:33:05 +02:00
|
|
|
from airtimefilemonitor.airtimenotifier import AirtimeNotifier
|
2011-07-13 23:38:11 +02:00
|
|
|
from airtimefilemonitor.mediamonitorcommon import MediaMonitorCommon
|
2011-06-23 17:33:05 +02:00
|
|
|
from airtimefilemonitor.airtimeprocessevent import AirtimeProcessEvent
|
|
|
|
from airtimefilemonitor.mediaconfig import AirtimeMediaConfig
|
2011-07-05 21:48:50 +02:00
|
|
|
from airtimefilemonitor.workerprocess import MediaMonitorWorkerProcess
|
2011-06-29 00:01:30 +02:00
|
|
|
from airtimefilemonitor.airtimemediamonitorbootstrap import AirtimeMediaMonitorBootstrap
|
2011-06-17 17:54:36 +02:00
|
|
|
|
2012-04-27 23:25:12 +02:00
|
|
|
def configure_locale():
|
2012-05-09 18:21:53 +02:00
|
|
|
logger.debug("Before %s", locale.nl_langinfo(locale.CODESET))
|
2012-04-27 22:08:51 +02:00
|
|
|
current_locale = locale.getlocale()
|
|
|
|
|
|
|
|
if current_locale[1] is None:
|
|
|
|
logger.debug("No locale currently set. Attempting to get default 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")
|
|
|
|
if os.path.exists("/etc/default/locale"):
|
|
|
|
config = ConfigObj('/etc/default/locale')
|
|
|
|
lang = config.get('LANG')
|
|
|
|
new_locale = lang
|
|
|
|
else:
|
|
|
|
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.debug("New locale set to: " + locale.setlocale(locale.LC_ALL, new_locale))
|
|
|
|
|
2012-05-10 20:36:59 +02:00
|
|
|
reload(sys)
|
|
|
|
sys.setdefaultencoding("UTF-8")
|
2012-04-27 22:08:51 +02:00
|
|
|
current_locale_encoding = locale.getlocale()[1].lower()
|
2012-05-10 17:16:51 +02:00
|
|
|
logger.debug("sys default encoding %s", sys.getdefaultencoding())
|
2012-05-09 18:21:53 +02:00
|
|
|
logger.debug("After %s", locale.nl_langinfo(locale.CODESET))
|
2012-04-27 22:08:51 +02:00
|
|
|
|
|
|
|
if current_locale_encoding not in ['utf-8', 'utf8']:
|
|
|
|
logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % current_locale_encoding)
|
2012-05-04 19:39:48 +02:00
|
|
|
sys.exit(1)
|
2012-04-27 22:08:51 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
# configure logging
|
|
|
|
try:
|
|
|
|
logging.config.fileConfig("logging.cfg")
|
2012-04-21 00:32:10 +02:00
|
|
|
|
|
|
|
#need to wait for Python 2.7 for this..
|
|
|
|
#logging.captureWarnings(True)
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
LogWriter.override_std_err(logger)
|
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
except Exception, e:
|
|
|
|
print 'Error configuring logging: ', e
|
2011-06-29 00:01:30 +02:00
|
|
|
sys.exit(1)
|
2011-06-28 11:39:54 +02:00
|
|
|
|
2011-07-18 21:27:22 +02:00
|
|
|
logger.info("\n\n*** Media Monitor bootup ***\n\n")
|
|
|
|
|
2012-04-24 22:24:54 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
try:
|
2012-04-27 23:25:12 +02:00
|
|
|
configure_locale()
|
2012-04-24 22:24:54 +02:00
|
|
|
|
2011-06-29 00:01:30 +02:00
|
|
|
config = AirtimeMediaConfig(logger)
|
2011-08-05 20:40:03 +02:00
|
|
|
api_client = apc.api_client_factory(config.cfg)
|
2011-09-16 23:51:28 +02:00
|
|
|
api_client.register_component("media-monitor")
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Setting up monitor")
|
|
|
|
response = None
|
|
|
|
while response is None:
|
2011-07-05 21:48:50 +02:00
|
|
|
response = api_client.setup_media_monitor()
|
2011-06-28 11:39:54 +02:00
|
|
|
time.sleep(5)
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-08-05 20:40:03 +02:00
|
|
|
storage_directory = apc.encode_to(response["stor"], 'utf-8')
|
2011-08-23 02:36:16 +02:00
|
|
|
watched_dirs = apc.encode_to(response["watched_dirs"], 'utf-8')
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Storage Directory is: %s", storage_directory)
|
2011-07-11 18:21:30 +02:00
|
|
|
config.storage_directory = os.path.normpath(storage_directory)
|
2012-05-09 06:02:02 +02:00
|
|
|
config.imported_directory = os.path.normpath(os.path.join(storage_directory, 'imported'))
|
|
|
|
config.organize_directory = os.path.normpath(os.path.join(storage_directory, 'organize'))
|
|
|
|
config.recorded_directory = os.path.normpath(os.path.join(storage_directory, 'recorded'))
|
|
|
|
config.problem_directory = os.path.normpath(os.path.join(storage_directory, 'problem_files'))
|
|
|
|
|
|
|
|
dirs = [config.imported_directory, config.organize_directory, config.recorded_directory, config.problem_directory]
|
|
|
|
for d in dirs:
|
|
|
|
if not os.path.exists(d):
|
|
|
|
os.makedirs(d, 02775)
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
multi_queue = mpQueue()
|
|
|
|
logger.info("Initializing event processor")
|
2011-07-13 23:38:11 +02:00
|
|
|
|
2011-07-05 21:48:50 +02:00
|
|
|
wm = WatchManager()
|
2011-10-04 23:20:22 +02:00
|
|
|
mmc = MediaMonitorCommon(config, wm=wm)
|
2011-09-27 20:49:03 +02:00
|
|
|
pe = AirtimeProcessEvent(queue=multi_queue, airtime_config=config, wm=wm, mmc=mmc, api_client=api_client)
|
2011-07-05 21:48:50 +02:00
|
|
|
|
2012-05-09 06:02:02 +02:00
|
|
|
bootstrap = AirtimeMediaMonitorBootstrap(logger, pe, api_client, mmc, wm, config)
|
2011-06-30 17:56:48 +02:00
|
|
|
bootstrap.scan()
|
2011-07-06 20:42:43 +02:00
|
|
|
|
2011-11-03 20:25:09 +01:00
|
|
|
notifier = AirtimeNotifier(wm, pe, read_freq=0, timeout=0, airtime_config=config, api_client=api_client, bootstrap=bootstrap, mmc=mmc)
|
2011-07-06 20:42:43 +02:00
|
|
|
notifier.coalesce_events()
|
|
|
|
|
2011-07-16 02:28:00 +02:00
|
|
|
#create 5 worker threads
|
2012-05-09 06:02:02 +02:00
|
|
|
wp = MediaMonitorWorkerProcess(config, mmc)
|
2011-06-30 17:56:48 +02:00
|
|
|
for i in range(5):
|
2011-08-05 00:36:40 +02:00
|
|
|
threadName = "Thread #%d" % i
|
|
|
|
t = Thread(target=wp.process_file_events, name=threadName, args=(multi_queue, notifier))
|
2011-07-16 01:49:35 +02:00
|
|
|
t.start()
|
2011-06-30 17:56:48 +02:00
|
|
|
|
2011-07-14 18:05:43 +02:00
|
|
|
wdd = notifier.watch_directory(storage_directory)
|
2011-06-28 11:39:54 +02:00
|
|
|
logger.info("Added watch to %s", storage_directory)
|
|
|
|
logger.info("wdd result %s", wdd[storage_directory])
|
2011-08-23 02:36:16 +02:00
|
|
|
|
|
|
|
for dir in watched_dirs:
|
|
|
|
wdd = notifier.watch_directory(dir)
|
|
|
|
logger.info("Added watch to %s", dir)
|
|
|
|
logger.info("wdd result %s", wdd[dir])
|
2011-06-28 11:39:54 +02:00
|
|
|
|
2011-06-29 10:46:46 +02:00
|
|
|
notifier.loop(callback=pe.notifier_loop_callback)
|
2011-06-29 00:01:30 +02:00
|
|
|
|
2011-06-28 11:39:54 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
notifier.stop()
|
2011-06-29 00:01:30 +02:00
|
|
|
logger.info("Keyboard Interrupt")
|
2011-06-28 11:39:54 +02:00
|
|
|
except Exception, e:
|
|
|
|
logger.error('Exception: %s', e)
|
2012-04-22 19:42:45 +02:00
|
|
|
logger.error("traceback: %s", traceback.format_exc())
|