Ported the remaining functions that parsed p4 shell output over to the p4 python...
authorSimon Hausmann <hausmann@kde.org>
Wed, 31 Jan 2007 21:31:28 +0000 (22:31 +0100)
committerSimon Hausmann <hausmann@kde.org>
Wed, 31 Jan 2007 21:31:28 +0000 (22:31 +0100)
Signed-off-by: Simon Hausmann <hausmann@kde.org>
contrib/fast-import/p4-fast-export.py

index d3e65f0f390538a1489f044d851a72e866e352c7..45d5157961f0e094586ca0da319e4f0f0eb7f726 100644 (file)
@@ -57,47 +57,8 @@ def p4Cmd(cmd):
         result.update(entry)
     return result;
 
-def describe(change):
-    describeOutput = p4Cmd("describe %s" % change)
-
-    author = describeOutput["user"]
-    epoch = describeOutput["time"]
-
-    log = describeOutput["desc"]
-
-    changed = []
-    removed = []
-
-    i = 0
-    while describeOutput.has_key("depotFile%s" % i):
-        path = describeOutput["depotFile%s" % i]
-        rev = describeOutput["rev%s" % i]
-        action = describeOutput["action%s" % i]
-        path = path + "#" + rev
-
-        if action == "delete":
-            removed.append(path)
-        else:
-            changed.append(path)
-
-        i = i + 1
-
-    return author, log, epoch, changed, removed
-
-def p4Stat(path):
-    output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
-    fileSize = 0
-    mode = 644
-    for line in output:
-        if line.startswith("... headType x"):
-            mode = 755
-        elif line.startswith("... fileSize "):
-            fileSize = long(line[12:])
-    return mode, fileSize
-
-def stripRevision(path):
-    hashPos = path.rindex("#")
-    return path[:hashPos]
+def p4FileSize(path):
+    return int(p4Cmd("fstat -Ol \"%s\"" % path)["fileSize"])
 
 def getUserMap():
     users = {}
@@ -127,38 +88,50 @@ gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
 
 cnt = 1
 for change in changes:
-    [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
+    description = p4Cmd("describe %s" % change)
+
     sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
     cnt = cnt + 1
 
+    epoch = description["time"]
+    author = description["user"]
+
     gitStream.write("commit refs/heads/master\n")
     if author in users:
         gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
     else:
         gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
     gitStream.write("data <<EOT\n")
-    gitStream.write(log)
+    gitStream.write(description["desc"])
     gitStream.write("EOT\n\n")
 
-    for f in changedFiles:
-        if not f.startswith(prefix):
-            sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
+    fnum = 0
+    while description.has_key("depotFile%s" % fnum):
+        path = description["depotFile%s" % fnum]
+        if not path.startswith(prefix):
+            print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
+            fnum = fnum + 1
             continue
-        relpath = f[len(prefix):]
 
-        [mode, fileSize] = p4Stat(f)
+        rev = description["rev%s" % fnum]
+        depotPath = path + "#" + rev
+        relPath = path[len(prefix):]
+        action = description["action%s" % fnum]
 
-        gitStream.write("M %s inline %s\n" % (mode, stripRevision(relpath)))
-        gitStream.write("data %s\n" % fileSize)
-        gitStream.write(os.popen("p4 print -q \"%s\"" % f).read())
-        gitStream.write("\n")
+        if action == "delete":
+            gitStream.write("D %s\n" % relPath)
+        else:
+            fileSize = p4FileSize(depotPath)
+            mode = 644
+            if description["type%s" % fnum].startswith("x"):
+                mode = 755
 
-    for f in removedFiles:
-        if not f.startswith(prefix):
-            sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
-            continue
-        relpath = f[len(prefix):]
-        gitStream.write("D %s\n" % stripRevision(relpath))
+            gitStream.write("M %s inline %s\n" % (mode, relPath))
+            gitStream.write("data %s\n" % fileSize)
+            gitStream.write(os.popen("p4 print -q \"%s\"" % depotPath).read())
+            gitStream.write("\n")
+
+        fnum = fnum + 1
 
     gitStream.write("\n")