##// END OF EJS Templates
darcs2hg: improved logging
Sébastien Pierre -
r2586:bb63d29c default
parent child Browse files
Show More
@@ -1,131 +1,134 b''
1 1 #!/usr/bin/env python
2 2 # Encoding: iso-8859-1
3 3 # vim: tw=80 ts=4 sw=4 noet
4 4 # -----------------------------------------------------------------------------
5 5 # Project : Basic Darcs to Mercurial conversion script
6 6 # -----------------------------------------------------------------------------
7 # Author : Sebastien Pierre <sebastien@xprima.com>
7 # Authors : Sebastien Pierre <sebastien@xprima.com>
8 # TK Soh <teekaysoh@gmail.com>
9 # -----------------------------------------------------------------------------
8 10 # Creation : 24-May-2006
9 # Last mod : 26-May-2006
10 # History :
11 # 26-May-2006 - Updated
12 # 24-May-2006 - First implementation
11 # Last mod : 01-Jun-2006
13 12 # -----------------------------------------------------------------------------
14 13
15 14 import os, sys
16 15 import tempfile
17 16 import xml.dom.minidom as xml_dom
18 17 from time import strptime, mktime
19 18
20 19 DARCS_REPO = None
21 20 HG_REPO = None
22 21
23 22 USAGE = """\
24 23 %s DARCSREPO HGREPO
25 24
26 25 Converts the given Darcs repository to a new Mercurial repository. The given
27 26 HGREPO must not exist, as it will be created and filled up (this will avoid
28 27 overwriting valuable data.
29 28
30 29 """ % (os.path.basename(sys.argv[0]))
31 30
32 31 # ------------------------------------------------------------------------------
33 32 #
34 33 # Utilities
35 34 #
36 35 # ------------------------------------------------------------------------------
37 36
38 def cmd(text, path=None):
37 def cmd(text, path=None, silent=False):
39 38 """Executes a command, in the given directory (if any), and returns the
40 39 command result as a string."""
41 40 cwd = None
42 41 if path:
43 42 path = os.path.abspath(path)
44 43 cwd = os.getcwd()
45 44 os.chdir(path)
46 print text
45 if not silent: print "> ", text
47 46 res = os.popen(text).read()
48 47 if path:
49 48 os.chdir(cwd)
50 49 return res
51 50
52 51 def writefile(path, data):
53 52 """Writes the given data into the given file."""
54 53 f = file(path, "w") ; f.write(data) ; f.close()
55 54
55 def error( *args ):
56 sys.stderr.write("ERROR:")
57 for a in args: sys.stderr.write(str(a))
58 sys.stderr.write("\n")
59 sys.exit(-1)
60
56 61 # ------------------------------------------------------------------------------
57 62 #
58 63 # Darcs interface
59 64 #
60 65 # ------------------------------------------------------------------------------
61 66
62 67 def darcs_changes(darcsRepo):
63 68 """Gets the changes list from the given darcs repository. This returns the
64 69 chronological list of changes as (change name, change summary)."""
65 70 changes = cmd("darcs changes --reverse --xml-output", darcsRepo)
66 71 doc = xml_dom.parseString(changes)
67 res = []
68 72 for patch_node in doc.childNodes[0].childNodes:
69 73 name = filter(lambda n:n.nodeName == "name", patch_node.childNodes)
70 74 comm = filter(lambda n:n.nodeName == "comment", patch_node.childNodes)
71 75 if not name:continue
72 76 else: name = name[0].childNodes[0].data
73 77 if not comm: comm = ""
74 78 else: comm = comm[0].childNodes[0].data
75 79 author = patch_node.getAttribute("author")
76 80 date = patch_node.getAttribute("date")
77 81 hash = patch_node.getAttribute("hash")
78 82 yield hash, author, date, name, comm
79 83
80 84 def darcs_pull(hg_repo, darcs_repo, change):
81 85 cmd("darcs pull '%s' --all --patches='%s'" % (darcs_repo, change), hg_repo)
82 86
83 87 # ------------------------------------------------------------------------------
84 88 #
85 89 # Mercurial interface
86 90 #
87 91 # ------------------------------------------------------------------------------
88 92
89 93 def hg_commit( hg_repo, text, author, date ):
90 94 fd, tmpfile = tempfile.mkstemp(prefix="darcs2hg_")
91 95 writefile(tmpfile, text)
92 96 cmd("hg add -X _darcs", hg_repo)
93 97 cmd("hg remove -X _darcs --after", hg_repo)
94 98 cmd("hg commit -l %s -u '%s' -d '%s 0'" % (tmpfile, author, date), hg_repo)
95 99 os.unlink(tmpfile)
96 100
97 101 # ------------------------------------------------------------------------------
98 102 #
99 103 # Main
100 104 #
101 105 # ------------------------------------------------------------------------------
102 106
103 107 if __name__ == "__main__":
104 108 args = sys.argv[1:]
105 109 # We parse the arguments
106 110 if len(args) == 2:
107 111 darcs_repo = os.path.abspath(args[0])
108 112 hg_repo = os.path.abspath(args[1])
109 113 else:
110 114 print USAGE
111 115 sys.exit(-1)
112 116 # Initializes the target repo
113 117 if not os.path.isdir(darcs_repo + "/_darcs"):
114 print "No darcs directory found at: " + darc_repo
118 print "No darcs directory found at: " + darcs_repo
115 119 sys.exit(-1)
116 120 if not os.path.isdir(hg_repo):
117 121 os.mkdir(hg_repo)
118 122 else:
119 123 print "Given HG repository must not exist. It will be created"
120 124 sys.exit(-1)
121 125 cmd("hg init '%s'" % (hg_repo))
122 126 cmd("darcs initialize", hg_repo)
123 127 # Get the changes from the Darcs repository
124 128 for hash, author, date, summary, description in darcs_changes(darcs_repo):
125 129 text = summary + "\n" + description
126 130 darcs_pull(hg_repo, darcs_repo, hash)
127 131 epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
128 132 hg_commit(hg_repo, text, author, epoch)
129 133
130 134 # EOF
131
General Comments 0
You need to be logged in to leave comments. Login now