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)

No comments:

Post a Comment

How To Bypass Kerberos(kinit) Authentication

Whenever you try to setuid and impersonate as someone else to run something, it is very likely that you will run into kerberos/kinit issues....