summaryrefslogtreecommitdiff
path: root/third_party/android_testrunner/run_command.py
blob: 5c82c61210de2e889e4dabf1868e09a323e6d5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python2.4
#
#
# Copyright 2007, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# System imports
import os
import signal
import subprocess
import tempfile
import threading
import time
import sys

# local imports
import errors
import logger

_abort_on_error = False

def SetAbortOnError(abort=True):
  """Sets behavior of RunCommand to throw AbortError if command process returns
  a negative error code"""
  global _abort_on_error
  _abort_on_error = abort

def RunCommand(cmd, timeout_time=None, retry_count=3, return_output=True,
               stdin_input=None):
  """Spawn and retry a subprocess to run the given shell command.

  Args:
    cmd: shell command to run
    timeout_time: time in seconds to wait for command to run before aborting.
    retry_count: number of times to retry command
    return_output: if True return output of command as string. Otherwise,
      direct output of command to stdout.
    stdin_input: data to feed to stdin
  Returns:
    output of command
  """
  result = None
  while True:
    try:
      result = RunOnce(cmd, timeout_time=timeout_time,
                       return_output=return_output, stdin_input=stdin_input)
    except errors.WaitForResponseTimedOutError:
      if retry_count == 0:
        raise
      retry_count -= 1
      logger.Log("No response for %s, retrying" % cmd)
    else:
      # Success
      return result

def RunOnce(cmd, timeout_time=None, return_output=True, stdin_input=None):
  """Spawns a subprocess to run the given shell command.

  Args:
    cmd: shell command to run
    timeout_time: time in seconds to wait for command to run before aborting.
    return_output: if True return output of command as string. Otherwise,
      direct output of command to stdout.
    stdin_input: data to feed to stdin
  Returns:
    output of command
  Raises:
    errors.WaitForResponseTimedOutError if command did not complete within
      timeout_time seconds.
    errors.AbortError is command returned error code and SetAbortOnError is on.
  """
  start_time = time.time()
  so = []
  global _abort_on_error, error_occurred
  error_occurred = False

  if return_output:
    output_dest = tempfile.TemporaryFile(bufsize=0)
  else:
    # None means direct to stdout
    output_dest = None
  if stdin_input:
    stdin_dest = subprocess.PIPE
  else:
    stdin_dest = None
  stderr_dest = subprocess.STDOUT
  if os.environ.get('ADB_TRACE'):
    stderr_dest = sys.stdout
  pipe = subprocess.Popen(
      cmd,
      executable='/bin/bash',
      stdin=stdin_dest,
      stdout=output_dest,
      stderr=stderr_dest,
      shell=True, close_fds=True,
      preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL))

  def Run():
    global error_occurred
    try:
      pipe.communicate(input=stdin_input)
      output = None
      if return_output:
        output_dest.seek(0)
        output = output_dest.read()
        output_dest.close()
      if output is not None and len(output) > 0:
        so.append(output)
    except OSError, e:
      logger.SilentLog("failed to retrieve stdout from: %s" % cmd)
      logger.Log(e)
      so.append("ERROR")
      error_occurred = True
    if pipe.returncode:
      logger.SilentLog("Error: %s returned %d error code" %(cmd,
          pipe.returncode))
      error_occurred = True

  t = threading.Thread(target=Run)
  t.start()
  t.join(timeout_time)
  if t.isAlive():
    try:
      pipe.kill()
    except OSError:
      # Can't kill a dead process.
      pass
    finally:
      logger.SilentLog("about to raise a timeout for: %s" % cmd)
      raise errors.WaitForResponseTimedOutError

  output = "".join(so)
  if _abort_on_error and error_occurred:
    raise errors.AbortError(msg=output)

  return "".join(so)


def RunHostCommand(binary, valgrind=False):
  """Run a command on the host (opt using valgrind).

  Runs the host binary and returns the exit code.
  If successfull, the output (stdout and stderr) are discarded,
  but printed in case of error.
  The command can be run under valgrind in which case all the
  output are always discarded.

  Args:
    binary: full path of the file to be run.
    valgrind: If True the command will be run under valgrind.

  Returns:
    The command exit code (int)
  """
  if not valgrind:
    subproc = subprocess.Popen(binary, stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    subproc.wait()
    if subproc.returncode != 0:         # In case of error print the output
      print subproc.communicate()[0]
    return subproc.returncode
  else:
    # Need the full path to valgrind to avoid other versions on the system.
    subproc = subprocess.Popen(["/usr/bin/valgrind", "--tool=memcheck",
                                "--leak-check=yes", "-q", binary],
                               stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    # Cannot rely on the retcode of valgrind. Instead look for an empty output.
    valgrind_out = subproc.communicate()[0].strip()
    if valgrind_out:
      print valgrind_out
      return 1
    else:
      return 0


def HasValgrind():
  """Check that /usr/bin/valgrind exists.

  We look for the fullpath to avoid picking up 'alternative' valgrind
  on the system.

  Returns:
    True if a system valgrind was found.
  """
  return os.path.exists("/usr/bin/valgrind")