Wednesday, May 22, 2013

Working With Perforce -G (Python Marshalled Object) Option

Most common pitfall is to directly get the output pirnted in stdout and marshal it:-

#!/usr/bin/env python
cmd = 'p4 -G changelists -m 3'
mo = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
result = marshal.loads(mo)
pprint(result)

Nope. This won't work correctly.
You will only get the last data in the list of 3 items.

This is the correct way to do it:-

#!/usr/bin/env python
cmd = 'p4 -G changelists -m 3'
mo = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

result = []
try:
   while 1:
      output = marshal.load(mo.stdout)
      result.append(output)
except EOFError:
   pass
finally:
   mo.stdout.close()

pprint(result)

Display Not Found / No protocol specified

When faced with these errors, it most probably means your X11 forwarding authorization is not set up (or messed up). You will need to manual...