|
@@ -12,37 +12,27 @@ import errno
|
|
import io
|
|
import io
|
|
import logging
|
|
import logging
|
|
import os
|
|
import os
|
|
-
|
|
|
|
-try:
|
|
|
|
- import Queue
|
|
|
|
-except ImportError: # For Py3 compatibility
|
|
|
|
- import queue as Queue
|
|
|
|
-
|
|
|
|
import subprocess
|
|
import subprocess
|
|
import sys
|
|
import sys
|
|
-import time
|
|
|
|
import threading
|
|
import threading
|
|
|
|
|
|
# Cache the string-escape codec to ensure subprocess can find it later.
|
|
# Cache the string-escape codec to ensure subprocess can find it later.
|
|
# See crbug.com/912292#c2 for context.
|
|
# See crbug.com/912292#c2 for context.
|
|
if sys.version_info.major == 2:
|
|
if sys.version_info.major == 2:
|
|
|
|
+ import Queue
|
|
codecs.lookup('string-escape')
|
|
codecs.lookup('string-escape')
|
|
-
|
|
|
|
-# TODO(crbug.com/953884): Remove this when python3 migration is done.
|
|
|
|
-try:
|
|
|
|
- basestring
|
|
|
|
-except NameError:
|
|
|
|
|
|
+else:
|
|
|
|
+ import queue as Queue
|
|
# pylint: disable=redefined-builtin
|
|
# pylint: disable=redefined-builtin
|
|
- basestring = str
|
|
|
|
|
|
+ basestring = (str, bytes)
|
|
|
|
|
|
|
|
|
|
# Constants forwarded from subprocess.
|
|
# Constants forwarded from subprocess.
|
|
PIPE = subprocess.PIPE
|
|
PIPE = subprocess.PIPE
|
|
STDOUT = subprocess.STDOUT
|
|
STDOUT = subprocess.STDOUT
|
|
# Sends stdout or stderr to os.devnull.
|
|
# Sends stdout or stderr to os.devnull.
|
|
-VOID = object()
|
|
|
|
-# Error code when a process was killed because it timed out.
|
|
|
|
-TIMED_OUT = -2001
|
|
|
|
|
|
+VOID = open(os.devnull, 'w')
|
|
|
|
+VOID_INPUT = open(os.devnull, 'r')
|
|
|
|
|
|
|
|
|
|
class CalledProcessError(subprocess.CalledProcessError):
|
|
class CalledProcessError(subprocess.CalledProcessError):
|
|
@@ -106,42 +96,6 @@ def get_english_env(env):
|
|
return env
|
|
return env
|
|
|
|
|
|
|
|
|
|
-class NagTimer(object):
|
|
|
|
- """
|
|
|
|
- Triggers a callback when a time interval passes without an event being fired.
|
|
|
|
-
|
|
|
|
- For example, the event could be receiving terminal output from a subprocess;
|
|
|
|
- and the callback could print a warning to stderr that the subprocess appeared
|
|
|
|
- to be hung.
|
|
|
|
- """
|
|
|
|
- def __init__(self, interval, cb):
|
|
|
|
- self.interval = interval
|
|
|
|
- self.cb = cb
|
|
|
|
- self.timer = threading.Timer(self.interval, self.fn)
|
|
|
|
- self.last_output = self.previous_last_output = 0
|
|
|
|
-
|
|
|
|
- def start(self):
|
|
|
|
- self.last_output = self.previous_last_output = time.time()
|
|
|
|
- self.timer.start()
|
|
|
|
-
|
|
|
|
- def event(self):
|
|
|
|
- self.last_output = time.time()
|
|
|
|
-
|
|
|
|
- def fn(self):
|
|
|
|
- now = time.time()
|
|
|
|
- if self.last_output == self.previous_last_output:
|
|
|
|
- self.cb(now - self.previous_last_output)
|
|
|
|
- # Use 0.1 fudge factor, just in case
|
|
|
|
- # (self.last_output - now) is very close to zero.
|
|
|
|
- sleep_time = (self.last_output - now - 0.1) % self.interval
|
|
|
|
- self.previous_last_output = self.last_output
|
|
|
|
- self.timer = threading.Timer(sleep_time + 0.1, self.fn)
|
|
|
|
- self.timer.start()
|
|
|
|
-
|
|
|
|
- def cancel(self):
|
|
|
|
- self.timer.cancel()
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class Popen(subprocess.Popen):
|
|
class Popen(subprocess.Popen):
|
|
"""Wraps subprocess.Popen() with various workarounds.
|
|
"""Wraps subprocess.Popen() with various workarounds.
|
|
|
|
|
|
@@ -184,33 +138,6 @@ class Popen(subprocess.Popen):
|
|
tmp_str += '; cwd=%s' % kwargs['cwd']
|
|
tmp_str += '; cwd=%s' % kwargs['cwd']
|
|
logging.debug(tmp_str)
|
|
logging.debug(tmp_str)
|
|
|
|
|
|
- self.stdout_cb = None
|
|
|
|
- self.stderr_cb = None
|
|
|
|
- self.stdin_is_void = False
|
|
|
|
- self.stdout_is_void = False
|
|
|
|
- self.stderr_is_void = False
|
|
|
|
- self.cmd_str = tmp_str
|
|
|
|
-
|
|
|
|
- if kwargs.get('stdin') is VOID:
|
|
|
|
- kwargs['stdin'] = open(os.devnull, 'r')
|
|
|
|
- self.stdin_is_void = True
|
|
|
|
-
|
|
|
|
- for stream in ('stdout', 'stderr'):
|
|
|
|
- if kwargs.get(stream) in (VOID, os.devnull):
|
|
|
|
- kwargs[stream] = open(os.devnull, 'w')
|
|
|
|
- setattr(self, stream + '_is_void', True)
|
|
|
|
- if callable(kwargs.get(stream)):
|
|
|
|
- setattr(self, stream + '_cb', kwargs[stream])
|
|
|
|
- kwargs[stream] = PIPE
|
|
|
|
-
|
|
|
|
- self.start = time.time()
|
|
|
|
- self.timeout = None
|
|
|
|
- self.nag_timer = None
|
|
|
|
- self.nag_max = None
|
|
|
|
- self.shell = kwargs.get('shell', None)
|
|
|
|
- # Silence pylint on MacOSX
|
|
|
|
- self.returncode = None
|
|
|
|
-
|
|
|
|
try:
|
|
try:
|
|
with self.popen_lock:
|
|
with self.popen_lock:
|
|
super(Popen, self).__init__(args, **kwargs)
|
|
super(Popen, self).__init__(args, **kwargs)
|
|
@@ -231,205 +158,25 @@ class Popen(subprocess.Popen):
|
|
'Check that %s or %s exist and have execution permission.'
|
|
'Check that %s or %s exist and have execution permission.'
|
|
% (str(e), kwargs.get('cwd'), args[0]))
|
|
% (str(e), kwargs.get('cwd'), args[0]))
|
|
|
|
|
|
- def _tee_threads(self, input): # pylint: disable=redefined-builtin
|
|
|
|
- """Does I/O for a process's pipes using threads.
|
|
|
|
-
|
|
|
|
- It's the simplest and slowest implementation. Expect very slow behavior.
|
|
|
|
-
|
|
|
|
- If there is a callback and it doesn't keep up with the calls, the timeout
|
|
|
|
- effectiveness will be delayed accordingly.
|
|
|
|
- """
|
|
|
|
- # Queue of either of <threadname> when done or (<threadname>, data). In
|
|
|
|
- # theory we would like to limit to ~64kb items to not cause large memory
|
|
|
|
- # usage when the callback blocks. It is not done because it slows down
|
|
|
|
- # processing on OSX10.6 by a factor of 2x, making it even slower than
|
|
|
|
- # Windows! Revisit this decision if it becomes a problem, e.g. crash
|
|
|
|
- # because of memory exhaustion.
|
|
|
|
- queue = Queue.Queue()
|
|
|
|
- done = threading.Event()
|
|
|
|
- nag = None
|
|
|
|
-
|
|
|
|
- def write_stdin():
|
|
|
|
- try:
|
|
|
|
- stdin_io = io.BytesIO(input)
|
|
|
|
- while True:
|
|
|
|
- data = stdin_io.read(1024)
|
|
|
|
- if data:
|
|
|
|
- self.stdin.write(data)
|
|
|
|
- else:
|
|
|
|
- self.stdin.close()
|
|
|
|
- break
|
|
|
|
- finally:
|
|
|
|
- queue.put('stdin')
|
|
|
|
-
|
|
|
|
- def _queue_pipe_read(pipe, name):
|
|
|
|
- """Queues characters read from a pipe into a queue."""
|
|
|
|
- try:
|
|
|
|
- while True:
|
|
|
|
- data = pipe.read(1)
|
|
|
|
- if not data:
|
|
|
|
- break
|
|
|
|
- if nag:
|
|
|
|
- nag.event()
|
|
|
|
- queue.put((name, data))
|
|
|
|
- finally:
|
|
|
|
- queue.put(name)
|
|
|
|
-
|
|
|
|
- def timeout_fn():
|
|
|
|
- try:
|
|
|
|
- done.wait(self.timeout)
|
|
|
|
- finally:
|
|
|
|
- queue.put('timeout')
|
|
|
|
-
|
|
|
|
- def wait_fn():
|
|
|
|
- try:
|
|
|
|
- self.wait()
|
|
|
|
- finally:
|
|
|
|
- queue.put('wait')
|
|
|
|
-
|
|
|
|
- # Starts up to 5 threads:
|
|
|
|
- # Wait for the process to quit
|
|
|
|
- # Read stdout
|
|
|
|
- # Read stderr
|
|
|
|
- # Write stdin
|
|
|
|
- # Timeout
|
|
|
|
- threads = {
|
|
|
|
- 'wait': threading.Thread(target=wait_fn),
|
|
|
|
- }
|
|
|
|
- if self.timeout is not None:
|
|
|
|
- threads['timeout'] = threading.Thread(target=timeout_fn)
|
|
|
|
- if self.stdout_cb:
|
|
|
|
- threads['stdout'] = threading.Thread(
|
|
|
|
- target=_queue_pipe_read, args=(self.stdout, 'stdout'))
|
|
|
|
- if self.stderr_cb:
|
|
|
|
- threads['stderr'] = threading.Thread(
|
|
|
|
- target=_queue_pipe_read, args=(self.stderr, 'stderr'))
|
|
|
|
- if input:
|
|
|
|
- threads['stdin'] = threading.Thread(target=write_stdin)
|
|
|
|
- elif self.stdin:
|
|
|
|
- # Pipe but no input, make sure it's closed.
|
|
|
|
- self.stdin.close()
|
|
|
|
- for t in threads.itervalues():
|
|
|
|
- t.start()
|
|
|
|
-
|
|
|
|
- if self.nag_timer:
|
|
|
|
- def _nag_cb(elapsed):
|
|
|
|
- logging.warn(' No output for %.0f seconds from command:' % elapsed)
|
|
|
|
- logging.warn(' %s' % self.cmd_str)
|
|
|
|
- if (self.nag_max and
|
|
|
|
- int('%.0f' % (elapsed / self.nag_timer)) >= self.nag_max):
|
|
|
|
- queue.put('timeout')
|
|
|
|
- done.set() # Must do this so that timeout thread stops waiting.
|
|
|
|
- nag = NagTimer(self.nag_timer, _nag_cb)
|
|
|
|
- nag.start()
|
|
|
|
-
|
|
|
|
- timed_out = False
|
|
|
|
- try:
|
|
|
|
- # This thread needs to be optimized for speed.
|
|
|
|
- while threads:
|
|
|
|
- item = queue.get()
|
|
|
|
- if item[0] == 'stdout':
|
|
|
|
- self.stdout_cb(item[1])
|
|
|
|
- elif item[0] == 'stderr':
|
|
|
|
- self.stderr_cb(item[1])
|
|
|
|
- else:
|
|
|
|
- # A thread terminated.
|
|
|
|
- if item in threads:
|
|
|
|
- threads[item].join()
|
|
|
|
- del threads[item]
|
|
|
|
- if item == 'wait':
|
|
|
|
- # Terminate the timeout thread if necessary.
|
|
|
|
- done.set()
|
|
|
|
- elif item == 'timeout' and not timed_out and self.poll() is None:
|
|
|
|
- logging.debug('Timed out after %.0fs: killing' % (
|
|
|
|
- time.time() - self.start))
|
|
|
|
- self.kill()
|
|
|
|
- timed_out = True
|
|
|
|
- finally:
|
|
|
|
- # Stop the threads.
|
|
|
|
- done.set()
|
|
|
|
- if nag:
|
|
|
|
- nag.cancel()
|
|
|
|
- if 'wait' in threads:
|
|
|
|
- # Accelerate things, otherwise it would hang until the child process is
|
|
|
|
- # done.
|
|
|
|
- logging.debug('Killing child because of an exception')
|
|
|
|
- self.kill()
|
|
|
|
- # Join threads.
|
|
|
|
- for thread in threads.itervalues():
|
|
|
|
- thread.join()
|
|
|
|
- if timed_out:
|
|
|
|
- self.returncode = TIMED_OUT
|
|
|
|
-
|
|
|
|
- # pylint: disable=arguments-differ,W0622
|
|
|
|
- def communicate(self, input=None, timeout=None, nag_timer=None,
|
|
|
|
- nag_max=None):
|
|
|
|
- """Adds timeout and callbacks support.
|
|
|
|
-
|
|
|
|
- Returns (stdout, stderr) like subprocess.Popen().communicate().
|
|
|
|
-
|
|
|
|
- - The process will be killed after |timeout| seconds and returncode set to
|
|
|
|
- TIMED_OUT.
|
|
|
|
- - If the subprocess runs for |nag_timer| seconds without producing terminal
|
|
|
|
- output, print a warning to stderr.
|
|
|
|
- """
|
|
|
|
- self.timeout = timeout
|
|
|
|
- self.nag_timer = nag_timer
|
|
|
|
- self.nag_max = nag_max
|
|
|
|
- if (not self.timeout and not self.nag_timer and
|
|
|
|
- not self.stdout_cb and not self.stderr_cb):
|
|
|
|
- return super(Popen, self).communicate(input)
|
|
|
|
-
|
|
|
|
- if self.timeout and self.shell:
|
|
|
|
- raise TypeError(
|
|
|
|
- 'Using timeout and shell simultaneously will cause a process leak '
|
|
|
|
- 'since the shell will be killed instead of the child process.')
|
|
|
|
-
|
|
|
|
- stdout = None
|
|
|
|
- stderr = None
|
|
|
|
- # Convert to a lambda to workaround python's deadlock.
|
|
|
|
- # http://docs.python.org/library/subprocess.html#subprocess.Popen.wait
|
|
|
|
- # When the pipe fills up, it would deadlock this process.
|
|
|
|
- if self.stdout and not self.stdout_cb and not self.stdout_is_void:
|
|
|
|
- stdout = []
|
|
|
|
- self.stdout_cb = stdout.append
|
|
|
|
- if self.stderr and not self.stderr_cb and not self.stderr_is_void:
|
|
|
|
- stderr = []
|
|
|
|
- self.stderr_cb = stderr.append
|
|
|
|
- self._tee_threads(input)
|
|
|
|
- if stdout is not None:
|
|
|
|
- stdout = ''.join(stdout)
|
|
|
|
- if stderr is not None:
|
|
|
|
- stderr = ''.join(stderr)
|
|
|
|
- return (stdout, stderr)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def communicate(args, timeout=None, nag_timer=None, nag_max=None, **kwargs):
|
|
|
|
- """Wraps subprocess.Popen().communicate() and add timeout support.
|
|
|
|
|
|
+
|
|
|
|
+def communicate(args, **kwargs):
|
|
|
|
+ """Wraps subprocess.Popen().communicate().
|
|
|
|
|
|
Returns ((stdout, stderr), returncode).
|
|
Returns ((stdout, stderr), returncode).
|
|
|
|
|
|
- - The process will be killed after |timeout| seconds and returncode set to
|
|
|
|
- TIMED_OUT.
|
|
|
|
- If the subprocess runs for |nag_timer| seconds without producing terminal
|
|
- If the subprocess runs for |nag_timer| seconds without producing terminal
|
|
output, print a warning to stderr.
|
|
output, print a warning to stderr.
|
|
- Automatically passes stdin content as input so do not specify stdin=PIPE.
|
|
- Automatically passes stdin content as input so do not specify stdin=PIPE.
|
|
"""
|
|
"""
|
|
- stdin = kwargs.pop('stdin', None)
|
|
|
|
- if stdin is not None:
|
|
|
|
- if isinstance(stdin, basestring):
|
|
|
|
- # When stdin is passed as an argument, use it as the actual input data and
|
|
|
|
- # set the Popen() parameter accordingly.
|
|
|
|
- kwargs['stdin'] = PIPE
|
|
|
|
- else:
|
|
|
|
- kwargs['stdin'] = stdin
|
|
|
|
- stdin = None
|
|
|
|
|
|
+ stdin = None
|
|
|
|
+ # When stdin is passed as an argument, use it as the actual input data and
|
|
|
|
+ # set the Popen() parameter accordingly.
|
|
|
|
+ if 'stdin' in kwargs and isinstance(kwargs['stdin'], basestring):
|
|
|
|
+ stdin = kwargs['stdin']
|
|
|
|
+ kwargs['stdin'] = PIPE
|
|
|
|
|
|
proc = Popen(args, **kwargs)
|
|
proc = Popen(args, **kwargs)
|
|
- if stdin:
|
|
|
|
- return proc.communicate(stdin, timeout, nag_timer), proc.returncode
|
|
|
|
- else:
|
|
|
|
- return proc.communicate(None, timeout, nag_timer), proc.returncode
|
|
|
|
|
|
+ return proc.communicate(stdin), proc.returncode
|
|
|
|
|
|
|
|
|
|
def call(args, **kwargs):
|
|
def call(args, **kwargs):
|
|
@@ -472,7 +219,7 @@ def capture(args, **kwargs):
|
|
- Discards returncode.
|
|
- Discards returncode.
|
|
- Blocks stdin by default if not specified since no output will be visible.
|
|
- Blocks stdin by default if not specified since no output will be visible.
|
|
"""
|
|
"""
|
|
- kwargs.setdefault('stdin', VOID)
|
|
|
|
|
|
+ kwargs.setdefault('stdin', VOID_INPUT)
|
|
|
|
|
|
# Like check_output, deny the caller from using stdout arg.
|
|
# Like check_output, deny the caller from using stdout arg.
|
|
return communicate(args, stdout=PIPE, **kwargs)[0][0]
|
|
return communicate(args, stdout=PIPE, **kwargs)[0][0]
|
|
@@ -488,7 +235,7 @@ def check_output(args, **kwargs):
|
|
- Blocks stdin by default if not specified since no output will be visible.
|
|
- Blocks stdin by default if not specified since no output will be visible.
|
|
- As per doc, "The stdout argument is not allowed as it is used internally."
|
|
- As per doc, "The stdout argument is not allowed as it is used internally."
|
|
"""
|
|
"""
|
|
- kwargs.setdefault('stdin', VOID)
|
|
|
|
|
|
+ kwargs.setdefault('stdin', VOID_INPUT)
|
|
if 'stdout' in kwargs:
|
|
if 'stdout' in kwargs:
|
|
raise ValueError('stdout argument not allowed, it would be overridden.')
|
|
raise ValueError('stdout argument not allowed, it would be overridden.')
|
|
return check_call_out(args, stdout=PIPE, **kwargs)[0]
|
|
return check_call_out(args, stdout=PIPE, **kwargs)[0]
|