# Path: updater.py
# def prepare(test_mode=False):
#     '''
#     Creates a new instance of the distributor. Sets up the signal
#     management.
# 
#     test_mode (optional) whether the test mode should be used or
#     not. If so, the normal reader is replaced with a mock that always
#     return zero messages
#     '''
#     global distributor
#     distributor = OnlineDistributor(py)
# 
#     if test_mode:
#         from mock import Mock
#         from deliver.read import Reader
#         reader = Mock(spec=Reader)
#         reader.new_messages.return_value = []
#         distributor._reader = reader
# 
#     signal.signal(signal.SIGTERM, terminate)
# 
# def loop():
#     '''
#     Loops indefinitely, updating the distributor and then sleeping for
#     a defined time.
#     '''
#     print 'Starting...'
#     sleep = py['sleep_interval']
# 
#     try:
#         while run:
#             success = update()
#             sleep = sleeping_time(sleep, success)
#             time.sleep(sleep)
#     except Exception:
#         logging.exception('Oh noes! Exception')


import argparse
import os
from supay import Daemon
from updater import prepare, loop



curdir = None
def init_d():
    global curdir
    curdir = os.path.abspath(os.path.curdir)
    return Daemon(name='deliver', catch_all_log=curdir, pid_dir=curdir)

def run():
    daemon = init_d()
    daemon.start()
    # Undo some of the changes done by start, otherwise it won't work
    os.chdir(curdir)
    prepare()

# Path: updater.py
# def prepare(test_mode=False):
#     '''
#     Creates a new instance of the distributor. Sets up the signal
#     management.
# 
#     test_mode (optional) whether the test mode should be used or
#     not. If so, the normal reader is replaced with a mock that always
#     return zero messages
#     '''
#     global distributor
#     distributor = OnlineDistributor(py)
# 
#     if test_mode:
#         from mock import Mock
#         from deliver.read import Reader
#         reader = Mock(spec=Reader)
#         reader.new_messages.return_value = []
#         distributor._reader = reader
# 
#     signal.signal(signal.SIGTERM, terminate)
# 
# def loop():
#     '''
#     Loops indefinitely, updating the distributor and then sleeping for
#     a defined time.
#     '''
#     print 'Starting...'
#     sleep = py['sleep_interval']
# 
#     try:
#         while run:
#             success = update()
#             sleep = sleeping_time(sleep, success)
#             time.sleep(sleep)
#     except Exception:
#         logging.exception('Oh noes! Exception')


# Path: deliverdaemon.py
import argparse
import os
from supay import Daemon
from updater import prepare, loop



curdir = None
def init_d():
    global curdir
    curdir = os.path.abspath(os.path.curdir)
    return Daemon(name='deliver', catch_all_log=curdir, pid_dir=curdir)

def run():
    daemon = init_d()
    daemon.start()
    # Undo some of the changes done by start, otherwise it won't work
    os.chdir(curdir)
    prepare()

    loop()
