2014-10-22 16:15:23 +00:00
|
|
|
#!/usr/bin/env python
|
2014-06-13 17:04:00 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
#
|
2019-03-31 12:43:32 +00:00
|
|
|
# Copyright (C) 2014-2019 Alban 'spl0k' Féron
|
2014-06-13 17:04:00 +00:00
|
|
|
#
|
2019-03-31 12:43:32 +00:00
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
from signal import signal, SIGTERM, SIGINT
|
2014-06-13 17:04:00 +00:00
|
|
|
|
2017-11-27 21:30:13 +00:00
|
|
|
from supysonic.config import IniConfig
|
2019-04-08 17:02:37 +00:00
|
|
|
from supysonic.daemon import Daemon
|
2019-03-31 12:43:32 +00:00
|
|
|
from supysonic.db import init_database, release_database
|
2014-10-26 18:37:14 +00:00
|
|
|
|
2019-03-31 12:43:32 +00:00
|
|
|
logger = logging.getLogger('supysonic')
|
|
|
|
|
2019-04-08 17:02:37 +00:00
|
|
|
daemon = None
|
2019-03-31 12:43:32 +00:00
|
|
|
|
|
|
|
def setup_logging(config):
|
|
|
|
if config['log_file']:
|
|
|
|
if config['log_file'] == '/dev/null':
|
|
|
|
log_handler = logging.NullHandler()
|
|
|
|
else:
|
|
|
|
log_handler = TimedRotatingFileHandler(config['log_file'], when = 'midnight')
|
|
|
|
log_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s"))
|
|
|
|
else:
|
|
|
|
log_handler = logging.StreamHandler()
|
|
|
|
log_handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
|
|
|
|
logger.addHandler(log_handler)
|
|
|
|
if 'log_level' in config:
|
|
|
|
level = getattr(logging, config['log_level'].upper(), logging.NOTSET)
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
def __terminate(signum, frame):
|
|
|
|
logger.debug("Got signal %i. Stopping...", signum)
|
2019-04-08 17:02:37 +00:00
|
|
|
daemon.terminate()
|
2019-03-31 12:43:32 +00:00
|
|
|
release_database()
|
|
|
|
|
2017-10-08 14:22:04 +00:00
|
|
|
if __name__ == "__main__":
|
2017-11-27 21:30:13 +00:00
|
|
|
config = IniConfig.from_common_locations()
|
2019-03-31 12:43:32 +00:00
|
|
|
setup_logging(config.DAEMON)
|
|
|
|
|
|
|
|
signal(SIGTERM, __terminate)
|
|
|
|
signal(SIGINT, __terminate)
|
|
|
|
|
|
|
|
init_database(config.BASE['database_uri'])
|
2019-04-08 17:02:37 +00:00
|
|
|
daemon = Daemon(config.DAEMON['socket'])
|
|
|
|
daemon.run(config)
|
2019-03-31 12:43:32 +00:00
|
|
|
release_database()
|