<commit_before>import sys, os
sys.path.append( os.sep.join( ('..', 'src') ) )

import traceback
import getpass
import datetime
import subprocess
import time

import libraries
import MySQLdb
import weblab.database.Model as Model
import weblab.user_processing.coordinator.CoordinatorModel as CoordinatorModel

import voodoo.sessions.DbLockData as DbLockData
import voodoo.sessions.SessionSqlalchemyData as SessionSqlalchemyData

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

try:
    import weblab_administrator_credentials as wac
except ImportError:
    print >> sys.stderr, "Error: weblab_administrator_credentials.py not found. Did you execute create_weblab_administrator.py first?" 
    sys.exit(1)

try:
	from configuration import weblab_db_username, weblab_db_password, core_coordinator_db_username , core_coordinator_db_password, weblab_sessions_db_username, weblab_sessions_db_password
except ImportError, e:
	print >> sys.stderr, "Error: configuration.py doesn't exist or doesn't have all the required parameters: %s " % e
	sys.exit(2)


def create_database(admin_username, admin_password, database_name, new_user, new_password, host = "localhost"):
    args = {
            'DATABASE_NAME' : database_name,
            'USER'          : new_user,
            'PASSWORD'      : <PASSWORD>,
            'HOST'          : host
        }
    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@%(HOST)s IDENTIFIED BY '%(PASSWORD)s';" % args
    
    for sentence in (sentence1, sentence2, sentence3):
        try:
            connection = MySQLdb.connect(user = admin_username, passwd = <PASSWORD>)
        except MySQLdb.OperationalError, oe:
            traceback.print_exc()
            print >> sys.stderr, ""
            print >> sys.stderr, "    Tip: did you run create_weblab_administrator.py first?"
            print >> sys.stderr, ""
            sys.exit(-1)
        cursor = connection.cursor()
        cursor.execute(sentence)
        connection.commit()
        connection.close()



create_database(wac.wl_admin_username, wac.wl_admin_password, "WebLab",             weblab_db_username, weblab_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "<PASSWORD>",        weblab_db_username, weblab_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "<PASSWORD>", core_coordinator_db_username, core_coordinator_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "<PASSWORD>",     weblab_sessions_db_username, weblab_sessions_db_password)

print "Databases created."

def _insert_required_initial_data(engine):
    Session = sessionmaker(bind=engine)    
    session = Session()
    
    db = Model.DbAuthType("DB")
    session.add(db)
    ldap = Model.DbAuthType("LDAP")
    session.add(ldap)
    iptrusted = Model.DbAuthType("TRUSTED-IP-ADDRESSES")
    session.add(iptrusted)
    facebook = Model.DbAuthType("FACEBOOK")
    session.add(facebook)
    session.commit()

    experiment_allowed = Model.DbPermissionType(
            'experiment_allowed',
            'This type has a parameter which is the permanent ID (not a INT) of an Experiment. Users which have this permission will have access to the experiment defined in this parameter',
            user_applicable = True,
            group_applicable = True,
            ee_applicable = True
    )
    session.add(experiment_allowed)
    experiment_allowed_p1 = Model.DbPermissionTypeParameter(experiment_allowed, 'experiment_permanent_id', 'string', 'the unique name of the experiment')
    session.add(experiment_allowed_p1)
    experiment_allowed_p2 = Model.DbPermissionTypeParameter(experiment_allowed, 'experiment_category_id', 'string', 'the unique name of the category of experiment')
    session.add(experiment_allowed_p2)
    experiment_allowed_p3 = Model.DbPermissionTypeParameter(experiment_allowed, 'time_allowed', 'float', 'Time allowed (in seconds)')
    session.add(experiment_allowed_p3)
    session.commit()    
    
    admin_panel_access = Model.DbPermissionType(
            'admin_panel_access',
            'Users with this permission will be allowed to access the administration panel. The only parameter determines if the user has full_privileges to use the admin panel.',
            user_applicable = True,
            group_applicable = True,
            ee_applicable = True
    )
    session.add(admin_panel_access)
    admin_panel_access_p1 = Model.DbPermissionTypeParameter(admin_panel_access, 'full_privileges', 'bool', 'full privileges (True) or not (False)')
    session.add(admin_panel_access_p1)
    session.commit()

#####################################################################
# 
# Populating main database
# 

print "Populating 'WebLab' database...   \t\t", 

engine = create_engine('mysql://%s:%s@localhost/WebLab' % (weblab_db_username, weblab_db_password), echo = False)
metadata = Model.Base.metadata
metadata.drop_all(engine)
metadata.create_all(engine)

_insert_required_initial_data(engine)

print "[done]"

#####################################################################
# 
# Populating tests database
# 

print "Populating 'WebLabTests' database...   \t\t", 

engine = create_engine('mysql://%s:%s@localhost/WebLabTests' % (weblab_db_username, weblab_db_password), echo = False)
metadata = Model.Base.metadata
metadata.drop_all(engine)
metadata.create_all(engine)   

_insert_required_initial_data(engine)
 
Session = sessionmaker(bind=engine)    
session = Session()

db = session.query(Model.DbAuthType).filter_by(name="DB").one()
ldap = session.query(Model.DbAuthType).filter_by(name="LDAP").one()
iptrusted = session.query(Model.DbAuthType).filter_by(name="TRUSTED-IP-ADDRESSES").one()
facebook = session.query(Model.DbAuthType).filter_by(name="FACEBOOK").one()

experiment_allowed = session.query(Model.DbPermissionType).filter_by(name="experiment_allowed").one()
experiment_allowed_p1 = [ p for p in experiment_allowed.parameters if p.name == "experiment_permanent_id" ][0]
experiment_allowed_p2 = [ p for p in experiment_allowed.parameters if p.name == "experiment_category_id" ][0]
experiment_allowed_p3 = [ p for p in experiment_allowed.parameters if p.name == "time_allowed" ][0]

admin_panel_access = session.query(Model.DbPermissionType).filter_by(name="admin_panel_access").one()
admin_panel_access_p1 = [ p for p in admin_panel_access.parameters if p.name == "full_privileges" ][0]

# Auths
weblab_db = Model.DbAuth(db, "WebLab DB", 1)
session.add(weblab_db)

cdk_ldap = Model.DbAuth(ldap, "Configuration of CDK at Deusto", 2, "ldap_uri=ldaps://castor.cdk.deusto.es;domain=cdk.deusto.es;base=dc=cdk,dc=deusto,dc=es")
session.add(cdk_ldap)

deusto_ldap = Model.DbAuth(ldap, "Configuration of DEUSTO at Deusto", 3, "ldap_uri=ldaps://altair.cdk.deusto.es;domain=deusto.es;base=dc=deusto,dc=es")
session.add(deusto_ldap)

localhost_ip = Model.DbAuth(iptrusted, "trusting in localhost", 4, "127.0.0.1")
session.add(localhost_ip)

auth_facebook = Model.DbAuth(facebook, "Facebook", 5)
session.add(auth_facebook)

# Roles
administrator = Model.DbRole("administrator")
session.add(administrator)

professor = Model.DbRole("professor")
session.add(professor)

student = Model.DbRole("student")<commit_msg>[DB]: Added OpenID auth and authtype.<commit_after>import sys, os
sys.path.append( os.sep.join( ('..', 'src') ) )

import traceback
import getpass
import datetime
import subprocess
import time

import libraries
import MySQLdb
import weblab.database.Model as Model
import weblab.user_processing.coordinator.CoordinatorModel as CoordinatorModel

import voodoo.sessions.DbLockData as DbLockData
import voodoo.sessions.SessionSqlalchemyData as SessionSqlalchemyData

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

try:
    import weblab_administrator_credentials as wac
except ImportError:
    print >> sys.stderr, "Error: weblab_administrator_credentials.py not found. Did you execute create_weblab_administrator.py first?" 
    sys.exit(1)

try:
	from configuration import weblab_db_username, weblab_db_password, core_coordinator_db_username , core_coordinator_db_password, weblab_sessions_db_username, weblab_sessions_db_password
except ImportError, e:
	print >> sys.stderr, "Error: configuration.py doesn't exist or doesn't have all the required parameters: %s " % e
	sys.exit(2)


def create_database(admin_username, admin_password, database_name, new_user, new_password, host = "localhost"):
    args = {
            'DATABASE_NAME' : database_name,
            'USER'          : new_user,
            'PASSWORD'      : <PASSWORD>,
            'HOST'          : host
        }
    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@%(HOST)s IDENTIFIED BY '%(PASSWORD)s';" % args
    
    for sentence in (sentence1, sentence2, sentence3):
        try:
            connection = MySQLdb.connect(user = admin_username, passwd = <PASSWORD>)
        except MySQLdb.OperationalError, oe:
            traceback.print_exc()
            print >> sys.stderr, ""
            print >> sys.stderr, "    Tip: did you run create_weblab_administrator.py first?"
            print >> sys.stderr, ""
            sys.exit(-1)
        cursor = connection.cursor()
        cursor.execute(sentence)
        connection.commit()
        connection.close()



create_database(wac.wl_admin_username, wac.wl_admin_password, "WebLab",             weblab_db_username, weblab_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "<PASSWORD>",        weblab_db_username, weblab_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "WebLabCoordination", core_coordinator_db_username, core_coordinator_db_password)
create_database(wac.wl_admin_username, wac.wl_admin_password, "<PASSWORD>",     weblab_sessions_db_username, weblab_sessions_db_password)

print "Databases created."

def _insert_required_initial_data(engine):
    Session = sessionmaker(bind=engine)    
    session = Session()
    
    db = Model.DbAuthType("DB")
    session.add(db)
    ldap = Model.DbAuthType("LDAP")
    session.add(ldap)
    iptrusted = Model.DbAuthType("TRUSTED-IP-ADDRESSES")
    session.add(iptrusted)
    facebook = Model.DbAuthType("FACEBOOK")
    session.add(facebook)
    openid = Model.DbAuthType("OPENID")
    session.add(openid)
    session.commit()

    experiment_allowed = Model.DbPermissionType(
            'experiment_allowed',
            'This type has a parameter which is the permanent ID (not a INT) of an Experiment. Users which have this permission will have access to the experiment defined in this parameter',
            user_applicable = True,
            group_applicable = True,
            ee_applicable = True
    )
    session.add(experiment_allowed)
    experiment_allowed_p1 = Model.DbPermissionTypeParameter(experiment_allowed, 'experiment_permanent_id', 'string', 'the unique name of the experiment')
    session.add(experiment_allowed_p1)
    experiment_allowed_p2 = Model.DbPermissionTypeParameter(experiment_allowed, 'experiment_category_id', 'string', 'the unique name of the category of experiment')
    session.add(experiment_allowed_p2)
    experiment_allowed_p3 = Model.DbPermissionTypeParameter(experiment_allowed, 'time_allowed', 'float', 'Time allowed (in seconds)')
    session.add(experiment_allowed_p3)
    session.commit()    
    
    admin_panel_access = Model.DbPermissionType(
            'admin_panel_access',
            'Users with this permission will be allowed to access the administration panel. The only parameter determines if the user has full_privileges to use the admin panel.',
            user_applicable = True,
            group_applicable = True,
            ee_applicable = True
    )
    session.add(admin_panel_access)
    admin_panel_access_p1 = Model.DbPermissionTypeParameter(admin_panel_access, 'full_privileges', 'bool', 'full privileges (True) or not (False)')
    session.add(admin_panel_access_p1)
    session.commit()

#####################################################################
# 
# Populating main database
# 

print "Populating 'WebLab' database...   \t\t", 

engine = create_engine('mysql://%s:%s@localhost/WebLab' % (weblab_db_username, weblab_db_password), echo = False)
metadata = Model.Base.metadata
metadata.drop_all(engine)
metadata.create_all(engine)

_insert_required_initial_data(engine)

print "[done]"

#####################################################################
# 
# Populating tests database
# 

print "Populating 'WebLabTests' database...   \t\t", 

engine = create_engine('mysql://%s:%s@localhost/WebLabTests' % (weblab_db_username, weblab_db_password), echo = False)
metadata = Model.Base.metadata
metadata.drop_all(engine)
metadata.create_all(engine)   

_insert_required_initial_data(engine)
 
Session = sessionmaker(bind=engine)    
session = Session()

db = session.query(Model.DbAuthType).filter_by(name="DB").one()
ldap = session.query(Model.DbAuthType).filter_by(name="LDAP").one()
iptrusted = session.query(Model.DbAuthType).filter_by(name="TRUSTED-IP-ADDRESSES").one()
facebook = session.query(Model.DbAuthType).filter_by(name="FACEBOOK").one()
openid = session.query(Model.DbAuthType).filter_by(name="OPENID").one()

experiment_allowed = session.query(Model.DbPermissionType).filter_by(name="experiment_allowed").one()
experiment_allowed_p1 = [ p for p in experiment_allowed.parameters if p.name == "experiment_permanent_id" ][0]
experiment_allowed_p2 = [ p for p in experiment_allowed.parameters if p.name == "experiment_category_id" ][0]
experiment_allowed_p3 = [ p for p in experiment_allowed.parameters if p.name == "time_allowed" ][0]

admin_panel_access = session.query(Model.DbPermissionType).filter_by(name="admin_panel_access").one()
admin_panel_access_p1 = [ p for p in admin_panel_access.parameters if p.name == "full_privileges" ][0]

# Auths
weblab_db = Model.DbAuth(db, "WebLab DB", 1)
session.add(weblab_db)

cdk_ldap = Model.DbAuth(ldap, "Configuration of CDK at Deusto", 2, "ldap_uri=ldaps://castor.cdk.deusto.es;domain=cdk.deusto.es;base=dc=cdk,dc=deusto,dc=es")
session.add(cdk_ldap)

deusto_ldap = Model.DbAuth(ldap, "Configuration of DEUSTO at Deusto", 3, "ldap_uri=ldaps://altair.cdk.deusto.es;domain=deusto.es;base=dc=deusto,dc=es")
session.add(deusto_ldap)

localhost_ip = Model.DbAuth(iptrusted, "trusting in localhost", 4, "127.0.0.1")
session.add(localhost_ip)

auth_facebook = Model.DbAuth(facebook, "Facebook", 5)
session.add(auth_facebook)

auth_openid = Model.DbAuth(openid, "OpenID", 6)
session.add(auth_openid)

# Roles
administrator = Model.DbRole("administrator")
session.add(administrator)

professor = Model.DbRole("professor")
session.add(professor)

student = Model.DbRole("student")
{'fraction_non_alphanumeric': 0.12331689108118918, 'fraction_numerical': 0.004866017864284762, 'mean_word_length': 4.680802726240061, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020261', 'n_tokens_mistral': 4873, 'n_tokens_neox': 4728, 'n_words': 966}