Friday 6 February 2004 — This is more than 19 years old. Be careful.
I like Perforce a lot. It’s lean and clean. I’ve always liked their approach of making the command line fully functional and then building on it as a way of providing multiple interfaces and platforms. But here’s a twist on that idea that I just discovered: every command can take a -G switch which makes the output be marshalled Python objects, to aid in scripting.
To try it out, use this script:
# p4py.py: show off Perforce Python-marshalled output.
import marshal, os, pprint, sys
P4 = '"c:/program files/perforce/p4"'
fP4 = os.popen(P4 + ' -G ' + ' '.join(sys.argv[1:]), 'rb')
d = marshal.load(fP4)
This script will accept any p4 command line and show the output as Python objects:
$ p4py describe 14262
'desc': 'A week ago I could not even spell programmer, and now I are one!\n',
Granted, a genuine API would be even better, and the data structure presented here is a little odd (why the parallel dict keys to represent more than one changed file?), but this is a lot better than screen scraping.
$ px -g describe 91118
'desc': 'Little buglet fix from BrianQ.\n',
Anyway, yes, Perforce's -G is pretty cool.
Add a comment: