#! /usr/bin/python # -*- coding: iso-8859-1 -*- """ Convert a repository to mercurial (hg). This runs with baz 1.1.1 and also with tla 1.3-1 - if you do changes, maybe either try to avoid stuff requiring latest versions (it means much trouble for people trying to convert). TODO: * test reading tla multiline comments (had no testcase) * commit timestamps are UTC now, maybe use localtime? @license: BSD ??? @copyright: 2005 Sam Tardieu @copyright: 2005 Ollivier Robert @copyright: 2005 Thomas Waldmann (rewrite, optimize) @copyright: 2006 David MENTRE (full history, tested with tla 1.3) """ import os, sys, time # it works with either one: archcmd = "tla" #archcmd = "baz" def get_revisions(archive): """get revision list of archive""" os.system("%s get %s tmp-archive" % (archcmd, archive)) revlist = os.popen("cd tmp-archive && %s ancestry-graph --reverse" % archcmd, "r").readlines() os.system("rm -rf tmp-archive") version_list = [] for r in revlist: version_list.append(r.split('\t')[0]) return version_list def make_log(fullrev, mercurial_dir): author = date = "" ; summary = 0 for l in os.popen("%s cat-archive-log %s" % (archcmd, fullrev)): if summary == 0: if l.startswith("Creator: "): author = l[9:].strip() elif l.startswith("Standard-date: "): # this is UTC datestr = l[15:].strip() t = time.mktime(time.strptime(datestr, "%Y-%m-%d %H:%M:%S %Z")) date = "%d 0" % int(t) elif l.startswith("Summary: "): shortsummary = l[9:].strip() summary = 1 elif summary == 1: # skip one line summary = [shortsummary] else: # we have begun reading the summary, continues until eof... stripped = l.rstrip() if l and stripped != summary[0]: summary.append(stripped) summary.extend(["", "imported from: %s" % fullrev]) fd = open('tmp-msg', 'w') fd.write('\n'.join(summary) + '\n') fd.close() os.system("cd %s && hg commit --addremove -l ../tmp-msg --date '%s' --user '%s'" % (mercurial_dir, date, author)) def make_initial_revision(fullrev, mercurial_dir): """make initial hg repository""" try: sys.stdout.write(">>> '%s'\n" % fullrev) os.system("%s get %s %s" % (archcmd, fullrev, mercurial_dir)) hgi = open("%s/.hgignore" % mercurial_dir, "w") hgi.write("""\ .arch-ids/.* \{arch\}/.* """) hgi.close() os.system("cd %s && hg init" % mercurial_dir) make_log(fullrev, mercurial_dir) finally: os.system ("rm tmp-msg") def import_revision(archive, fullrev, mercurial_dir): """import a single arch revision into an hg repo""" try: sys.stdout.write(">>> '%s'\n" % fullrev) os.system("cd %s && %s replay %s" % (mercurial_dir, archcmd, fullrev)) make_log(fullrev, mercurial_dir) finally: os.system ("rm tmp-msg") try: cmd, archive, mercurial_dir = sys.argv[:3] except: sys.stdout.write("""\ Usage: arch-to-hg.py archive target_dir archive: the tla/baz archive you are converting from target_dir: where to put conversion results in hg format, must be some directory in the current directory, like "outputdir". """) version_list = get_revisions(archive) make_initial_revision(version_list[0], mercurial_dir) for fullrev in version_list[1:]: import_revision(archive, fullrev, mercurial_dir)