Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2013-2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | |
| 17 | """ |
| 18 | Utility functions for working with Android devices through adb. |
| 19 | |
| 20 | """ |
| 21 | # pylint: disable=E1103 |
| 22 | import os |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 23 | import pexpect |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 24 | import time |
| 25 | import subprocess |
| 26 | import logging |
| 27 | import re |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 28 | import threading |
| 29 | import tempfile |
| 30 | import Queue |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 31 | from collections import defaultdict |
| 32 | |
Sebastian Goscik | 8de24b5 | 2016-03-23 15:10:26 +0000 | [diff] [blame] | 33 | from devlib.exception import TargetError, HostError, DevlibError |
Marc Bonnici | 68be9d8 | 2017-07-14 16:59:52 +0100 | [diff] [blame] | 34 | from devlib.utils.misc import check_output, which, memoized, ABI_MAP |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 35 | from devlib.utils.misc import escape_single_quotes, escape_double_quotes |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 36 | from devlib import host |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 37 | |
| 38 | |
| 39 | logger = logging.getLogger('android') |
| 40 | |
| 41 | MAX_ATTEMPTS = 5 |
Valentin Schneider | 2d96840 | 2017-06-12 18:59:44 +0100 | [diff] [blame] | 42 | AM_START_ERROR = re.compile(r"Error: Activity.*") |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 43 | |
| 44 | # See: |
| 45 | # http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels |
| 46 | ANDROID_VERSION_MAP = { |
Sebastian Goscik | 0c11289 | 2016-02-15 15:35:56 +0000 | [diff] [blame] | 47 | 23: 'MARSHMALLOW', |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 48 | 22: 'LOLLYPOP_MR1', |
| 49 | 21: 'LOLLYPOP', |
| 50 | 20: 'KITKAT_WATCH', |
| 51 | 19: 'KITKAT', |
| 52 | 18: 'JELLY_BEAN_MR2', |
| 53 | 17: 'JELLY_BEAN_MR1', |
| 54 | 16: 'JELLY_BEAN', |
| 55 | 15: 'ICE_CREAM_SANDWICH_MR1', |
| 56 | 14: 'ICE_CREAM_SANDWICH', |
| 57 | 13: 'HONEYCOMB_MR2', |
| 58 | 12: 'HONEYCOMB_MR1', |
| 59 | 11: 'HONEYCOMB', |
| 60 | 10: 'GINGERBREAD_MR1', |
| 61 | 9: 'GINGERBREAD', |
| 62 | 8: 'FROYO', |
| 63 | 7: 'ECLAIR_MR1', |
| 64 | 6: 'ECLAIR_0_1', |
| 65 | 5: 'ECLAIR', |
| 66 | 4: 'DONUT', |
| 67 | 3: 'CUPCAKE', |
| 68 | 2: 'BASE_1_1', |
| 69 | 1: 'BASE', |
| 70 | } |
| 71 | |
| 72 | |
| 73 | # Initialized in functions near the botton of the file |
| 74 | android_home = None |
| 75 | platform_tools = None |
| 76 | adb = None |
| 77 | aapt = None |
| 78 | fastboot = None |
| 79 | |
| 80 | |
| 81 | class AndroidProperties(object): |
| 82 | |
| 83 | def __init__(self, text): |
| 84 | self._properties = {} |
| 85 | self.parse(text) |
| 86 | |
| 87 | def parse(self, text): |
| 88 | self._properties = dict(re.findall(r'\[(.*?)\]:\s+\[(.*?)\]', text)) |
| 89 | |
| 90 | def iteritems(self): |
| 91 | return self._properties.iteritems() |
| 92 | |
| 93 | def __iter__(self): |
| 94 | return iter(self._properties) |
| 95 | |
| 96 | def __getattr__(self, name): |
| 97 | return self._properties.get(name) |
| 98 | |
| 99 | __getitem__ = __getattr__ |
| 100 | |
| 101 | |
| 102 | class AdbDevice(object): |
| 103 | |
| 104 | def __init__(self, name, status): |
| 105 | self.name = name |
| 106 | self.status = status |
| 107 | |
| 108 | def __cmp__(self, other): |
| 109 | if isinstance(other, AdbDevice): |
| 110 | return cmp(self.name, other.name) |
| 111 | else: |
| 112 | return cmp(self.name, other) |
| 113 | |
| 114 | def __str__(self): |
| 115 | return 'AdbDevice({}, {})'.format(self.name, self.status) |
| 116 | |
| 117 | __repr__ = __str__ |
| 118 | |
| 119 | |
| 120 | class ApkInfo(object): |
| 121 | |
| 122 | version_regex = re.compile(r"name='(?P<name>[^']+)' versionCode='(?P<vcode>[^']+)' versionName='(?P<vname>[^']+)'") |
| 123 | name_regex = re.compile(r"name='(?P<name>[^']+)'") |
| 124 | |
| 125 | def __init__(self, path=None): |
| 126 | self.path = path |
| 127 | self.package = None |
| 128 | self.activity = None |
| 129 | self.label = None |
| 130 | self.version_name = None |
| 131 | self.version_code = None |
Marc Bonnici | 68be9d8 | 2017-07-14 16:59:52 +0100 | [diff] [blame] | 132 | self.native_code = None |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 133 | self.parse(path) |
| 134 | |
| 135 | def parse(self, apk_path): |
| 136 | _check_env() |
| 137 | command = [aapt, 'dump', 'badging', apk_path] |
| 138 | logger.debug(' '.join(command)) |
Brendan Jackman | 99aca25 | 2017-10-09 18:30:06 +0100 | [diff] [blame] | 139 | try: |
| 140 | output = subprocess.check_output(command, stderr=subprocess.STDOUT) |
| 141 | except subprocess.CalledProcessError as e: |
| 142 | raise HostError('Error parsing APK file {}. `aapt` says:\n{}' |
| 143 | .format(apk_path, e.output)) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 144 | for line in output.split('\n'): |
| 145 | if line.startswith('application-label:'): |
| 146 | self.label = line.split(':')[1].strip().replace('\'', '') |
| 147 | elif line.startswith('package:'): |
| 148 | match = self.version_regex.search(line) |
| 149 | if match: |
| 150 | self.package = match.group('name') |
| 151 | self.version_code = match.group('vcode') |
| 152 | self.version_name = match.group('vname') |
| 153 | elif line.startswith('launchable-activity:'): |
| 154 | match = self.name_regex.search(line) |
| 155 | self.activity = match.group('name') |
Marc Bonnici | 68be9d8 | 2017-07-14 16:59:52 +0100 | [diff] [blame] | 156 | elif line.startswith('native-code'): |
| 157 | apk_abis = [entry.strip() for entry in line.split(':')[1].split("'") if entry.strip()] |
| 158 | mapped_abis = [] |
| 159 | for apk_abi in apk_abis: |
| 160 | found = False |
| 161 | for abi, architectures in ABI_MAP.iteritems(): |
| 162 | if apk_abi in architectures: |
| 163 | mapped_abis.append(abi) |
| 164 | found = True |
| 165 | break |
| 166 | if not found: |
| 167 | mapped_abis.append(apk_abi) |
| 168 | self.native_code = mapped_abis |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 169 | else: |
| 170 | pass # not interested |
| 171 | |
| 172 | |
| 173 | class AdbConnection(object): |
| 174 | |
| 175 | # maintains the count of parallel active connections to a device, so that |
| 176 | # adb disconnect is not invoked untill all connections are closed |
| 177 | active_connections = defaultdict(int) |
Sergei Trofimov | 89256fd | 2016-05-17 14:00:01 +0100 | [diff] [blame] | 178 | default_timeout = 10 |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 179 | ls_command = 'ls' |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 180 | |
| 181 | @property |
| 182 | def name(self): |
| 183 | return self.device |
| 184 | |
Sebastian Goscik | 8de24b5 | 2016-03-23 15:10:26 +0000 | [diff] [blame] | 185 | @property |
| 186 | @memoized |
| 187 | def newline_separator(self): |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 188 | output = adb_command(self.device, |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 189 | "shell '({}); echo \"\n$?\"'".format(self.ls_command), adb_server=self.adb_server) |
Sebastian Goscik | 8de24b5 | 2016-03-23 15:10:26 +0000 | [diff] [blame] | 190 | if output.endswith('\r\n'): |
| 191 | return '\r\n' |
| 192 | elif output.endswith('\n'): |
| 193 | return '\n' |
| 194 | else: |
| 195 | raise DevlibError("Unknown line ending") |
| 196 | |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 197 | # Again, we need to handle boards where the default output format from ls is |
| 198 | # single column *and* boards where the default output is multi-column. |
| 199 | # We need to do this purely because the '-1' option causes errors on older |
| 200 | # versions of the ls tool in Android pre-v7. |
| 201 | def _setup_ls(self): |
| 202 | command = "shell '(ls -1); echo \"\n$?\"'" |
Brendan Jackman | 3298205 | 2017-03-01 18:49:27 +0000 | [diff] [blame] | 203 | try: |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 204 | output = adb_command(self.device, command, timeout=self.timeout, adb_server=self.adb_server) |
Brendan Jackman | 3298205 | 2017-03-01 18:49:27 +0000 | [diff] [blame] | 205 | except subprocess.CalledProcessError as e: |
| 206 | raise HostError( |
| 207 | 'Failed to set up ls command on Android device. Output:\n' |
| 208 | + e.output) |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 209 | lines = output.splitlines() |
| 210 | retval = lines[-1].strip() |
| 211 | if int(retval) == 0: |
| 212 | self.ls_command = 'ls -1' |
| 213 | else: |
| 214 | self.ls_command = 'ls' |
Sergei Trofimov | 97a8997 | 2017-05-12 10:45:42 +0100 | [diff] [blame] | 215 | logger.debug("ls command is set to {}".format(self.ls_command)) |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 216 | |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 217 | def __init__(self, device=None, timeout=None, platform=None, adb_server=None): |
Sergei Trofimov | 89256fd | 2016-05-17 14:00:01 +0100 | [diff] [blame] | 218 | self.timeout = timeout if timeout is not None else self.default_timeout |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 219 | if device is None: |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 220 | device = adb_get_device(timeout=timeout, adb_server=adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 221 | self.device = device |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 222 | self.adb_server = adb_server |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 223 | adb_connect(self.device) |
| 224 | AdbConnection.active_connections[self.device] += 1 |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 225 | self._setup_ls() |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 226 | |
| 227 | def push(self, source, dest, timeout=None): |
| 228 | if timeout is None: |
| 229 | timeout = self.timeout |
Sebastian Goscik | 1424ceb | 2016-02-15 15:27:19 +0000 | [diff] [blame] | 230 | command = "push '{}' '{}'".format(source, dest) |
Brendan Jackman | e17b9c3 | 2017-04-28 11:26:35 +0100 | [diff] [blame] | 231 | if not os.path.exists(source): |
| 232 | raise HostError('No such file "{}"'.format(source)) |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 233 | return adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 234 | |
| 235 | def pull(self, source, dest, timeout=None): |
| 236 | if timeout is None: |
| 237 | timeout = self.timeout |
Patrick Bellasi | c93e3d6 | 2015-11-25 11:19:08 +0000 | [diff] [blame] | 238 | # Pull all files matching a wildcard expression |
| 239 | if os.path.isdir(dest) and \ |
Sebastian Goscik | aab487c | 2016-02-15 15:21:40 +0000 | [diff] [blame] | 240 | ('*' in source or '?' in source): |
Chris Redpath | e8e945a | 2016-10-14 14:35:58 +0100 | [diff] [blame] | 241 | command = 'shell {} {}'.format(self.ls_command, source) |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 242 | output = adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server) |
Patrick Bellasi | c93e3d6 | 2015-11-25 11:19:08 +0000 | [diff] [blame] | 243 | for line in output.splitlines(): |
Brendan Jackman | ee38a42 | 2016-11-18 17:48:48 +0000 | [diff] [blame] | 244 | command = "pull '{}' '{}'".format(line.strip(), dest) |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 245 | adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server) |
Patrick Bellasi | c93e3d6 | 2015-11-25 11:19:08 +0000 | [diff] [blame] | 246 | return |
Sebastian Goscik | 1424ceb | 2016-02-15 15:27:19 +0000 | [diff] [blame] | 247 | command = "pull '{}' '{}'".format(source, dest) |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 248 | return adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 249 | |
Anouk Van Laer | 29a7940 | 2017-01-31 12:48:58 +0000 | [diff] [blame] | 250 | def execute(self, command, timeout=None, check_exit_code=False, |
| 251 | as_root=False, strip_colors=True): |
Patrick Bellasi | c2329bd | 2016-03-28 12:29:45 +0100 | [diff] [blame] | 252 | return adb_shell(self.device, command, timeout, check_exit_code, |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 253 | as_root, self.newline_separator,adb_server=self.adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 254 | |
| 255 | def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False): |
| 256 | return adb_background_shell(self.device, command, stdout, stderr, as_root) |
| 257 | |
| 258 | def close(self): |
| 259 | AdbConnection.active_connections[self.device] -= 1 |
| 260 | if AdbConnection.active_connections[self.device] <= 0: |
| 261 | adb_disconnect(self.device) |
| 262 | del AdbConnection.active_connections[self.device] |
| 263 | |
| 264 | def cancel_running_command(self): |
| 265 | # adbd multiplexes commands so that they don't interfer with each |
| 266 | # other, so there is no need to explicitly cancel a running command |
| 267 | # before the next one can be issued. |
| 268 | pass |
| 269 | |
| 270 | |
Patrick Bellasi | da588ea | 2017-02-16 13:08:31 +0000 | [diff] [blame] | 271 | def fastboot_command(command, timeout=None, device=None): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 272 | _check_env() |
Patrick Bellasi | da588ea | 2017-02-16 13:08:31 +0000 | [diff] [blame] | 273 | target = '-s {}'.format(device) if device else '' |
| 274 | full_command = 'fastboot {} {}'.format(target, command) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 275 | logger.debug(full_command) |
| 276 | output, _ = check_output(full_command, timeout, shell=True) |
| 277 | return output |
| 278 | |
| 279 | |
| 280 | def fastboot_flash_partition(partition, path_to_image): |
| 281 | command = 'flash {} {}'.format(partition, path_to_image) |
| 282 | fastboot_command(command) |
| 283 | |
| 284 | |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 285 | def adb_get_device(timeout=None, adb_server=None): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 286 | """ |
| 287 | Returns the serial number of a connected android device. |
| 288 | |
| 289 | If there are more than one device connected to the machine, or it could not |
| 290 | find any device connected, :class:`devlib.exceptions.HostError` is raised. |
| 291 | """ |
| 292 | # TODO this is a hacky way to issue a adb command to all listed devices |
| 293 | |
Brendan Jackman | a290d28 | 2017-09-20 13:02:44 +0100 | [diff] [blame] | 294 | # Ensure server is started so the 'daemon started successfully' message |
| 295 | # doesn't confuse the parsing below |
| 296 | adb_command(None, 'start-server', adb_server=adb_server) |
| 297 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 298 | # The output of calling adb devices consists of a heading line then |
| 299 | # a list of the devices sperated by new line |
| 300 | # The last line is a blank new line. in otherwords, if there is a device found |
| 301 | # then the output length is 2 + (1 for each device) |
| 302 | start = time.time() |
| 303 | while True: |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 304 | output = adb_command(None, "devices", adb_server=adb_server).splitlines() # pylint: disable=E1103 |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 305 | output_length = len(output) |
| 306 | if output_length == 3: |
| 307 | # output[1] is the 2nd line in the output which has the device name |
| 308 | # Splitting the line by '\t' gives a list of two indexes, which has |
| 309 | # device serial in 0 number and device type in 1. |
| 310 | return output[1].split('\t')[0] |
| 311 | elif output_length > 3: |
| 312 | message = '{} Android devices found; either explicitly specify ' +\ |
Sebastian Goscik | aab487c | 2016-02-15 15:21:40 +0000 | [diff] [blame] | 313 | 'the device you want, or make sure only one is connected.' |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 314 | raise HostError(message.format(output_length - 2)) |
| 315 | else: |
| 316 | if timeout < time.time() - start: |
| 317 | raise HostError('No device is connected and available') |
| 318 | time.sleep(1) |
| 319 | |
| 320 | |
| 321 | def adb_connect(device, timeout=None, attempts=MAX_ATTEMPTS): |
| 322 | _check_env() |
Patrick Bellasi | f714dd3 | 2016-07-15 11:18:09 +0100 | [diff] [blame] | 323 | # Connect is required only for ADB-over-IP |
| 324 | if "." not in device: |
| 325 | logger.debug('Device connected via USB, connect not required') |
| 326 | return |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 327 | tries = 0 |
| 328 | output = None |
| 329 | while tries <= attempts: |
| 330 | tries += 1 |
| 331 | if device: |
| 332 | command = 'adb connect {}'.format(device) |
| 333 | logger.debug(command) |
| 334 | output, _ = check_output(command, shell=True, timeout=timeout) |
| 335 | if _ping(device): |
| 336 | break |
| 337 | time.sleep(10) |
| 338 | else: # did not connect to the device |
| 339 | message = 'Could not connect to {}'.format(device or 'a device') |
| 340 | if output: |
| 341 | message += '; got: "{}"'.format(output) |
| 342 | raise HostError(message) |
| 343 | |
| 344 | |
| 345 | def adb_disconnect(device): |
| 346 | _check_env() |
| 347 | if not device: |
| 348 | return |
Chris Redpath | 119fd7d | 2016-10-06 16:34:14 +0100 | [diff] [blame] | 349 | if ":" in device and device in adb_list_devices(): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 350 | command = "adb disconnect " + device |
| 351 | logger.debug(command) |
| 352 | retval = subprocess.call(command, stdout=open(os.devnull, 'wb'), shell=True) |
| 353 | if retval: |
| 354 | raise TargetError('"{}" returned {}'.format(command, retval)) |
| 355 | |
| 356 | |
| 357 | def _ping(device): |
| 358 | _check_env() |
| 359 | device_string = ' -s {}'.format(device) if device else '' |
| 360 | command = "adb{} shell \"ls / > /dev/null\"".format(device_string) |
| 361 | logger.debug(command) |
| 362 | result = subprocess.call(command, stderr=subprocess.PIPE, shell=True) |
| 363 | if not result: |
| 364 | return True |
| 365 | else: |
| 366 | return False |
| 367 | |
| 368 | |
Patrick Bellasi | c2329bd | 2016-03-28 12:29:45 +0100 | [diff] [blame] | 369 | def adb_shell(device, command, timeout=None, check_exit_code=False, |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 370 | as_root=False, newline_separator='\r\n', adb_server=None): # NOQA |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 371 | _check_env() |
| 372 | if as_root: |
Sergei Trofimov | 171cc25 | 2015-12-14 17:21:47 +0000 | [diff] [blame] | 373 | command = 'echo \'{}\' | su'.format(escape_single_quotes(command)) |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 374 | device_part = [] |
| 375 | if adb_server: |
| 376 | device_part = ['-H', adb_server] |
| 377 | device_part += ['-s', device] if device else [] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 378 | |
Brendan Jackman | 44fe037 | 2017-01-25 12:06:38 +0000 | [diff] [blame] | 379 | # On older combinations of ADB/Android versions, the adb host command always |
| 380 | # exits with 0 if it was able to run the command on the target, even if the |
| 381 | # command failed (https://code.google.com/p/android/issues/detail?id=3254). |
| 382 | # Homogenise this behaviour by running the command then echoing the exit |
| 383 | # code. |
| 384 | adb_shell_command = '({}); echo \"\n$?\"'.format(command) |
| 385 | actual_command = ['adb'] + device_part + ['shell', adb_shell_command] |
| 386 | logger.debug('adb {} shell {}'.format(' '.join(device_part), command)) |
| 387 | raw_output, error = check_output(actual_command, timeout, shell=False) |
| 388 | if raw_output: |
| 389 | try: |
| 390 | output, exit_code, _ = raw_output.rsplit(newline_separator, 2) |
| 391 | except ValueError: |
| 392 | exit_code, _ = raw_output.rsplit(newline_separator, 1) |
| 393 | output = '' |
| 394 | else: # raw_output is empty |
| 395 | exit_code = '969696' # just because |
| 396 | output = '' |
| 397 | |
| 398 | if check_exit_code: |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 399 | exit_code = exit_code.strip() |
Valentin Schneider | 2d96840 | 2017-06-12 18:59:44 +0100 | [diff] [blame] | 400 | re_search = AM_START_ERROR.findall('{}\n{}'.format(output, error)) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 401 | if exit_code.isdigit(): |
| 402 | if int(exit_code): |
Brendan Jackman | 44fe037 | 2017-01-25 12:06:38 +0000 | [diff] [blame] | 403 | message = ('Got exit code {}\nfrom target command: {}\n' |
| 404 | 'STDOUT: {}\nSTDERR: {}') |
| 405 | raise TargetError(message.format(exit_code, command, output, error)) |
Valentin Schneider | 2d96840 | 2017-06-12 18:59:44 +0100 | [diff] [blame] | 406 | elif re_search: |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 407 | message = 'Could not start activity; got the following:\n{}' |
Valentin Schneider | 2d96840 | 2017-06-12 18:59:44 +0100 | [diff] [blame] | 408 | raise TargetError(message.format(re_search[0])) |
| 409 | else: # not all digits |
| 410 | if re_search: |
| 411 | message = 'Could not start activity; got the following:\n{}' |
| 412 | raise TargetError(message.format(re_search[0])) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 413 | else: |
| 414 | message = 'adb has returned early; did not get an exit code. '\ |
Sergei Trofimov | 85036fb | 2017-07-12 13:44:47 +0100 | [diff] [blame] | 415 | 'Was kill-server invoked?\nOUTPUT:\n-----\n{}\n'\ |
| 416 | '-----\nERROR:\n-----\n{}\n-----' |
| 417 | raise TargetError(message.format(raw_output, error)) |
Brendan Jackman | 44fe037 | 2017-01-25 12:06:38 +0000 | [diff] [blame] | 418 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 419 | return output |
| 420 | |
| 421 | |
| 422 | def adb_background_shell(device, command, |
| 423 | stdout=subprocess.PIPE, |
| 424 | stderr=subprocess.PIPE, |
| 425 | as_root=False): |
| 426 | """Runs the sepcified command in a subprocess, returning the the Popen object.""" |
| 427 | _check_env() |
| 428 | if as_root: |
| 429 | command = 'echo \'{}\' | su'.format(escape_single_quotes(command)) |
| 430 | device_string = ' -s {}'.format(device) if device else '' |
| 431 | full_command = 'adb{} shell "{}"'.format(device_string, escape_double_quotes(command)) |
| 432 | logger.debug(full_command) |
| 433 | return subprocess.Popen(full_command, stdout=stdout, stderr=stderr, shell=True) |
| 434 | |
| 435 | |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 436 | def adb_list_devices(adb_server=None): |
| 437 | output = adb_command(None, 'devices',adb_server=adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 438 | devices = [] |
| 439 | for line in output.splitlines(): |
| 440 | parts = [p.strip() for p in line.split()] |
| 441 | if len(parts) == 2: |
| 442 | devices.append(AdbDevice(*parts)) |
| 443 | return devices |
| 444 | |
| 445 | |
Brendan Jackman | da22bef | 2017-09-21 12:32:18 +0100 | [diff] [blame] | 446 | def get_adb_command(device, command, timeout=None,adb_server=None): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 447 | _check_env() |
Anthony Barbier | 7919a56 | 2017-07-25 13:48:33 +0100 | [diff] [blame] | 448 | device_string = "" |
| 449 | if adb_server != None: |
| 450 | device_string = ' -H {}'.format(adb_server) |
| 451 | device_string += ' -s {}'.format(device) if device else '' |
Brendan Jackman | da22bef | 2017-09-21 12:32:18 +0100 | [diff] [blame] | 452 | return "adb{} {}".format(device_string, command) |
| 453 | |
| 454 | def adb_command(device, command, timeout=None,adb_server=None): |
| 455 | full_command = get_adb_command(device, command, timeout, adb_server) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 456 | logger.debug(full_command) |
| 457 | output, _ = check_output(full_command, timeout, shell=True) |
| 458 | return output |
| 459 | |
Valentin Schneider | 24d5630 | 2017-09-13 11:47:51 +0100 | [diff] [blame] | 460 | def grant_app_permissions(target, package): |
| 461 | """ |
| 462 | Grant an app all the permissions it may ask for |
| 463 | """ |
| 464 | dumpsys = target.execute('dumpsys package {}'.format(package)) |
| 465 | |
| 466 | permissions = re.search( |
| 467 | 'requested permissions:\s*(?P<permissions>(android.permission.+\s*)+)', dumpsys |
| 468 | ) |
Sergei Trofimov | 90040e8 | 2017-09-13 14:03:59 +0100 | [diff] [blame] | 469 | if permissions is None: |
| 470 | return |
Valentin Schneider | 24d5630 | 2017-09-13 11:47:51 +0100 | [diff] [blame] | 471 | permissions = permissions.group('permissions').replace(" ", "").splitlines() |
| 472 | |
| 473 | for permission in permissions: |
| 474 | try: |
| 475 | target.execute('pm grant {} {}'.format(package, permission)) |
| 476 | except TargetError: |
| 477 | logger.debug('Cannot grant {}'.format(permission)) |
| 478 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 479 | |
| 480 | # Messy environment initialisation stuff... |
| 481 | |
| 482 | class _AndroidEnvironment(object): |
| 483 | |
| 484 | def __init__(self): |
| 485 | self.android_home = None |
| 486 | self.platform_tools = None |
| 487 | self.adb = None |
| 488 | self.aapt = None |
| 489 | self.fastboot = None |
| 490 | |
| 491 | |
| 492 | def _initialize_with_android_home(env): |
| 493 | logger.debug('Using ANDROID_HOME from the environment.') |
| 494 | env.android_home = android_home |
| 495 | env.platform_tools = os.path.join(android_home, 'platform-tools') |
Sergei Trofimov | d5460e1 | 2016-12-13 11:26:32 +0000 | [diff] [blame] | 496 | os.environ['PATH'] = env.platform_tools + os.pathsep + os.environ['PATH'] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 497 | _init_common(env) |
| 498 | return env |
| 499 | |
| 500 | |
| 501 | def _initialize_without_android_home(env): |
Javi Merino | 7f32efc | 2015-12-15 13:43:56 +0000 | [diff] [blame] | 502 | adb_full_path = which('adb') |
| 503 | if adb_full_path: |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 504 | env.adb = 'adb' |
| 505 | else: |
| 506 | raise HostError('ANDROID_HOME is not set and adb is not in PATH. ' |
| 507 | 'Have you installed Android SDK?') |
| 508 | logger.debug('Discovering ANDROID_HOME from adb path.') |
Javi Merino | 7f32efc | 2015-12-15 13:43:56 +0000 | [diff] [blame] | 509 | env.platform_tools = os.path.dirname(adb_full_path) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 510 | env.android_home = os.path.dirname(env.platform_tools) |
| 511 | _init_common(env) |
| 512 | return env |
| 513 | |
| 514 | |
| 515 | def _init_common(env): |
| 516 | logger.debug('ANDROID_HOME: {}'.format(env.android_home)) |
| 517 | build_tools_directory = os.path.join(env.android_home, 'build-tools') |
| 518 | if not os.path.isdir(build_tools_directory): |
| 519 | msg = '''ANDROID_HOME ({}) does not appear to have valid Android SDK install |
| 520 | (cannot find build-tools)''' |
| 521 | raise HostError(msg.format(env.android_home)) |
| 522 | versions = os.listdir(build_tools_directory) |
| 523 | for version in reversed(sorted(versions)): |
| 524 | aapt_path = os.path.join(build_tools_directory, version, 'aapt') |
| 525 | if os.path.isfile(aapt_path): |
| 526 | logger.debug('Using aapt for version {}'.format(version)) |
| 527 | env.aapt = aapt_path |
| 528 | break |
| 529 | else: |
| 530 | raise HostError('aapt not found. Please make sure at least one Android ' |
| 531 | 'platform is installed.') |
| 532 | |
| 533 | |
| 534 | def _check_env(): |
| 535 | global android_home, platform_tools, adb, aapt # pylint: disable=W0603 |
| 536 | if not android_home: |
| 537 | android_home = os.getenv('ANDROID_HOME') |
| 538 | if android_home: |
| 539 | _env = _initialize_with_android_home(_AndroidEnvironment()) |
| 540 | else: |
| 541 | _env = _initialize_without_android_home(_AndroidEnvironment()) |
| 542 | android_home = _env.android_home |
| 543 | platform_tools = _env.platform_tools |
| 544 | adb = _env.adb |
| 545 | aapt = _env.aapt |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 546 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 547 | class LogcatMonitor(object): |
Brendan Jackman | fb5a260 | 2017-09-21 13:20:46 +0100 | [diff] [blame] | 548 | """ |
| 549 | Helper class for monitoring Anroid's logcat |
| 550 | |
| 551 | :param target: Android target to monitor |
| 552 | :type target: :class:`AndroidTarget` |
| 553 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 554 | :param regexps: List of uncompiled regular expressions to filter on the |
Brendan Jackman | fb5a260 | 2017-09-21 13:20:46 +0100 | [diff] [blame] | 555 | device. Logcat entries that don't match any will not be |
| 556 | seen. If omitted, all entries will be sent to host. |
| 557 | :type regexps: list(str) |
| 558 | """ |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 559 | |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 560 | @property |
| 561 | def logfile(self): |
| 562 | return self._logfile |
| 563 | |
| 564 | def __init__(self, target, regexps=None): |
| 565 | super(LogcatMonitor, self).__init__() |
| 566 | |
| 567 | self.target = target |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 568 | self._regexps = regexps |
| 569 | |
| 570 | def start(self, outfile=None): |
Brendan Jackman | fb5a260 | 2017-09-21 13:20:46 +0100 | [diff] [blame] | 571 | """ |
| 572 | Start logcat and begin monitoring |
| 573 | |
| 574 | :param outfile: Optional path to file to store all logcat entries |
| 575 | :type outfile: str |
| 576 | """ |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 577 | if outfile: |
Brendan Jackman | a679d57 | 2017-10-20 16:08:32 +0100 | [diff] [blame] | 578 | self._logfile = open(outfile, 'w') |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 579 | else: |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 580 | self._logfile = tempfile.NamedTemporaryFile() |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 581 | |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 582 | self.target.clear_logcat() |
| 583 | |
| 584 | logcat_cmd = 'logcat' |
| 585 | |
| 586 | # Join all requested regexps with an 'or' |
| 587 | if self._regexps: |
| 588 | regexp = '{}'.format('|'.join(self._regexps)) |
| 589 | if len(self._regexps) > 1: |
| 590 | regexp = '({})'.format(regexp) |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 591 | logcat_cmd = '{} -e "{}"'.format(logcat_cmd, regexp) |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 592 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 593 | logcat_cmd = get_adb_command(self.target.conn.device, logcat_cmd) |
| 594 | |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 595 | logger.debug('logcat command ="{}"'.format(logcat_cmd)) |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 596 | self._logcat = pexpect.spawn(logcat_cmd, logfile=self._logfile) |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 597 | |
| 598 | def stop(self): |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 599 | self._logcat.terminate() |
| 600 | self._logfile.close() |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 601 | |
| 602 | def get_log(self): |
| 603 | """ |
| 604 | Return the list of lines found by the monitor |
| 605 | """ |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 606 | with open(self._logfile.name) as fh: |
| 607 | return [line for line in fh] |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 608 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 609 | def clear_log(self): |
Brendan Jackman | 71d5b8b | 2017-10-12 14:39:27 +0100 | [diff] [blame] | 610 | with open(self._logfile.name, 'w') as fh: |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 611 | pass |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 612 | |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 613 | def search(self, regexp): |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 614 | """ |
| 615 | Search a line that matches a regexp in the logcat log |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 616 | Return immediatly |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 617 | """ |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 618 | return [line for line in self.get_log() if re.match(regexp, line)] |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 619 | |
| 620 | def wait_for(self, regexp, timeout=30): |
| 621 | """ |
| 622 | Search a line that matches a regexp in the logcat log |
| 623 | Wait for it to appear if it's not found |
Brendan Jackman | fb5a260 | 2017-09-21 13:20:46 +0100 | [diff] [blame] | 624 | |
| 625 | :param regexp: regexp to search |
| 626 | :type regexp: str |
| 627 | |
| 628 | :param timeout: Timeout in seconds, before rasing RuntimeError. |
| 629 | ``None`` means wait indefinitely |
| 630 | :type timeout: number |
| 631 | |
| 632 | :returns: List of matched strings |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 633 | """ |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 634 | log = self.get_log() |
| 635 | res = [line for line in log if re.match(regexp, line)] |
Valentin Schneider | 6bda093 | 2017-09-12 15:44:25 +0100 | [diff] [blame] | 636 | |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 637 | # Found some matches, return them |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 638 | if len(res) > 0: |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 639 | return res |
| 640 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 641 | # Store the number of lines we've searched already, so we don't have to |
| 642 | # re-grep them after 'expect' returns |
| 643 | next_line_num = len(log) |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 644 | |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 645 | try: |
| 646 | self._logcat.expect(regexp, timeout=timeout) |
| 647 | except pexpect.TIMEOUT: |
Valentin Schneider | 035181a | 2017-09-11 16:55:02 +0100 | [diff] [blame] | 648 | raise RuntimeError('Logcat monitor timeout ({}s)'.format(timeout)) |
Brendan Jackman | 1d85501 | 2017-09-21 12:30:13 +0100 | [diff] [blame] | 649 | |
| 650 | return [line for line in self.get_log()[next_line_num:] |
| 651 | if re.match(regexp, line)] |