blob: c2fed96f14a4ce587b828d378245b8d9cfac2c95 [file] [log] [blame]
Sergei Trofimov4e6afe92015-10-09 09:30:04 +01001# 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"""
18Utility functions for working with Android devices through adb.
19
20"""
21# pylint: disable=E1103
22import os
23import time
24import subprocess
25import logging
26import re
27from collections import defaultdict
28
Sebastian Goscik8de24b52016-03-23 15:10:26 +000029from devlib.exception import TargetError, HostError, DevlibError
30from devlib.utils.misc import check_output, which, memoized
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010031from devlib.utils.misc import escape_single_quotes, escape_double_quotes
32
33
34logger = logging.getLogger('android')
35
36MAX_ATTEMPTS = 5
37AM_START_ERROR = re.compile(r"Error: Activity class {[\w|.|/]*} does not exist")
38
39# See:
40# http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
41ANDROID_VERSION_MAP = {
Sebastian Goscik0c112892016-02-15 15:35:56 +000042 23: 'MARSHMALLOW',
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010043 22: 'LOLLYPOP_MR1',
44 21: 'LOLLYPOP',
45 20: 'KITKAT_WATCH',
46 19: 'KITKAT',
47 18: 'JELLY_BEAN_MR2',
48 17: 'JELLY_BEAN_MR1',
49 16: 'JELLY_BEAN',
50 15: 'ICE_CREAM_SANDWICH_MR1',
51 14: 'ICE_CREAM_SANDWICH',
52 13: 'HONEYCOMB_MR2',
53 12: 'HONEYCOMB_MR1',
54 11: 'HONEYCOMB',
55 10: 'GINGERBREAD_MR1',
56 9: 'GINGERBREAD',
57 8: 'FROYO',
58 7: 'ECLAIR_MR1',
59 6: 'ECLAIR_0_1',
60 5: 'ECLAIR',
61 4: 'DONUT',
62 3: 'CUPCAKE',
63 2: 'BASE_1_1',
64 1: 'BASE',
65}
66
67
68# Initialized in functions near the botton of the file
69android_home = None
70platform_tools = None
71adb = None
72aapt = None
73fastboot = None
74
75
76class AndroidProperties(object):
77
78 def __init__(self, text):
79 self._properties = {}
80 self.parse(text)
81
82 def parse(self, text):
83 self._properties = dict(re.findall(r'\[(.*?)\]:\s+\[(.*?)\]', text))
84
85 def iteritems(self):
86 return self._properties.iteritems()
87
88 def __iter__(self):
89 return iter(self._properties)
90
91 def __getattr__(self, name):
92 return self._properties.get(name)
93
94 __getitem__ = __getattr__
95
96
97class AdbDevice(object):
98
99 def __init__(self, name, status):
100 self.name = name
101 self.status = status
102
103 def __cmp__(self, other):
104 if isinstance(other, AdbDevice):
105 return cmp(self.name, other.name)
106 else:
107 return cmp(self.name, other)
108
109 def __str__(self):
110 return 'AdbDevice({}, {})'.format(self.name, self.status)
111
112 __repr__ = __str__
113
114
115class ApkInfo(object):
116
117 version_regex = re.compile(r"name='(?P<name>[^']+)' versionCode='(?P<vcode>[^']+)' versionName='(?P<vname>[^']+)'")
118 name_regex = re.compile(r"name='(?P<name>[^']+)'")
119
120 def __init__(self, path=None):
121 self.path = path
122 self.package = None
123 self.activity = None
124 self.label = None
125 self.version_name = None
126 self.version_code = None
127 self.parse(path)
128
129 def parse(self, apk_path):
130 _check_env()
131 command = [aapt, 'dump', 'badging', apk_path]
132 logger.debug(' '.join(command))
133 output = subprocess.check_output(command)
134 for line in output.split('\n'):
135 if line.startswith('application-label:'):
136 self.label = line.split(':')[1].strip().replace('\'', '')
137 elif line.startswith('package:'):
138 match = self.version_regex.search(line)
139 if match:
140 self.package = match.group('name')
141 self.version_code = match.group('vcode')
142 self.version_name = match.group('vname')
143 elif line.startswith('launchable-activity:'):
144 match = self.name_regex.search(line)
145 self.activity = match.group('name')
146 else:
147 pass # not interested
148
149
150class AdbConnection(object):
151
152 # maintains the count of parallel active connections to a device, so that
153 # adb disconnect is not invoked untill all connections are closed
154 active_connections = defaultdict(int)
155
156 @property
157 def name(self):
158 return self.device
159
Sebastian Goscik8de24b52016-03-23 15:10:26 +0000160 @property
161 @memoized
162 def newline_separator(self):
163 output = adb_command(self.device, "shell '(ls); echo \"\n$?\"'")
164 if output.endswith('\r\n'):
165 return '\r\n'
166 elif output.endswith('\n'):
167 return '\n'
168 else:
169 raise DevlibError("Unknown line ending")
170
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100171 def __init__(self, device=None, timeout=10):
172 self.timeout = timeout
173 if device is None:
174 device = adb_get_device(timeout=timeout)
175 self.device = device
176 adb_connect(self.device)
177 AdbConnection.active_connections[self.device] += 1
178
179 def push(self, source, dest, timeout=None):
180 if timeout is None:
181 timeout = self.timeout
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000182 command = "push '{}' '{}'".format(source, dest)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100183 return adb_command(self.device, command, timeout=timeout)
184
185 def pull(self, source, dest, timeout=None):
186 if timeout is None:
187 timeout = self.timeout
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000188 # Pull all files matching a wildcard expression
189 if os.path.isdir(dest) and \
Sebastian Goscikaab487c2016-02-15 15:21:40 +0000190 ('*' in source or '?' in source):
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000191 command = 'shell ls {}'.format(source)
192 output = adb_command(self.device, command, timeout=timeout)
193 for line in output.splitlines():
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000194 command = "pull '{}' '{}'".format(line, dest)
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000195 adb_command(self.device, command, timeout=timeout)
196 return
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000197 command = "pull '{}' '{}'".format(source, dest)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100198 return adb_command(self.device, command, timeout=timeout)
199
200 def execute(self, command, timeout=None, check_exit_code=False, as_root=False):
201 return adb_shell(self.device, command, timeout, check_exit_code, as_root)
202
203 def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
204 return adb_background_shell(self.device, command, stdout, stderr, as_root)
205
206 def close(self):
207 AdbConnection.active_connections[self.device] -= 1
208 if AdbConnection.active_connections[self.device] <= 0:
209 adb_disconnect(self.device)
210 del AdbConnection.active_connections[self.device]
211
212 def cancel_running_command(self):
213 # adbd multiplexes commands so that they don't interfer with each
214 # other, so there is no need to explicitly cancel a running command
215 # before the next one can be issued.
216 pass
217
218
219def fastboot_command(command, timeout=None):
220 _check_env()
221 full_command = "fastboot {}".format(command)
222 logger.debug(full_command)
223 output, _ = check_output(full_command, timeout, shell=True)
224 return output
225
226
227def fastboot_flash_partition(partition, path_to_image):
228 command = 'flash {} {}'.format(partition, path_to_image)
229 fastboot_command(command)
230
231
232def adb_get_device(timeout=None):
233 """
234 Returns the serial number of a connected android device.
235
236 If there are more than one device connected to the machine, or it could not
237 find any device connected, :class:`devlib.exceptions.HostError` is raised.
238 """
239 # TODO this is a hacky way to issue a adb command to all listed devices
240
241 # The output of calling adb devices consists of a heading line then
242 # a list of the devices sperated by new line
243 # The last line is a blank new line. in otherwords, if there is a device found
244 # then the output length is 2 + (1 for each device)
245 start = time.time()
246 while True:
247 output = adb_command(None, "devices").splitlines() # pylint: disable=E1103
248 output_length = len(output)
249 if output_length == 3:
250 # output[1] is the 2nd line in the output which has the device name
251 # Splitting the line by '\t' gives a list of two indexes, which has
252 # device serial in 0 number and device type in 1.
253 return output[1].split('\t')[0]
254 elif output_length > 3:
255 message = '{} Android devices found; either explicitly specify ' +\
Sebastian Goscikaab487c2016-02-15 15:21:40 +0000256 'the device you want, or make sure only one is connected.'
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100257 raise HostError(message.format(output_length - 2))
258 else:
259 if timeout < time.time() - start:
260 raise HostError('No device is connected and available')
261 time.sleep(1)
262
263
264def adb_connect(device, timeout=None, attempts=MAX_ATTEMPTS):
265 _check_env()
266 tries = 0
267 output = None
268 while tries <= attempts:
269 tries += 1
270 if device:
271 command = 'adb connect {}'.format(device)
272 logger.debug(command)
273 output, _ = check_output(command, shell=True, timeout=timeout)
274 if _ping(device):
275 break
276 time.sleep(10)
277 else: # did not connect to the device
278 message = 'Could not connect to {}'.format(device or 'a device')
279 if output:
280 message += '; got: "{}"'.format(output)
281 raise HostError(message)
282
283
284def adb_disconnect(device):
285 _check_env()
286 if not device:
287 return
288 if ":" in device:
289 command = "adb disconnect " + device
290 logger.debug(command)
291 retval = subprocess.call(command, stdout=open(os.devnull, 'wb'), shell=True)
292 if retval:
293 raise TargetError('"{}" returned {}'.format(command, retval))
294
295
296def _ping(device):
297 _check_env()
298 device_string = ' -s {}'.format(device) if device else ''
299 command = "adb{} shell \"ls / > /dev/null\"".format(device_string)
300 logger.debug(command)
301 result = subprocess.call(command, stderr=subprocess.PIPE, shell=True)
302 if not result:
303 return True
304 else:
305 return False
306
307
308def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA
309 _check_env()
310 if as_root:
Sergei Trofimov171cc252015-12-14 17:21:47 +0000311 command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100312 device_string = ' -s {}'.format(device) if device else ''
313 full_command = 'adb{} shell "{}"'.format(device_string,
314 escape_double_quotes(command))
315 logger.debug(full_command)
316 if check_exit_code:
Sergei Trofimovf52bf792015-12-11 17:18:18 +0000317 actual_command = "adb{} shell '({}); echo \"\n$?\"'".format(device_string,
Sergei Trofimov64261a62015-11-24 12:50:02 +0000318 escape_single_quotes(command))
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100319 raw_output, error = check_output(actual_command, timeout, shell=True)
320 if raw_output:
321 try:
Sebastian Goscik8de24b52016-03-23 15:10:26 +0000322 output, exit_code, _ = raw_output.rsplit(self.newline_separator, 2)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100323 except ValueError:
Sebastian Goscik8de24b52016-03-23 15:10:26 +0000324 exit_code, _ = raw_output.rsplit(self.newline_separator, 1)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100325 output = ''
326 else: # raw_output is empty
327 exit_code = '969696' # just because
328 output = ''
329
330 exit_code = exit_code.strip()
331 if exit_code.isdigit():
332 if int(exit_code):
333 message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'
334 raise TargetError(message.format(exit_code, full_command, output, error))
335 elif AM_START_ERROR.findall(output):
336 message = 'Could not start activity; got the following:'
337 message += '\n{}'.format(AM_START_ERROR.findall(output)[0])
338 raise TargetError(message)
339 else: # not all digits
340 if AM_START_ERROR.findall(output):
341 message = 'Could not start activity; got the following:\n{}'
342 raise TargetError(message.format(AM_START_ERROR.findall(output)[0]))
343 else:
344 message = 'adb has returned early; did not get an exit code. '\
345 'Was kill-server invoked?'
346 raise TargetError(message)
347 else: # do not check exit code
348 output, _ = check_output(full_command, timeout, shell=True)
349 return output
350
351
352def adb_background_shell(device, command,
353 stdout=subprocess.PIPE,
354 stderr=subprocess.PIPE,
355 as_root=False):
356 """Runs the sepcified command in a subprocess, returning the the Popen object."""
357 _check_env()
358 if as_root:
359 command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
360 device_string = ' -s {}'.format(device) if device else ''
361 full_command = 'adb{} shell "{}"'.format(device_string, escape_double_quotes(command))
362 logger.debug(full_command)
363 return subprocess.Popen(full_command, stdout=stdout, stderr=stderr, shell=True)
364
365
366def adb_list_devices():
367 output = adb_command(None, 'devices')
368 devices = []
369 for line in output.splitlines():
370 parts = [p.strip() for p in line.split()]
371 if len(parts) == 2:
372 devices.append(AdbDevice(*parts))
373 return devices
374
375
376def adb_command(device, command, timeout=None):
377 _check_env()
378 device_string = ' -s {}'.format(device) if device else ''
379 full_command = "adb{} {}".format(device_string, command)
380 logger.debug(full_command)
381 output, _ = check_output(full_command, timeout, shell=True)
382 return output
383
384
385# Messy environment initialisation stuff...
386
387class _AndroidEnvironment(object):
388
389 def __init__(self):
390 self.android_home = None
391 self.platform_tools = None
392 self.adb = None
393 self.aapt = None
394 self.fastboot = None
395
396
397def _initialize_with_android_home(env):
398 logger.debug('Using ANDROID_HOME from the environment.')
399 env.android_home = android_home
400 env.platform_tools = os.path.join(android_home, 'platform-tools')
401 os.environ['PATH'] += os.pathsep + env.platform_tools
402 _init_common(env)
403 return env
404
405
406def _initialize_without_android_home(env):
Javi Merino7f32efc2015-12-15 13:43:56 +0000407 adb_full_path = which('adb')
408 if adb_full_path:
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100409 env.adb = 'adb'
410 else:
411 raise HostError('ANDROID_HOME is not set and adb is not in PATH. '
412 'Have you installed Android SDK?')
413 logger.debug('Discovering ANDROID_HOME from adb path.')
Javi Merino7f32efc2015-12-15 13:43:56 +0000414 env.platform_tools = os.path.dirname(adb_full_path)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100415 env.android_home = os.path.dirname(env.platform_tools)
416 _init_common(env)
417 return env
418
419
420def _init_common(env):
421 logger.debug('ANDROID_HOME: {}'.format(env.android_home))
422 build_tools_directory = os.path.join(env.android_home, 'build-tools')
423 if not os.path.isdir(build_tools_directory):
424 msg = '''ANDROID_HOME ({}) does not appear to have valid Android SDK install
425 (cannot find build-tools)'''
426 raise HostError(msg.format(env.android_home))
427 versions = os.listdir(build_tools_directory)
428 for version in reversed(sorted(versions)):
429 aapt_path = os.path.join(build_tools_directory, version, 'aapt')
430 if os.path.isfile(aapt_path):
431 logger.debug('Using aapt for version {}'.format(version))
432 env.aapt = aapt_path
433 break
434 else:
435 raise HostError('aapt not found. Please make sure at least one Android '
436 'platform is installed.')
437
438
439def _check_env():
440 global android_home, platform_tools, adb, aapt # pylint: disable=W0603
441 if not android_home:
442 android_home = os.getenv('ANDROID_HOME')
443 if android_home:
444 _env = _initialize_with_android_home(_AndroidEnvironment())
445 else:
446 _env = _initialize_without_android_home(_AndroidEnvironment())
447 android_home = _env.android_home
448 platform_tools = _env.platform_tools
449 adb = _env.adb
450 aapt = _env.aapt