Beispiel #1
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    try:
        locale.setlocale(locale.LC_ALL, "")
        htpc.SYS_ENCODING = locale.getpreferredencoding()
    except (locale.Error, IOError):
        pass

    # for OSes that are poorly configured I'll just force UTF-8
    if not htpc.SYS_ENCODING or htpc.SYS_ENCODING in ('ANSI_X3.4-1968',
                                                      'US-ASCII', 'ASCII'):
        htpc.SYS_ENCODING = 'UTF-8'

    if not hasattr(sys, "setdefaultencoding"):
        reload(sys)

    # python 2.7.9 verifies certs by default. This disables it
    if sys.version_info >= (2, 7, 9):
        import ssl
        ssl._create_default_https_context = ssl._create_unverified_context

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    htpc.SCRIPTDIR = os.path.join(htpc.DATADIR, 'scripts')

    if not os.path.isdir(htpc.SCRIPTDIR):
        os.makedirs(htpc.SCRIPTDIR)

    from mako.lookup import TemplateLookup

    # Enable dev mode if needed
    htpc.DEV = args.dev

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Set default loglevel
    htpc.LOGLEVEL = htpc.settings.get('app_loglevel', 'info')
    if args.loglevel:
        htpc.LOGLEVEL = args.loglevel.lower()
        htpc.settings.set('app_loglevel', args.loglevel.lower())

    # Check for SSL
    htpc.USE_SSL = htpc.settings.get('app_use_ssl')
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir

    if not htpc.WEBDIR.startswith('/'):
        htpc.WEBDIR = '/' + htpc.WEBDIR
    if not htpc.WEBDIR.endswith('/'):
        htpc.WEBDIR += '/'

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(
        directories=[os.path.join(htpc.TEMPLATE, 'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Is used for hiding logout in the menu
    if htpc.USERNAME and htpc.PASSWORD:
        htpc.AUTH = True
    else:
        htpc.AUTH = False

    # Resets the htpc manager password and username
    if args.resetauth:
        htpc.USERNAME = htpc.settings.set('app_username', '')
        htpc.PASSWORD = htpc.settings.set('app_password', '')

    htpc.NOCOLOR = args.nocolor

    # Open webbrowser
    if args.openbrowser or htpc.settings.get('openbrowser') and not htpc.DEV:
        browser_ssl = 's' if htpc.SSLCERT and htpc.SSLKEY and htpc.settings.get(
            'app_use_ssl') else ''
        if htpc.settings.get('app_host') == '0.0.0.0':
            browser_host = 'localhost'
        else:
            browser_host = htpc.settings.get('app_host', 'localhost')
        openbrowser = 'http%s://%s:%s%s' % (browser_ssl, str(browser_host),
                                            htpc.PORT, htpc.WEBDIR[:-1])
        webbrowser.open(openbrowser, new=2, autoraise=True)

    # Select if you want to control processes and open from HTPC Manager
    htpc.SHELL = args.shell

    # Select whether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Initialize Scheduler
    init_sched()

    # Inititialize root and settings page
    load_modules()

    logger = logging.getLogger('root')
    if args.debug:
        logger.warning('Commandline parameter --debug has been deprecated')
    if args.shell:
        logger.warning('Shell parameter --shell has been deprecated')

    htpc.ARGS = sys.argv

    # Start the server
    from htpc.server import start
    start()
Beispiel #2
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata/')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    from mako.lookup import TemplateLookup

    # Enable debug mode if needed
    htpc.DEBUG = args.debug

    # Set loglevel
    htpc.LOGLEVEL = args.loglevel.lower()

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Check for SSL
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir
    if not (htpc.WEBDIR.endswith('/')):
        htpc.WEBDIR += '/'

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(
        directories=[os.path.join(htpc.TEMPLATE, 'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Select wether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Start the server
    from htpc.server import start
    start()
Beispiel #3
0
def main():
    import os, sys, shutil, argparse, htpc

    # Set root and insert bundled libraies into path
    htpc.rundir = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.rundir, 'libs'))

    from sqlobject import connectionForURI, sqlhub
    from mako.lookup import TemplateLookup

    # Set default datadir
    datadir = os.path.join(htpc.rundir, 'userdata/')

    # Get variables from commandline
    parser = argparse.ArgumentParser()
    parser.add_argument('--datadir', default=datadir,
                        help='Set the datadirectory')
    parser.add_argument('--db', default=None,
                        help='Use a custom database')
    parser.add_argument('--host', default=None,
                        help='Use a specific host/IP')
    parser.add_argument('--port', type=int,
                        help='Use a specific port')
    parser.add_argument('--daemon', action='store_true', default=False,
                        help='Daemonize process')
    parser.add_argument('--pid', default=False,
                        help='Generate PID file at location')
    parser.add_argument('--debug', action='store_true', default=False,
                        help='Print debug text')
    args = parser.parse_args()

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.datadir = args.datadir
    if not os.path.isdir(htpc.datadir):
        os.makedirs(htpc.datadir)
    if not os.access(htpc.datadir, os.W_OK):
        sys.exit("No write access to datadir")

    # Set default database and overwrite if supplied through commandline
    htpc.db = os.path.join(htpc.datadir, 'database.db')
    if args.db:
        htpc.db = args.db
    # Initiate database connection
    sqlhub.processConnection = connectionForURI('sqlite:' + htpc.db)

    # Inititialize root and settings page
    htpc.modules = []
    from htpc.root import Root
    htpc.root = Root()
    from htpc.settings import Settings
    from htpc.updater import Updater
    import modules

    settings = htpc.settings.Settings()

    htpc.template = os.path.join('interfaces/', settings.get('app_template','default'))
    htpc.lookup = TemplateLookup(directories=[htpc.template])

    # Overwrite host setting if supplied through commandline
    htpc.host = settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.host = args.host

    # Overwrite port setting if supplied through commandline
    htpc.port = int(settings.get('app_port', 8085))
    if args.port:
        htpc.port = args.port

    htpc.username = settings.get('app_username')
    htpc.password = settings.get('app_password')

    htpc.daemon = False
    if args.daemon and sys.platform != 'win32':
        htpc.daemon = True

    htpc.pid = False
    if args.pid:
        htpc.pid = args.pid

    htpc.debug = False
    if args.debug:
        htpc.debug = True

    from htpc.server import start
    start()
Beispiel #4
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata/')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    from mako.lookup import TemplateLookup

    # Enable debug mode if needed
    htpc.DEBUG = args.debug

    # Set loglevel
    htpc.LOGLEVEL = args.loglevel.lower()

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Check for SSL
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir
    if not (htpc.WEBDIR.endswith('/')):
        htpc.WEBDIR += '/'

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(
        directories=[os.path.join(htpc.TEMPLATE, 'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Is used for hiding logout in the menu
    if htpc.USERNAME and htpc.PASSWORD:
        htpc.AUTH = True
    else:
        htpc.AUTH = False

    # Resets the htpc manager password and username
    if args.resetauth:
        htpc.USERNAME = htpc.settings.set('app_username', '')
        htpc.PASSWORD = htpc.settings.set('app_password', '')

    # Open webbrowser
    if args.openbrowser or htpc.settings.get('openbrowser') and not htpc.DEBUG:
        browser_ssl = 's' if htpc.SSLCERT and htpc.SSLKEY else ''
        if htpc.settings.get('app_host') == '0.0.0.0':
            browser_host = 'localhost'
        else:
            browser_host = htpc.settings.get('app_host', 'localhost')
        openbrowser = 'http%s://%s:%s%s' % (browser_ssl, str(browser_host),
                                            htpc.PORT, htpc.WEBDIR[:-1])
        webbrowser.open(openbrowser, new=2, autoraise=True)

    #Select if you want to controll processes and popen from HTPC-Manager
    htpc.SHELL = args.shell

    # Select wether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Start the server
    from htpc.server import start
    start()
Beispiel #5
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata/')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    from mako.lookup import TemplateLookup

    # Enable debug mode if needed
    htpc.DEBUG = args.debug

    # Set loglevel
    htpc.LOGLEVEL = args.loglevel.lower()
    from htpc.log import Log

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Check for SSL
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir
    if not(htpc.WEBDIR.endswith('/')):
        htpc.WEBDIR += '/'

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(directories=[os.path.join(htpc.TEMPLATE,'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Select wether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Start the server
    from htpc.server import start
    start()
Beispiel #6
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata/')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    from mako.lookup import TemplateLookup

    # Enable debug mode if needed
    htpc.DEBUG = args.debug

    # Set loglevel
    htpc.LOGLEVEL = args.loglevel.lower()

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Check for SSL
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir
    if not(htpc.WEBDIR.endswith('/')):
        htpc.WEBDIR += '/'

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(directories=[os.path.join(htpc.TEMPLATE, 'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Is used for hiding logout in the menu
    if htpc.USERNAME and htpc.PASSWORD:
        htpc.AUTH = True
    else:
        htpc.AUTH = False

     # Resets the htpc manager password and username
    if args.resetauth:
        htpc.USERNAME = htpc.settings.set('app_username', '')
        htpc.PASSWORD = htpc.settings.set('app_password', '')

    # Open webbrowser
    if args.openbrowser or htpc.settings.get('openbrowser') and not htpc.DEBUG:
        browser_ssl = 's' if htpc.SSLCERT and htpc.SSLKEY else ''
        if htpc.settings.get('app_host') == '0.0.0.0':
            browser_host = 'localhost'
        else:
            browser_host = htpc.settings.get('app_host', 'localhost')
        openbrowser = 'http%s://%s:%s%s' % (browser_ssl, str(browser_host), htpc.PORT, htpc.WEBDIR[:-1])
        webbrowser.open(openbrowser, new=2, autoraise=True)


    #Select if you want to controll processes and popen from HTPC-Manager
    htpc.SHELL = args.shell

    # Select wether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Start the server
    from htpc.server import start
    start()
Beispiel #7
0
def main():
    """
    Main function is called at startup.
    """

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    from sqlobject import connectionForURI, sqlhub
    from mako.lookup import TemplateLookup

    args = parse_arguments()

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata/')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to datadir")

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db
    # Initiate database connection
    sqlhub.processConnection = connectionForURI('sqlite:' + htpc.DB)

    # Load settings from database
    from htpc.settings import Settings
    settings = Settings()

    htpc.WEBDIR = settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir
    if not(htpc.WEBDIR.endswith('/')):
        htpc.WEBDIR += '/'

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(directories=[htpc.TEMPLATE])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = settings.get('app_username')
    htpc.PASSWORD = settings.get('app_password')

    htpc.DAEMON = False
    if args.daemon and sys.platform != 'win32':
        htpc.DAEMON = True

    htpc.PID = False
    if args.pid:
        htpc.PID = args.pid

    htpc.DEBUG = False
    if args.debug:
        htpc.DEBUG = True

    from htpc.server import start
    start()
Beispiel #8
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, "libs"))

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, "userdata/")
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    # Initialize the logger
    global logger
    logger = logging.getLogger()
    logch = logging.StreamHandler()
    logfh = logging.FileHandler(os.path.join(htpc.DATADIR, "htpcmanager.log"))

    logformatter = logging.Formatter("%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s")
    logfh.setFormatter(logformatter)
    logch.setFormatter(logformatter)

    loginfo = "ERROR"
    # Set a custom loglevel if supplied via the command line
    if args.loglevel:
        loglevels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
        if args.loglevel in loglevels:
            if args.loglevel == "DEBUG":
                logger.setLevel(logging.DEBUG)
                logch.setLevel(logging.DEBUG)
                logfh.setLevel(logging.DEBUG)
                loginfo = "DEBUG"
            elif args.loglevel == "INFO":
                logger.setLevel(logging.INFO)
                logch.setLevel(logging.INFO)
                logfh.setLevel(logging.INFO)
                loginfo = "INFO"
            elif args.loglevel == "WARNING":
                logger.setLevel(logging.WARNING)
                logch.setLevel(logging.WARNING)
                logfh.setLevel(logging.WARNING)
                loginfo = "WARNING"
            elif args.loglevel == "ERROR":
                logger.setLevel(logging.ERROR)
                logch.setLevel(logging.ERROR)
                logfh.setLevel(logging.ERROR)
                loginfo = "ERROR"
            elif args.loglevel == "CRITICAL":
                logger.setLevel(logging.CRITICAL)
                logch.setLevel(logging.CRITICAL)
                logfh.setLevel(logging.CRITICAL)
                loginfo = "CRITICAL"
        else:
            logger.setLevel(logging.ERROR)
            logch.setLevel(logging.ERROR)
            logfh.setLevel(logging.ERROR)
    else:
        logger.setLevel(logging.ERROR)
        logch.setLevel(logging.ERROR)
        logfh.setLevel(logging.ERROR)

    logger.addHandler(logfh)
    logger.addHandler(logch)

    logger.critical("------------------------")
    logger.critical("Welcome to HTPC-Manager!")
    logger.critical("------------------------")
    logger.critical("Loglevel set to " + loginfo)

    from sqlobject import connectionForURI, sqlhub
    from mako.lookup import TemplateLookup

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, "database.db")
    if args.db:
        htpc.DB = args.db
        logger.info("Connecting to database " + htpc.DB)
    # Initiate database connection
    sqlhub.processConnection = connectionForURI("sqlite:" + htpc.DB)

    # Load settings from database
    from htpc.settings import Settings

    settings = Settings()

    htpc.WEBDIR = settings.get("app_webdir", "/")
    if args.webdir:
        htpc.WEBDIR = args.webdir
        logger.info("Using context root " + htpc.WEBDIR)
    if not (htpc.WEBDIR.endswith("/")):
        htpc.WEBDIR += "/"

    # Inititialize root and settings page
    load_modules()

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, "interfaces/", settings.get("app_template", "default"))
    htpc.LOOKUP = TemplateLookup(directories=[os.path.join(htpc.TEMPLATE, "html/")])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = settings.get("app_host", "0.0.0.0")
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(settings.get("app_port", 8085))
    if args.port:
        htpc.PORT = args.port

    logger.info("Starting on " + htpc.HOST + ":" + str(htpc.PORT))
    htpc.USERNAME = settings.get("app_username")
    htpc.PASSWORD = settings.get("app_password")

    htpc.DAEMON = False
    if args.daemon and sys.platform != "win32":
        logger.info("Setting up daemon-mode")
        htpc.DAEMON = True

    if args.daemon and sys.platform == "win32":
        logger.error("You are using Windows - I cannot setup daemon mode. Please use the pythonw executable instead.")
        logger.error("More information at http://docs.python.org/2/using/windows.html.")

    htpc.PID = False
    if args.pid:
        htpc.PID = args.pid

    htpc.DEBUG = False
    if args.debug:
        logger.info("Enabling DEBUG-Messages")
        logger.setLevel(logging.DEBUG)
        htpc.DEBUG = True

    from htpc.server import start

    start()
Beispiel #9
0
def main():
    """
    Main function is called at startup.
    """
    # Parse runtime arguments
    args = parse_arguments()

    # Set root and insert bundled libraies into path
    htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))

    try:
        locale.setlocale(locale.LC_ALL, "")
        htpc.SYS_ENCODING = locale.getpreferredencoding()
    except (locale.Error, IOError):
        pass

    # for OSes that are poorly configured I'll just force UTF-8
    if not htpc.SYS_ENCODING or htpc.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
        htpc.SYS_ENCODING = 'UTF-8'

    if not hasattr(sys, "setdefaultencoding"):
            reload(sys)

    # python 2.7.9 verifies certs by default. This disables it
    if sys.version_info >= (2, 7, 9):
        import ssl
        ssl._create_default_https_context = ssl._create_unverified_context

    # Set datadir, create if it doesn't exist and exit if it isn't writable.
    htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata')
    if args.datadir:
        htpc.DATADIR = args.datadir
    if not os.path.isdir(htpc.DATADIR):
        os.makedirs(htpc.DATADIR)
    if not os.access(htpc.DATADIR, os.W_OK):
        sys.exit("No write access to userdata folder")

    htpc.SCRIPTDIR = os.path.join(htpc.DATADIR, 'scripts')

    if not os.path.isdir(htpc.SCRIPTDIR):
        os.makedirs(htpc.SCRIPTDIR)

    from mako.lookup import TemplateLookup

    # Enable dev mode if needed
    htpc.DEV = args.dev

    # Set default database and overwrite if supplied through commandline
    htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
    if args.db:
        htpc.DB = args.db

    # Load settings from database
    from htpc.settings import Settings
    htpc.settings = Settings()

    # Set default loglevel
    htpc.LOGLEVEL = htpc.settings.get('app_loglevel', 'info')
    if args.loglevel:
        htpc.LOGLEVEL = args.loglevel.lower()
        htpc.settings.set('app_loglevel', args.loglevel.lower())

    # Check for SSL
    htpc.USE_SSL = htpc.settings.get('app_use_ssl')
    htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
    htpc.SSLKEY = htpc.settings.get('app_ssl_key')

    htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
    if args.webdir:
        htpc.WEBDIR = args.webdir

    if not htpc.WEBDIR.startswith('/'):
        htpc.WEBDIR = '/' + htpc.WEBDIR
    if not htpc.WEBDIR.endswith('/'):
        htpc.WEBDIR += '/'

    htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
                                 htpc.settings.get('app_template', 'default'))
    htpc.LOOKUP = TemplateLookup(directories=[os.path.join(htpc.TEMPLATE, 'html/')])

    # Overwrite host setting if supplied through commandline
    htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
    if args.host:
        htpc.HOST = args.host

    # Overwrite port setting if supplied through commandline
    htpc.PORT = int(htpc.settings.get('app_port', 8085))
    if args.port:
        htpc.PORT = args.port

    htpc.USERNAME = htpc.settings.get('app_username')
    htpc.PASSWORD = htpc.settings.get('app_password')

    # Is used for hiding logout in the menu
    if htpc.USERNAME and htpc.PASSWORD:
        htpc.AUTH = True
    else:
        htpc.AUTH = False

    # Resets the htpc manager password and username
    if args.resetauth:
        htpc.USERNAME = htpc.settings.set('app_username', '')
        htpc.PASSWORD = htpc.settings.set('app_password', '')

    htpc.NOCOLOR = args.nocolor

    # Open webbrowser
    if args.openbrowser or htpc.settings.get('openbrowser') and not htpc.DEV:
        browser_ssl = 's' if htpc.SSLCERT and htpc.SSLKEY and htpc.settings.get('app_use_ssl') else ''
        if htpc.settings.get('app_host') == '0.0.0.0':
            browser_host = 'localhost'
        else:
            browser_host = htpc.settings.get('app_host', 'localhost')
        openbrowser = 'http%s://%s:%s%s' % (browser_ssl, str(browser_host), htpc.PORT, htpc.WEBDIR[:-1])
        webbrowser.open(openbrowser, new=2, autoraise=True)

    # Select if you want to control processes and open from HTPC Manager
    htpc.SHELL = args.shell

    # Select whether to run as daemon
    htpc.DAEMON = args.daemon

    # Set Application PID
    htpc.PID = args.pid

    # Initialize Scheduler
    init_sched()

    # Inititialize root and settings page
    load_modules()

    logger = logging.getLogger('root')
    if args.debug:
        logger.warning('Commandline parameter --debug has been deprecated')
    if args.shell:
        logger.warning('Shell parameter --shell has been deprecated')

    htpc.ARGS = sys.argv

    # Start the server
    from htpc.server import start
    start()