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