def _check_database_connection(what, metadata, upgrader_class, directory, verbose, db_engine, db_host, db_port, db_name, db_user, db_passwd, options, stdout, stderr, exit_func):
    if verbose: print("Checking database connection for %s..." % what, end="", file=stdout); stdout.flush()

    if db_engine == 'sqlite':
        base_location = os.path.join(os.path.abspath(directory), 'db', '%s.db' % db_name)
        if sys.platform.startswith('win'):
            sqlite_location     = base_location
            location = '/' + base_location
        else:
            sqlite_location = '/' + base_location
            location = '/' + base_location
        sqlite3.connect(database = sqlite_location).close()
    else:
        if db_port is not None:
            port_str = ':%s' % db_port
        else:
            port_str = ''

        if db_engine == 'mysql':
            try:
                import MySQLdb
                assert MySQLdb is not None # Avoid warnings
            except ImportError:
                try:
                    import pymysql_sa
                except __HOLE__:
                    pass
                else:
                    pymysql_sa.make_default_mysql_dialect()

        location = "%(user)s:%(password)s@%(host)s%(port)s/%(name)s" % {
                        'user'     : db_user,
                        'password' : db_passwd,
                        'host'     : db_host,
                        'name'     : db_name,
                        'port'     : port_str,
                    }

    db_str = "%(engine)s://%(location)s" % {
                        'engine'   : db_engine,
                        'location' : location,
                    }

    getconn = generate_getconn(db_engine, db_user, db_passwd, db_host, db_port, db_name, dirname = directory)
    pool = sqlalchemy.pool.QueuePool(getconn)

    try:
        engine = create_engine(db_str, echo = False, pool = pool)
        engine.execute("select 1")
    except Exception as e:
        print("error: database used for %s is misconfigured" % what, file=stderr)
        print("error: %s"  % str(e), file=stderr)
        if verbose:
            traceback.print_exc(file=stderr)
        else:
            print("error: Use -v to get more detailed information", file=stderr)

        try:
            create_database = deploy.generate_create_database(db_engine)
        except Exception as e:
            print("error: You must create the database and the db credentials", file=stderr)
            print("error: reason: there was an error trying to offer you the creation of users:", str(e), file=stderr)
            exit_func(-1)
        else:
            if create_database is None:
                print("error: You must create the database and the db credentials", file=stderr)
                print("error: reason: weblab does not support creating a database with engine %s" % db_engine, file=stderr)
                exit_func(-1)
            else:
                if options[Creation.NOT_INTERACTIVE]:
                    should_create = True
                else:
                    should_create = raw_input('Would you like to create it now? (y/N) ').lower().startswith('y')
                    if not should_create:
                        print("not creating", file=stderr)
                        exit_func(-1)
                if db_engine == 'sqlite':
                    create_database(admin_username = None, admin_password = None, database_name = db_name, new_user = None, new_password = None, db_dir = os.path.join(directory, 'db'))
                elif db_engine == 'mysql':
                    if Creation.MYSQL_ADMIN_USER in options and Creation.MYSQL_ADMIN_PASSWORD in options:
                        admin_username = options[Creation.MYSQL_ADMIN_USER]
                        admin_password = options[Creation.MYSQL_ADMIN_PASSWORD]
                    else:
                        if options[Creation.NOT_INTERACTIVE]:
                            exit_func(-5)
                        global DB_ROOT, DB_PASSWORD
                        if DB_ROOT is None or DB_PASSWORD is None:
                            admin_username = raw_input("Enter the MySQL administrator username [default: root]: ") or 'root'
                            admin_password = getpass.getpass("Enter the MySQL administrator password: ".encode('utf8'))
                        else:
                            admin_username = DB_ROOT
                            admin_password = DB_PASSWORD
                    try:
                        create_database("Did you type your password incorrectly?", admin_username, admin_password, db_name, db_user, db_passwd, db_host, db_port)
                    except Exception as e:
                        print("error: could not create database. reason:", str(e), file=stderr)
                        exit_func(-1)
                    else:
                        DB_ROOT     = admin_username
                        DB_PASSWORD = admin_password
                else:
                    print("error: You must create the database and the db credentials", file=stderr)
                    print("error: reason: weblab does not support gathering information to create a database with engine %s" % db_engine, file=stderr)
                    exit_func(-1)
                engine = create_engine(db_str, echo = False, pool = pool)



    if verbose: print("[done]", file=stdout)
    if verbose: print("Adding information to the %s database..." % what, end="", file=stdout); stdout.flush()
    metadata.drop_all(engine)
    metadata.create_all(engine)

    if upgrader_class is not None:
        if 'alembic_version' in metadata.tables:
            upgrader = upgrader_class(db_str)
            Session = sessionmaker(bind=engine)
            session = Session()
            session._model_changes = {}  # To bypass some kind of bug in some flask-sqlalchemy version.
            session.execute(
                metadata.tables['alembic_version'].insert().values(version_num = upgrader.head)
            )
            session.commit()
            session.close()

    if verbose: print("[done]", file=stdout)
    return engine
dataset/ETHPy150Open weblabdeusto/weblabdeusto/server/src/weblab/admin/script/creation.py/_check_database_connection