"""Conversion pipeline templates.The problem:------------Suppose you have some data that you want to convert to another format,such as from GIF image format to PPM image format. Maybe theconversion involves several steps (e.g. piping it through compress oruuencode). Some of the conversion steps may require that their inputis a disk file, others may be able to read standard input; similar fortheir output. The input to the entire conversion may also be readfrom a disk file or from an open file, and similar for its output.The module lets you construct a pipeline template by sticking one ormore conversion steps together. It will take care of creating andremoving temporary files if they are necessary to hold intermediatedata. You can then use the template to do conversions from manydifferent sources to many different destinations. The temporaryfile names used are different each time the template is used.The templates are objects so you can create templates for manydifferent conversion steps and store them in a dictionary, forinstance.Directions:-----------To create a template: t = Template()To add a conversion step to a template: t.append(command, kind)where kind is a string of two characters: the first is '-' if thecommand reads its standard input or 'f' if it requires a file; thesecond likewise for the output. The command must be valid /bin/shsyntax. If input or output files are required, they are passed as$IN and $OUT; otherwise, it must be possible to use the command ina pipeline.To add a conversion step at the beginning: t.prepend(command, kind)To convert a file to another file using a template: sts = t.copy(infile, outfile)If infile or outfile are the empty string, standard input is read orstandard output is written, respectively. The return value is theexit status of the conversion pipeline.To open a file for reading or writing through a conversion pipeline: fp = t.open(file, mode)where mode is 'r' to read the file, or 'w' to write it -- just likefor the built-in function open() or for os.popen().To create a new template object initialized to a given one: t2 = t.clone()"""# 'importreimportosimporttempfileimportstring__all__=["Template"]# Conversion step kindsFILEIN_FILEOUT='ff'# Must read & write real filesSTDIN_FILEOUT='-f'# Must write a real fileFILEIN_STDOUT='f-'# Must read a real fileSTDIN_STDOUT='--'# Normal pipeline elementSOURCE='.-'# Must be first, writes stdoutSINK='-.'# Must be last, reads stdinstepkinds=[FILEIN_FILEOUT,STDIN_FILEOUT,FILEIN_STDOUT,STDIN_STDOUT, \
SOURCE,SINK]classTemplate:"""Class representing a pipeline template."""def__init__(self):"""Template() returns a fresh pipeline template."""self.debugging=0self.reset()def__repr__(self):"""t.__repr__() implements repr(t)."""return'<Template instance, steps=%r>'%(self.steps,)defreset(self):"""t.reset() restores a pipeline template to its initial state."""self.steps=[]defclone(self):"""t.clone() returns a new pipeline template with identical initial state as the current one."""t=Template()t.steps=self.steps[:]t.debugging=self.debuggingreturntdefdebug(self,flag):"""t.debug(flag) turns debugging on or off."""self.debugging=flagdefappend(self,cmd,kind):"""t.append(cmd, kind) adds a new step at the end."""iftype(cmd)isnottype(''):raiseTypeError, \
'Template.append: cmd must be a string'ifkindnotinstepkinds:raiseValueError, \
'Template.append: bad kind %r'%(kind,)ifkind==SOURCE:raiseValueError, \
'Template.append: SOURCE can only be prepended'ifself.stepsandself.steps[-1][1]==SINK:raiseValueError, \
'Template.append: already ends with SINK'ifkind[0]=='f'andnotre.search(r'\$IN\b',cmd):raiseValueError, \
'Template.append: missing $IN in cmd'ifkind[1]=='f'andnotre.search(r'\$OUT\b',cmd):raiseValueError, \
'Template.append: missing $OUT in cmd'self.steps.append((cmd,kind))defprepend(self,cmd,kind):"""t.prepend(cmd, kind) adds a new step at the front."""iftype(cmd)isnottype(''):raiseTypeError, \
'Template.prepend: cmd must be a string'ifkindnotinstepkinds:raiseValueError, \
'Template.prepend: bad kind %r'%(kind,)ifkind==SINK:raiseValueError, \
'Template.prepend: SINK can only be appended'ifself.stepsandself.steps[0][1]==SOURCE:raiseValueError, \
'Template.prepend: already begins with SOURCE'ifkind[0]=='f'andnotre.search(r'\$IN\b',cmd):raiseValueError, \
'Template.prepend: missing $IN in cmd'ifkind[1]=='f'andnotre.search(r'\$OUT\b',cmd):raiseValueError, \
'Template.prepend: missing $OUT in cmd'self.steps.insert(0,(cmd,kind))defopen(self,file,rw):"""t.open(file, rw) returns a pipe or file object open for reading or writing; the file is the other end of the pipeline."""ifrw=='r':returnself.open_r(file)ifrw=='w':returnself.open_w(file)raiseValueError, \
'Template.open: rw must be \'r\' or \'w\', not %r'%(rw,)defopen_r(self,file):"""t.open_r(file) and t.open_w(file) implement t.open(file, 'r') and t.open(file, 'w') respectively."""ifnotself.steps:returnopen(file,'r')ifself.steps[-1][1]==SINK:raiseValueError, \
'Template.open_r: pipeline ends width SINK'cmd=self.makepipeline(file,'')returnos.popen(cmd,'r')defopen_w(self,file):ifnotself.steps:returnopen(file,'w')ifself.steps[0][1]==SOURCE:raiseValueError, \
'Template.open_w: pipeline begins with SOURCE'cmd=self.makepipeline('',file)returnos.popen(cmd,'w')defcopy(self,infile,outfile):returnos.system(self.makepipeline(infile,outfile))defmakepipeline(self,infile,outfile):cmd=makepipeline(infile,self.steps,outfile)ifself.debugging:printcmdcmd='set -x; '+cmdreturncmddefmakepipeline(infile,steps,outfile):# Build a list with for each command:# [input filename or '', command string, kind, output filename or '']list=[]forcmd,kindinsteps:list.append(['',cmd,kind,''])## Make sure there is at least one step#ifnotlist:list.append(['','cat','--',''])## Take care of the input and output ends#[cmd,kind]=list[0][1:3]ifkind[0]=='f'andnotinfile:list.insert(0,['','cat','--',''])list[0][0]=infile#[cmd,kind]=list[-1][1:3]ifkind[1]=='f'andnotoutfile:list.append(['','cat','--',''])list[-1][-1]=outfile## Invent temporary files to connect stages that need files#garbage=[]foriinrange(1,len(list)):lkind=list[i-1][2]rkind=list[i][2]iflkind[1]=='f'orrkind[0]=='f':(fd,temp)=tempfile.mkstemp()os.close(fd)garbage.append(temp)list[i-1][-1]=list[i][0]=temp#foriteminlist:[inf,cmd,kind,outf]=itemifkind[1]=='f':cmd='OUT='+quote(outf)+'; '+cmdifkind[0]=='f':cmd='IN='+quote(inf)+'; '+cmdifkind[0]=='-'andinf:cmd=cmd+' <'+quote(inf)ifkind[1]=='-'andoutf:cmd=cmd+' >'+quote(outf)item[1]=cmd#cmdlist=list[0][1]foriteminlist[1:]:[cmd,kind]=item[1:3]ifitem[0]=='':if'f'inkind:cmd='{ '+cmd+'; }'cmdlist=cmdlist+' |\n'+cmdelse:cmdlist=cmdlist+'\n'+cmd#ifgarbage:rmcmd='rm -f'forfileingarbage:rmcmd=rmcmd+' '+quote(file)trapcmd='trap '+quote(rmcmd+'; exit')+' 1 2 3 13 14 15'cmdlist=trapcmd+'\n'+cmdlist+'\n'+rmcmd#returncmdlist# Reliably quote a string as a single argument for /bin/sh# Safe unquoted_safechars=frozenset(string.ascii_letters+string.digits+'@%_-+=:,./')defquote(file):"""Return a shell-escaped version of the file string."""forcinfile:ifcnotin_safechars:breakelse:ifnotfile:return"''"returnfile# use single quotes, and put single quotes into double quotes# the string $'b is then quoted as '$'"'"'b'return"'"+file.replace("'","'\"'\"'")+"'"