cros_subprocess.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # Copyright (c) 2012 The Chromium OS Authors.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. #
  5. # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
  6. # Licensed to PSF under a Contributor Agreement.
  7. # See http://www.python.org/2.4/license for licensing details.
  8. """Subprocress execution
  9. This module holds a subclass of subprocess.Popen with our own required
  10. features, mainly that we get access to the subprocess output while it
  11. is running rather than just at the end. This makes it easiler to show
  12. progress information and filter output in real time.
  13. """
  14. import errno
  15. import os
  16. import pty
  17. import select
  18. import subprocess
  19. import sys
  20. import unittest
  21. # Import these here so the caller does not need to import subprocess also.
  22. PIPE = subprocess.PIPE
  23. STDOUT = subprocess.STDOUT
  24. PIPE_PTY = -3 # Pipe output through a pty
  25. stay_alive = True
  26. class Popen(subprocess.Popen):
  27. """Like subprocess.Popen with ptys and incremental output
  28. This class deals with running a child process and filtering its output on
  29. both stdout and stderr while it is running. We do this so we can monitor
  30. progress, and possibly relay the output to the user if requested.
  31. The class is similar to subprocess.Popen, the equivalent is something like:
  32. Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  33. But this class has many fewer features, and two enhancement:
  34. 1. Rather than getting the output data only at the end, this class sends it
  35. to a provided operation as it arrives.
  36. 2. We use pseudo terminals so that the child will hopefully flush its output
  37. to us as soon as it is produced, rather than waiting for the end of a
  38. line.
  39. Use CommunicateFilter() to handle output from the subprocess.
  40. """
  41. def __init__(self, args, stdin=None, stdout=PIPE_PTY, stderr=PIPE_PTY,
  42. shell=False, cwd=None, env=None, **kwargs):
  43. """Cut-down constructor
  44. Args:
  45. args: Program and arguments for subprocess to execute.
  46. stdin: See subprocess.Popen()
  47. stdout: See subprocess.Popen(), except that we support the sentinel
  48. value of cros_subprocess.PIPE_PTY.
  49. stderr: See subprocess.Popen(), except that we support the sentinel
  50. value of cros_subprocess.PIPE_PTY.
  51. shell: See subprocess.Popen()
  52. cwd: Working directory to change to for subprocess, or None if none.
  53. env: Environment to use for this subprocess, or None to inherit parent.
  54. kwargs: No other arguments are supported at the moment. Passing other
  55. arguments will cause a ValueError to be raised.
  56. """
  57. stdout_pty = None
  58. stderr_pty = None
  59. if stdout == PIPE_PTY:
  60. stdout_pty = pty.openpty()
  61. stdout = os.fdopen(stdout_pty[1])
  62. if stderr == PIPE_PTY:
  63. stderr_pty = pty.openpty()
  64. stderr = os.fdopen(stderr_pty[1])
  65. super(Popen, self).__init__(args, stdin=stdin,
  66. stdout=stdout, stderr=stderr, shell=shell, cwd=cwd, env=env,
  67. **kwargs)
  68. # If we're on a PTY, we passed the slave half of the PTY to the subprocess.
  69. # We want to use the master half on our end from now on. Setting this here
  70. # does make some assumptions about the implementation of subprocess, but
  71. # those assumptions are pretty minor.
  72. # Note that if stderr is STDOUT, then self.stderr will be set to None by
  73. # this constructor.
  74. if stdout_pty is not None:
  75. self.stdout = os.fdopen(stdout_pty[0])
  76. if stderr_pty is not None:
  77. self.stderr = os.fdopen(stderr_pty[0])
  78. # Insist that unit tests exist for other arguments we don't support.
  79. if kwargs:
  80. raise ValueError("Unit tests do not test extra args - please add tests")
  81. def CommunicateFilter(self, output):
  82. """Interact with process: Read data from stdout and stderr.
  83. This method runs until end-of-file is reached, then waits for the
  84. subprocess to terminate.
  85. The output function is sent all output from the subprocess and must be
  86. defined like this:
  87. def Output([self,] stream, data)
  88. Args:
  89. stream: the stream the output was received on, which will be
  90. sys.stdout or sys.stderr.
  91. data: a string containing the data
  92. Note: The data read is buffered in memory, so do not use this
  93. method if the data size is large or unlimited.
  94. Args:
  95. output: Function to call with each fragment of output.
  96. Returns:
  97. A tuple (stdout, stderr, combined) which is the data received on
  98. stdout, stderr and the combined data (interleaved stdout and stderr).
  99. Note that the interleaved output will only be sensible if you have
  100. set both stdout and stderr to PIPE or PIPE_PTY. Even then it depends on
  101. the timing of the output in the subprocess. If a subprocess flips
  102. between stdout and stderr quickly in succession, by the time we come to
  103. read the output from each we may see several lines in each, and will read
  104. all the stdout lines, then all the stderr lines. So the interleaving
  105. may not be correct. In this case you might want to pass
  106. stderr=cros_subprocess.STDOUT to the constructor.
  107. This feature is still useful for subprocesses where stderr is
  108. rarely used and indicates an error.
  109. Note also that if you set stderr to STDOUT, then stderr will be empty
  110. and the combined output will just be the same as stdout.
  111. """
  112. read_set = []
  113. write_set = []
  114. stdout = None # Return
  115. stderr = None # Return
  116. if self.stdin:
  117. # Flush stdio buffer. This might block, if the user has
  118. # been writing to .stdin in an uncontrolled fashion.
  119. self.stdin.flush()
  120. if input:
  121. write_set.append(self.stdin)
  122. else:
  123. self.stdin.close()
  124. if self.stdout:
  125. read_set.append(self.stdout)
  126. stdout = []
  127. if self.stderr and self.stderr != self.stdout:
  128. read_set.append(self.stderr)
  129. stderr = []
  130. combined = []
  131. input_offset = 0
  132. while read_set or write_set:
  133. try:
  134. rlist, wlist, _ = select.select(read_set, write_set, [], 0.2)
  135. except select.error as e:
  136. if e.args[0] == errno.EINTR:
  137. continue
  138. raise
  139. if not stay_alive:
  140. self.terminate()
  141. if self.stdin in wlist:
  142. # When select has indicated that the file is writable,
  143. # we can write up to PIPE_BUF bytes without risk
  144. # blocking. POSIX defines PIPE_BUF >= 512
  145. chunk = input[input_offset : input_offset + 512]
  146. bytes_written = os.write(self.stdin.fileno(), chunk)
  147. input_offset += bytes_written
  148. if input_offset >= len(input):
  149. self.stdin.close()
  150. write_set.remove(self.stdin)
  151. if self.stdout in rlist:
  152. data = ""
  153. # We will get an error on read if the pty is closed
  154. try:
  155. data = os.read(self.stdout.fileno(), 1024)
  156. except OSError:
  157. pass
  158. if data == "":
  159. self.stdout.close()
  160. read_set.remove(self.stdout)
  161. else:
  162. stdout.append(data)
  163. combined.append(data)
  164. if output:
  165. output(sys.stdout, data)
  166. if self.stderr in rlist:
  167. data = ""
  168. # We will get an error on read if the pty is closed
  169. try:
  170. data = os.read(self.stderr.fileno(), 1024)
  171. except OSError:
  172. pass
  173. if data == "":
  174. self.stderr.close()
  175. read_set.remove(self.stderr)
  176. else:
  177. stderr.append(data)
  178. combined.append(data)
  179. if output:
  180. output(sys.stderr, data)
  181. # All data exchanged. Translate lists into strings.
  182. if stdout is not None:
  183. stdout = ''.join(stdout)
  184. else:
  185. stdout = ''
  186. if stderr is not None:
  187. stderr = ''.join(stderr)
  188. else:
  189. stderr = ''
  190. combined = ''.join(combined)
  191. # Translate newlines, if requested. We cannot let the file
  192. # object do the translation: It is based on stdio, which is
  193. # impossible to combine with select (unless forcing no
  194. # buffering).
  195. if self.universal_newlines and hasattr(file, 'newlines'):
  196. if stdout:
  197. stdout = self._translate_newlines(stdout)
  198. if stderr:
  199. stderr = self._translate_newlines(stderr)
  200. self.wait()
  201. return (stdout, stderr, combined)
  202. # Just being a unittest.TestCase gives us 14 public methods. Unless we
  203. # disable this, we can only have 6 tests in a TestCase. That's not enough.
  204. #
  205. # pylint: disable=R0904
  206. class TestSubprocess(unittest.TestCase):
  207. """Our simple unit test for this module"""
  208. class MyOperation:
  209. """Provides a operation that we can pass to Popen"""
  210. def __init__(self, input_to_send=None):
  211. """Constructor to set up the operation and possible input.
  212. Args:
  213. input_to_send: a text string to send when we first get input. We will
  214. add \r\n to the string.
  215. """
  216. self.stdout_data = ''
  217. self.stderr_data = ''
  218. self.combined_data = ''
  219. self.stdin_pipe = None
  220. self._input_to_send = input_to_send
  221. if input_to_send:
  222. pipe = os.pipe()
  223. self.stdin_read_pipe = pipe[0]
  224. self._stdin_write_pipe = os.fdopen(pipe[1], 'w')
  225. def Output(self, stream, data):
  226. """Output handler for Popen. Stores the data for later comparison"""
  227. if stream == sys.stdout:
  228. self.stdout_data += data
  229. if stream == sys.stderr:
  230. self.stderr_data += data
  231. self.combined_data += data
  232. # Output the input string if we have one.
  233. if self._input_to_send:
  234. self._stdin_write_pipe.write(self._input_to_send + '\r\n')
  235. self._stdin_write_pipe.flush()
  236. def _BasicCheck(self, plist, oper):
  237. """Basic checks that the output looks sane."""
  238. self.assertEqual(plist[0], oper.stdout_data)
  239. self.assertEqual(plist[1], oper.stderr_data)
  240. self.assertEqual(plist[2], oper.combined_data)
  241. # The total length of stdout and stderr should equal the combined length
  242. self.assertEqual(len(plist[0]) + len(plist[1]), len(plist[2]))
  243. def test_simple(self):
  244. """Simple redirection: Get process list"""
  245. oper = TestSubprocess.MyOperation()
  246. plist = Popen(['ps']).CommunicateFilter(oper.Output)
  247. self._BasicCheck(plist, oper)
  248. def test_stderr(self):
  249. """Check stdout and stderr"""
  250. oper = TestSubprocess.MyOperation()
  251. cmd = 'echo fred >/dev/stderr && false || echo bad'
  252. plist = Popen([cmd], shell=True).CommunicateFilter(oper.Output)
  253. self._BasicCheck(plist, oper)
  254. self.assertEqual(plist [0], 'bad\r\n')
  255. self.assertEqual(plist [1], 'fred\r\n')
  256. def test_shell(self):
  257. """Check with and without shell works"""
  258. oper = TestSubprocess.MyOperation()
  259. cmd = 'echo test >/dev/stderr'
  260. self.assertRaises(OSError, Popen, [cmd], shell=False)
  261. plist = Popen([cmd], shell=True).CommunicateFilter(oper.Output)
  262. self._BasicCheck(plist, oper)
  263. self.assertEqual(len(plist [0]), 0)
  264. self.assertEqual(plist [1], 'test\r\n')
  265. def test_list_args(self):
  266. """Check with and without shell works using list arguments"""
  267. oper = TestSubprocess.MyOperation()
  268. cmd = ['echo', 'test', '>/dev/stderr']
  269. plist = Popen(cmd, shell=False).CommunicateFilter(oper.Output)
  270. self._BasicCheck(plist, oper)
  271. self.assertEqual(plist [0], ' '.join(cmd[1:]) + '\r\n')
  272. self.assertEqual(len(plist [1]), 0)
  273. oper = TestSubprocess.MyOperation()
  274. # this should be interpreted as 'echo' with the other args dropped
  275. cmd = ['echo', 'test', '>/dev/stderr']
  276. plist = Popen(cmd, shell=True).CommunicateFilter(oper.Output)
  277. self._BasicCheck(plist, oper)
  278. self.assertEqual(plist [0], '\r\n')
  279. def test_cwd(self):
  280. """Check we can change directory"""
  281. for shell in (False, True):
  282. oper = TestSubprocess.MyOperation()
  283. plist = Popen('pwd', shell=shell, cwd='/tmp').CommunicateFilter(oper.Output)
  284. self._BasicCheck(plist, oper)
  285. self.assertEqual(plist [0], '/tmp\r\n')
  286. def test_env(self):
  287. """Check we can change environment"""
  288. for add in (False, True):
  289. oper = TestSubprocess.MyOperation()
  290. env = os.environ
  291. if add:
  292. env ['FRED'] = 'fred'
  293. cmd = 'echo $FRED'
  294. plist = Popen(cmd, shell=True, env=env).CommunicateFilter(oper.Output)
  295. self._BasicCheck(plist, oper)
  296. self.assertEqual(plist [0], add and 'fred\r\n' or '\r\n')
  297. def test_extra_args(self):
  298. """Check we can't add extra arguments"""
  299. self.assertRaises(ValueError, Popen, 'true', close_fds=False)
  300. def test_basic_input(self):
  301. """Check that incremental input works
  302. We set up a subprocess which will prompt for name. When we see this prompt
  303. we send the name as input to the process. It should then print the name
  304. properly to stdout.
  305. """
  306. oper = TestSubprocess.MyOperation('Flash')
  307. prompt = 'What is your name?: '
  308. cmd = 'echo -n "%s"; read name; echo Hello $name' % prompt
  309. plist = Popen([cmd], stdin=oper.stdin_read_pipe,
  310. shell=True).CommunicateFilter(oper.Output)
  311. self._BasicCheck(plist, oper)
  312. self.assertEqual(len(plist [1]), 0)
  313. self.assertEqual(plist [0], prompt + 'Hello Flash\r\r\n')
  314. def test_isatty(self):
  315. """Check that ptys appear as terminals to the subprocess"""
  316. oper = TestSubprocess.MyOperation()
  317. cmd = ('if [ -t %d ]; then echo "terminal %d" >&%d; '
  318. 'else echo "not %d" >&%d; fi;')
  319. both_cmds = ''
  320. for fd in (1, 2):
  321. both_cmds += cmd % (fd, fd, fd, fd, fd)
  322. plist = Popen(both_cmds, shell=True).CommunicateFilter(oper.Output)
  323. self._BasicCheck(plist, oper)
  324. self.assertEqual(plist [0], 'terminal 1\r\n')
  325. self.assertEqual(plist [1], 'terminal 2\r\n')
  326. # Now try with PIPE and make sure it is not a terminal
  327. oper = TestSubprocess.MyOperation()
  328. plist = Popen(both_cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  329. shell=True).CommunicateFilter(oper.Output)
  330. self._BasicCheck(plist, oper)
  331. self.assertEqual(plist [0], 'not 1\n')
  332. self.assertEqual(plist [1], 'not 2\n')
  333. if __name__ == '__main__':
  334. unittest.main()