##// END OF EJS Templates
darcs2hg: improved logging
darcs2hg: improved logging

File last commit:

r2586:bb63d29c default
r2586:bb63d29c default
Show More
darcs2hg.py
134 lines | 4.4 KiB | text/x-python | PythonLexer
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 #!/usr/bin/env python
# Encoding: iso-8859-1
# vim: tw=80 ts=4 sw=4 noet
# -----------------------------------------------------------------------------
# Project : Basic Darcs to Mercurial conversion script
# -----------------------------------------------------------------------------
Sébastien Pierre
darcs2hg: improved logging
r2586 # Authors : Sebastien Pierre <sebastien@xprima.com>
# TK Soh <teekaysoh@gmail.com>
# -----------------------------------------------------------------------------
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 # Creation : 24-May-2006
Sébastien Pierre
darcs2hg: improved logging
r2586 # Last mod : 01-Jun-2006
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 # -----------------------------------------------------------------------------
import os, sys
TK Soh
various fixes to darcs conversion script...
r2352 import tempfile
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 import xml.dom.minidom as xml_dom
TK Soh
various fixes to darcs conversion script...
r2352 from time import strptime, mktime
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349
DARCS_REPO = None
HG_REPO = None
USAGE = """\
%s DARCSREPO HGREPO
Converts the given Darcs repository to a new Mercurial repository. The given
HGREPO must not exist, as it will be created and filled up (this will avoid
overwriting valuable data.
TK Soh
various fixes to darcs conversion script...
r2352 """ % (os.path.basename(sys.argv[0]))
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349
# ------------------------------------------------------------------------------
#
# Utilities
#
# ------------------------------------------------------------------------------
Sébastien Pierre
darcs2hg: improved logging
r2586 def cmd(text, path=None, silent=False):
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 """Executes a command, in the given directory (if any), and returns the
command result as a string."""
cwd = None
if path:
path = os.path.abspath(path)
cwd = os.getcwd()
os.chdir(path)
Sébastien Pierre
darcs2hg: improved logging
r2586 if not silent: print "> ", text
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 res = os.popen(text).read()
if path:
os.chdir(cwd)
return res
def writefile(path, data):
"""Writes the given data into the given file."""
f = file(path, "w") ; f.write(data) ; f.close()
Sébastien Pierre
darcs2hg: improved logging
r2586 def error( *args ):
sys.stderr.write("ERROR:")
for a in args: sys.stderr.write(str(a))
sys.stderr.write("\n")
sys.exit(-1)
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 # ------------------------------------------------------------------------------
#
# Darcs interface
#
# ------------------------------------------------------------------------------
def darcs_changes(darcsRepo):
"""Gets the changes list from the given darcs repository. This returns the
chronological list of changes as (change name, change summary)."""
changes = cmd("darcs changes --reverse --xml-output", darcsRepo)
doc = xml_dom.parseString(changes)
for patch_node in doc.childNodes[0].childNodes:
name = filter(lambda n:n.nodeName == "name", patch_node.childNodes)
comm = filter(lambda n:n.nodeName == "comment", patch_node.childNodes)
if not name:continue
else: name = name[0].childNodes[0].data
if not comm: comm = ""
else: comm = comm[0].childNodes[0].data
TK Soh
various fixes to darcs conversion script...
r2352 author = patch_node.getAttribute("author")
date = patch_node.getAttribute("date")
Nils Decker
darcs2hg.py: use darcs patch hash as patch identifier...
r2585 hash = patch_node.getAttribute("hash")
yield hash, author, date, name, comm
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349
def darcs_pull(hg_repo, darcs_repo, change):
cmd("darcs pull '%s' --all --patches='%s'" % (darcs_repo, change), hg_repo)
# ------------------------------------------------------------------------------
#
# Mercurial interface
#
# ------------------------------------------------------------------------------
TK Soh
various fixes to darcs conversion script...
r2352 def hg_commit( hg_repo, text, author, date ):
fd, tmpfile = tempfile.mkstemp(prefix="darcs2hg_")
writefile(tmpfile, text)
cmd("hg add -X _darcs", hg_repo)
cmd("hg remove -X _darcs --after", hg_repo)
cmd("hg commit -l %s -u '%s' -d '%s 0'" % (tmpfile, author, date), hg_repo)
os.unlink(tmpfile)
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349
# ------------------------------------------------------------------------------
#
# Main
#
# ------------------------------------------------------------------------------
if __name__ == "__main__":
args = sys.argv[1:]
# We parse the arguments
if len(args) == 2:
darcs_repo = os.path.abspath(args[0])
hg_repo = os.path.abspath(args[1])
else:
print USAGE
sys.exit(-1)
# Initializes the target repo
if not os.path.isdir(darcs_repo + "/_darcs"):
Sébastien Pierre
darcs2hg: improved logging
r2586 print "No darcs directory found at: " + darcs_repo
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 sys.exit(-1)
if not os.path.isdir(hg_repo):
os.mkdir(hg_repo)
else:
print "Given HG repository must not exist. It will be created"
sys.exit(-1)
cmd("hg init '%s'" % (hg_repo))
cmd("darcs initialize", hg_repo)
# Get the changes from the Darcs repository
Nils Decker
darcs2hg.py: use darcs patch hash as patch identifier...
r2585 for hash, author, date, summary, description in darcs_changes(darcs_repo):
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349 text = summary + "\n" + description
Nils Decker
darcs2hg.py: use darcs patch hash as patch identifier...
r2585 darcs_pull(hg_repo, darcs_repo, hash)
TK Soh
various fixes to darcs conversion script...
r2352 epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
hg_commit(hg_repo, text, author, epoch)
Sébastien Pierre
darcs2hg.py: import darcs project into mercurial...
r2349
# EOF