views:

260

answers:

1

I'm trying to redirect a patch command output using a named pipe. I tried like this:

fifo = os.path.join(self.path, 'pipe')
os.mkfifo(fifo)
op = os.popen('cat '+ fifo)
proc = Popen(['patch', current_keyframe, '--input='+fpath, '--output='+fifo], stdin=PIPE, stdout=PIPE)
os.unlink(fifo)
print op.read()

But my script stops at Popen() call just like patch command didn't completed. How I can make it work properly?

+1  A: 

You aren't waiting for the patch command to finish before you read from the fifo. Replace the subprocess.Popen() call with subprocess.call(), and remove the stdin/stdout redirections you aren't using. Also, use open(fifo) to read from the fifo, not os.popen('cat ' + fifo).

You realize, I hope, that you can avoid the FIFO entirely? After p = Popen(['patch', '--input', fpath], stdout=PIPE), you can just read patch's output from p.stdout.

Thomas Wouters
patch command doesn't return its result to stdout, that's the problem. But thanks, I'll try.
Enchantner
Right, the default with patch is to replace the file you're patching. If you say `--output=-` patch very helpfully replies `patch: **** can't output patches to standard output`.
Jason Orendorff
Hm, and here I thought patch was useful. (I'll leave the egg on my face in the original post :)
Thomas Wouters