From: Eric S. Raymond Date: Thu, 11 Nov 2010 03:24:57 +0000 (-0500) Subject: Refactoring step. X-Git-Tag: 0.1~69 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=ea94feffb0d86b72028635416237b37ba07b98ab;p=rcs-fast-import.git Refactoring step. --- diff --git a/rcs-fast-import b/rcs-fast-import index 446caba..8127b6d 100755 --- a/rcs-fast-import +++ b/rcs-fast-import @@ -236,6 +236,10 @@ class Repository: self.import_line = 0 self.inline_count = 0 self.basedir = os.getcwd() + def __enter__(self): + return self + def __exit__(self, type, value, traceback): + self.cleanup() def cleanup(self): nuke(self.subdir(), "rcs-fast-import: cleaning up %s" % self.subdir()) def subdir(self, name=None): @@ -609,57 +613,54 @@ if __name__ == '__main__': elif (switch == '-V'): print "rcs-fast-import version %s" % version - repo = Repository() - repo.fast_import(sys.stdin) vcs = RCS() - try: - for event in repo.events: - if isinstance(event, Blob) or isinstance(event, Reset) or isinstance(event, Passthrough): - continue - elif isinstance(event, Tag): - if verbose: - announce("Tagging %s with %s" % (event.committish, event.name)) - vcs.tag(event) - elif isinstance(event, Commit): - committables = [] - for op in event.fileops: - if op.op == "M": - if verbose: - announce("%5s M %s" % (event.mark, op.path)) - committables.append((op.path, op.mode, op.copyname)) - elif op.op in ("D", "deleteall"): - # If we delete masters, we lose ability to - # retrieve old versions. - if do_deletes: + with Repository() as repo: + repo.fast_import(sys.stdin) + for event in repo.events: + if isinstance(event, Blob) or isinstance(event, Reset) or isinstance(event, Passthrough): + continue + elif isinstance(event, Tag): + if verbose: + announce("Tagging %s with %s" % (event.committish, event.name)) + vcs.tag(event) + elif isinstance(event, Commit): + committables = [] + for op in event.fileops: + if op.op == "M": + if verbose: + announce("%5s M %s" % (event.mark, op.path)) + committables.append((op.path, op.mode, op.copyname)) + elif op.op in ("D", "deleteall"): + # If we delete masters, we lose ability to + # retrieve old versions. + if do_deletes: + if verbose: + announce("%5s D %s" % (event.mark, op.path)) + if not dryrun: + vcs.remove(op.path) + elif verbose: + announce("%5s D %s ignored" % (event.mark, op.path)) + elif op.op == "R": if verbose: - announce("%5s D %s" % (event.mark, op.path)) + announce("%5s R '%s' '%s'" % (event.mark, op.source, op.target)) if not dryrun: - vcs.remove(op.path) - elif verbose: - announce("%5s D %s ignored" % (event.mark, op.path)) - elif op.op == "R": - if verbose: - announce("%5s R '%s' '%s'" % (event.mark, op.source, op.target)) - if not dryrun: - vcs.rename(op.source, op.target) - elif op.op == "C": - if verbose: - announce("%5s C '%s' '%s'" % (event.mark, op.source, op.target)) - if not dryrun: - vcs.copy(ops.source, op.target) - else: - raise FatalException("unknown op type %s" % op.op) - vcs.commit(event, committables) - vcs.checkout(checkout_mode) + vcs.rename(op.source, op.target) + elif op.op == "C": + if verbose: + announce("%5s C '%s' '%s'" % (event.mark, op.source, op.target)) + if not dryrun: + vcs.copy(ops.source, op.target) + else: + raise FatalException("unknown op type %s" % op.op) + vcs.commit(event, committables) + vcs.checkout(checkout_mode) raise SystemExit, 0 except FatalException, e: complain(e.msg) - repo.cleanup() raise SystemExit, 1 except OSError, e: complain(e) - repo.cleanup() raise SystemExit, 1 # end