1 #!/usr/bin/python
2 #
3 # p4-fast-export.py
4 #
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
7 #
8 # TODO: - fix date parsing (how hard can it be?)
9 # - support integrations (at least p4i)
10 # - support incremental imports
11 # - create tags
12 # - instead of reading all files into a variable try to pipe from
13 # - p4 print directly to stdout. need to figure out file size somehow
14 # though.
15 # - support p4 submit (hah!)
16 # - don't hardcode the import to master
17 #
18 import os, string, sys
20 if len(sys.argv) != 2:
21 sys.stderr.write("usage: %s //depot/path[@revRange]\n" % sys.argv[0]);
22 sys.stderr.write("\n example:\n");
23 sys.stderr.write(" %s //depot/my/project/ -- to import everything\n");
24 sys.stderr.write(" %s //depot/my/project/@1,6 -- to import only from revision 1 to 6\n");
25 sys.stderr.write("\n");
26 sys.stderr.write(" (a ... is not needed in the path p4 specification, it's added implicitly)\n");
27 sys.stderr.write("\n");
28 sys.exit(1)
30 prefix = sys.argv[1]
31 changeRange = ""
32 try:
33 atIdx = prefix.index("@")
34 changeRange = prefix[atIdx:]
35 prefix = prefix[0:atIdx]
36 except ValueError:
37 changeRange = ""
39 if not prefix.endswith("/"):
40 prefix += "/"
42 def describe(change):
43 output = os.popen("p4 describe %s" % change).readlines()
45 firstLine = output[0]
47 author = firstLine.split(" ")[3]
48 author = author[:author.find("@")]
50 filesSection = 0
51 try:
52 filesSection = output.index("Affected files ...\n")
53 except ValueError:
54 sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
55 return [], [], [], []
57 differencesSection = 0
58 try:
59 differencesSection = output.index("Differences ...\n")
60 except ValueError:
61 sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
62 return [], [], [], []
64 log = output[2:filesSection - 1]
66 lines = output[filesSection + 2:differencesSection - 1]
68 changed = []
69 removed = []
71 for line in lines:
72 # chop off "... " and trailing newline
73 line = line[4:len(line) - 1]
75 lastSpace = line.rfind(" ")
76 if lastSpace == -1:
77 sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
78 continue
80 operation = line[lastSpace + 1:]
81 path = line[:lastSpace]
83 if operation == "delete":
84 removed.append(path)
85 else:
86 changed.append(path)
88 return author, log, changed, removed
90 def p4cat(path):
91 return os.popen("p4 print -q \"%s\"" % path).read()
93 def stripRevision(path):
94 hashPos = path.rindex("#")
95 return path[:hashPos]
97 def getUserMap():
98 users = {}
99 output = os.popen("p4 users")
100 for line in output:
101 firstSpace = line.index(" ")
102 secondSpace = line.index(" ", firstSpace + 1)
103 key = line[:firstSpace]
104 email = line[firstSpace + 1:secondSpace]
105 openParenPos = line.index("(", secondSpace)
106 closedParenPos = line.index(")", openParenPos)
107 name = line[openParenPos + 1:closedParenPos]
109 users[key] = name + " " + email
111 return users
114 users = getUserMap()
116 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
118 changes = []
119 for line in output:
120 changeNum = line.split(" ")[1]
121 changes.append(changeNum)
123 changes.reverse()
125 sys.stderr.write("\n")
127 cnt = 0
128 for change in changes:
129 [ author, log, changedFiles, removedFiles ] = describe(change)
130 sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
131 cnt = cnt + 1
132 # sys.stderr.write("%s\n" % log)
133 # sys.stderr.write("%s\n" % changedFiles)
134 # sys.stderr.write("%s\n" % removedFiles)
136 print "commit refs/heads/master"
137 if author in users:
138 print "committer %s 1 2" % users[author]
139 else:
140 print "committer %s <a@b> 1 2" % author
141 print "data <<EOT"
142 for l in log:
143 print l[:len(l) - 1]
144 print "EOT"
146 print ""
148 for f in changedFiles:
149 if not f.startswith(prefix):
150 sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
151 continue
152 relpath = f[len(prefix):]
153 print "M 644 inline %s" % stripRevision(relpath)
154 data = p4cat(f)
155 print "data %s" % len(data)
156 sys.stdout.write(data)
157 print ""
159 for f in removedFiles:
160 if not f.startswith(prefix):
161 sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
162 continue
163 relpath = f[len(prefix):]
164 print "D %s" % stripRevision(relpath)
166 print ""