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