|
27 | 27 | import threading |
28 | 28 | import time |
29 | 29 | import logging |
| 30 | +import select |
| 31 | +import fcntl |
30 | 32 |
|
31 | 33 | from devlib.utils.misc import InitCheckpoint |
32 | 34 |
|
|
36 | 38 | def _kill_pgid_cmd(pgid, sig, busybox): |
37 | 39 | return '{} kill -{} -{}'.format(busybox, sig.value, pgid) |
38 | 40 |
|
| 41 | +def _popen_communicate(bg, popen, input, timeout): |
| 42 | + try: |
| 43 | + stdout, stderr = popen.communicate(input=input, timeout=timeout) |
| 44 | + except subprocess.TimeoutExpired: |
| 45 | + bg.cancel() |
| 46 | + raise |
| 47 | + |
| 48 | + ret = popen.returncode |
| 49 | + if ret: |
| 50 | + raise subprocess.CalledProcessError( |
| 51 | + ret, |
| 52 | + popen.args, |
| 53 | + stdout, |
| 54 | + stderr, |
| 55 | + ) |
| 56 | + else: |
| 57 | + return (stdout, stderr) |
| 58 | + |
39 | 59 |
|
40 | 60 | class ConnectionBase(InitCheckpoint): |
41 | 61 | """ |
@@ -126,6 +146,21 @@ def wait(self): |
126 | 146 | Block until the background command completes, and return its exit code. |
127 | 147 | """ |
128 | 148 |
|
| 149 | + def communicate(self, input=b'', timeout=None): |
| 150 | + """ |
| 151 | + Block until the background command completes while reading stdout and stderr. |
| 152 | + Return ``tuple(stdout, stderr)``. If the return code is non-zero, |
| 153 | + raises a :exc:`subprocess.CalledProcessError` exception. |
| 154 | + """ |
| 155 | + try: |
| 156 | + return self._communicate(input=input, timeout=timeout) |
| 157 | + finally: |
| 158 | + self.close() |
| 159 | + |
| 160 | + @abstractmethod |
| 161 | + def _communicate(self, input, timeout): |
| 162 | + pass |
| 163 | + |
129 | 164 | @abstractmethod |
130 | 165 | def poll(self): |
131 | 166 | """ |
@@ -214,6 +249,9 @@ def pid(self): |
214 | 249 | def wait(self): |
215 | 250 | return self.popen.wait() |
216 | 251 |
|
| 252 | + def _communicate(self, input, timeout): |
| 253 | + return _popen_communicate(self, self.popen, input, timeout) |
| 254 | + |
217 | 255 | def poll(self): |
218 | 256 | return self.popen.poll() |
219 | 257 |
|
@@ -273,6 +311,85 @@ def wait(self): |
273 | 311 | self.redirect_thread.join() |
274 | 312 | return status |
275 | 313 |
|
| 314 | + def _communicate(self, input, timeout): |
| 315 | + stdout = self._stdout |
| 316 | + stderr = self._stderr |
| 317 | + stdin = self._stdin |
| 318 | + chan = self.chan |
| 319 | + |
| 320 | + # For some reason, file descriptors in the read-list of select() can |
| 321 | + # still end up blocking in .read(), so make the non-blocking to avoid a |
| 322 | + # deadlock. Since _communicate() will consume all input and all output |
| 323 | + # until the command dies, we can do whatever we want with the pipe |
| 324 | + # without affecting external users. |
| 325 | + for s in (stdout, stderr): |
| 326 | + fcntl.fcntl(s.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
| 327 | + |
| 328 | + out = {stdout: [], stderr: []} |
| 329 | + ret = None |
| 330 | + can_send = True |
| 331 | + |
| 332 | + select_timeout = 1 |
| 333 | + if timeout is not None: |
| 334 | + select_timeout = min(select_timeout, 1) |
| 335 | + |
| 336 | + def create_out(): |
| 337 | + return ( |
| 338 | + b''.join(out[stdout]), |
| 339 | + b''.join(out[stderr]) |
| 340 | + ) |
| 341 | + |
| 342 | + start = monotonic() |
| 343 | + |
| 344 | + while ret is None: |
| 345 | + # Even if ret is not None anymore, we need to drain the streams |
| 346 | + ret = self.poll() |
| 347 | + |
| 348 | + if timeout is not None and ret is None and monotonic() - start >= timeout: |
| 349 | + self.cancel() |
| 350 | + _stdout, _stderr = create_out() |
| 351 | + raise subprocess.TimeoutExpired(self.cmd, timeout, _stdout, _stderr) |
| 352 | + |
| 353 | + can_send &= (not chan.closed) & bool(input) |
| 354 | + wlist = [chan] if can_send else [] |
| 355 | + |
| 356 | + if can_send and chan.send_ready(): |
| 357 | + try: |
| 358 | + n = chan.send(input) |
| 359 | + # stdin might have been closed already |
| 360 | + except OSError: |
| 361 | + can_send = False |
| 362 | + chan.shutdown_write() |
| 363 | + else: |
| 364 | + input = input[n:] |
| 365 | + if not input: |
| 366 | + # Send EOF on stdin |
| 367 | + chan.shutdown_write() |
| 368 | + |
| 369 | + rs, ws, _ = select.select( |
| 370 | + [x for x in (stdout, stderr) if not x.closed], |
| 371 | + wlist, |
| 372 | + [], |
| 373 | + select_timeout, |
| 374 | + ) |
| 375 | + |
| 376 | + for r in rs: |
| 377 | + chunk = r.read() |
| 378 | + if chunk: |
| 379 | + out[r].append(chunk) |
| 380 | + |
| 381 | + _stdout, _stderr = create_out() |
| 382 | + |
| 383 | + if ret: |
| 384 | + raise subprocess.CalledProcessError( |
| 385 | + ret, |
| 386 | + self.cmd, |
| 387 | + _stdout, |
| 388 | + _stderr, |
| 389 | + ) |
| 390 | + else: |
| 391 | + return (_stdout, _stderr) |
| 392 | + |
276 | 393 | def poll(self): |
277 | 394 | # Wait for the redirection thread to finish, otherwise we would |
278 | 395 | # indicate the caller that the command is finished and that the streams |
@@ -356,6 +473,10 @@ def pid(self): |
356 | 473 | def wait(self): |
357 | 474 | return self.adb_popen.wait() |
358 | 475 |
|
| 476 | + def _communicate(self, input, timeout): |
| 477 | + return _popen_communicate(self, self.adb_popen, input, timeout) |
| 478 | + |
| 479 | + |
359 | 480 | def poll(self): |
360 | 481 | return self.adb_popen.poll() |
361 | 482 |
|
|
0 commit comments