收录日期:2019/10/22 14:30:59 时间:2009-11-11 09:15:44 标签:pickle,perforce

Perforce command line has a special switch, -G, that supposedly causes its output to be machine readable, using python's "pickle" serialization format. Is this actually so, in general?

For example, consider the output of p4 -G diff -duw3 <file1> <file2> <file3>. The output, as far as I can tell, is a sequence of: pickle, raw diff, pickle, raw diff, pickle, raw diff. It does not appear to contain any delimiters that would enable one to reliably locate the pickle/diff boundaries.

Am I missing something or is this "machine-readable" format not actually machine-readable? How can I find the boundaries between pickles and raw diffs in its output?

p4 -G outputs its data in marshaled form not pickled.

But you are right - p4 -G diff -duw3 also won't unmarshal, so I guess there is a problem there.

p4 -G opened unmarshals fine though. However any kind of diff fails.

Here is a relevant knowledge base article: http://kb.perforce.com/ToolsScripts/PerforceUtilities/UsingP4G

#!/usr/bin/env python
import marshal
import subprocess

# proc = subprocess.Popen(["p4","-G","diff","-duw3","configure.ac","Makefile.am"],stdout=subprocess.PIPE)
proc = subprocess.Popen(["p4","-G","diff"],stdout=subprocess.PIPE)
# proc = subprocess.Popen(["p4","-G","opened"],stdout=subprocess.PIPE)
pipe = proc.stdout
output = []
try:
    while 1:
        record = marshal.load(pipe)
        output.append(record)
except EOFError:
    pass
pipe.close()
proc.wait()

# print list of dictionary records
c = 0
for dict in output:
    c = c + 1
    print "\n--%d--" % c
    for key in dict.keys():
        print "%s: %s" % ( key, dict[key] )