Esempio n. 1
0
def RelStorageConfigurationFactory(key, dbconfig):
    if not RELSTORAGE:
        raise Exception("You must install the relstorage package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    options = Options(**dbconfig['options'])
    if dbconfig['type'] == 'postgres':
        from relstorage.adapters.postgresql import PostgreSQLAdapter
        dsn = "dbname={dbname} user={user} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
        adapter = PostgreSQLAdapter(dsn=dsn, options=options)
    rs = RelStorage(adapter=adapter, options=options)
    db = DB(rs)
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    rs = RelStorage(adapter=adapter, options=options)
    db = RequestAwareDB(rs, **config)
    return Database(key, db)
Esempio n. 2
0
def RelStorageConfigurationFactory(key, dbconfig):
    if not RELSTORAGE:
        raise Exception("You must install the relstorage package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    options = Options(**dbconfig['options'])
    if dbconfig['type'] == 'postgres':
        from relstorage.adapters.postgresql import PostgreSQLAdapter
        dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
        adapter = PostgreSQLAdapter(dsn=dsn, options=options)
    rs = RelStorage(adapter=adapter, options=options)
    db = DB(rs)
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    rs = RelStorage(adapter=adapter, options=options)
    db = RequestAwareDB(rs, **config)
    return Database(key, db)
Esempio n. 3
0
def ZEODatabaseConfigurationFactory(key, dbconfig):
    if not ZEOSERVER:
        raise Exception("You must install the ZEO package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    # Try to open it normal to create the root object
    address = (dbconfig['address'], dbconfig['port'])

    zeoconfig = dbconfig.get('zeoconfig', {})
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = DB(cs)

    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()

    # Set request aware database for app
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = RequestAwareDB(cs, **config)
    return Database(key, db)
Esempio n. 4
0
def ZEODatabaseConfigurationFactory(key, dbconfig):
    if not ZEOSERVER:
        raise Exception("You must install the ZEO package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    # Try to open it normal to create the root object
    address = (dbconfig['address'], dbconfig['port'])

    zeoconfig = dbconfig.get('zeoconfig', {})
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = DB(cs)

    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()

    # Set request aware database for app
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = RequestAwareDB(cs, **config)
    return Database(key, db)
Esempio n. 5
0
async def close_utilities(app):
    for utility in getAllUtilitiesRegisteredFor(IAsyncUtility):
        asyncio.ensure_future(utility.finalize(app=app), loop=app.loop)
    for db in app.router._root:
        if IDatabase.providedBy(db[1]):
            try:
                db[1]._db.close()
            except RequestNotFound:
                pass
Esempio n. 6
0
def run_migrations(root, migrations):
    # prevent circular imports...
    from plone.server.interfaces import IDatabase
    sites = []
    for _id, db in root:
        if IDatabase.providedBy(db):
            sites.extend([db[s_id] for s_id in db.keys()])

    for site in sites:
        run_site_migrations(site, migrations)
Esempio n. 7
0
def ZODBDatabaseConfigurationFactory(key, dbconfig):
    config = dbconfig.get('configuration', {})
    fs = ZODB.FileStorage.FileStorage(dbconfig['path'])
    db = DB(fs)
    try:
        rootobj = db.open().root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
        rootobj = None
    except:
        pass
    finally:
        db.close()
    # Set request aware database for app
    db = RequestAwareDB(dbconfig['path'], **config)
    return Database(key, db)
Esempio n. 8
0
    def run(self, arguments, settings, app):
        app_settings['root_user']['password'] = TESTING_SETTINGS['root_user']['password']

        root = getUtility(IApplication, name='root')

        # get sites to run command against
        if arguments.site:
            sites = {
                arguments.site: traverse_to_path(app, arguments.site)
            }
        else:
            sites = {}
            for _id, db in root:
                if IDatabase.providedBy(db):
                    for s_id in db.keys():
                        sites['/' + _id + '/' + s_id] = (db, db[s_id])

        if len(sites) == 0:
            print('No sites found')

        if arguments.app:
            apps = [arguments.app]
        else:
            apps = migrate.get_migratable_applications()

        if not arguments.report:
            # run them...
            for db, site in sites.values():
                for app in apps:
                    registry = site['_registry']
                    try:
                        installed_versions = registry[MIGRATION_DATA_REGISTRY_KEY]
                    except KeyError:
                        installed_versions = {}
                    _migrations = migrate.get_migrations(
                        app, to_version=arguments.to_version,
                        from_version=installed_versions.get(app))
                    if len(_migrations) > 0:
                        trns = db.get_transaction_manager()
                        if not arguments.dry_run and not arguments.report:
                            trns.begin()
                        migrate.run_site_migrations(site, _migrations, db)
                        if not arguments.dry_run and not arguments.report:
                            trns.commit()

        self.report(arguments, sites, apps)
Esempio n. 9
0
def ZODBDatabaseConfigurationFactory(key, dbconfig):
    config = dbconfig.get('configuration', {})
    fs = ZODB.FileStorage.FileStorage(dbconfig['path'])
    db = DB(fs)
    try:
        rootobj = db.open().root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
        rootobj = None
    except:
        pass
    finally:
        db.close()
    # Set request aware database for app
    db = RequestAwareDB(dbconfig['path'], **config)
    return Database(key, db)
Esempio n. 10
0
    def __call__(self):
        result = {
            'databases': [],
            'static_file': [],
            'static_directory': []
        }

        allowed = self.request.security.checkPermission(
            'plone.GetDatabases', self.application)

        for x in self.application._dbs.keys():
            if IDatabase.providedBy(self.application._dbs[x]) and allowed:
                result['databases'].append(x)
            if IStaticFile.providedBy(self.application._dbs[x]):
                result['static_file'].append(x)
            if IStaticDirectory.providedBy(self.application._dbs[x]):
                result['static_directory'].append(x)
        return result
Esempio n. 11
0
async def traverse(request, parent, path):
    """Do not use outside the main router function."""
    if IApplication.providedBy(parent):
        request.application = parent

    if not path:
        return parent, path

    assert request is not None  # could be used for permissions, etc

    try:
        if path[0].startswith('_'):
            raise HTTPUnauthorized()
        context = parent[path[0]]
    except TypeError:
        return parent, path
    except KeyError:
        return parent, path

    if IDatabase.providedBy(context):
        if SHARED_CONNECTION:
            request.conn = context.conn
        else:
            # Create a new conection
            request.conn = context.open()
        # Check the transaction
        request._db_write_enabled = False
        request._db_id = context.id
        context = request.conn.root()

    if ISite.providedBy(context):
        request._site_id = context.id
        request.site = context
        request.site_settings = context['_registry']
        layers = request.site_settings.get(ACTIVE_LAYERS_KEY, [])
        for layer in layers:
            alsoProvides(request, import_class(layer))

    return await traverse(request, context, path[1:])
Esempio n. 12
0
def NewtConfigurationFactory(key, dbconfig):
    if not NEWT:
        raise Exception("You must install the newt.db package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
    adapter = newt.db.storage(dsn=dsn, **dbconfig['options'])
    db = newt.db.DB(dsn, **dbconfig['options'])
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    adapter = newt.db.storage(dsn, **dbconfig['options'])
    db = newt.db._db.NewtDB(RequestAwareDB(adapter, **config))
    return Database(key, db)
Esempio n. 13
0
def NewtConfigurationFactory(key, dbconfig):
    if not NEWT:
        raise Exception("You must install the newt.db package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
    adapter = newt.db.storage(dsn=dsn, **dbconfig['options'])
    db = newt.db.DB(dsn, **dbconfig['options'])
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    adapter = newt.db.storage(dsn, **dbconfig['options'])
    db = newt.db._db.NewtDB(RequestAwareDB(adapter, **config))
    return Database(key, db)