<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import tempfile <EOL> import string <EOL> __all__ = [ "<STR_LIT>" ] <EOL> FILEIN_FILEOUT = '<STR_LIT>' <EOL> STDIN_FILEOUT = '<STR_LIT>' <EOL> FILEIN_STDOUT = '<STR_LIT>' <EOL> STDIN_STDOUT = '<STR_LIT>' <EOL> SOURCE = '<STR_LIT>' <EOL> SINK = '<STR_LIT>' <EOL> stepkinds = [ FILEIN_FILEOUT , STDIN_FILEOUT , FILEIN_STDOUT , STDIN_STDOUT , SOURCE , SINK ] <EOL> class Template : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . debugging = <NUM_LIT:0> <EOL> self . reset ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . steps , ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . steps = [ ] <EOL> def clone ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( ) <EOL> t . steps = self . steps [ : ] <EOL> t . debugging = self . debugging <EOL> return t <EOL> def debug ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> self . debugging = flag <EOL> def append ( self , cmd , kind ) : <EOL> """<STR_LIT>""" <EOL> if type ( cmd ) is not type ( '<STR_LIT>' ) : <EOL> raise TypeError , '<STR_LIT>' <EOL> if kind not in stepkinds : <EOL> raise ValueError , '<STR_LIT>' % ( kind , ) <EOL> if kind == SOURCE : <EOL> raise ValueError , '<STR_LIT>' <EOL> if self . steps and self . steps [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] == SINK : <EOL> raise ValueError , '<STR_LIT>' <EOL> if kind [ <NUM_LIT:0> ] == '<STR_LIT:f>' and not re . search ( r'<STR_LIT>' , cmd ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> if kind [ <NUM_LIT:1> ] == '<STR_LIT:f>' and not re . search ( r'<STR_LIT>' , cmd ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> self . steps . append ( ( cmd , kind ) ) <EOL> def prepend ( self , cmd , kind ) : <EOL> """<STR_LIT>""" <EOL> if type ( cmd ) is not type ( '<STR_LIT>' ) : <EOL> raise TypeError , '<STR_LIT>' <EOL> if kind not in stepkinds : <EOL> raise ValueError , '<STR_LIT>' % ( kind , ) <EOL> if kind == SINK : <EOL> raise ValueError , '<STR_LIT>' <EOL> if self . steps and self . steps [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == SOURCE : <EOL> raise ValueError , '<STR_LIT>' <EOL> if kind [ <NUM_LIT:0> ] == '<STR_LIT:f>' and not re . search ( r'<STR_LIT>' , cmd ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> if kind [ <NUM_LIT:1> ] == '<STR_LIT:f>' and not re . search ( r'<STR_LIT>' , cmd ) : <EOL> raise ValueError , '<STR_LIT>' <EOL> self . steps . insert ( <NUM_LIT:0> , ( cmd , kind ) ) <EOL> def open ( self , file , rw ) : <EOL> """<STR_LIT>""" <EOL> if rw == '<STR_LIT:r>' : <EOL> return self . open_r ( file ) <EOL> if rw == '<STR_LIT:w>' : <EOL> return self . open_w ( file ) <EOL> raise ValueError , '<STR_LIT>' % ( rw , ) <EOL> def open_r ( self , file ) : <EOL> """<STR_LIT>""" <EOL> if not self . steps : <EOL> return open ( file , '<STR_LIT:r>' ) <EOL> if self . steps [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] == SINK : <EOL> raise ValueError , '<STR_LIT>' <EOL> cmd = self . makepipeline ( file , '<STR_LIT>' ) <EOL> return os . popen ( cmd , '<STR_LIT:r>' ) <EOL> def open_w ( self , file ) : <EOL> if not self . steps : <EOL> return open ( file , '<STR_LIT:w>' ) <EOL> if self . steps [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == SOURCE : <EOL> raise ValueError , '<STR_LIT>' <EOL> cmd = self . makepipeline ( '<STR_LIT>' , file ) <EOL> return os . popen ( cmd , '<STR_LIT:w>' ) <EOL> def copy ( self , infile , outfile ) : <EOL> return os . system ( self . makepipeline ( infile , outfile ) ) <EOL> def makepipeline ( self , infile , outfile ) : <EOL> cmd = makepipeline ( infile , self . steps , outfile ) <EOL> if self . debugging : <EOL> print cmd <EOL> cmd = '<STR_LIT>' + cmd <EOL> return cmd <EOL> def makepipeline ( infile , steps , outfile ) : <EOL> list = [ ] <EOL> for cmd , kind in steps : <EOL> list . append ( [ '<STR_LIT>' , cmd , kind , '<STR_LIT>' ] ) <EOL> if not list : <EOL> list . append ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> [ cmd , kind ] = list [ <NUM_LIT:0> ] [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> if kind [ <NUM_LIT:0> ] == '<STR_LIT:f>' and not infile : <EOL> list . insert ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = infile <EOL> [ cmd , kind ] = list [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> if kind [ <NUM_LIT:1> ] == '<STR_LIT:f>' and not outfile : <EOL> list . append ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> list [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] = outfile <EOL> garbage = [ ] <EOL> for i in range ( <NUM_LIT:1> , len ( list ) ) : <EOL> lkind = list [ i - <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> rkind = list [ i ] [ <NUM_LIT:2> ] <EOL> if lkind [ <NUM_LIT:1> ] == '<STR_LIT:f>' or rkind [ <NUM_LIT:0> ] == '<STR_LIT:f>' : <EOL> ( fd , temp ) = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> garbage . append ( temp ) <EOL> list [ i - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] = list [ i ] [ <NUM_LIT:0> ] = temp <EOL> for item in list : <EOL> [ inf , cmd , kind , outf ] = item <EOL> if kind [ <NUM_LIT:1> ] == '<STR_LIT:f>' : <EOL> cmd = '<STR_LIT>' + quote ( outf ) + '<STR_LIT>' + cmd <EOL> if kind [ <NUM_LIT:0> ] == '<STR_LIT:f>' : <EOL> cmd = '<STR_LIT>' + quote ( inf ) + '<STR_LIT>' + cmd <EOL> if kind [ <NUM_LIT:0> ] == '<STR_LIT:->' and inf : <EOL> cmd = cmd + '<STR_LIT>' + quote ( inf ) <EOL> if kind [ <NUM_LIT:1> ] == '<STR_LIT:->' and outf : <EOL> cmd = cmd + '<STR_LIT>' + quote ( outf ) <EOL> item [ <NUM_LIT:1> ] = cmd <EOL> cmdlist = list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> for item in list [ <NUM_LIT:1> : ] : <EOL> [ cmd , kind ] = item [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> if item [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> if '<STR_LIT:f>' in kind : <EOL> cmd = '<STR_LIT>' + cmd + '<STR_LIT>' <EOL> cmdlist = cmdlist + '<STR_LIT>' + cmd <EOL> else : <EOL> cmdlist = cmdlist + '<STR_LIT:\n>' + cmd <EOL> if garbage : <EOL> rmcmd = '<STR_LIT>' <EOL> for file in garbage : <EOL> rmcmd = rmcmd + '<STR_LIT:U+0020>' + quote ( file ) <EOL> trapcmd = '<STR_LIT>' + quote ( rmcmd + '<STR_LIT>' ) + '<STR_LIT>' <EOL> cmdlist = trapcmd + '<STR_LIT:\n>' + cmdlist + '<STR_LIT:\n>' + rmcmd <EOL> return cmdlist <EOL> _safechars = string . ascii_letters + string . digits + '<STR_LIT>' <EOL> _funnychars = '<STR_LIT>' <EOL> def quote ( file ) : <EOL> for c in file : <EOL> if c not in _safechars : <EOL> break <EOL> else : <EOL> return file <EOL> if '<STR_LIT>' not in file : <EOL> return '<STR_LIT>' + file + '<STR_LIT>' <EOL> res = '<STR_LIT>' <EOL> for c in file : <EOL> if c in _funnychars : <EOL> c = '<STR_LIT:\\>' + c <EOL> res = res + c <EOL> return '<STR_LIT:">' + res + '<STR_LIT:">' <EOL> def test ( ) : <EOL> print '<STR_LIT>' <EOL> t = Template ( ) <EOL> t . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> t . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> t . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> t . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> t . debug ( <NUM_LIT:1> ) <EOL> FILE = '<STR_LIT>' <EOL> t . copy ( FILE , '<STR_LIT>' ) <EOL> print '<STR_LIT>' </s>
