Exemple #1
0
        def __transaction_test(*args, **kwargs):
            paths = bottle_request_paths
            if isinstance(paths, str):
                paths = [paths]

            conn = squery.Connection()
            db = squery.Database(conn)
            config = {'content.contentdir': tempfile.mkdtemp()}
            migrations.migrate(db,
                               in_pkg('migrations', 'sessions'),
                               'librarian.migrations.sessions',
                               config)

            patchers = []
            for brp in paths:
                patcher = mock.patch(brp)
                bottle_request = patcher.start()
                bottle_request.db.sessions = db
                patchers.append(patcher)

            bottle_request.app.config = {'session.lifetime': 1}
            result = func(*args, **kwargs)

            [p.stop() for p in patchers]

            shutil.rmtree(config['content.contentdir'])
            return result
Exemple #2
0
def test_migrate(run, load_mod, get_new, get_mods, get_version, *ignored):
    get_version.return_value = 2
    get_mods.return_value = ['01_test', '02_test', '03_test']
    get_new.return_value = [('03_test', 3)]
    db = mock.Mock()
    mod.migrate(db, '/foo', 'mypkg', {})
    get_version.assert_called_once_with(db)
    get_mods.assert_called_once_with('/foo')
    get_new.assert_called_once_with(get_mods.return_value, 3)
    load_mod.assert_called_once_with('03_test', '/foo', 'mypkg')
    run.assert_called_once_with(3, db, load_mod.return_value, {})
    assert db.refresh_table_stats.called
Exemple #3
0
def prestart(config, logfile=None, debug=False):
    ensure_dir(dirname(config['logging.output']))
    log_config({
        'version': 1,
        'root': {
            'handlers': ['file'],
            'level': logging.DEBUG,
        },
        'handlers': {
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': logfile or config['logging.output'],
                'maxBytes': config['logging.size'],
                'backupCount': config['logging.backups'],
            },
        },
        'formatters': {
            'default': {
                'format': config['logging.format'],
                'datefmt': config['logging.date_format'],
            },
        },
    })
    debug = debug or config['librarian.debug']
    logging.info('Configuring Librarian environment')

    database_configs = database_utils.get_database_configs(config)
    for db_name, db_path in database_configs.items():
        logging.debug('Using database {}'.format(db_path))

    # Make sure all necessary directories are present
    for db_path in database_configs.values():
        ensure_dir(dirname(db_path))

    ensure_dir(config['content.spooldir'])
    ensure_dir(config['content.appdir'])
    ensure_dir(config['content.unpackdir'])
    ensure_dir(config['content.contentdir'])
    ensure_dir(config['content.covers'])

    # Run database migrations
    databases = squery.get_databases(database_configs, debug=debug)
    for db_name, db in databases.items():
        migrations.migrate(db,
                           in_pkg('migrations', db_name),
                           'librarian.migrations.{0}'.format(db_name),
                           config)

    logging.debug("Finished running migrations")
    return databases
Exemple #4
0
def start(logfile=None, profile=False):
    """ Start the application """

    config = app.config

    log_config({
        'version': 1,
        'root': {
            'handlers': ['file'],
            'level': logging.DEBUG,
        },
        'handlers': {
            'file': {
                'class' : 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': logfile or config['logging.output'],
                'maxBytes': int(config['logging.size']),
                'backupCount': int(config['logging.backups'])
            },
        },
        'formatters': {
            'default': {
                'format' : config['logging.format'],
                'datefmt' : config['logging.date_format']
            },
        },
    })

    # Make sure all necessary directories are present
    ensure_dir(dirname(config['logging.output']))
    ensure_dir(dirname(config['database.path']))
    ensure_dir(config['content.spooldir'])
    ensure_dir(config['content.appdir'])
    ensure_dir(config['content.contentdir'])
    ensure_dir(config['content.covers'])

    # Run database migrations
    db = squery.Database(config['database.path'])
    migrations.migrate(db, in_pkg('migrations'))
    logging.debug("Finished running migrations")
    db.disconnect()

    app.install(squery.database_plugin)

    # Set some basic configuration
    bottle.TEMPLATE_PATH.insert(0, in_pkg('views'))
    bottle.BaseTemplate.defaults.update({
        'app_version': __version__,
        'request': request,
        # Translators, used as default page title
        'title': _('Librarian'),
        'style': 'screen',  # Default stylesheet
        'h': helpers,
        'updates': Lazy(lambda: len(list(get_zipballs()))),
        'readable_license': lambda s: dict(LICENSES).get(s, LICENSES[0][1]),
        'is_rtl': Lazy(lambda: request.locale in RTL_LANGS),
        'u': to_unicode,
    })

    # Add middlewares
    wsgiapp = app  # Pass this variable to WSGI middlewares instead of ``app``
    wsgiapp = I18NPlugin(wsgiapp, langs=LOCALES, default_locale=DEFAULT_LOCALE,
                         domain='librarian', locale_dir=in_pkg('locales'))

    # Srart the server
    logging.info('Starting Librarian')

    if profile:
        # Instrument the app to perform profiling
        print('Profiling enabled')
        from repoze.profile import ProfileMiddleware
        wsgiapp = ProfileMiddleware(
                   wsgiapp,
                   log_filename=config['profiling.logfile'],
                   cachegrind_filename=config['profiling.outfile'],
                   discard_first_request=True,
                   flush_at_shutdown=True,
                   path='/__profile__',
                   unwind=False,
        )

    bottle.debug(config['librarian.debug'] == 'yes')
    bottle.run(app=wsgiapp,
               server=config['librarian.server'],
               host=config['librarian.bind'],
               reloader=config['librarian.server'] == 'wsgiref',
               port=int(config['librarian.port']))
Exemple #5
0
def start(logfile=None):
    """ Start the application """

    config = app.config

    log_config({
        'version': 1,
        'root': {
            'handlers': ['file'],
            'level': logging.DEBUG,
        },
        'handlers': {
            'file': {
                'class' : 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': logfile or config['logging.output'],
                'maxBytes': int(config['logging.size']),
                'backupCount': int(config['logging.backups'])
            },
        },
        'formatters': {
            'default': {
                'format' : config['logging.format'],
                'datefmt' : config['logging.date_format']
            },
        },
    })

    # Run database migrations
    db = squery.Database(config['database.path'])
    migrations.migrate(db, in_pkg('migrations'))
    logging.debug("Finished running migrations")
    db.disconnect()

    app.install(squery.database_plugin)

    # Set some basic configuration
    bottle.TEMPLATE_PATH.insert(0, in_pkg('views'))
    bottle.BaseTemplate.defaults.update({
        'app_version': __version__,
        'request': request,
        # Translators, used as default page title
        'title': _('Librarian'),
        'style': 'screen',  # Default stylesheet
        'h': helpers,
        'updates': Lazy(lambda: len(list(get_zipballs()))),
        'readable_license': lambda s: dict(LICENSES).get(s, LICENSES[0][1]),
        'is_rtl': Lazy(lambda: request.locale in RTL_LANGS),
    })

    # Add middlewares
    wsgiapp = app  # Pass this variable to WSGI middlewares instead of ``app``
    wsgiapp = I18NPlugin(wsgiapp, langs=LOCALES, default_locale=DEFAULT_LOCALE,
                         domain='librarian', locale_dir=in_pkg('locales'))

    # Srart the server
    logging.info('Starting Librarian')
    bottle.run(app=wsgiapp,
               server=config['librarian.server'],
               host=config['librarian.bind'],
               port=int(config['librarian.port']),
               reloader=config['librarian.debug'] == 'yes',
               debug=config['librarian.debug'] == 'yes')
Exemple #6
0
def start(logfile=None):
    """ Start the application """

    config = app.config

    log_config({
        'version': 1,
        'root': {
            'handlers': ['file'],
            'level': logging.DEBUG,
        },
        'handlers': {
            'file': {
                'class' : 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': logfile or config['logging.output'],
                'maxBytes': int(config['logging.size']),
                'backupCount': int(config['logging.backups'])
            },
        },
        'formatters': {
            'default': {
                'format' : config['logging.format'],
                'datefmt' : config['logging.date_format']
            },
        },
    })

    # Import gnupg keys
    try:
        content_crypto.import_key(in_pkg('keys', config['content.key']),
                                  keyring=config['content.keyring'])
    except content_crypto.KeyImportError as err:
        logging.error("Could not import keys: %s" % err)
        warn(AppStartupWarning(err))

    # Run database migrations
    db = squery.Database(config['database.path'])
    migrations.migrate(db, in_pkg('migrations'))
    logging.debug("Finished running migrations")
    db.disconnect()

    app.install(squery.database_plugin)

    # Set some basic configuration
    bottle.TEMPLATE_PATH.insert(0, in_pkg('views'))
    bottle.BaseTemplate.defaults.update({
        'app_version': __version__,
        'request': request,
        # Translators, used as default page title
        'title': _('Librarian'),
        'style': 'screen',  # Default stylesheet
        'h': helpers,
    })

    # Add middlewares
    wsgiapp = app  # Pass this variable to WSGI middlewares instead of ``app``
    wsgiapp = I18NPlugin(wsgiapp, langs=LANGS, default_locale=DEFAULT_LOCALE,
                         domain='librarian', locale_dir=in_pkg('locales'))

    # Srart the server
    logging.info('Starting Librarian')
    bottle.run(app=wsgiapp,
               server=config['librarian.server'],
               host=config['librarian.bind'],
               port=int(config['librarian.port']),
               reloader=config['librarian.debug'] == 'yes',
               debug=config['librarian.debug'] == 'yes')
Exemple #7
0
def start(logfile=None, profile=False):
    """ Start the application """

    config = app.config

    log_config({
        'version': 1,
        'root': {
            'handlers': ['file'],
            'level': logging.DEBUG,
        },
        'handlers': {
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': logfile or config['logging.output'],
                'maxBytes': int(config['logging.size']),
                'backupCount': int(config['logging.backups'])
            },
        },
        'formatters': {
            'default': {
                'format': config['logging.format'],
                'datefmt': config['logging.date_format']
            },
        },
    })

    # Make sure all necessary directories are present
    ensure_dir(dirname(config['logging.output']))
    ensure_dir(dirname(config['database.path']))
    ensure_dir(config['content.spooldir'])
    ensure_dir(config['content.appdir'])
    ensure_dir(config['content.contentdir'])
    ensure_dir(config['content.covers'])

    # Run database migrations
    db = squery.Database(config['database.path'])
    migrations.migrate(db, in_pkg('migrations'))
    logging.debug("Finished running migrations")
    db.disconnect()

    app.install(squery.database_plugin)

    # Set some basic configuration
    bottle.TEMPLATE_PATH.insert(0, in_pkg('views'))
    bottle.BaseTemplate.defaults.update({
        'app_version':
        __version__,
        'request':
        request,
        # Translators, used as default page title
        'title':
        _('Librarian'),
        'style':
        'screen',  # Default stylesheet
        'h':
        helpers,
        'updates':
        Lazy(lambda: len(list(get_zipballs()))),
        'readable_license':
        lambda s: dict(LICENSES).get(s, LICENSES[0][1]),
        'is_rtl':
        Lazy(lambda: request.locale in RTL_LANGS),
        'u':
        to_unicode,
    })

    # Add middlewares
    wsgiapp = app  # Pass this variable to WSGI middlewares instead of ``app``
    wsgiapp = I18NPlugin(wsgiapp,
                         langs=LOCALES,
                         default_locale=DEFAULT_LOCALE,
                         domain='librarian',
                         locale_dir=in_pkg('locales'))

    # Srart the server
    logging.info('Starting Librarian')

    if profile:
        # Instrument the app to perform profiling
        print('Profiling enabled')
        from repoze.profile import ProfileMiddleware
        wsgiapp = ProfileMiddleware(
            wsgiapp,
            log_filename=config['profiling.logfile'],
            cachegrind_filename=config['profiling.outfile'],
            discard_first_request=True,
            flush_at_shutdown=True,
            path='/__profile__',
            unwind=False,
        )

    bottle.debug(config['librarian.debug'] == 'yes')
    bottle.run(app=wsgiapp,
               server=config['librarian.server'],
               host=config['librarian.bind'],
               reloader=config['librarian.server'] == 'wsgiref',
               port=int(config['librarian.port']))