def configure_db(self): def _patch_session(): import sqlalchemy from sqlalchemy import event from sqlalchemy.orm import sessionmaker self.addCleanup(setattr, self.Session, 'engine', self.Session.engine) self.addCleanup(setattr, self.Session, 'session_factory', self.Session.session_factory) self.Session.engine = sqlalchemy.create_engine('sqlite:///' + os.path.join(config['root'], 'data', 'test.db')) event.listen(self.Session.engine, 'connect', lambda conn, record: conn.execute('pragma foreign_keys=ON')) self.Session.session_factory = sessionmaker(bind=self.Session.engine, autoflush=False, autocommit=False) if self.Session: possible_plugin = self.__class__.__module__.split('.')[0] if _is_plugin_name(possible_plugin): with use_plugin_virtualenv(possible_plugin): _patch_session() else: _patch_session()
def patch_session(Session, request): def _patch(): import sqlalchemy from sqlalchemy import event from sqlalchemy.orm import sessionmaker orig_engine, orig_factory = Session.engine, Session.session_factory request.addfinalizer(lambda: setattr(Session, 'engine', orig_engine)) request.addfinalizer(lambda: setattr(Session, 'session_factory', orig_factory)) Session.engine = sqlalchemy.create_engine('sqlite:////tmp/test.db') event.listen(Session.engine, 'connect', lambda conn, record: conn.execute('pragma foreign_keys=ON')) Session.session_factory = sessionmaker(bind=Session.engine, autoflush=False, autocommit=False) Session.initialize_db(drop=True) possible_plugin = Session.__module__.split('.')[0] if _is_plugin_name(possible_plugin): with use_plugin_virtualenv(possible_plugin): _patch() else: _patch()
def run_plugin_entry_point(): if len(argv) < 2: print('usage: {} ENTRY_POINT_NAME ...'.format(argv[0])) exit(1) if len(argv) == 2 and argv[1] in ['-h', '--help']: print('known entry points:') print('\n'.join(sorted(_entry_points))) exit(0) del argv[:1] # we want the entry point name to be the first argument ep_name = argv[0] if ep_name not in _entry_points: print('no entry point exists with name {!r}'.format(ep_name)) exit(2) func = _entry_points[ep_name] plugin_name = func.__module__.split('.')[0] with use_plugin_virtualenv(plugin_name): func()