blob: 8328b223cc219ffca06683837191e4dbeba08531 [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
29from devlib.exception import TargetError, HostError
30from devlib.utils.misc import check_output, which
31from 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
160 def __init__(self, device=None, timeout=10):
161 self.timeout = timeout
162 if device is None:
163 device = adb_get_device(timeout=timeout)
164 self.device = device
165 adb_connect(self.device)
166 AdbConnection.active_connections[self.device] += 1
167
168 def push(self, source, dest, timeout=None):
169 if timeout is None:
170 timeout = self.timeout
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000171 command = "push '{}' '{}'".format(source, dest)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100172 return adb_command(self.device, command, timeout=timeout)
173
174 def pull(self, source, dest, timeout=None):
175 if timeout is None:
176 timeout = self.timeout
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000177 # Pull all files matching a wildcard expression
178 if os.path.isdir(dest) and \
Sebastian Goscikaab487c2016-02-15 15:21:40 +0000179 ('*' in source or '?' in source):
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000180 command = 'shell ls {}'.format(source)
181 output = adb_command(self.device, command, timeout=timeout)
182 for line in output.splitlines():
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000183 command = "pull '{}' '{}'".format(line, dest)
Patrick Bellasic93e3d62015-11-25 11:19:08 +0000184 adb_command(self.device, command, timeout=timeout)
185 return
Sebastian Goscik1424ceb2016-02-15 15:27:19 +0000186 command = "pull '{}' '{}'".format(source, dest)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100187 return adb_command(self.device, command, timeout=timeout)
188
189 def execute(self, command, timeout=None, check_exit_code=False, as_root=False):
190 return adb_shell(self.device, command, timeout, check_exit_code, as_root)
191
192 def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
193 return adb_background_shell(self.device, command, stdout, stderr, as_root)
194
195 def close(self):
196 AdbConnection.active_connections[self.device] -= 1
197 if AdbConnection.active_connections[self.device] <= 0:
198 adb_disconnect(self.device)
199 del AdbConnection.active_connections[self.device]
200
201 def cancel_running_command(self):
202 # adbd multiplexes commands so that they don't interfer with each
203 # other, so there is no need to explicitly cancel a running command
204 # before the next one can be issued.
205 pass
206
207
208def fastboot_command(command, timeout=None):
209 _check_env()
210 full_command = "fastboot {}".format(command)
211 logger.debug(full_command)
212 output, _ = check_output(full_command, timeout, shell=True)
213 return output
214
215
216def fastboot_flash_partition(partition, path_to_image):
217 command = 'flash {} {}'.format(partition, path_to_image)
218 fastboot_command(command)
219
220
221def adb_get_device(timeout=None):
222 """
223 Returns the serial number of a connected android device.
224
225 If there are more than one device connected to the machine, or it could not
226 find any device connected, :class:`devlib.exceptions.HostError` is raised.
227 """
228 # TODO this is a hacky way to issue a adb command to all listed devices
229
230 # The output of calling adb devices consists of a heading line then
231 # a list of the devices sperated by new line
232 # The last line is a blank new line. in otherwords, if there is a device found
233 # then the output length is 2 + (1 for each device)
234 start = time.time()
235 while True:
236 output = adb_command(None, "devices").splitlines() # pylint: disable=E1103
237 output_length = len(output)
238 if output_length == 3:
239 # output[1] is the 2nd line in the output which has the device name
240 # Splitting the line by '\t' gives a list of two indexes, which has
241 # device serial in 0 number and device type in 1.
242 return output[1].split('\t')[0]
243 elif output_length > 3:
244 message = '{} Android devices found; either explicitly specify ' +\
Sebastian Goscikaab487c2016-02-15 15:21:40 +0000245 'the device you want, or make sure only one is connected.'
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100246 raise HostError(message.format(output_length - 2))
247 else:
248 if timeout < time.time() - start:
249 raise HostError('No device is connected and available')
250 time.sleep(1)
251
252
253def adb_connect(device, timeout=None, attempts=MAX_ATTEMPTS):
254 _check_env()
255 tries = 0
256 output = None
257 while tries <= attempts:
258 tries += 1
259 if device:
260 command = 'adb connect {}'.format(device)
261 logger.debug(command)
262 output, _ = check_output(command, shell=True, timeout=timeout)
263 if _ping(device):
264 break
265 time.sleep(10)
266 else: # did not connect to the device
267 message = 'Could not connect to {}'.format(device or 'a device')
268 if output:
269 message += '; got: "{}"'.format(output)
270 raise HostError(message)
271
272
273def adb_disconnect(device):
274 _check_env()
275 if not device:
276 return
277 if ":" in device:
278 command = "adb disconnect " + device
279 logger.debug(command)
280 retval = subprocess.call(command, stdout=open(os.devnull, 'wb'), shell=True)
281 if retval:
282 raise TargetError('"{}" returned {}'.format(command, retval))
283
284
285def _ping(device):
286 _check_env()
287 device_string = ' -s {}'.format(device) if device else ''
288 command = "adb{} shell \"ls / > /dev/null\"".format(device_string)
289 logger.debug(command)
290 result = subprocess.call(command, stderr=subprocess.PIPE, shell=True)
291 if not result:
292 return True
293 else:
294 return False
295
296
297def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA
298 _check_env()
299 if as_root:
Sergei Trofimov171cc252015-12-14 17:21:47 +0000300 command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100301 device_string = ' -s {}'.format(device) if device else ''
302 full_command = 'adb{} shell "{}"'.format(device_string,
303 escape_double_quotes(command))
304 logger.debug(full_command)
305 if check_exit_code:
Sergei Trofimovf52bf792015-12-11 17:18:18 +0000306 actual_command = "adb{} shell '({}); echo \"\n$?\"'".format(device_string,
Sergei Trofimov64261a62015-11-24 12:50:02 +0000307 escape_single_quotes(command))
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100308 raw_output, error = check_output(actual_command, timeout, shell=True)
309 if raw_output:
310 try:
Sergei Trofimov64261a62015-11-24 12:50:02 +0000311 output, exit_code, _ = raw_output.rsplit('\r\n', 2)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100312 except ValueError:
Sergei Trofimov64261a62015-11-24 12:50:02 +0000313 exit_code, _ = raw_output.rsplit('\r\n', 1)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100314 output = ''
315 else: # raw_output is empty
316 exit_code = '969696' # just because
317 output = ''
318
319 exit_code = exit_code.strip()
320 if exit_code.isdigit():
321 if int(exit_code):
322 message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'
323 raise TargetError(message.format(exit_code, full_command, output, error))
324 elif AM_START_ERROR.findall(output):
325 message = 'Could not start activity; got the following:'
326 message += '\n{}'.format(AM_START_ERROR.findall(output)[0])
327 raise TargetError(message)
328 else: # not all digits
329 if AM_START_ERROR.findall(output):
330 message = 'Could not start activity; got the following:\n{}'
331 raise TargetError(message.format(AM_START_ERROR.findall(output)[0]))
332 else:
333 message = 'adb has returned early; did not get an exit code. '\
334 'Was kill-server invoked?'
335 raise TargetError(message)
336 else: # do not check exit code
337 output, _ = check_output(full_command, timeout, shell=True)
338 return output
339
340
341def adb_background_shell(device, command,
342 stdout=subprocess.PIPE,
343 stderr=subprocess.PIPE,
344 as_root=False):
345 """Runs the sepcified command in a subprocess, returning the the Popen object."""
346 _check_env()
347 if as_root:
348 command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
349 device_string = ' -s {}'.format(device) if device else ''
350 full_command = 'adb{} shell "{}"'.format(device_string, escape_double_quotes(command))
351 logger.debug(full_command)
352 return subprocess.Popen(full_command, stdout=stdout, stderr=stderr, shell=True)
353
354
355def adb_list_devices():
356 output = adb_command(None, 'devices')
357 devices = []
358 for line in output.splitlines():
359 parts = [p.strip() for p in line.split()]
360 if len(parts) == 2:
361 devices.append(AdbDevice(*parts))
362 return devices
363
364
365def adb_command(device, command, timeout=None):
366 _check_env()
367 device_string = ' -s {}'.format(device) if device else ''
368 full_command = "adb{} {}".format(device_string, command)
369 logger.debug(full_command)
370 output, _ = check_output(full_command, timeout, shell=True)
371 return output
372
373
374# Messy environment initialisation stuff...
375
376class _AndroidEnvironment(object):
377
378 def __init__(self):
379 self.android_home = None
380 self.platform_tools = None
381 self.adb = None
382 self.aapt = None
383 self.fastboot = None
384
385
386def _initialize_with_android_home(env):
387 logger.debug('Using ANDROID_HOME from the environment.')
388 env.android_home = android_home
389 env.platform_tools = os.path.join(android_home, 'platform-tools')
390 os.environ['PATH'] += os.pathsep + env.platform_tools
391 _init_common(env)
392 return env
393
394
395def _initialize_without_android_home(env):
Javi Merino7f32efc2015-12-15 13:43:56 +0000396 adb_full_path = which('adb')
397 if adb_full_path:
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100398 env.adb = 'adb'
399 else:
400 raise HostError('ANDROID_HOME is not set and adb is not in PATH. '
401 'Have you installed Android SDK?')
402 logger.debug('Discovering ANDROID_HOME from adb path.')
Javi Merino7f32efc2015-12-15 13:43:56 +0000403 env.platform_tools = os.path.dirname(adb_full_path)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100404 env.android_home = os.path.dirname(env.platform_tools)
405 _init_common(env)
406 return env
407
408
409def _init_common(env):
410 logger.debug('ANDROID_HOME: {}'.format(env.android_home))
411 build_tools_directory = os.path.join(env.android_home, 'build-tools')
412 if not os.path.isdir(build_tools_directory):
413 msg = '''ANDROID_HOME ({}) does not appear to have valid Android SDK install
414 (cannot find build-tools)'''
415 raise HostError(msg.format(env.android_home))
416 versions = os.listdir(build_tools_directory)
417 for version in reversed(sorted(versions)):
418 aapt_path = os.path.join(build_tools_directory, version, 'aapt')
419 if os.path.isfile(aapt_path):
420 logger.debug('Using aapt for version {}'.format(version))
421 env.aapt = aapt_path
422 break
423 else:
424 raise HostError('aapt not found. Please make sure at least one Android '
425 'platform is installed.')
426
427
428def _check_env():
429 global android_home, platform_tools, adb, aapt # pylint: disable=W0603
430 if not android_home:
431 android_home = os.getenv('ANDROID_HOME')
432 if android_home:
433 _env = _initialize_with_android_home(_AndroidEnvironment())
434 else:
435 _env = _initialize_without_android_home(_AndroidEnvironment())
436 android_home = _env.android_home
437 platform_tools = _env.platform_tools
438 adb = _env.adb
439 aapt = _env.aapt