Exemplo n.º 1
0
def main():
	#DIFFEREMT
    # rename this thread
    threading.currentThread().name = "MAIN"
    # Set paths
    if hasattr(sys, 'frozen'):
        lazylibrarian.FULL_PATH = os.path.abspath(sys.executable)
    else:
        lazylibrarian.FULL_PATH = os.path.abspath(__file__)

    lazylibrarian.PROG_DIR = os.path.dirname(lazylibrarian.FULL_PATH)
    lazylibrarian.ARGS = sys.argv[1:]

    lazylibrarian.SYS_ENCODING = None

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

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

	#check the version when the application starts
    from lazylibrarian import versioncheck
    lazylibrarian.CURRENT_VERSION = versioncheck.getVersion()
    LATEST_VERSION = versioncheck.checkGithub()

    # Set arguments
    from optparse import OptionParser

    p = OptionParser()
    p.add_option('-d', '--daemon', action = "store_true",
                 dest = 'daemon', help = "Run the server as a daemon")
    p.add_option('-q', '--quiet', action = "store_true",
                 dest = 'quiet', help = "Don't log to console")
    p.add_option('--debug', action="store_true",
                 dest = 'debug', help = "Show debuglog messages")
    p.add_option('--nolaunch', action = "store_true",
                 dest = 'nolaunch', help="Don't start browser")
    p.add_option('--port',
                 dest = 'port', default = None,
                 help = "Force webinterface to listen on this port")
    p.add_option('--datadir',
                 dest = 'datadir', default = None,
                 help = "Path to the data directory")
    p.add_option('--config',
                 dest = 'config', default = None,
                 help = "Path to config.ini file")
    p.add_option('-p', '--pidfile',
                 dest = 'pidfile', default = None,
                 help = "Store the process id in the given file")

    options, args = p.parse_args()

    if options.debug:
        lazylibrarian.LOGLEVEL = 2

    if options.quiet:
        lazylibrarian.LOGLEVEL = 0

    if options.daemon:
        if not sys.platform == 'win32':
            lazylibrarian.DAEMON = True
            lazylibrarian.LOGLEVEL = 0
            lazylibrarian.daemonize()
        else:
            print "Daemonize not supported under Windows, starting normally"

    if options.nolaunch:
        lazylibrarian.LAUNCH_BROWSER = False

    if options.datadir:
        lazylibrarian.DATADIR = str(options.datadir)
    else:
        lazylibrarian.DATADIR = lazylibrarian.PROG_DIR

    if options.config:
        lazylibrarian.CONFIGFILE = str(options.config)
    else:
        lazylibrarian.CONFIGFILE = os.path.join(lazylibrarian.DATADIR, "config.ini")

    if options.pidfile:
        if lazylibrarian.DAEMON:
            lazylibrarian.PIDFILE = str(options.pidfile)

    # create and check (optional) paths
    if not os.path.exists(lazylibrarian.DATADIR):
        try:
            os.makedirs(lazylibrarian.DATADIR)
        except OSError:
            raise SystemExit('Could not create data directory: ' + lazylibrarian.DATADIR + '. Exit ...')

    if not os.access(lazylibrarian.DATADIR, os.W_OK):
        raise SystemExit('Cannot write to the data directory: ' + lazylibrarian.DATADIR + '. Exit ...')

    # create database and config
    lazylibrarian.DBFILE = os.path.join(lazylibrarian.DATADIR, 'lazylibrarian.db')
    lazylibrarian.CFG = ConfigObj(lazylibrarian.CONFIGFILE, encoding='utf-8')

    lazylibrarian.initialize()

    if options.port:
        HTTP_PORT = int(options.port)
        logger.info('Starting LazyLibrarian on forced port: %s' % HTTP_PORT)
    else:
        HTTP_PORT = int(lazylibrarian.HTTP_PORT)
        logger.info('Starting LazyLibrarian on port: %s' % lazylibrarian.HTTP_PORT)

    if lazylibrarian.DAEMON:
        lazylibrarian.daemonize()

    # Try to start the server. 
    webStart.initialize({
                    'http_port': HTTP_PORT,
                    'http_host': lazylibrarian.HTTP_HOST,
                    'http_root': lazylibrarian.HTTP_ROOT,
                    'http_user': lazylibrarian.HTTP_USER,
                    'http_pass': lazylibrarian.HTTP_PASS,
            })

    if lazylibrarian.LAUNCH_BROWSER and not options.nolaunch:
        lazylibrarian.launch_browser(lazylibrarian.HTTP_HOST, lazylibrarian.HTTP_PORT, lazylibrarian.HTTP_ROOT)

    lazylibrarian.start()

    while True:
        if not lazylibrarian.SIGNAL:

            try:
                time.sleep(1)
            except KeyboardInterrupt:
                lazylibrarian.shutdown()
        else:
            if lazylibrarian.SIGNAL == 'shutdown':
                lazylibrarian.shutdown()
            elif lazylibrarian.SIGNAL == 'restart':
                lazylibrarian.shutdown(restart=True)
            else:
                lazylibrarian.shutdown(restart=True, update=True)
            lazylibrarian.SIGNAL = None
    return
Exemplo n.º 2
0
 def checkForUpdates(self):
     #check the version when the application starts
     from lazylibrarian import versioncheck
     lazylibrarian.CURRENT_VERSION = versioncheck.getVersion()
     versioncheck.checkGithub()
     raise cherrypy.HTTPRedirect("config")
Exemplo n.º 3
0
 def checkForUpdates(self):
     #check the version when the application starts
     from lazylibrarian import versioncheck
     lazylibrarian.CURRENT_VERSION = versioncheck.getVersion()
     versioncheck.checkGithub()
     raise cherrypy.HTTPRedirect("config")
Exemplo n.º 4
0
def initialize():
    with INIT_LOCK:
        global __INITIALIZED__, FULL_PATH, PROG_DIR, LOGLEVEL, DAEMON, DATADIR, CONFIGFILE, CFG, LOGDIR, HTTP_HOST, HTTP_PORT, HTTP_USER, HTTP_PASS, HTTP_ROOT, HTTP_LOOK, LAUNCH_BROWSER, CACHEDIR, \
            IMP_ONLYISBN, IMP_PREFLANG, SAB_HOST, SAB_PORT, SAB_API, SAB_USER, SAB_PASS, DESTINATION_DIR, DESTINATION_COPY, DOWNLOAD_DIR, SAB_CAT, USENET_RETENTION, BLACKHOLE, BLACKHOLEDIR, GR_API, \
            GIT_PATH, GIT_USER, GIT_PROJECT, GIT_BRANCH, CURRENT_VERSION, LATEST_VERSION, CHECK_UPDATE, CHECK_UPDATE_INTERVAL, \
            NZBMATRIX, NZBMATRIX_USER, NZBMATRIX_API, NEWZNAB, NEWZNAB_HOST, NEWZNAB_API, NEWZBIN, NEWZBIN_UID, NEWZBIN_PASS, NEWZNAB2, NEWZNAB_HOST2, NEWZNAB_API2, EBOOK_TYPE

        if __INITIALIZED__:
            return False

        CheckSection('General')
        CheckSection('SABnzbd')

        try:
            HTTP_PORT = check_setting_int(CFG, 'General', 'http_port', 8082)
        except:
            HTTP_PORT = 8082

        if HTTP_PORT < 21 or HTTP_PORT > 65535:
            HTTP_PORT = 8082

        HTTP_HOST = check_setting_str(CFG, 'General', 'http_host', '0.0.0.0')
        HTTP_USER = check_setting_str(CFG, 'General', 'http_user', '')
        HTTP_PASS = check_setting_str(CFG, 'General', 'http_pass', '')
        HTTP_ROOT = check_setting_str(CFG, 'General', 'http_root', '')
        HTTP_LOOK = check_setting_str(CFG, 'General', 'http_look', 'default')
        GIT_PATH = check_setting_str(CFG, 'General', 'git_path', '')
        GIT_USER = check_setting_str(CFG, 'General', 'git_user', 'herman-rogers')
        GIT_PROJECT = check_setting_str(CFG, 'General', 'git_project', 'LazyLibrarian-1')
        GIT_BRANCH = check_setting_str(CFG, 'General', 'git_branch', 'master')
        
        CHECK_UPDATE = bool(check_setting_int(CFG, 'General', 'check_update', 1))
        CHECK_UPDATE_INTERVAL = check_setting_int(CFG, 'General', 'check_update_interval', 360)

        LAUNCH_BROWSER = bool(check_setting_int(CFG, 'General', 'launch_browser', 1))
        LOGDIR = check_setting_str(CFG, 'General', 'logdir', '')

        IMP_PREFLANG = check_setting_str(CFG, 'General', 'imp_preflang', IMP_PREFLANG)
        IMP_ONLYISBN = bool(check_setting_int(CFG, 'General', 'imp_onlyisbn', 0))

        SAB_HOST = check_setting_str(CFG, 'SABnzbd', 'sab_host', '')
        SAB_PORT = check_setting_str(CFG, 'SABnzbd', 'sab_port', '')
        SAB_USER = check_setting_str(CFG, 'SABnzbd', 'sab_user', '')
        SAB_PASS = check_setting_str(CFG, 'SABnzbd', 'sab_pass', '')
        SAB_API = check_setting_str(CFG, 'SABnzbd', 'sab_api', '')
        SAB_CAT = check_setting_str(CFG, 'SABnzbd', 'sab_cat', '')


        DESTINATION_COPY = bool(check_setting_int(CFG, 'General', 'destination_copy', 0))
        DESTINATION_DIR = check_setting_str(CFG, 'General','destination_dir', '')
        DOWNLOAD_DIR = check_setting_str(CFG, 'General', 'download_dir', '')
        BLACKHOLE = bool(check_setting_int(CFG, 'General', 'blackhole', 0))
        BLACKHOLEDIR = check_setting_str(CFG, 'General', 'blackholedir', '')
        USENET_RETENTION = check_setting_str(CFG, 'General', 'usenet_retention', '')

        NZBMATRIX = bool(check_setting_int(CFG, 'NZBMatrix', 'nzbmatrix', 0))
        NZBMATRIX_USER = check_setting_str(CFG, 'NZBMatrix', 'nzbmatrix_user', '')
        NZBMATRIX_API = check_setting_str(CFG, 'NZBMatrix', 'nzbmatrix_api', '')
        
        NEWZNAB = bool(check_setting_int(CFG, 'Newznab', 'newznab', 0))
        NEWZNAB_HOST = check_setting_str(CFG, 'Newznab', 'newznab_host', '')
        NEWZNAB_API = check_setting_str(CFG, 'Newznab', 'newznab_api', '')

        NEWZNAB2 = bool(check_setting_int(CFG, 'Newznab2', 'newznab2', 0))
        NEWZNAB_HOST2 = check_setting_str(CFG, 'Newznab2', 'newznab_host2', '')
        NEWZNAB_API2 = check_setting_str(CFG, 'Newznab2', 'newznab_api2', '')

        NEWZBIN = bool(check_setting_int(CFG, 'Newzbin', 'newzbin', 0))
        NEWZBIN_UID = check_setting_str(CFG, 'Newzbin', 'newzbin_uid', '')
        NEWZBIN_PASS = check_setting_str(CFG, 'Newzbin', 'newzbin_pass', '')
        EBOOK_TYPE = check_setting_str(CFG, 'General', 'ebook_type', 'epub')

        GR_API = check_setting_str(CFG, 'General', 'gr_api', 'ckvsiSDsuqh7omh74ZZ6Q')

        if not LOGDIR:
            LOGDIR = os.path.join(DATADIR, 'Logs')

        # Create logdir
        if not os.path.exists(LOGDIR):
            try:
                os.makedirs(LOGDIR)
            except OSError:
                if LOGLEVEL:
                    print LOGDIR + ":"
                    print ' Unable to create folder for logs. Only logging to console.'

        # Start the logger, silence console logging if we need to
        logger.lazylibrarian_log.initLogger(loglevel=LOGLEVEL)

        # Announce ourself
        logger.info(u'Starting LazyLibrarian.')

        # Put the cache dir in the data dir for now
        CACHEDIR = os.path.join(DATADIR, 'cache')
        if not os.path.exists(CACHEDIR):
            try:
                logger.debug(u'Creating cachedir: %s' % CACHEDIR)
                os.makedirs(CACHEDIR)
            except OSError:
                logger.error(u'Could not create cachedir. Check permissions of: %s' % DATADIR)

        # Clearing cache
        if os.path.exists(".ProviderCache"):
            for f in os.listdir(".ProviderCache"):
                logger.debug(u'Clearing cache.')
                os.unlink("%s/%s" % (".ProviderCache", f))
        # Clearing throttling timeouts
        logger.debug(u'Clearing throttling timeouts')
        t = SimpleCache.ThrottlingProcessor()
        t.lastRequestTime.clear()

        # Initialize the database
        try:
            logger.info(u'Checking the database.')
            dbcheck()
        except Exception, e:
            logger.error(u"Can't connect to the database: %s" % e)

        # Get the currently installed version - returns None, 'win32' or the git hash
        # Also sets INSTALL_TYPE variable to 'win', 'git' or 'source'
        logger.info(u'Getting current version.')
        from lazylibrarian import versioncheck
        CURRENT_VERSION = versioncheck.getVersion()
        logger.debug(u'Current version is: %s' % CURRENT_VERSION)

        logger.debug(u'Initialize completed.')
        __INITIALIZED__ = True
        return True