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