예제 #1
0
 def setUp(self):
     super().setUp()
     assert isinstance(connection, Connection)
     self.transaction = connection.begin_nested()
     s = scoped_session(sessionmaker(bind=connection))
     session.set_scoped_session(s)
     self.addCleanup(self.cleanup)
예제 #2
0
def server_run(args):
    app = make_app(args.debug)
    wait_for_db: bool = args.wait_for_database

    connection_string = get_connection_string()
    connection = try_create_connection(connection_string, wait_for_db,
                                       app.logger)

    state = AlembicHelper(connection)
    strategy = SchemaStrategist(state).determine_schema_strategy()
    strategy()

    @app.before_request
    def get_time():
        g.request_time = time.time()

    @app.teardown_request
    def time_response(exception=None):
        request_time = g.pop('request_time', None)

        if request_time:
            time_taken = time.time() - request_time
            if time_taken > 0.5:
                app.logger.warn(
                    "Response took {duration} seconds for request {path}".
                    format(path=request.full_path, duration=time_taken))

    engine = create_engine(connection_string, echo=args.profile)
    set_scoped_session(
        scoped_session(sessionmaker(bind=engine),
                       scopefunc=lambda: _request_ctx_stack.top))

    def lookup_translation():
        ctx = _request_ctx_stack.top
        if ctx is None:
            return None
        translations = getattr(ctx, 'pycroft_translations', None)
        if translations is None:
            translations = Translations()
            for module in (pycroft, web):
                os.path.dirname(module.__file__)
                dirname = os.path.join(ctx.app.root_path, 'translations')
                translations.merge(Translations.load(dirname, [get_locale()]))
            ctx.pycroft_translations = translations
        return translations

    set_translation_lookup(lookup_translation)
    app.config.from_pyfile('flask.cfg')
    if args.profile:
        app.config['PROFILE'] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
    app.run(debug=args.debug, port=args.port, host=args.host, threaded=True)
예제 #3
0
파일: task.py 프로젝트: agdsn/pycroft
    def __init__(self):
        in_celery = sys.argv and sys.argv[0].endswith('celery') \
                                   and 'worker' in sys.argv

        if in_celery:
            connection_string = get_connection_string()

            self.connection, self.engine = try_create_connection(connection_string,
                                                                 5,
                                                                 logger=logging.getLogger("tasks"),
                                                                 echo=False)

            set_scoped_session(scoped_session(sessionmaker(bind=self.engine)))
예제 #4
0
def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    if not SchemaStrategist(state).is_up_to_date:
        print("Schema is not up to date!")
        return

    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")
예제 #5
0
파일: cli.py 프로젝트: agdsn/abe-importer
def main(abe_uri_file: str, pycroft_uri_file: str, dry_run: bool, refresh: bool, verbose: bool):
    colorama.init()
    abe_session = create_session(read_uri(uri_file=abe_uri_file))
    _pyc_scoped_session = create_scoped_session(read_uri(pycroft_uri_file))
    pyc_session.set_scoped_session(_pyc_scoped_session)
    pycroft_session = pyc_session.session

    logger_name = 'abe-importer'
    logger = setup_logger(logger_name, verbose)

    check_connections(abe_session, pycroft_session, logger=logger)

    view_too_old = ldap_view_too_old(abe_session)
    if refresh:
        logger.info("Refreshing LDAP view due to CLI parameters…")
        logger.info("HINT: You can disable this with --no-refresh")
    if view_too_old:
        logger.warning("LDAP view is older than one day, forcing refresh…")
    if refresh or view_too_old:
        refresh_ldap_view(abe_session)
        logger.info("…Done.")
    else:
        logger.info("Skipping LDAP refresh.  Use --refresh to force it.")

    try:
        objs = do_import(abe_session, pycroft_session, logger)
    except ImportException:
        exit(1)
        return  # Don't judge me, this keeps pycharm silent

    if dry_run:
        exit(0)
        return

    if click.confirm(f'Do you want to add {len(objs)} new entries to the pycroft repository?',
                     abort=True):
        pycroft_session.add_all(objs)
        pycroft_session.commit()
    else:
        pycroft_session.rollback()
예제 #6
0
def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    if not SchemaStrategist(state).is_up_to_date:
        print("Schema is not up to date!")
        return

    set_scoped_session(
        scoped_session(sessionmaker(bind=engine),
                       scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")
예제 #7
0
 def setUp(self):
     super(SQLAlchemyTestCase, self).setUp()
     self.transaction = connection.begin_nested()
     s = scoped_session(sessionmaker(bind=connection))
     session.set_scoped_session(s)
     self.addCleanup(self.cleanup)
예제 #8
0
def establish_and_return_session(connection_string):
    engine = create_engine(connection_string)
    set_scoped_session(scoped_session(sessionmaker(bind=engine)))
    return global_session  # from pycroft.model.session
예제 #9
0
파일: interactive.py 프로젝트: JuKu/pycroft
def setup():
    # TODO: don't set a scoped session, just a regular one
    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))
예제 #10
0
def prepare_server(args) -> tuple[PycroftFlask, Callable]:
    """returns both the prepared app and a callback executing `app.run`"""
    if args.echo:
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
        logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
        logging.getLogger('sqlalchemy.pool').setLevel(logging.DEBUG)
    app = make_app(args.debug)

    logging.getLogger('pycroft').addHandler(default_handler)

    wait_for_db: bool = args.wait_for_database

    connection_string = get_connection_string()
    connection, engine = try_create_connection(connection_string, wait_for_db, app.logger,
                                               reflections=False)

    state = AlembicHelper(connection)
    if args.force_schema_create:
        strategy = SchemaStrategist(state).create_then_stamp
    else:
        strategy = SchemaStrategist(state).determine_schema_strategy()
    strategy()

    @app.before_request
    def get_time():
        g.request_time = time.time()

    @app.teardown_request
    def time_response(exception=None):
        request_time = g.pop('request_time', None)

        if request_time:
            time_taken = time.time() - request_time
            if time_taken > 0.5:
                app.logger.warning(
                    "Response took %s seconds for request %s",
                    time_taken, request.full_path,
                )

    connection, engine = try_create_connection(connection_string, wait_for_db, app.logger,
                                               args.profile)

    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    def lookup_translation():
        ctx = _request_ctx_stack.top
        if ctx is None:
            return None
        translations = getattr(ctx, 'pycroft_translations', None)
        if translations is None:
            translations = Translations()
            for module in (pycroft, web):
                os.path.dirname(module.__file__)
                dirname = os.path.join(ctx.app.root_path, 'translations')
                translations.merge(Translations.load(dirname, [get_locale()]))
            ctx.pycroft_translations = translations
        return translations

    set_translation_lookup(lookup_translation)
    app.config.from_pyfile('flask.cfg')
    if args.profile:
        app.config['PROFILE'] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
    return app, lambda: app.run(debug=args.debug, port=args.port, host=args.host, threaded=True)
예제 #11
0
파일: conftest.py 프로젝트: agdsn/pycroft
def session(connection, request):
    transaction = connection.begin_nested()
    request.addfinalizer(lambda: rollback(transaction))
    s = scoped_session(sessionmaker(bind=connection))
    pyc_session.set_scoped_session(s)
    return s
예제 #12
0
파일: server_run.py 프로젝트: agdsn/pycroft
def server_run(args):
    app = make_app(args.debug)

    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    if args.wait_for_database == 0:
        max_wait = float('inf')
    else:
        max_wait = time.clock_gettime(time.CLOCK_MONOTONIC) + args.wait_for_database
    for timeout in chain([1, 2, 5, 10, 30], repeat(60)):
        try:
            connection = engine.connect()
            break
        except OperationalError:
            # Important: Use %r to print the URL, passwords are hidden by the
            # __repr__ of sqlalchemy.engine.URL
            app.logger.warn("Could not connect to database %r", engine.url)
            timeout = min(timeout,
                          max_wait - time.clock_gettime(time.CLOCK_MONOTONIC))
            if timeout > 0:
                app.logger.info("Waiting for %d seconds", timeout)
                time.sleep(timeout)
            else:
                raise
    state = AlembicHelper(connection)
    strategy = SchemaStrategist(state).determine_schema_strategy()
    strategy()

    print("If you're running in a docker setup, the port may differ"
          " from what is given below."
          " It is probably http://0.0.0.0:5001")

    @app.before_request
    def get_time():
        g.request_time = time.time()

    @app.teardown_request
    def time_response(exception=None):
        time_taken = time.time() - g.request_time
        if time_taken > 0.5:
            app.logger.warn(
                "Response took {duration} seconds for request {path}".format(
                    path=request.full_path, duration=time_taken))

    engine = create_engine(connection_string, echo=args.profile)
    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    def lookup_translation():
        ctx = _request_ctx_stack.top
        if ctx is None:
            return None
        translations = getattr(ctx, 'pycroft_translations', None)
        if translations is None:
            translations = Translations()
            for module in (pycroft, web):
                os.path.dirname(module.__file__)
                dirname = os.path.join(ctx.app.root_path, 'translations')
                translations.merge(Translations.load(dirname, [get_locale()]))
            ctx.pycroft_translations = translations
        return translations

    set_translation_lookup(lookup_translation)
    app.config.from_pyfile('flask.cfg')
    if args.profile:
        app.config['PROFILE'] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
    app.run(debug=args.debug, port=args.port, host=args.host, threaded=True)
예제 #13
0
파일: __init__.py 프로젝트: agdsn/pycroft
 def setUp(self):
     super(SQLAlchemyTestCase, self).setUp()
     self.transaction = connection.begin_nested()
     s = scoped_session(sessionmaker(bind=connection))
     session.set_scoped_session(s)
     self.addCleanup(self.cleanup)
예제 #14
0
파일: exporter.py 프로젝트: agdsn/pycroft
def establish_and_return_session(connection_string):
    engine = create_engine(connection_string)
    set_scoped_session(scoped_session(sessionmaker(bind=engine)))
    return global_session  # from pycroft.model.session