示例#1
0
文件: klapi.py 项目: farhany/klapi
def before_request():
    setup.create_db()
    _db = db.connect(settings.settings())
    res = db.select(_db, 'users', ['pass'], 'name=\'%s\'' % ('admin'))
    if res is None or not res:
        admin_pass = utils.generatePassword(20)
        db.insert(_db, 'users', ['admin', admin_pass, utils.generateApiKey()])
        print('Password for admin: "%s", keep this in safe place!\n' %
              (admin_pass))
示例#2
0
def command_update_static(*args):
    """
        Update static files
    """
    import setup
    setup.create_db()
    old_oxjs = os.path.join(settings.static_path, 'oxjs')
    oxjs = os.path.join(settings.base_dir, '..', 'oxjs')
    if os.path.exists(old_oxjs) and not os.path.exists(oxjs):
        shutil.move(old_oxjs, oxjs)
    if not os.path.exists(oxjs):
        r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
    elif os.path.exists(os.path.join(oxjs, '.git')):
        os.system('cd "%s" && git pull' % oxjs)
    r('python3', os.path.join(oxjs, 'tools', 'build', 'build.py'))
    r('python3', os.path.join(settings.static_path, 'py', 'build.py'))
    reader = os.path.join(settings.base_dir, '..', 'reader')
    if not os.path.exists(reader):
        r('git', 'clone', 'https://git.0x2620.org/openmedialibrary_reader.git', reader)
    elif os.path.exists(os.path.join(reader, '.git')):
        os.system('cd "%s" && git pull' % reader)
示例#3
0
def command_update_static(*args):
    """
        Update static files
    """
    import setup
    setup.create_db()
    old_oxjs = os.path.join(settings.static_path, 'oxjs')
    oxjs = os.path.join(settings.base_dir, '..', 'oxjs')
    if os.path.exists(old_oxjs) and not os.path.exists(oxjs):
        shutil.move(old_oxjs, oxjs)
    if not os.path.exists(oxjs):
        r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
    elif os.path.exists(os.path.join(oxjs, '.git')):
        os.system('cd "%s" && git pull' % oxjs)
    r('python3', os.path.join(oxjs, 'tools', 'build', 'build.py'))
    r('python3', os.path.join(settings.static_path, 'py', 'build.py'))
    reader = os.path.join(settings.base_dir, '..', 'reader')
    if not os.path.exists(reader):
        r('git', 'clone', 'https://git.0x2620.org/openmedialibrary_reader.git',
          reader)
    elif os.path.exists(os.path.join(reader, '.git')):
        os.system('cd "%s" && git pull' % reader)
示例#4
0
def command_setup(*args):
    """
        Setup new node
    """
    import setup
    setup.create_db()
示例#5
0
def run():
    setup.create_db()
    PID = sys.argv[2] if len(sys.argv) > 2 else None

    log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
    if not PID:
        logging.basicConfig(level=logging.DEBUG, format=log_format)
    else:
        logging.basicConfig(level=logging.DEBUG,
            filename=settings.log_path, filemode='w',
            format=log_format)
    options = {
        'debug': False,
        'log_function': log_request,
        'gzip': True
    }

    handlers = [
        (r'/(favicon.ico)', StaticFileHandler, {'path': settings.static_path}),
        (r'/static/oxjs/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'oxjs')}),
        (r'/static/cbr.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'cbr.js')}),
        (r'/static/epub.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'epub.js')}),
        (r'/static/pdf.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'pdf.js')}),
        (r'/static/txt.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'txt.js')}),
        (r'/static/(.*)', StaticFileHandler, {'path': settings.static_path}),
        (r'/(.*)/epub/(.*)', EpubHandler),
        (r'/(.*?)/reader/', ReaderHandler),
        (r'/(.*?)/cbr/', FileHandler),
        (r'/(.*?)/pdf/', FileHandler),
        (r'/(.*?)/txt/', FileHandler),
        (r'/(.*?)/get/', FileHandler, {
            'attachment': True
        }),
        (r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
        (r'/api/upload/', UploadHandler, dict(context=db.session)),
        (r'/api/', oxtornado.ApiHandler, dict(context=db.session)),
        (r'/ws', websocket.Handler),
        (r"(.*)", MainHandler),
    ]

    http_server = HTTPServer(Application(handlers, **options))

    http_server.listen(settings.server['port'], settings.server['address'])

    if PID:
        with open(PID, 'w') as pid:
            pid.write('%s' % os.getpid())

    state.main = IOLoop.instance()
    state.cache = Cache(ttl=60)
    state.tasks = tasks.Tasks()

    def start_node():
        import downloads
        import nodes
        import tor
        import bandwidth
        state.bandwidth = bandwidth.Bandwidth()
        state.tor = tor.Tor()
        state.node = node.server.start()
        state.downloads = downloads.Downloads()
        #state.scraping = downloads.ScrapeThread()
        state.nodes = nodes.Nodes()
        def publish():
            if not state.tor.is_online():
                state.main.call_later(1, publish)
            else:
                nodes.publish_node()
        state.main.add_callback(publish)
    state.main.add_callback(start_node)
    if ':' in settings.server['address']:
        host = '[%s]' % settings.server['address']
    elif not settings.server['address']:
        host = '127.0.0.1'
    else:
        host = settings.server['address']
    url = 'http://%s:%s/' % (host, settings.server['port'])
    print('open browser at %s' % url)
    logger.debug('Starting OML %s at %s', settings.VERSION, url)

    def shutdown():
        if state.tor:
            state.tor._shutdown = True
        if state.downloads:
            logger.debug('shutdown downloads')
            state.downloads.join()
        if state.scraping:
            logger.debug('shutdown scraping')
            state.scraping.join()
        logger.debug('shutdown http_server')
        http_server.stop()
        if state.tasks:
            logger.debug('shutdown tasks')
            state.tasks.join()
        if state.nodes:
            logger.debug('shutdown nodes')
            state.nodes.join()
        if state.node:
            state.node.stop()
        if state.tor:
            logger.debug('shutdown tor')
            state.tor.shutdown()
        if PID and os.path.exists(PID):
            logger.debug('remove %s', PID)
            os.unlink(PID)

    signal.signal(signal.SIGTERM, shutdown)

    try:
        state.main.start()
    except:
        print('shutting down...')
    shutdown()
示例#6
0
def command_setup(*args):
    """
        Setup new node
    """
    import setup
    setup.create_db()
示例#7
0
def run():
    setup.create_db()
    PID = sys.argv[2] if len(sys.argv) > 2 else None

    log_format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s'
    if not PID:
        logging.basicConfig(level=logging.DEBUG, format=log_format)
    else:
        logging.basicConfig(level=logging.DEBUG,
                            filename=settings.log_path,
                            filemode='w',
                            format=log_format)
    options = {'debug': False, 'log_function': log_request, 'gzip': True}

    handlers = [
        (r'/(favicon.ico)', StaticFileHandler, {
            'path': settings.static_path
        }),
        (r'/static/oxjs/(.*)', StaticFileHandler, {
            'path': os.path.join(settings.base_dir, '..', 'oxjs')
        }),
        (r'/static/cbr.js/(.*)', StaticFileHandler, {
            'path': os.path.join(settings.base_dir, '..', 'reader', 'cbr.js')
        }),
        (r'/static/epub.js/(.*)', StaticFileHandler, {
            'path': os.path.join(settings.base_dir, '..', 'reader', 'epub.js')
        }),
        (r'/static/pdf.js/(.*)', StaticFileHandler, {
            'path': os.path.join(settings.base_dir, '..', 'reader', 'pdf.js')
        }),
        (r'/static/txt.js/(.*)', StaticFileHandler, {
            'path': os.path.join(settings.base_dir, '..', 'reader', 'txt.js')
        }),
        (r'/static/(.*)', StaticFileHandler, {
            'path': settings.static_path
        }),
        (r'/(.*)/epub/(.*)', EpubHandler),
        (r'/(.*?)/reader/', ReaderHandler),
        (r'/(.*?)/cbr/', FileHandler),
        (r'/(.*?)/pdf/', FileHandler),
        (r'/(.*?)/txt/', FileHandler),
        (r'/(.*?)/get/', FileHandler, {
            'attachment': True
        }),
        (r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
        (r'/api/upload/', UploadHandler, dict(context=db.session)),
        (r'/api/', oxtornado.ApiHandler, dict(context=db.session)),
        (r'/ws', websocket.Handler),
        (r"(.*)", MainHandler),
    ]

    http_server = HTTPServer(Application(handlers, **options))

    http_server.listen(settings.server['port'], settings.server['address'])

    if PID:
        with open(PID, 'w') as pid:
            pid.write('%s' % os.getpid())

    state.main = IOLoop.instance()
    state.cache = Cache(ttl=60)
    state.tasks = tasks.Tasks()

    def start_node():
        import downloads
        import nodes
        import tor
        import bandwidth
        state.bandwidth = bandwidth.Bandwidth()
        state.tor = tor.Tor()
        state.node = node.server.start()
        state.downloads = downloads.Downloads()
        #state.scraping = downloads.ScrapeThread()
        state.nodes = nodes.Nodes()

        def publish():
            if not state.tor.is_online():
                state.main.call_later(1, publish)
            else:
                nodes.publish_node()

        state.main.add_callback(publish)

    state.main.add_callback(start_node)
    if ':' in settings.server['address']:
        host = '[%s]' % settings.server['address']
    elif not settings.server['address']:
        host = '127.0.0.1'
    else:
        host = settings.server['address']
    url = 'http://%s:%s/' % (host, settings.server['port'])
    print('open browser at %s' % url)
    logger.debug('Starting OML %s at %s', settings.VERSION, url)

    def shutdown():
        if state.tor:
            state.tor._shutdown = True
        if state.downloads:
            logger.debug('shutdown downloads')
            state.downloads.join()
        if state.scraping:
            logger.debug('shutdown scraping')
            state.scraping.join()
        logger.debug('shutdown http_server')
        http_server.stop()
        if state.tasks:
            logger.debug('shutdown tasks')
            state.tasks.join()
        if state.nodes:
            logger.debug('shutdown nodes')
            state.nodes.join()
        if state.node:
            state.node.stop()
        if state.tor:
            logger.debug('shutdown tor')
            state.tor.shutdown()
        if PID and os.path.exists(PID):
            logger.debug('remove %s', PID)
            os.unlink(PID)

    signal.signal(signal.SIGTERM, shutdown)

    try:
        state.main.start()
    except:
        print('shutting down...')
    shutdown()