Start moving the git-p4 tools into one single script.
authorSimon Hausmann <hausmann@kde.org>
Mon, 19 Mar 2007 19:59:12 +0000 (20:59 +0100)
committerSimon Hausmann <hausmann@kde.org>
Mon, 19 Mar 2007 19:59:12 +0000 (20:59 +0100)
Signed-off-by: Simon Hausmann <hausmann@kde.org>
contrib/fast-import/git-p4.py [new file with mode: 0755]
contrib/fast-import/p4-clean-tags.py [deleted file]
contrib/fast-import/p4-debug.py [deleted file]
diff --git a/contrib/fast-import/git-p4.py b/contrib/fast-import/git-p4.py
new file mode 100755 (executable)
index 0000000..8008156
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env python
+#
+# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
+#
+# Author: Simon Hausmann <hausmann@kde.org>
+# License: MIT <http://www.opensource.org/licenses/mit-license.php>
+#
+
+import optparse, sys, os, marshal, popen2
+
+def p4CmdList(cmd):
+    cmd = "p4 -G %s" % cmd
+    pipe = os.popen(cmd, "rb")
+
+    result = []
+    try:
+        while True:
+            entry = marshal.load(pipe)
+            result.append(entry)
+    except EOFError:
+        pass
+    pipe.close()
+
+    return result
+
+def p4Cmd(cmd):
+    list = p4CmdList(cmd)
+    result = {}
+    for entry in list:
+        result.update(entry)
+    return result;
+
+def die(msg):
+    sys.stderr.write(msg + "\n")
+    sys.exit(1)
+
+def currentGitBranch():
+    return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
+
+class P4Debug:
+    def __init__(self):
+        self.options = [
+        ]
+
+    def run(self, args):
+        for output in p4CmdList(" ".join(args)):
+            print output
+
+class P4CleanTags:
+    def __init__(self):
+        self.options = [
+#                optparse.make_option("--branch", dest="branch", default="refs/heads/master")
+        ]
+    def run(self, args):
+        branch = currentGitBranch()
+        print "Cleaning out stale p4 import tags..."
+        sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
+        output = sout.read()
+        try:
+            tagIdx = output.index(" tags/p4/")
+        except:
+            print "Cannot find any p4/* tag. Nothing to do."
+            sys.exit(0)
+
+        try:
+            caretIdx = output.index("^")
+        except:
+            caretIdx = len(output) - 1
+        rev = int(output[tagIdx + 9 : caretIdx])
+
+        allTags = os.popen("git tag -l p4/").readlines()
+        for i in range(len(allTags)):
+            allTags[i] = int(allTags[i][3:-1])
+
+        allTags.sort()
+
+        allTags.remove(rev)
+
+        for rev in allTags:
+            print os.popen("git tag -d p4/%s" % rev).read()
+
+        print "%s tags removed." % len(allTags)
+
+def printUsage(commands):
+    print "usage: %s <command> [options]" % sys.argv[0]
+    print ""
+    print "valid commands: %s" % ", ".join(commands)
+    print ""
+    print "Try %s <command> --help for command specific help." % sys.argv[0]
+    print ""
+
+commands = {
+    "debug" : P4Debug(),
+    "clean-tags" : P4CleanTags()
+}
+
+if len(sys.argv[1:]) == 0:
+    printUsage(commands.keys())
+    sys.exit(2)
+
+cmd = ""
+cmdName = sys.argv[1]
+try:
+    cmd = commands[cmdName]
+except KeyError:
+    print "unknown command %s" % cmdName
+    print ""
+    printUsage(commands.keys())
+    sys.exit(2)
+
+parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options)
+
+(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
+
+cmd.run(args)
diff --git a/contrib/fast-import/p4-clean-tags.py b/contrib/fast-import/p4-clean-tags.py
deleted file mode 100755 (executable)
index 924ff89..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/usr/bin/python
-#
-# p4-debug.py
-#
-# Author: Simon Hausmann <hausmann@kde.org>
-# License: MIT <http://www.opensource.org/licenses/mit-license.php>
-#
-# removes unused p4 import tags
-#
-import os, string, sys
-import popen2, getopt
-
-branch = "refs/heads/master"
-
-try:
-    opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ])
-except getopt.GetoptError:
-    print "fixme, syntax error"
-    sys.exit(1)
-
-for o, a in opts:
-    if o == "--branch":
-        branch = "refs/heads/" + a
-
-sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
-output = sout.read()
-tagIdx = output.index(" tags/p4/")
-try:
-    caretIdx = output.index("^")
-except:
-    caretIdx = len(output) - 1
-rev = int(output[tagIdx + 9 : caretIdx])
-
-allTags = os.popen("git tag -l p4/").readlines()
-for i in range(len(allTags)):
-    allTags[i] = int(allTags[i][3:-1])
-
-allTags.sort()
-
-allTags.remove(rev)
-
-for rev in allTags:
-    print os.popen("git tag -d p4/%s" % rev).read()
diff --git a/contrib/fast-import/p4-debug.py b/contrib/fast-import/p4-debug.py
deleted file mode 100755 (executable)
index 8fb159f..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/usr/bin/python
-#
-# p4-debug.py
-#
-# Author: Simon Hausmann <hausmann@kde.org>
-# License: MIT <http://www.opensource.org/licenses/mit-license.php>
-#
-# executes a p4 command with -G and prints the resulting python dicts
-#
-import os, string, sys
-import marshal, popen2
-
-cmd = ""
-for arg in sys.argv[1:]:
-    cmd += arg + " "
-
-pipe = os.popen("p4 -G %s" % cmd, "rb")
-try:
-    while True:
-        entry = marshal.load(pipe)
-        print entry
-except EOFError:
-    pass
-pipe.close()
-