00001 __copyright__ = """
00002 Copyright 2008 Henryk Modzelewski
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 '''
00022 Module to execute a pipe chain.
00023
00024 Execute 'python /path_to_module/MultiPipe.py' to see the examples.
00025 '''
00026
00027 import pipes
00028
00029 VERBOSE=False
00030
00031 def FileIn(fin,cmds):
00032 '''
00033 Function to process a file through a shell pipe.
00034 FileIn(fin,cmds)
00035 fin - string holding name of the input file
00036 cmds - list of strings representing commands in the pipe
00037 Returns 0 if successful.
00038 '''
00039 cnt = len(cmds)
00040 strm = cnt*['--']
00041 pip = []
00042 t=pipes.Template()
00043 if cnt:
00044 strm[cnt-1]=strm[cnt-1][0]+'.'
00045 for i in range(cnt):
00046 pip.append([cmds[i],strm[i]])
00047 t.append(cmds[i],strm[i])
00048 if VERBOSE: print pip,'<',fin
00049 return t.copy(fin,'')
00050
00051 def FileOut(cmds,fout):
00052 '''
00053 Function to save the output from a shell pipe to a file.
00054 FileOut(cmds,fout)
00055 cmds - list of strings representing commands in the pipe
00056 fout - string holding name of the output file
00057 Returns 0 if successful.
00058 '''
00059 cnt = len(cmds)
00060 strm = cnt*['--']
00061 pip = []
00062 t=pipes.Template()
00063 if cnt:
00064 strm[0] = '.'+strm[0][1]
00065 pip.append([cmds[0],strm[0]])
00066 t.prepend(cmds[0],strm[0])
00067 for i in range(1,cnt):
00068 pip.append([cmds[i],strm[i]])
00069 t.append(cmds[i],strm[i])
00070 if VERBOSE: print pip,'>',fout
00071 return t.copy('',fout)
00072
00073 def FileInOut(fin,cmds,fout):
00074 '''
00075 Function to process a file through a shell pipe and save the output to another file.
00076 FileInOut(fin,cmds,fout)
00077 fin - string holding name of the input file
00078 cmds - list of strings representing commands in the pipe
00079 fout - string holding name of the output file
00080 Returns 0 if successful.
00081 '''
00082 cnt = len(cmds)
00083 strm = cnt*['--']
00084 pip = []
00085 t=pipes.Template()
00086 for i in range(cnt):
00087 pip.append([cmds[i],strm[i]])
00088 t.append(cmds[i],strm[i])
00089 if VERBOSE: print pip,'<',fin,'>',fout
00090 return t.copy(fin,fout)
00091
00092
00093 if __name__ == '__main__':
00094 print __doc__
00095
00096 print 80*'-'
00097 print FileOut.__doc__
00098 print "FileOut(['ps -xl','tail -7','head -5'],'/tmp/ps-xl0')"
00099 FileOut(['ps -xl','tail -7','head -5'],'/tmp/ps-xl0')
00100 print 'Result:\n',open('/tmp/ps-xl0').read()
00101
00102 print 80*'-'
00103 print FileInOut.__doc__
00104 print "FileInOut('/tmp/ps-xl0',['head -3','tail -1'],'/tmp/ps-xl1')"
00105 FileInOut('/tmp/ps-xl0',['head -3','tail -1'],'/tmp/ps-xl1')
00106 print 'Result:\n',open('/tmp/ps-xl1').read()
00107
00108 print 80*'-'
00109 print FileIn.__doc__
00110 print "FileIn('/tmp/ps-xl0',['wc'])"
00111 print 'Result:\n'
00112 FileIn('/tmp/ps-xl0',['wc'])
00113
00114 print 80*'-'