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