def _migrate(url, repository, version, upgrade, err, **opts): engine = opts.pop('engine') url = str(engine.url) schema = ControlledSchema(engine, repository) version = _migrate_version(schema, version, upgrade, err) changeset = schema.changeset(version) for ver, change in changeset: nextver = ver + changeset.step log.info('%s -> %s... ', ver, nextver) if opts.get('preview_sql'): if isinstance(change, PythonScript): log.info(change.preview_sql(url, changeset.step, **opts)) elif isinstance(change, SqlScript): log.info(change.source()) elif opts.get('preview_py'): if not isinstance(change, PythonScript): raise exceptions.UsageError( "Python source can be only displayed" " for python migration files") source_ver = max(ver, nextver) module = schema.repository.version(source_ver).script().module funcname = upgrade and "upgrade" or "downgrade" func = getattr(module, funcname) log.info(inspect.getsource(func)) else: schema.runchange(ver, change, changeset.step) log.info('done')
def help(cmd=None, **opts): """%prog help COMMAND Displays help on a given command. """ if cmd is None: raise exceptions.UsageError(None) try: func = globals()[cmd] except: raise exceptions.UsageError( "'%s' isn't a valid command. Try 'help COMMAND'" % cmd) ret = func.__doc__ if sys.argv[0]: ret = ret.replace('%prog', sys.argv[0]) return ret
def source(version, dest=None, repository=None, **opts): """%prog source VERSION [DESTINATION] --repository=REPOSITORY_PATH Display the Python code for a particular version in this repository. Save it to the file at DESTINATION or, if omitted, send to stdout. """ if repository is None: raise exceptions.UsageError("A repository must be specified") repo = Repository(repository) ret = repo.version(version).script().source() if dest is not None: dest = open(dest, 'w') dest.write(ret) dest.close() ret = None return ret