The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # |
| 4 | # Copyright 2007, The Android Open Source Project |
| 5 | # |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 9 | # |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 11 | # |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 16 | # limitations under the License. |
| 17 | |
| 18 | # System imports |
| 19 | import os |
| 20 | import signal |
| 21 | import subprocess |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 22 | import threading |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 23 | import time |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 24 | |
| 25 | # local imports |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 26 | import errors |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 27 | import logger |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 28 | |
| 29 | _abort_on_error = False |
| 30 | |
| 31 | def SetAbortOnError(abort=True): |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 32 | """Sets behavior of RunCommand to throw AbortError if command process returns |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 33 | a negative error code""" |
| 34 | global _abort_on_error |
| 35 | _abort_on_error = abort |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 36 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 37 | def RunCommand(cmd, timeout_time=None, retry_count=3, return_output=True): |
| 38 | """Spawns a subprocess to run the given shell command, and checks for |
| 39 | timeout_time. If return_output is True, the output of the command is returned |
| 40 | as a string. Otherwise, output of command directed to stdout """ |
| 41 | |
| 42 | result = None |
| 43 | while True: |
| 44 | try: |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 45 | result = RunOnce(cmd, timeout_time=timeout_time, |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 46 | return_output=return_output) |
| 47 | except errors.WaitForResponseTimedOutError: |
| 48 | if retry_count == 0: |
| 49 | raise |
| 50 | retry_count -= 1 |
| 51 | logger.Log("No response for %s, retrying" % cmd) |
| 52 | else: |
| 53 | # Success |
| 54 | return result |
| 55 | |
| 56 | def RunOnce(cmd, timeout_time=None, return_output=True): |
| 57 | start_time = time.time() |
| 58 | so = [] |
| 59 | pid = [] |
| 60 | global _abort_on_error |
| 61 | error_occurred = False |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 62 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 63 | def Run(): |
| 64 | if return_output: |
| 65 | output_dest = subprocess.PIPE |
| 66 | else: |
| 67 | # None means direct to stdout |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 68 | output_dest = None |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 69 | pipe = subprocess.Popen( |
| 70 | cmd, |
| 71 | executable='/bin/bash', |
| 72 | stdout=output_dest, |
| 73 | stderr=subprocess.STDOUT, |
| 74 | shell=True) |
| 75 | pid.append(pipe.pid) |
| 76 | try: |
| 77 | output = pipe.communicate()[0] |
| 78 | if output is not None and len(output) > 0: |
| 79 | so.append(output) |
| 80 | except OSError, e: |
| 81 | logger.SilentLog("failed to retrieve stdout from: %s" % cmd) |
| 82 | logger.Log(e) |
| 83 | so.append("ERROR") |
| 84 | error_occurred = True |
| 85 | if pipe.returncode < 0: |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 86 | logger.SilentLog("Error: %s was terminated by signal %d" %(cmd, |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 87 | pipe.returncode)) |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 88 | error_occurred = True |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 89 | |
| 90 | t = threading.Thread(target=Run) |
| 91 | t.start() |
| 92 | |
| 93 | break_loop = False |
| 94 | while not break_loop: |
| 95 | if not t.isAlive(): |
| 96 | break_loop = True |
| 97 | |
| 98 | # Check the timeout |
| 99 | if (not break_loop and timeout_time is not None |
| 100 | and time.time() > start_time + timeout_time): |
| 101 | try: |
| 102 | os.kill(pid[0], signal.SIGKILL) |
| 103 | except OSError: |
| 104 | # process already dead. No action required. |
| 105 | pass |
| 106 | |
| 107 | logger.SilentLog("about to raise a timeout for: %s" % cmd) |
| 108 | raise errors.WaitForResponseTimedOutError |
| 109 | if not break_loop: |
| 110 | time.sleep(0.1) |
| 111 | |
| 112 | t.join() |
| 113 | |
| 114 | if _abort_on_error and error_occurred: |
| 115 | raise errors.AbortError |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 116 | |
The Android Open Source Project | 2b83cbd | 2009-03-05 17:04:45 -0800 | [diff] [blame] | 117 | return "".join(so) |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame^] | 118 | |
| 119 | |
| 120 | def RunHostCommand(binary, valgrind=False): |
| 121 | """Run a command on the host (opt using valgrind). |
| 122 | |
| 123 | Runs the host binary. Does not capture any output but it |
| 124 | returns the exit code. The command can be run under valgrind. |
| 125 | |
| 126 | Args: |
| 127 | binary: basename of the file to be run. It is expected to be under |
| 128 | out/host/linux-x86/bin. |
| 129 | valgrind: If True the command will be run under valgrind. |
| 130 | |
| 131 | Returns: |
| 132 | The command exit code (int) |
| 133 | """ |
| 134 | full_path = os.path.join("out", "host", "linux-x86", "bin", binary) |
| 135 | if not valgrind: |
| 136 | return subprocess.call(full_path) |
| 137 | else: |
| 138 | return subprocess.call(["/usr/bin/valgrind", "-q", full_path]) |