contrib/darcs2hg.py
author Vadim Gelfer <vadim.gelfer@gmail.com>
Sat, 27 May 2006 20:45:22 -0700
changeset 2351 075d2ddc4639
parent 2349 88c881bda888
child 2352 61909dfb316d
permissions -rwxr-xr-x
contrib: move hg-menu.vim into contrib/vim directory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     1
#!/usr/bin/env python
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     2
# Encoding: iso-8859-1
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     3
# vim: tw=80 ts=4 sw=4 noet
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     4
# -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     5
# Project   : Basic Darcs to Mercurial conversion script
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     6
# -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     7
# Author    : Sebastien Pierre <sebastien@xprima.com>
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     8
# Creation  : 24-May-2006
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     9
# Last mod  : 26-May-2006
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    10
# History   :
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    11
#             26-May-2006 - Updated
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    12
#             24-May-2006 - First implementation
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    13
# -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    14
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    15
import os, sys
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    16
import xml.dom.minidom as xml_dom
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    17
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    18
DARCS_REPO = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    19
HG_REPO    = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    20
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    21
USAGE = """\
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    22
%s DARCSREPO HGREPO
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    23
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    24
    Converts the given Darcs repository to a new Mercurial repository. The given
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    25
    HGREPO must not exist, as it will be created and filled up (this will avoid
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    26
    overwriting valuable data.
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    27
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    28
""" % (os.path.basename(__file__))
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    29
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    30
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    31
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    32
# Utilities
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    33
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    34
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    35
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    36
def cmd(text, path=None):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    37
	"""Executes a command, in the given directory (if any), and returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    38
	command result as a string."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    39
	cwd = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    40
	if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    41
		path = os.path.abspath(path)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    42
		cwd  = os.getcwd()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    43
		os.chdir(path)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    44
	print text
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    45
	res = os.popen(text).read()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    46
	if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    47
		os.chdir(cwd)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    48
	return res
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    49
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    50
def writefile(path, data):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    51
	"""Writes the given data into the given file."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    52
	f = file(path, "w") ; f.write(data)  ; f.close()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    53
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    54
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    55
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    56
# Darcs interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    57
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    58
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    59
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    60
def darcs_changes(darcsRepo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    61
	"""Gets the changes list from the given darcs repository. This returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    62
	chronological list of changes as (change name, change summary)."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    63
	changes    = cmd("darcs changes --reverse --xml-output", darcsRepo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    64
	doc        = xml_dom.parseString(changes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    65
	res        = []
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    66
	for patch_node in doc.childNodes[0].childNodes:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    67
		name = filter(lambda n:n.nodeName == "name", patch_node.childNodes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    68
		comm = filter(lambda n:n.nodeName == "comment", patch_node.childNodes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    69
		if not name:continue
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    70
		else: name = name[0].childNodes[0].data
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    71
		if not comm: comm = ""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    72
		else: comm = comm[0].childNodes[0].data
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    73
		res.append([name, comm])
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    74
	return res
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    75
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    76
def darcs_pull(hg_repo, darcs_repo, change):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    77
	cmd("darcs pull '%s' --all --patches='%s'" % (darcs_repo, change), hg_repo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    78
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    79
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    80
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    81
# Mercurial interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    82
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    83
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    84
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    85
def hg_commit( hg_repo, text ):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    86
	writefile("/tmp/msg", text)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    87
	cmd("hg add -X _darcs *", hg_repo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    88
	cmd("hg commit -l /tmp/msg", hg_repo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    89
	os.unlink("/tmp/msg")
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    90
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    91
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    92
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    93
# Main
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    94
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    95
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    96
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    97
if __name__ == "__main__":
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    98
	args = sys.argv[1:]
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    99
	# We parse the arguments
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   100
	if len(args)   == 2:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   101
		darcs_repo = os.path.abspath(args[0])
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   102
		hg_repo    = os.path.abspath(args[1])
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   103
	else:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   104
		print USAGE
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   105
		sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   106
	# Initializes the target repo
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   107
	if not os.path.isdir(darcs_repo + "/_darcs"):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   108
		print "No darcs directory found at: " + darc_repo
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   109
		sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   110
	if not os.path.isdir(hg_repo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   111
		os.mkdir(hg_repo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   112
	else:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   113
		print "Given HG repository must not exist. It will be created"
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   114
		sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   115
	cmd("hg init '%s'" % (hg_repo))
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   116
	cmd("darcs initialize", hg_repo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   117
	# Get the changes from the Darcs repository
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   118
	for summary, description in darcs_changes(darcs_repo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   119
		text = summary + "\n" + description
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   120
		darcs_pull(hg_repo, darcs_repo, summary)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   121
		hg_commit(hg_repo, text)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   122
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   123
# EOF
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   124