Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Conversion pipeline templates. |
| 2 | |
| 3 | The problem: |
| 4 | ------------ |
| 5 | |
Fred Drake | 0f715d2 | 2001-07-20 18:53:34 +0000 | [diff] [blame] | 6 | Suppose you have some data that you want to convert to another format, |
| 7 | such as from GIF image format to PPM image format. Maybe the |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 8 | conversion involves several steps (e.g. piping it through compress or |
| 9 | uuencode). Some of the conversion steps may require that their input |
| 10 | is a disk file, others may be able to read standard input; similar for |
| 11 | their output. The input to the entire conversion may also be read |
| 12 | from a disk file or from an open file, and similar for its output. |
| 13 | |
| 14 | The module lets you construct a pipeline template by sticking one or |
| 15 | more conversion steps together. It will take care of creating and |
| 16 | removing temporary files if they are necessary to hold intermediate |
| 17 | data. You can then use the template to do conversions from many |
| 18 | different sources to many different destinations. The temporary |
| 19 | file names used are different each time the template is used. |
| 20 | |
| 21 | The templates are objects so you can create templates for many |
| 22 | different conversion steps and store them in a dictionary, for |
| 23 | instance. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 24 | |
| 25 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 26 | Directions: |
| 27 | ----------- |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 29 | To create a template: |
| 30 | t = Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 32 | To add a conversion step to a template: |
| 33 | t.append(command, kind) |
| 34 | where kind is a string of two characters: the first is '-' if the |
| 35 | command reads its standard input or 'f' if it requires a file; the |
| 36 | second likewise for the output. The command must be valid /bin/sh |
| 37 | syntax. If input or output files are required, they are passed as |
| 38 | $IN and $OUT; otherwise, it must be possible to use the command in |
| 39 | a pipeline. |
| 40 | |
| 41 | To add a conversion step at the beginning: |
| 42 | t.prepend(command, kind) |
| 43 | |
| 44 | To convert a file to another file using a template: |
| 45 | sts = t.copy(infile, outfile) |
| 46 | If infile or outfile are the empty string, standard input is read or |
| 47 | standard output is written, respectively. The return value is the |
| 48 | exit status of the conversion pipeline. |
| 49 | |
| 50 | To open a file for reading or writing through a conversion pipeline: |
| 51 | fp = t.open(file, mode) |
| 52 | where mode is 'r' to read the file, or 'w' to write it -- just like |
| 53 | for the built-in function open() or for os.popen(). |
| 54 | |
| 55 | To create a new template object initialized to a given one: |
| 56 | t2 = t.clone() |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 57 | """ # ' |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 58 | |
| 59 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 60 | import re |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 61 | import os |
| 62 | import tempfile |
| 63 | import string |
| 64 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 65 | __all__ = ["Template"] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 66 | |
| 67 | # Conversion step kinds |
| 68 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 69 | FILEIN_FILEOUT = 'ff' # Must read & write real files |
| 70 | STDIN_FILEOUT = '-f' # Must write a real file |
| 71 | FILEIN_STDOUT = 'f-' # Must read a real file |
| 72 | STDIN_STDOUT = '--' # Normal pipeline element |
| 73 | SOURCE = '.-' # Must be first, writes stdout |
| 74 | SINK = '-.' # Must be last, reads stdin |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 75 | |
| 76 | stepkinds = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 77 | SOURCE, SINK] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 78 | |
| 79 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 80 | class Template: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 81 | """Class representing a pipeline template.""" |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 82 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 83 | def __init__(self): |
| 84 | """Template() returns a fresh pipeline template.""" |
| 85 | self.debugging = 0 |
| 86 | self.reset() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 87 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 88 | def __repr__(self): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 89 | """t.__repr__() implements repr(t).""" |
| 90 | return '<Template instance, steps=%r>' % (self.steps,) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 91 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 92 | def reset(self): |
| 93 | """t.reset() restores a pipeline template to its initial state.""" |
| 94 | self.steps = [] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 95 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 96 | def clone(self): |
| 97 | """t.clone() returns a new pipeline template with identical |
| 98 | initial state as the current one.""" |
| 99 | t = Template() |
| 100 | t.steps = self.steps[:] |
| 101 | t.debugging = self.debugging |
| 102 | return t |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 103 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 104 | def debug(self, flag): |
| 105 | """t.debug(flag) turns debugging on or off.""" |
| 106 | self.debugging = flag |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 107 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 108 | def append(self, cmd, kind): |
| 109 | """t.append(cmd, kind) adds a new step at the end.""" |
| 110 | if type(cmd) is not type(''): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 111 | raise TypeError('Template.append: cmd must be a string') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 112 | if kind not in stepkinds: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 113 | raise ValueError('Template.append: bad kind %r' % (kind,)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 114 | if kind == SOURCE: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 115 | raise ValueError('Template.append: SOURCE can only be prepended') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 116 | if self.steps and self.steps[-1][1] == SINK: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 117 | raise ValueError('Template.append: already ends with SINK') |
Tim Peters | c09cee4 | 2001-04-25 03:43:14 +0000 | [diff] [blame] | 118 | if kind[0] == 'f' and not re.search(r'\$IN\b', cmd): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 119 | raise ValueError('Template.append: missing $IN in cmd') |
Tim Peters | c09cee4 | 2001-04-25 03:43:14 +0000 | [diff] [blame] | 120 | if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 121 | raise ValueError('Template.append: missing $OUT in cmd') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 122 | self.steps.append((cmd, kind)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 123 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 124 | def prepend(self, cmd, kind): |
| 125 | """t.prepend(cmd, kind) adds a new step at the front.""" |
| 126 | if type(cmd) is not type(''): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 127 | raise TypeError('Template.prepend: cmd must be a string') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 128 | if kind not in stepkinds: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 129 | raise ValueError('Template.prepend: bad kind %r' % (kind,)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 130 | if kind == SINK: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 131 | raise ValueError('Template.prepend: SINK can only be appended') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 132 | if self.steps and self.steps[0][1] == SOURCE: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 133 | raise ValueError('Template.prepend: already begins with SOURCE') |
Tim Peters | c09cee4 | 2001-04-25 03:43:14 +0000 | [diff] [blame] | 134 | if kind[0] == 'f' and not re.search(r'\$IN\b', cmd): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 135 | raise ValueError('Template.prepend: missing $IN in cmd') |
Tim Peters | c09cee4 | 2001-04-25 03:43:14 +0000 | [diff] [blame] | 136 | if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 137 | raise ValueError('Template.prepend: missing $OUT in cmd') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 138 | self.steps.insert(0, (cmd, kind)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 139 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 140 | def open(self, file, rw): |
| 141 | """t.open(file, rw) returns a pipe or file object open for |
| 142 | reading or writing; the file is the other end of the pipeline.""" |
| 143 | if rw == 'r': |
| 144 | return self.open_r(file) |
| 145 | if rw == 'w': |
| 146 | return self.open_w(file) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 147 | raise ValueError('Template.open: rw must be \'r\' or \'w\', not %r' |
| 148 | % (rw,)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 149 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 150 | def open_r(self, file): |
| 151 | """t.open_r(file) and t.open_w(file) implement |
| 152 | t.open(file, 'r') and t.open(file, 'w') respectively.""" |
| 153 | if not self.steps: |
| 154 | return open(file, 'r') |
| 155 | if self.steps[-1][1] == SINK: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 156 | raise ValueError('Template.open_r: pipeline ends width SINK') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 157 | cmd = self.makepipeline(file, '') |
| 158 | return os.popen(cmd, 'r') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 159 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 160 | def open_w(self, file): |
| 161 | if not self.steps: |
| 162 | return open(file, 'w') |
| 163 | if self.steps[0][1] == SOURCE: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 164 | raise ValueError('Template.open_w: pipeline begins with SOURCE') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 165 | cmd = self.makepipeline('', file) |
| 166 | return os.popen(cmd, 'w') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 167 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 168 | def copy(self, infile, outfile): |
| 169 | return os.system(self.makepipeline(infile, outfile)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 170 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 171 | def makepipeline(self, infile, outfile): |
| 172 | cmd = makepipeline(infile, self.steps, outfile) |
| 173 | if self.debugging: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 174 | print(cmd) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 175 | cmd = 'set -x; ' + cmd |
| 176 | return cmd |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def makepipeline(infile, steps, outfile): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 180 | # Build a list with for each command: |
| 181 | # [input filename or '', command string, kind, output filename or ''] |
| 182 | |
| 183 | list = [] |
| 184 | for cmd, kind in steps: |
| 185 | list.append(['', cmd, kind, '']) |
| 186 | # |
| 187 | # Make sure there is at least one step |
| 188 | # |
| 189 | if not list: |
| 190 | list.append(['', 'cat', '--', '']) |
| 191 | # |
| 192 | # Take care of the input and output ends |
| 193 | # |
| 194 | [cmd, kind] = list[0][1:3] |
| 195 | if kind[0] == 'f' and not infile: |
| 196 | list.insert(0, ['', 'cat', '--', '']) |
| 197 | list[0][0] = infile |
| 198 | # |
| 199 | [cmd, kind] = list[-1][1:3] |
| 200 | if kind[1] == 'f' and not outfile: |
| 201 | list.append(['', 'cat', '--', '']) |
| 202 | list[-1][-1] = outfile |
| 203 | # |
| 204 | # Invent temporary files to connect stages that need files |
| 205 | # |
| 206 | garbage = [] |
| 207 | for i in range(1, len(list)): |
| 208 | lkind = list[i-1][2] |
| 209 | rkind = list[i][2] |
| 210 | if lkind[1] == 'f' or rkind[0] == 'f': |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 211 | (fd, temp) = tempfile.mkstemp() |
| 212 | os.close(fd) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 213 | garbage.append(temp) |
| 214 | list[i-1][-1] = list[i][0] = temp |
| 215 | # |
| 216 | for item in list: |
| 217 | [inf, cmd, kind, outf] = item |
| 218 | if kind[1] == 'f': |
| 219 | cmd = 'OUT=' + quote(outf) + '; ' + cmd |
| 220 | if kind[0] == 'f': |
| 221 | cmd = 'IN=' + quote(inf) + '; ' + cmd |
| 222 | if kind[0] == '-' and inf: |
| 223 | cmd = cmd + ' <' + quote(inf) |
| 224 | if kind[1] == '-' and outf: |
| 225 | cmd = cmd + ' >' + quote(outf) |
| 226 | item[1] = cmd |
| 227 | # |
| 228 | cmdlist = list[0][1] |
| 229 | for item in list[1:]: |
| 230 | [cmd, kind] = item[1:3] |
| 231 | if item[0] == '': |
| 232 | if 'f' in kind: |
| 233 | cmd = '{ ' + cmd + '; }' |
| 234 | cmdlist = cmdlist + ' |\n' + cmd |
| 235 | else: |
| 236 | cmdlist = cmdlist + '\n' + cmd |
| 237 | # |
| 238 | if garbage: |
| 239 | rmcmd = 'rm -f' |
| 240 | for file in garbage: |
| 241 | rmcmd = rmcmd + ' ' + quote(file) |
| 242 | trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15' |
| 243 | cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd |
| 244 | # |
| 245 | return cmdlist |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | # Reliably quote a string as a single argument for /bin/sh |
| 249 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 250 | # Safe unquoted |
| 251 | _safechars = frozenset(string.ascii_letters + string.digits + '@%_-+=:,./') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 252 | |
| 253 | def quote(file): |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 254 | """Return a shell-escaped version of the file string.""" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 255 | for c in file: |
| 256 | if c not in _safechars: |
| 257 | break |
| 258 | else: |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 259 | if not file: |
| 260 | return "''" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 261 | return file |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 262 | # use single quotes, and put single quotes into double quotes |
| 263 | # the string $'b is then quoted as '$'"'"'b' |
| 264 | return "'" + file.replace("'", "'\"'\"'") + "'" |