OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 """Brings in Chrome Android's android_commands module, which itself is a |
| 5 thin(ish) wrapper around adb.""" |
4 import os | 6 import os |
5 import sys | 7 import sys |
6 | 8 |
7 """Brings in Chrome Android's android_commands module, which itself is a | |
8 thin(ish) wrapper around adb.""" | |
9 | |
10 # This is currently a thin wrapper around Chrome Android's | 9 # This is currently a thin wrapper around Chrome Android's |
11 # build scripts, located in chrome/build/android. This file exists mainly to | 10 # build scripts, located in chrome/build/android. This file exists mainly to |
12 # deal with locating the module. | 11 # deal with locating the module. |
13 | 12 |
14 # Get build/android scripts into our path. | 13 # Get build/android scripts into our path. |
15 sys.path.append( | 14 sys.path.append( |
16 os.path.abspath( | 15 os.path.abspath( |
17 os.path.join(os.path.dirname(__file__), | 16 os.path.join(os.path.dirname(__file__), |
18 '../../../build/android'))) | 17 '../../../build/android'))) |
19 try: | 18 try: |
20 from pylib import android_commands as real_android_commands | 19 from pylib import ( # pylint: disable=F0401 |
21 from pylib import forwarder | 20 android_commands as real_android_commands) |
22 from pylib import valgrind_tools | 21 from pylib import forwarder # pylint: disable=F0401 |
23 except: | 22 from pylib import valgrind_tools # pylint: disable=F0401 |
| 23 except Exception: |
24 real_android_commands = None | 24 real_android_commands = None |
25 | 25 |
26 def IsAndroidSupported(): | 26 def IsAndroidSupported(): |
27 return real_android_commands != None | 27 return real_android_commands != None |
28 | 28 |
29 def GetAttachedDevices(): | 29 def GetAttachedDevices(): |
30 """Returns a list of attached, online android devices. | 30 """Returns a list of attached, online android devices. |
31 | 31 |
32 If a preferred device has been set with ANDROID_SERIAL, it will be first in | 32 If a preferred device has been set with ANDROID_SERIAL, it will be first in |
33 the returned list.""" | 33 the returned list.""" |
34 return real_android_commands.GetAttachedDevices() | 34 return real_android_commands.GetAttachedDevices() |
35 | 35 |
36 class ADBCommands(object): | 36 class ADBCommands(object): |
37 """A thin wrapper around ADB""" | 37 """A thin wrapper around ADB""" |
38 | 38 |
39 def __init__(self, device): | 39 def __init__(self, device): |
40 self._adb = real_android_commands.AndroidCommands(device) | 40 self._adb = real_android_commands.AndroidCommands(device) |
41 | 41 |
| 42 def Adb(self): |
| 43 return self._adb |
| 44 |
42 def Forward(self, local, remote): | 45 def Forward(self, local, remote): |
43 ret = self._adb._adb.SendCommand('forward %s %s' % (local, remote)) | 46 ret = self._adb.Adb().SendCommand('forward %s %s' % (local, remote)) |
44 assert ret == '' | 47 assert ret == '' |
45 | 48 |
46 def RunShellCommand(self, command, timeout_time=20, log_result=False): | 49 def RunShellCommand(self, command, timeout_time=20, log_result=False): |
47 """Send a command to the adb shell and return the result. | 50 """Send a command to the adb shell and return the result. |
48 | 51 |
49 Args: | 52 Args: |
50 command: String containing the shell command to send. Must not include | 53 command: String containing the shell command to send. Must not include |
51 the single quotes as we use them to escape the whole command. | 54 the single quotes as we use them to escape the whole command. |
52 timeout_time: Number of seconds to wait for command to respond before | 55 timeout_time: Number of seconds to wait for command to respond before |
53 retrying, used by AdbInterface.SendShellCommand. | 56 retrying, used by AdbInterface.SendShellCommand. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 def Pull(self, remote, local): | 114 def Pull(self, remote, local): |
112 return self._adb.Adb().Pull(remote, local) | 115 return self._adb.Adb().Pull(remote, local) |
113 | 116 |
114 def FileExistsOnDevice(self, file_name): | 117 def FileExistsOnDevice(self, file_name): |
115 return self._adb.FileExistsOnDevice(file_name) | 118 return self._adb.FileExistsOnDevice(file_name) |
116 | 119 |
117 def IsRootEnabled(self): | 120 def IsRootEnabled(self): |
118 return self._adb.IsRootEnabled() | 121 return self._adb.IsRootEnabled() |
119 | 122 |
120 def HasForwarder(adb): | 123 def HasForwarder(adb): |
121 return adb.FileExistsOnDevice(forwarder.Forwarder._FORWARDER_PATH) | 124 return adb.FileExistsOnDevice('/data/local/tmp/forwarder') |
122 | 125 |
123 def HowToInstallForwarder(): | 126 def HowToInstallForwarder(): |
124 return 'adb push out/$BUILD_TYPE/forwarder %s' % ( | 127 return 'adb push out/$BUILD_TYPE/forwarder %s' % ( |
125 forwarder.Forwarder._FORWARDER_PATH) | 128 '/data/local/tmp/forwarder') |
126 | 129 |
127 class Forwarder(object): | 130 class Forwarder(object): |
128 def __init__(self, adb, host_port): | 131 def __init__(self, adb, host_port): |
129 assert HasForwarder(adb) | 132 assert HasForwarder(adb) |
130 | 133 |
131 port_pairs = [(0, host_port), ] | 134 port_pairs = [(0, host_port), ] |
132 tool = valgrind_tools.BaseTool() | 135 tool = valgrind_tools.BaseTool() |
133 | 136 |
134 self._host_port = host_port | 137 self._host_port = host_port |
135 | 138 |
136 # Currently, Forwarder requires that ../out/Debug/forwarder exists, | 139 # Currently, Forwarder requires that ../out/Debug/forwarder exists, |
137 # in case it needs to push it to the device. However, to get to here, | 140 # in case it needs to push it to the device. However, to get to here, |
138 # android_browser_finder has ensured that device HasForwarder, above. | 141 # android_browser_finder has ensured that device HasForwarder, above. |
139 # | 142 # |
140 # Therefore, here, we just need to instantiate the forwarder, no push | 143 # Therefore, here, we just need to instantiate the forwarder, no push |
141 # needed. | 144 # needed. |
142 # | 145 # |
143 # To do this, we monkey patch adb.PushIfNeeded to a noop. | 146 # To do this, we monkey patch adb.PushIfNeeded to a noop. |
144 # | 147 # |
145 # TODO(nduca): Fix build.android.pylib.Forwarder to not need this. | 148 # TODO(nduca): Fix build.android.pylib.Forwarder to not need this. |
146 real_push_if_needed = adb._adb.PushIfNeeded | 149 real_push_if_needed = adb.Adb().PushIfNeeded |
147 def FakePush(_, device_path): | 150 def FakePush(_, device_path): |
148 assert adb.FileExistsOnDevice(device_path) | 151 assert adb.FileExistsOnDevice(device_path) |
149 try: | 152 try: |
150 adb._adb.PushIfNeeded = FakePush | 153 adb.Adb().PushIfNeeded = FakePush |
151 self._forwarder = forwarder.Forwarder( | 154 self._forwarder = forwarder.Forwarder( |
152 adb._adb, port_pairs, | 155 adb.Adb(), port_pairs, |
153 tool, 'localhost', 'unused') | 156 tool, 'localhost', 'unused') |
154 finally: | 157 finally: |
155 adb._adb.PushIfNeeded = real_push_if_needed | 158 adb.Adb().PushIfNeeded = real_push_if_needed |
156 self._device_port = self._forwarder.DevicePortForHostPort(self._host_port) | 159 self._device_port = self._forwarder.DevicePortForHostPort(self._host_port) |
157 | 160 |
158 @property | 161 @property |
159 def url(self): | 162 def url(self): |
160 assert self._forwarder | 163 assert self._forwarder |
161 return 'http://localhost:%i' % self._device_port | 164 return 'http://localhost:%i' % self._device_port |
162 | 165 |
163 def Close(self): | 166 def Close(self): |
164 if self._forwarder: | 167 if self._forwarder: |
165 self._forwarder.Close() | 168 self._forwarder.Close() |
166 self._forwarder = None | 169 self._forwarder = None |
OLD | NEW |