pipes.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """Conversion pipeline templates.
  2. The problem:
  3. ------------
  4. Suppose you have some data that you want to convert to another format,
  5. such as from GIF image format to PPM image format. Maybe the
  6. conversion involves several steps (e.g. piping it through compress or
  7. uuencode). Some of the conversion steps may require that their input
  8. is a disk file, others may be able to read standard input; similar for
  9. their output. The input to the entire conversion may also be read
  10. from a disk file or from an open file, and similar for its output.
  11. The module lets you construct a pipeline template by sticking one or
  12. more conversion steps together. It will take care of creating and
  13. removing temporary files if they are necessary to hold intermediate
  14. data. You can then use the template to do conversions from many
  15. different sources to many different destinations. The temporary
  16. file names used are different each time the template is used.
  17. The templates are objects so you can create templates for many
  18. different conversion steps and store them in a dictionary, for
  19. instance.
  20. Directions:
  21. -----------
  22. To create a template:
  23. t = Template()
  24. To add a conversion step to a template:
  25. t.append(command, kind)
  26. where kind is a string of two characters: the first is '-' if the
  27. command reads its standard input or 'f' if it requires a file; the
  28. second likewise for the output. The command must be valid /bin/sh
  29. syntax. If input or output files are required, they are passed as
  30. $IN and $OUT; otherwise, it must be possible to use the command in
  31. a pipeline.
  32. To add a conversion step at the beginning:
  33. t.prepend(command, kind)
  34. To convert a file to another file using a template:
  35. sts = t.copy(infile, outfile)
  36. If infile or outfile are the empty string, standard input is read or
  37. standard output is written, respectively. The return value is the
  38. exit status of the conversion pipeline.
  39. To open a file for reading or writing through a conversion pipeline:
  40. fp = t.open(file, mode)
  41. where mode is 'r' to read the file, or 'w' to write it -- just like
  42. for the built-in function open() or for os.popen().
  43. To create a new template object initialized to a given one:
  44. t2 = t.clone()
  45. """ # '
  46. import re
  47. import os
  48. import tempfile
  49. import warnings
  50. # we import the quote function rather than the module for backward compat
  51. # (quote used to be an undocumented but used function in pipes)
  52. from shlex import quote
  53. warnings._deprecated(__name__, remove=(3, 13))
  54. __all__ = ["Template"]
  55. # Conversion step kinds
  56. FILEIN_FILEOUT = 'ff' # Must read & write real files
  57. STDIN_FILEOUT = '-f' # Must write a real file
  58. FILEIN_STDOUT = 'f-' # Must read a real file
  59. STDIN_STDOUT = '--' # Normal pipeline element
  60. SOURCE = '.-' # Must be first, writes stdout
  61. SINK = '-.' # Must be last, reads stdin
  62. stepkinds = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \
  63. SOURCE, SINK]
  64. class Template:
  65. """Class representing a pipeline template."""
  66. def __init__(self):
  67. """Template() returns a fresh pipeline template."""
  68. self.debugging = 0
  69. self.reset()
  70. def __repr__(self):
  71. """t.__repr__() implements repr(t)."""
  72. return '<Template instance, steps=%r>' % (self.steps,)
  73. def reset(self):
  74. """t.reset() restores a pipeline template to its initial state."""
  75. self.steps = []
  76. def clone(self):
  77. """t.clone() returns a new pipeline template with identical
  78. initial state as the current one."""
  79. t = Template()
  80. t.steps = self.steps[:]
  81. t.debugging = self.debugging
  82. return t
  83. def debug(self, flag):
  84. """t.debug(flag) turns debugging on or off."""
  85. self.debugging = flag
  86. def append(self, cmd, kind):
  87. """t.append(cmd, kind) adds a new step at the end."""
  88. if not isinstance(cmd, str):
  89. raise TypeError('Template.append: cmd must be a string')
  90. if kind not in stepkinds:
  91. raise ValueError('Template.append: bad kind %r' % (kind,))
  92. if kind == SOURCE:
  93. raise ValueError('Template.append: SOURCE can only be prepended')
  94. if self.steps and self.steps[-1][1] == SINK:
  95. raise ValueError('Template.append: already ends with SINK')
  96. if kind[0] == 'f' and not re.search(r'\$IN\b', cmd):
  97. raise ValueError('Template.append: missing $IN in cmd')
  98. if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd):
  99. raise ValueError('Template.append: missing $OUT in cmd')
  100. self.steps.append((cmd, kind))
  101. def prepend(self, cmd, kind):
  102. """t.prepend(cmd, kind) adds a new step at the front."""
  103. if not isinstance(cmd, str):
  104. raise TypeError('Template.prepend: cmd must be a string')
  105. if kind not in stepkinds:
  106. raise ValueError('Template.prepend: bad kind %r' % (kind,))
  107. if kind == SINK:
  108. raise ValueError('Template.prepend: SINK can only be appended')
  109. if self.steps and self.steps[0][1] == SOURCE:
  110. raise ValueError('Template.prepend: already begins with SOURCE')
  111. if kind[0] == 'f' and not re.search(r'\$IN\b', cmd):
  112. raise ValueError('Template.prepend: missing $IN in cmd')
  113. if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd):
  114. raise ValueError('Template.prepend: missing $OUT in cmd')
  115. self.steps.insert(0, (cmd, kind))
  116. def open(self, file, rw):
  117. """t.open(file, rw) returns a pipe or file object open for
  118. reading or writing; the file is the other end of the pipeline."""
  119. if rw == 'r':
  120. return self.open_r(file)
  121. if rw == 'w':
  122. return self.open_w(file)
  123. raise ValueError('Template.open: rw must be \'r\' or \'w\', not %r'
  124. % (rw,))
  125. def open_r(self, file):
  126. """t.open_r(file) and t.open_w(file) implement
  127. t.open(file, 'r') and t.open(file, 'w') respectively."""
  128. if not self.steps:
  129. return open(file, 'r')
  130. if self.steps[-1][1] == SINK:
  131. raise ValueError('Template.open_r: pipeline ends width SINK')
  132. cmd = self.makepipeline(file, '')
  133. return os.popen(cmd, 'r')
  134. def open_w(self, file):
  135. if not self.steps:
  136. return open(file, 'w')
  137. if self.steps[0][1] == SOURCE:
  138. raise ValueError('Template.open_w: pipeline begins with SOURCE')
  139. cmd = self.makepipeline('', file)
  140. return os.popen(cmd, 'w')
  141. def copy(self, infile, outfile):
  142. return os.system(self.makepipeline(infile, outfile))
  143. def makepipeline(self, infile, outfile):
  144. cmd = makepipeline(infile, self.steps, outfile)
  145. if self.debugging:
  146. print(cmd)
  147. cmd = 'set -x; ' + cmd
  148. return cmd
  149. def makepipeline(infile, steps, outfile):
  150. # Build a list with for each command:
  151. # [input filename or '', command string, kind, output filename or '']
  152. list = []
  153. for cmd, kind in steps:
  154. list.append(['', cmd, kind, ''])
  155. #
  156. # Make sure there is at least one step
  157. #
  158. if not list:
  159. list.append(['', 'cat', '--', ''])
  160. #
  161. # Take care of the input and output ends
  162. #
  163. [cmd, kind] = list[0][1:3]
  164. if kind[0] == 'f' and not infile:
  165. list.insert(0, ['', 'cat', '--', ''])
  166. list[0][0] = infile
  167. #
  168. [cmd, kind] = list[-1][1:3]
  169. if kind[1] == 'f' and not outfile:
  170. list.append(['', 'cat', '--', ''])
  171. list[-1][-1] = outfile
  172. #
  173. # Invent temporary files to connect stages that need files
  174. #
  175. garbage = []
  176. for i in range(1, len(list)):
  177. lkind = list[i-1][2]
  178. rkind = list[i][2]
  179. if lkind[1] == 'f' or rkind[0] == 'f':
  180. (fd, temp) = tempfile.mkstemp()
  181. os.close(fd)
  182. garbage.append(temp)
  183. list[i-1][-1] = list[i][0] = temp
  184. #
  185. for item in list:
  186. [inf, cmd, kind, outf] = item
  187. if kind[1] == 'f':
  188. cmd = 'OUT=' + quote(outf) + '; ' + cmd
  189. if kind[0] == 'f':
  190. cmd = 'IN=' + quote(inf) + '; ' + cmd
  191. if kind[0] == '-' and inf:
  192. cmd = cmd + ' <' + quote(inf)
  193. if kind[1] == '-' and outf:
  194. cmd = cmd + ' >' + quote(outf)
  195. item[1] = cmd
  196. #
  197. cmdlist = list[0][1]
  198. for item in list[1:]:
  199. [cmd, kind] = item[1:3]
  200. if item[0] == '':
  201. if 'f' in kind:
  202. cmd = '{ ' + cmd + '; }'
  203. cmdlist = cmdlist + ' |\n' + cmd
  204. else:
  205. cmdlist = cmdlist + '\n' + cmd
  206. #
  207. if garbage:
  208. rmcmd = 'rm -f'
  209. for file in garbage:
  210. rmcmd = rmcmd + ' ' + quote(file)
  211. trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
  212. cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
  213. #
  214. return cmdlist