Im trying to create a simple unix style pipe for chaining the output of one program to the input of another. I have it working on my Mac but when I try it on a linux server (CentOS) it fails, as the external programs are unable to understand the -
for some reason. I don't understand how this works on my Mac and not on a linux platform. My code so far is:
from subprocess import Popen, PIPE
# Commands to run
p1 = "samtools view -h small.bam".split()
p2 = "samtools view -hb -".split() # The '-' seems to be the problem
p3 = "samtools view -hb -o fini.bam -".split()
process_list = [p1, p2, p3]
class Pipe_manager(object):
"""Takes a list of commands to run and pipes their in/out together."""
def __init__(self, process_list):
self.process_list = process_list
self.open_processes = []
self.output = 0
print 'Starting processes'
self.__open_procs()
def __open_procs(self):
for i in range(len(self.process_list)):
if i == 0:
self.open_processes.append(Popen(self.process_list[i], stdout=PIPE))
else:
old_pipe = self.open_processes[i-1]
self.open_processes.append(Popen(self.process_list[i], stdin=old_pipe.stdout, stdout=PIPE))
self.__close_procs()
def __close_procs(self):
for j in range(len(self.open_processes)-1):
self.open_processes[j].stdout.close()
self.__get_output()
def __get_output(self):
self.output = self.open_processes[-1].communicate()[0]
P = Pipe_manager(process_list)
if P.output:
print P.output
The error messages are a bit program specific but may be of help here:
[main_samview] fail to read the header from "-".
What can I do to make unix work in this case?