def rsr_session(self, prog, volume, workspace, homedir, opts): """ Determine context, and from there get the session/dbref to initialize an SQLAlchemy session. The context depends on the current working directory, and defaults to the nearest workspace; perhaps a volume or the homedir. """ session = Session.init(prog.pwd, opts.session) log.note('Session: %s', session) if session.context and confparse.haspath(session.context, 'data.repository.root_dir'): prog.session = session yield dict(context=session.context) log.note('Context: %s', session.context) repo_root = session.context.settings.data.repository.root_dir else: repo_root = 'sa_migrate' # SA session repo_path = os.path.join(repo_root, opts.repo) if os.path.exists(repo_path): log.info("Reading SA migrate config for repo %r" % repo_path) # hard coded module name, root dir for local repos from sa_migrate import custom config = custom.read(repo_path) log.info("Reading SA migrate config from %r" % config) repo_opts = custom.migrate_opts(repo_path, config) dbref = repo_opts['url'] else: dbref = opts.dbref log.note('DBRef: %s', dbref) if opts.init_db: log.debug("Initializing SQLAlchemy session for %s", dbref) sa = SessionMixin.get_session(opts.session, dbref, opts.init_db, SqlBase=SqlBase) yield dict(sa=sa)
def txs_session(prog=None, sa=None, opts=None, settings=None): # default SA session dbref = opts.dbref if opts.init: log.debug("Initializing SQLAlchemy session for %s", dbref) sa = SessionMixin.get_session('default', opts.dbref, opts.init) # Host hostnamestr = current_hostname(opts.init, opts.interactive) if opts.init: hostname = hostname_find([hostnamestr], sa) assert not hostname or not isinstance(hostname, (tuple, list)), hostname if not hostname: log.note("New Name: %s", hostnamestr) hostname = Name( name=hostnamestr, date_added=datetime.now()) hostname.commit() else: log.warn("Name exists: %s", hostname) assert hostname host = host_find([hostname], sa) if not host: log.note("New Host: %s", hostnamestr) host = Host( hostname=hostname, date_added=datetime.now()) host.commit() else: log.warn("Host exists: %s", host) assert host else: host, name = sa.query(Host, Name)\ .join('hostname')\ .filter(Name.name == hostnamestr).one() if not host: log.crit("Could not get host") urlresolver = LocalPathResolver(host, sa) log.info("On %s", host) yield Keywords(sa=sa, ur=urlresolver)