Skip to content

Instantly share code, notes, and snippets.

@idot
Created August 16, 2009 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idot/168636 to your computer and use it in GitHub Desktop.
Save idot/168636 to your computer and use it in GitHub Desktop.
import os, sys, cPickle, ConfigParser, logging, traceback
assert sys.version_info[:2] >= ( 2, 4 )
new_path = [ os.path.join( os.getcwd(), "lib" ) ]
new_path.extend( sys.path[1:] ) # remove scripts/ from the path
new_path.append( os.path.join( os.getcwd(), "importer" )) #add own importer package
new_path.append("/usr/lib64/python2.5/site-packages") #add our locally installed packages
sys.path = new_path
from galaxy import eggs
import pkg_resources
import galaxy.model.mapping
import galaxy.util
pkg_resources.require( "SQLAlchemy >= 0.4" )
import sqlalchemy as sa
from sqlalchemy.orm.session import Session
from galaxy.model.orm import and_, eagerload
import sha
assert sys.version_info[:2] >= ( 2, 4 )
log = logging.getLogger(__name__)
def __main__():
app = parseConfig()
username = "name" + "1"
session = app.model.session
transaction = session.begin()
try:
createUser(app, username, "123456")
raise Exception('test for rollback', 'test for rollback')
transaction.commit()
except Exception:
transaction.rollback()
print("error: rollback")
user = app.model.User.filter_by(email=username).first()
if not user:
print "user not created"
else:
print "user created: " + user.email + " " + " ".join(map(lambda r: r.name, user.all_roles()))
def createUser(app, username, password):
"""creates a user and his private role ("r" + username) and associates them in the database"""
password = sha.new( password ).hexdigest()
user = app.model.User(username, password)
user.flush()
role = app.model.Role( name="r" + username, description="private role of " + username, type=app.model.Role.types.PRIVATE )
role.flush()
userRoleAssociation = app.model.UserRoleAssociation(user, role)
userRoleAssociation.flush()
class ImportData(object):
"""Encapsulates the state of a Universe application"""
def __init__( self, database_connection=None, file_path=None ):
if database_connection is None:
raise Exception( "ImportData requires a database_connection value" )
if file_path is None:
raise Exception( "ImportData requires a file_path value" )
self.database_connection = database_connection
self.file_path = file_path
# Setup the database engine and ORM
self.model = galaxy.model.mapping.init( self.file_path, self.database_connection, engine_options={}, create_tables=False )
def parseConfig():
"""Parses the config and inits the Application object"""
conf_parser = ConfigParser.ConfigParser( {'here':os.getcwd()} )
ini_file = sys.argv[1]
conf_parser.read( ini_file )
configuration = {}
for key, value in conf_parser.items( "app:main" ):
configuration[key] = value
if 'database_connection' in configuration:
database_connection = configuration['database_connection']
else:
database_connection = "sqlite:///%s?isolation_level=Immediate" % configuration["database_file"]
file_path = configuration['file_path']
app = ImportData( database_connection=database_connection, file_path=file_path )
return app
if __name__ == "__main__":
__main__()
#!/bin/sh
cd `dirname $0`
python -ES ./importer/importer.py universe_wsgi.ini $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment