e284b195024cf3cbbe90655babe2e650d9629855
   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:
   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#       - support p4 submit (hah!)
  14#       - don't hardcode the import to master
  15#
  16import os, string, sys, time
  17import marshal
  18
  19if len(sys.argv) != 2:
  20    sys.stderr.write("usage: %s //depot/path[@revRange]\n" % sys.argv[0]);
  21    sys.stderr.write("\n    example:\n");
  22    sys.stderr.write("    %s //depot/my/project/ -- to import everything\n");
  23    sys.stderr.write("    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6\n");
  24    sys.stderr.write("\n");
  25    sys.stderr.write("    (a ... is not needed in the path p4 specification, it's added implicitly)\n");
  26    sys.stderr.write("\n");
  27    sys.exit(1)
  28
  29prefix = sys.argv[1]
  30changeRange = ""
  31try:
  32    atIdx = prefix.index("@")
  33    changeRange = prefix[atIdx:]
  34    prefix = prefix[0:atIdx]
  35except ValueError:
  36    changeRange = ""
  37
  38if not prefix.endswith("/"):
  39    prefix += "/"
  40
  41def p4Cmd(cmd):
  42    pipe = os.popen("p4 -G %s" % cmd, "rb")
  43    result = {}
  44    try:
  45        while True:
  46            entry = marshal.load(pipe)
  47            result.update(entry)
  48    except EOFError:
  49        pass
  50    pipe.close()
  51    return result
  52
  53def describe(change):
  54    output = os.popen("p4 describe %s" % change).readlines()
  55
  56    firstLine = output[0]
  57
  58    splitted = firstLine.split(" ")
  59    author = splitted[3]
  60    author = author[:author.find("@")]
  61    tm = time.strptime(splitted[5] + " " + splitted[6], "%Y/%m/%d %H:%M:%S ")
  62    epoch = int(time.mktime(tm))
  63
  64    filesSection = 0
  65    try:
  66        filesSection = output.index("Affected files ...\n")
  67    except ValueError:
  68        sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
  69        return [], [], [], [], []
  70
  71    differencesSection = 0
  72    try:
  73        differencesSection = output.index("Differences ...\n")
  74    except ValueError:
  75        sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
  76        return [], [], [], [], []
  77
  78    log = output[2:filesSection - 1]
  79
  80    lines = output[filesSection + 2:differencesSection - 1]
  81
  82    changed = []
  83    removed = []
  84
  85    for line in lines:
  86        # chop off "... " and trailing newline
  87        line = line[4:len(line) - 1]
  88
  89        lastSpace = line.rfind(" ")
  90        if lastSpace == -1:
  91            sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
  92            continue
  93
  94        operation = line[lastSpace + 1:]
  95        path = line[:lastSpace]
  96
  97        if operation == "delete":
  98            removed.append(path)
  99        else:
 100            changed.append(path)
 101
 102    return author, log, epoch, changed, removed
 103
 104def p4Stat(path):
 105    output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
 106    fileSize = 0
 107    mode = 644
 108    for line in output:
 109        if line.startswith("... headType x"):
 110            mode = 755
 111        elif line.startswith("... fileSize "):
 112            fileSize = long(line[12:])
 113    return mode, fileSize
 114
 115def stripRevision(path):
 116    hashPos = path.rindex("#")
 117    return path[:hashPos]
 118
 119def getUserMap():
 120    users = {}
 121    output = os.popen("p4 users")
 122    for line in output:
 123        firstSpace = line.index(" ")
 124        secondSpace = line.index(" ", firstSpace + 1)
 125        key = line[:firstSpace]
 126        email = line[firstSpace + 1:secondSpace]
 127        openParenPos = line.index("(", secondSpace)
 128        closedParenPos = line.index(")", openParenPos)
 129        name = line[openParenPos + 1:closedParenPos]
 130
 131        users[key] = name + " " + email
 132
 133    return users
 134
 135users = getUserMap()
 136
 137output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
 138
 139changes = []
 140for line in output:
 141    changeNum = line.split(" ")[1]
 142    changes.append(changeNum)
 143
 144changes.reverse()
 145
 146sys.stderr.write("\n")
 147
 148tz = - time.timezone / 36
 149
 150cnt = 1
 151for change in changes:
 152    [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
 153    sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 154    cnt = cnt + 1
 155
 156    print "commit refs/heads/master"
 157    if author in users:
 158        print "committer %s %s %s" % (users[author], epoch, tz)
 159    else:
 160        print "committer %s <a@b> %s %s" % (author, epoch, tz)
 161    print "data <<EOT"
 162    for l in log:
 163        print l[:len(l) - 1]
 164    print "EOT"
 165
 166    print ""
 167
 168    for f in changedFiles:
 169        if not f.startswith(prefix):
 170            sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 171            continue
 172        relpath = f[len(prefix):]
 173
 174        [mode, fileSize] = p4Stat(f)
 175
 176        print "M %s inline %s" % (mode, stripRevision(relpath))
 177        print "data %s" % fileSize
 178        sys.stdout.flush();
 179        os.system("p4 print -q \"%s\"" % f)
 180        print ""
 181
 182    for f in removedFiles:
 183        if not f.startswith(prefix):
 184            sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 185            continue
 186        relpath = f[len(prefix):]
 187        print "D %s" % stripRevision(relpath)
 188
 189    print ""
 190
 191sys.stderr.write("\n")
 192