def generate_create_database(engine_str):
    "Generate a create_database function that creates the database"

    if engine_str == 'sqlite':

        import sqlite3
        dbi = sqlite3
        def create_database_sqlite(admin_username, admin_password, database_name, new_user, new_password, host = "localhost", port = None, db_dir = '.'):
            fname = os.path.join(db_dir, '%s.db' % database_name)
            if os.path.exists(fname):
                os.remove(fname)
            sqlite3.connect(database = fname).close()
        return create_database_sqlite

    elif engine_str == 'mysql':

        try:
            import MySQLdb
            dbi = MySQLdb
        except ImportError:
            try:
                import pymysql_sa
            except __HOLE__:
                raise Exception("Neither MySQLdb nor pymysql have been installed. First install them by running 'pip install pymysql' or 'pip install python-mysql'")
            pymysql_sa.make_default_mysql_dialect()
            import pymysql
            dbi = pymysql

        def create_database_mysql(error_message, admin_username, admin_password, database_name, new_user, new_password, host = "localhost", port = None, db_dir = '.'):
            args = {
                    'DATABASE_NAME' : database_name,
                    'USER'          : new_user,
                    'PASSWORD'      : new_password,
                }


            sentence1 = "DROP DATABASE IF EXISTS %(DATABASE_NAME)s;" % args
            sentence2 = "CREATE DATABASE %(DATABASE_NAME)s;" % args
            sentence3 = "GRANT ALL ON %(DATABASE_NAME)s.* TO '%(USER)s'@'%%' IDENTIFIED BY '%(PASSWORD)s';" % args
            sentence4 = "GRANT ALL ON %(DATABASE_NAME)s.* TO '%(USER)s'@'localhost' IDENTIFIED BY '%(PASSWORD)s';" % args
            sentence5 = "FLUSH PRIVILEGES;" % args

            try:
                kwargs = dict(db=database_name, user = admin_username, passwd = admin_password, host = host)
                if port is not None:
                    kwargs['port'] = port
                dbi.connect(**kwargs).close()
            except Exception, e:
                if e[1].startswith("Unknown database"):
                    sentence1 = "SELECT 1"

            for sentence in (sentence1, sentence2, sentence3, sentence4, sentence5):
                try:
                    kwargs = dict(user = admin_username, passwd = admin_password, host = host)
                    if port is not None:
                        kwargs['port'] = port
                    connection = dbi.connect(**kwargs)
                except dbi.OperationalError:
                    traceback.print_exc()
                    print("", file=sys.stderr)
                    print("    %s" % error_message, file=sys.stderr)
                    print("", file=sys.stderr)
                    sys.exit(-1)
                else:
                    cursor = connection.cursor()
                    cursor.execute(sentence)
                    connection.commit()
                    connection.close()
        return create_database_mysql

    else:
        return None
dataset/ETHPy150Open weblabdeusto/weblabdeusto/server/src/weblab/admin/deploy.py/generate_create_database