Attachment 'arch-to-hg.py'

Download

   1 #! /usr/bin/python
   2 # -*- coding: iso-8859-1 -*-
   3 """
   4    Convert a repository to mercurial (hg).
   5    
   6    This runs with baz 1.1.1  and also with tla 1.3-1 - if you do changes,
   7    maybe either try to avoid stuff requiring latest versions (it means
   8    much trouble for people trying to convert).
   9 
  10    TODO:
  11     * test reading tla multiline comments (had no testcase)
  12     * commit timestamps are UTC now, maybe use localtime?
  13 
  14    @license: BSD ???
  15    @copyright: 2005 Sam Tardieu
  16    @copyright: 2005 Ollivier Robert
  17    @copyright: 2005 Thomas Waldmann (rewrite, optimize)
  18 """
  19 import os, sys, time
  20 
  21 # it works with either one:
  22 archcmd = "tla"
  23 #archcmd = "baz"
  24 
  25 def get_revisions(archive):
  26     """get revision list of archive"""
  27     os.system("%s get %s tmp-archive" % (archcmd, archive))
  28     revlist = os.popen("cd tmp-archive && %s revisions" % archcmd, "r").readlines()
  29     os.system("rm -rf tmp-archive")
  30     return revlist
  31 
  32 def import_revision(archive, rev, mercurial_dir):
  33     """import a single arch revision into an hg repo"""
  34     try:
  35         rev = rev.strip()
  36         fullrev = "%s--%s" % (archive, rev)
  37         sys.stdout.write(">>> '%s'\n" % fullrev)
  38         if rev == 'base-0':
  39             os.system("%s get %s %s" % (archcmd, fullrev, mercurial_dir))
  40             hgi = open("%s/.hgignore" % mercurial_dir, "w")
  41             hgi.write("""\
  42 syntax: glob
  43 .arch-ids/*
  44 {arch}
  45 """)
  46             hgi.close()
  47             os.system("cd %s && hg init" % mercurial_dir)
  48         else:
  49             os.system("cd %s && %s replay %s" % (mercurial_dir, archcmd, fullrev))
  50         
  51         author = date = "" ; summary = 0
  52         for l in os.popen("%s cat-archive-log %s" % (archcmd, fullrev)):
  53             if summary == 0:
  54                 if l.startswith("Creator: "):
  55                     author = l[9:].strip()
  56                 elif l.startswith("Standard-date: "): # this is UTC
  57                     datestr = l[15:].strip()
  58                     t = time.mktime(time.strptime(datestr, "%Y-%m-%d %H:%M:%S %Z"))
  59                     date = "%d 0" % int(t)
  60                 elif l.startswith("Summary: "):
  61                     shortsummary = l[9:].strip()
  62                     summary = 1
  63             elif summary == 1: # skip one line
  64                 summary = [shortsummary]
  65             else: # we have begun reading the summary, continues until eof...
  66                 stripped = l.rstrip()
  67                 if l and stripped != summary[0]:
  68                     summary.append(stripped)
  69         summary.extend(["", "imported from: %s" % fullrev])
  70         fd = open('tmp-msg', 'w')
  71         fd.write('\n'.join(summary) + '\n')
  72         fd.close()
  73         
  74         os.system("cd %s && hg commit --addremove -l ../tmp-msg --date '%s' --user '%s'" % (
  75             mercurial_dir, date, author))
  76     finally:
  77         os.system ("rm tmp-msg")
  78 
  79 try:
  80     cmd, archive, mercurial_dir = sys.argv[:3]
  81 except:
  82     sys.stdout.write("""\
  83 Usage: arch-to-hg.py archive target_dir
  84 
  85 archive: the tla/baz archive you are converting from
  86 target_dir: where to put conversion results in hg format, must be some
  87             directory in the current directory, like "outputdir".
  88 """)
  89 
  90 revlist = get_revisions(archive)
  91 
  92 for rev in revlist:
  93     import_revision(archive, rev, mercurial_dir)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-12-19 06:17:14, 3.2 KB) [[attachment:arch-to-hg.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.