[p4] Using the -G option with Python

Nick Barnes Nick.Barnes at pobox.com
Wed Oct 10 02:46:10 PDT 2001


At 2001-10-09 18:06:00+0000, "Michael Malloy" writes:
> 
> Does anyone have any code that uses the -G option while running p4
> command within Python?  I am confused by this module and can't seem to
> find any good examples.  Here are something I have tried:
> 
> output = os.popen('p4 -G sync -f ...').read()
> marshal.loads(output)
> 
> The problem is that this only gives me the first file that was sync'ed,
> as far as I can tell.  Subsequent calls to loads() give the same result.
> Am I missing something?  I know that about 20 files are refreshed with
> this command.

The Python output is a series of marshalled objects.  You are reading
the whole output into a string and then unmarshalling the first
object. Running marshal.loads(output) again just unmarshals the first
object again. Instead you should use marshal.load() to unmarshal the
output one item at a time.  Like this:

$ python
Python 1.5.2 (#2, Jul 19 2000, 19:09:55)  [GCC 2.95.2 19991024 (release)] on freebsd4
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> import marshal
>>> stream = os.popen('p4 -G sync -f ...')
>>> results = []
>>> try:
...   while 1:
...     results.append(marshal.load(stream))
... except EOFError:
...   pass
... 
>>> len(results)
32
>>> results[0]
{'code': 'info', 'data': '//info.ravenbrook.com/mail/2001/08/01/00-01-00/0.txt#1 - refreshing /home/nb/info.ravenbrook.com/mail/2001/08/01/00-01-00/0.txt', 'level': 0}
>>> results[1]
{'code': 'info', 'data': '//info.ravenbrook.com/mail/2001/08/01/00-59-01/0.txt#1 - refreshing /home/nb/info.ravenbrook.com/mail/2001/08/01/00-59-01/0.txt', 'level': 0}
>>>

For a more sophisticated example, see the P4DTI code for doing this:

<http://www.ravenbrook.com/project/p4dti/master/code/replicator/p4.py>

Nick Barnes



More information about the perforce-user mailing list