[p4] Python scripting with Perforce

Darin Petty Darin.Petty at Configuresoft.com
Wed Oct 5 14:05:27 PDT 2005


Paul,
I didn't see another answer.
Marshal.load only reads the first record.
I use a home-made function to read the whole collection:

def RunMarshal(command):
    # run marshal.load in a loop
    results=[]
    stream=os.popen(command,'rb')
    try:
	while 1:
		results.append(marshal.load(stream))
    except EOFError:
	pass
    stream.close()
    return results

I hope that solves your problem with the counters.

Darin

Paul said:
	Hey all:
	
	I've just started playing with scripting some Perforce triggers
and such 
	with Python. I went with Python because of Perforce's -G option,
but 
	after playing with it for an afternoon, I'm slightly
disappointed.
	
	I'm curious if anyone knows:
	
	1. Why the marshal module was used, as opposed to the
pickle/shelve modules?
	
	I'm having problems fetching data from a Perforce ("p4
counters") 
	invocation. In further reading the Python documentation, I saw:
	
	[marshal] is not a general ``persistence'' module. For general 
	persistence and transfer of Python objects through RPC calls,
see the 
	modules pickle and shelve. The marshal module exists mainly to
support 
	reading and writing the ``pseudo-compiled'' code for Python
modules of 
	.pyc files. Therefore, the Python maintainers reserve the right
to 
	modify the marshal format in backward incompatible ways should
the need 
	arise. If you're serializing and de-serializing Python objects,
use the 
	pickle module instead.
	
	I'm wondering if my problems (namely that my counters invocation
only 
	returns the first counter) are related to possible changes that
may have 
	been made to the Python interpreter in 2.3.x that are
incompatible with 
	the binary marshaled data Perforce is dumping.
	
	2. Am I the only one that finds the structure of the marshaled
output 
	somewhat... confusing?
	
	The sample trigger on page 102 of 2005.1 Administrator's Guide
gives a 
	"p4 group -o" invocation with marshaled output; the output dumps
keys of 
	UserNNN, where N is 0 to the total number of users you have.
This 
	requires matching against the keys (see if it matches '^User',
as the 
	sample does) to see if it's a key you want the value to. Why
wasn't this 
	just put into an array of hashes called "Users"? Is this a
limitation of 
	the marshal module, or?
	
	I'd be curious to see the reasoning used here; I'm building some

	infrastructure that I rallied behind using Python for, most
notably 
	because of this convenient marshaling capability, but my initial

	impression of this feature is that it's somewhat incomplete and
fragile 
	in terms of specific Python interpreter version dependencies,
and if 
	that's actually a case, that makes this a no-go; I can't have 
	infrastructure breaking when we do a x.y.z+1 Python interpreter
upgrade.
	
	Is this accurate or am I doing something wrong?
	
	Later,
	Paul
	-- 
	Build/Release Engineer
	preed at vmware.com
<http://maillist.perforce.com/mailman/listinfo/perforce-user> 
	(650) 475-5248


Darin Petty
Project Manager, Support & Tools

Configuresoft, Inc.
Security. Compliance. Control.
www.configuresoft.com
Office: 719.687-2676
mailto:Darin.Petty at Configuresoft.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://maillist.perforce.com/pipermail/perforce-user/attachments/20051005/8ed233d9/attachment-0006.html>


More information about the perforce-user mailing list