Ejemplo n.º 1
0
def create_temporary_instance():
    """Create a sqlite based test instance in a temporary directory"""
    dbname = 'sqlite://database.db'
    instance_folder = mkdtemp(prefix='pyclanspheretest')

    # create database and all tables
    from pyClanSphere.database import db, init_database
    e = db.create_engine(dbname, instance_folder)
    from pyClanSphere.schema import users, user_privileges, privileges
    init_database(e)

    # create admin account
    from pyClanSphere.privileges import CLAN_ADMIN
    user_id = e.execute(users.insert(),
        username=u'TestAdmin',
        pw_hash=gen_pwhash('TestPassWord'),
        email=u'*****@*****.**',
        real_name=u'',
        description=u'',
        extra={},
        display_name='$username'
    ).inserted_primary_key[0]

    # insert a privilege for the user
    privilege_id = e.execute(privileges.insert(),
        name=CLAN_ADMIN.name
    ).inserted_primary_key[0]
    e.execute(user_privileges.insert(),
        user_id=user_id,
        privilege_id=privilege_id
    )

    # set up the initial config
    from pyClanSphere.config import Configuration
    config_filename = join(instance_folder, 'pyClanSphere.ini')
    cfg = Configuration(config_filename)
    t = cfg.edit()
    t.update(
        maintenance_mode=False,
        site_url='http://localtest',
        secret_key=gen_secret_key(),
        database_uri=dbname,
        iid=new_iid()
    )
    t.commit()
    
    from pyClanSphere import setup
    from pyClanSphere.upgrades.webapp import WebUpgrades
    instance = setup(instance_folder)
    
    if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
        # Fast Migration
        from pyClanSphere.upgrades import ManageDatabase
        manage = ManageDatabase(instance)
        upgrade = manage.cmd_upgrade()
        while True:
            try:
                upgrade.next()
            except StopIteration:
                break
        from pyClanSphere._core import _unload_pyClanSphere
        _unload_pyClanSphere()

        instance = setup(instance_folder)
        if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
            sys.stderr.write('Automatic db migration failed, check your scripts!\n')
            sys.exit(1)
    return instance, instance_folder
Ejemplo n.º 2
0
            privilege_id = e.execute(privileges.insert(),
                name=CLAN_ADMIN.name
            ).inserted_primary_key[0]
            e.execute(user_privileges.insert(),
                user_id=user_id,
                privilege_id=privilege_id
            )

            # set up the initial config
            config_filename = path.join(self.instance_folder, 'pyClanSphere.ini')
            cfg = Configuration(config_filename)
            t = cfg.edit()
            t.update(
                maintenance_mode=environment.MODE != 'development',
                site_url=request.url_root,
                secret_key=gen_secret_key(),
                database_uri=database_uri,
                language=request.translations.language,
                iid=new_iid(),
                # load one plugin by default for a better theme
                plugins='vessel_theme',
                theme='vessel'
            )
            cfg._comments['[pyClanSphere]'] = CONFIG_HEADER
            try:
                t.commit()
            except ConfigurationTransactionError:
                _ = request.translations.gettext
                error = _('The configuration file (%s) could not be opened '
                          'for writing. Please adjust your permissions and '
                          'try again.') % config_filename