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 import pipes
00022
00023 VERBOSE=False
00024
00025 def FileIn(fin,cmds):
00026 """
00027 Function to process a file through a shell pipe.
00028 FileIn(fin,cmds)
00029 fin - string holding name of the input file
00030 cmds - list of strings representing commands in the pipe
00031 Returns 0 if successful.
00032 """
00033 cnt = len(cmds)
00034 strm = cnt*['--']
00035 pip = []
00036 t=pipes.Template()
00037 if cnt:
00038 strm[cnt-1]=strm[cnt-1][0]+'.'
00039 for i in range(cnt):
00040 pip.append([cmds[i],strm[i]])
00041 t.append(cmds[i],strm[i])
00042 if VERBOSE: print pip,'<',fin
00043 return t.copy(fin,'')
00044
00045 def FileOut(cmds,fout):
00046 """
00047 Function to save the output from a shell pipe to a file.
00048 FileOut(cmds,fout)
00049 cmds - list of strings representing commands in the pipe
00050 fout - string holding name of the output file
00051 Returns 0 if successful.
00052 """
00053 cnt = len(cmds)
00054 strm = cnt*['--']
00055 pip = []
00056 t=pipes.Template()
00057 if cnt:
00058 strm[0] = '.'+strm[0][1]
00059 pip.append([cmds[0],strm[0]])
00060 t.prepend(cmds[0],strm[0])
00061 for i in range(1,cnt):
00062 pip.append([cmds[i],strm[i]])
00063 t.append(cmds[i],strm[i])
00064 if VERBOSE: print pip,'>',fout
00065 return t.copy('',fout)
00066
00067 def FileInOut(fin,cmds,fout):
00068 """
00069 Function to process a file through a shell pipe and save the output to another file.
00070 FileInOut(fin,cmds,fout)
00071 fin - string holding name of the input file
00072 cmds - list of strings representing commands in the pipe
00073 fout - string holding name of the output file
00074 Returns 0 if successful.
00075 """
00076 cnt = len(cmds)
00077 strm = cnt*['--']
00078 pip = []
00079 t=pipes.Template()
00080 for i in range(cnt):
00081 pip.append([cmds[i],strm[i]])
00082 t.append(cmds[i],strm[i])
00083 if VERBOSE: print pip,'<',fin,'>',fout
00084 return t.copy(fin,fout)
00085
00086
00087 if __name__ == '__main__':
00088 print __doc__
00089
00090 print 80*'-'
00091 print FileOut.__doc__
00092 print "FileOut(['ps -xl','tail -7','head -5'],'/tmp/ps-xl0')"
00093 FileOut(['ps -xl','tail -7','head -5'],'/tmp/ps-xl0')
00094 print 'Result:\n',open('/tmp/ps-xl0').read()
00095
00096 print 80*'-'
00097 print FileInOut.__doc__
00098 print "FileInOut('/tmp/ps-xl0',['head -3','tail -1'],'/tmp/ps-xl1')"
00099 FileInOut('/tmp/ps-xl0',['head -3','tail -1'],'/tmp/ps-xl1')
00100 print 'Result:\n',open('/tmp/ps-xl1').read()
00101
00102 print 80*'-'
00103 print FileIn.__doc__
00104 print "FileIn('/tmp/ps-xl0',['wc'])"
00105 print 'Result:\n'
00106 FileIn('/tmp/ps-xl0',['wc'])
00107
00108 print 80*'-'