OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 | 4 |
5 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
6 | 6 |
7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
8 """ | 8 """ |
9 # pylint: disable=W0613 | 9 # pylint: disable=unused-argument |
10 | 10 |
11 import logging | 11 import logging |
12 import multiprocessing | 12 import multiprocessing |
13 import os | 13 import os |
14 import re | 14 import re |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
17 import time | 17 import time |
18 import zipfile | 18 import zipfile |
19 | 19 |
20 import pylib.android_commands | 20 import pylib.android_commands |
21 from pylib import cmd_helper | 21 from pylib import cmd_helper |
22 from pylib import constants | 22 from pylib import constants |
23 from pylib.device import adb_wrapper | 23 from pylib.device import adb_wrapper |
24 from pylib.device import decorators | 24 from pylib.device import decorators |
25 from pylib.device import device_errors | 25 from pylib.device import device_errors |
26 from pylib.device import intent | 26 from pylib.device import intent |
| 27 from pylib.device import logcat_monitor |
27 from pylib.device.commands import install_commands | 28 from pylib.device.commands import install_commands |
28 from pylib.utils import apk_helper | 29 from pylib.utils import apk_helper |
29 from pylib.utils import device_temp_file | 30 from pylib.utils import device_temp_file |
30 from pylib.utils import host_utils | 31 from pylib.utils import host_utils |
31 from pylib.utils import md5sum | 32 from pylib.utils import md5sum |
32 from pylib.utils import parallelizer | 33 from pylib.utils import parallelizer |
33 from pylib.utils import timeout_retry | 34 from pylib.utils import timeout_retry |
34 from pylib.utils import zip_utils | 35 from pylib.utils import zip_utils |
35 | 36 |
36 _DEFAULT_TIMEOUT = 30 | 37 _DEFAULT_TIMEOUT = 30 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 # efficient than first appending an end-line to each line and then joining | 97 # efficient than first appending an end-line to each line and then joining |
97 # all of them together. | 98 # all of them together. |
98 return ''.join(s for line in lines for s in (line, '\n')) | 99 return ''.join(s for line in lines for s in (line, '\n')) |
99 | 100 |
100 | 101 |
101 class DeviceUtils(object): | 102 class DeviceUtils(object): |
102 | 103 |
103 _MAX_ADB_COMMAND_LENGTH = 512 | 104 _MAX_ADB_COMMAND_LENGTH = 512 |
104 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 105 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
105 | 106 |
| 107 # Property in /data/local.prop that controls Java assertions. |
| 108 JAVA_ASSERT_PROPERTY = 'dalvik.vm.enableassertions' |
| 109 |
106 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, | 110 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
107 default_retries=_DEFAULT_RETRIES): | 111 default_retries=_DEFAULT_RETRIES): |
108 """DeviceUtils constructor. | 112 """DeviceUtils constructor. |
109 | 113 |
110 Args: | 114 Args: |
111 device: Either a device serial, an existing AdbWrapper instance, or an | 115 device: Either a device serial, an existing AdbWrapper instance, or an |
112 an existing AndroidCommands instance. | 116 an existing AndroidCommands instance. |
113 default_timeout: An integer containing the default number of seconds to | 117 default_timeout: An integer containing the default number of seconds to |
114 wait for an operation to complete if no explicit value | 118 wait for an operation to complete if no explicit value |
115 is provided. | 119 is provided. |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 timeout_retry.WaitFor(boot_completed) | 347 timeout_retry.WaitFor(boot_completed) |
344 if wifi: | 348 if wifi: |
345 timeout_retry.WaitFor(wifi_enabled) | 349 timeout_retry.WaitFor(wifi_enabled) |
346 | 350 |
347 REBOOT_DEFAULT_TIMEOUT = 10 * _DEFAULT_TIMEOUT | 351 REBOOT_DEFAULT_TIMEOUT = 10 * _DEFAULT_TIMEOUT |
348 REBOOT_DEFAULT_RETRIES = _DEFAULT_RETRIES | 352 REBOOT_DEFAULT_RETRIES = _DEFAULT_RETRIES |
349 | 353 |
350 @decorators.WithTimeoutAndRetriesDefaults( | 354 @decorators.WithTimeoutAndRetriesDefaults( |
351 REBOOT_DEFAULT_TIMEOUT, | 355 REBOOT_DEFAULT_TIMEOUT, |
352 REBOOT_DEFAULT_RETRIES) | 356 REBOOT_DEFAULT_RETRIES) |
353 def Reboot(self, block=True, timeout=None, retries=None): | 357 def Reboot(self, block=True, wifi=False, timeout=None, retries=None): |
354 """Reboot the device. | 358 """Reboot the device. |
355 | 359 |
356 Args: | 360 Args: |
357 block: A boolean indicating if we should wait for the reboot to complete. | 361 block: A boolean indicating if we should wait for the reboot to complete. |
| 362 wifi: A boolean indicating if we should wait for wifi to be enabled after |
| 363 the reboot. The option has no effect unless |block| is also True. |
358 timeout: timeout in seconds | 364 timeout: timeout in seconds |
359 retries: number of retries | 365 retries: number of retries |
360 | 366 |
361 Raises: | 367 Raises: |
362 CommandTimeoutError on timeout. | 368 CommandTimeoutError on timeout. |
363 DeviceUnreachableError on missing device. | 369 DeviceUnreachableError on missing device. |
364 """ | 370 """ |
365 def device_offline(): | 371 def device_offline(): |
366 return not self.IsOnline() | 372 return not self.IsOnline() |
367 | 373 |
368 self.adb.Reboot() | 374 self.adb.Reboot() |
369 self._cache = {} | 375 self._cache = {} |
370 timeout_retry.WaitFor(device_offline, wait_period=1) | 376 timeout_retry.WaitFor(device_offline, wait_period=1) |
371 if block: | 377 if block: |
372 self.WaitUntilFullyBooted() | 378 self.WaitUntilFullyBooted(wifi=wifi) |
373 | 379 |
374 INSTALL_DEFAULT_TIMEOUT = 4 * _DEFAULT_TIMEOUT | 380 INSTALL_DEFAULT_TIMEOUT = 4 * _DEFAULT_TIMEOUT |
375 INSTALL_DEFAULT_RETRIES = _DEFAULT_RETRIES | 381 INSTALL_DEFAULT_RETRIES = _DEFAULT_RETRIES |
376 | 382 |
377 @decorators.WithTimeoutAndRetriesDefaults( | 383 @decorators.WithTimeoutAndRetriesDefaults( |
378 INSTALL_DEFAULT_TIMEOUT, | 384 INSTALL_DEFAULT_TIMEOUT, |
379 INSTALL_DEFAULT_RETRIES) | 385 INSTALL_DEFAULT_RETRIES) |
380 def Install(self, apk_path, reinstall=False, timeout=None, retries=None): | 386 def Install(self, apk_path, reinstall=False, timeout=None, retries=None): |
381 """Install an APK. | 387 """Install an APK. |
382 | 388 |
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1031 timeout: timeout in seconds | 1037 timeout: timeout in seconds |
1032 retries: number of retries | 1038 retries: number of retries |
1033 | 1039 |
1034 Returns: | 1040 Returns: |
1035 True if the device-side property changed and a restart is required as a | 1041 True if the device-side property changed and a restart is required as a |
1036 result, False otherwise. | 1042 result, False otherwise. |
1037 | 1043 |
1038 Raises: | 1044 Raises: |
1039 CommandTimeoutError on timeout. | 1045 CommandTimeoutError on timeout. |
1040 """ | 1046 """ |
1041 return self.old_interface.SetJavaAssertsEnabled(enabled) | 1047 def find_property(lines, property_name): |
| 1048 for index, line in enumerate(lines): |
| 1049 key, value = (s.strip() for s in line.split('=', 1)) |
| 1050 if key == property_name: |
| 1051 return index, value |
| 1052 return None, '' |
| 1053 |
| 1054 new_value = 'all' if enabled else '' |
| 1055 |
| 1056 # First ensure the desired property is persisted. |
| 1057 try: |
| 1058 properties = self.ReadFile( |
| 1059 constants.DEVICE_LOCAL_PROPERTIES_PATH).splitlines() |
| 1060 except device_errors.CommandFailedError: |
| 1061 properties = [] |
| 1062 index, value = find_property(properties, self.JAVA_ASSERT_PROPERTY) |
| 1063 if new_value != value: |
| 1064 if new_value: |
| 1065 new_line = '%s=%s' % (self.JAVA_ASSERT_PROPERTY, new_value) |
| 1066 if index is None: |
| 1067 properties.append(new_line) |
| 1068 else: |
| 1069 properties[index] = new_line |
| 1070 else: |
| 1071 assert index is not None # since new_value == '' and new_value != value |
| 1072 properties.pop(index) |
| 1073 self.WriteFile(constants.DEVICE_LOCAL_PROPERTIES_PATH, |
| 1074 _JoinLines(properties)) |
| 1075 |
| 1076 # Next, check the current runtime value is what we need, and |
| 1077 # if not, set it and report that a reboot is required. |
| 1078 value = self.GetProp(self.JAVA_ASSERT_PROPERTY) |
| 1079 if new_value != value: |
| 1080 self.SetProp(self.JAVA_ASSERT_PROPERTY, new_value) |
| 1081 return True |
| 1082 else: |
| 1083 return False |
1042 | 1084 |
1043 | 1085 |
1044 @property | 1086 @property |
1045 def build_description(self): | 1087 def build_description(self): |
1046 """Returns the build description of the system. | 1088 """Returns the build description of the system. |
1047 | 1089 |
1048 For example: | 1090 For example: |
1049 nakasi-user 4.4.4 KTU84P 1227136 release-keys | 1091 nakasi-user 4.4.4 KTU84P 1227136 release-keys |
1050 """ | 1092 """ |
1051 return self.GetProp('ro.build.description', cache=True) | 1093 return self.GetProp('ro.build.description', cache=True) |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 Returns: | 1323 Returns: |
1282 A 2-tuple containing: | 1324 A 2-tuple containing: |
1283 - A dict containing the overall memory usage statistics for the PID. | 1325 - A dict containing the overall memory usage statistics for the PID. |
1284 - A dict containing memory usage statistics broken down by mapping. | 1326 - A dict containing memory usage statistics broken down by mapping. |
1285 | 1327 |
1286 Raises: | 1328 Raises: |
1287 CommandTimeoutError on timeout. | 1329 CommandTimeoutError on timeout. |
1288 """ | 1330 """ |
1289 return self.old_interface.GetMemoryUsageForPid(pid) | 1331 return self.old_interface.GetMemoryUsageForPid(pid) |
1290 | 1332 |
| 1333 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1334 def GetLogcatMonitor(self, timeout=None, retries=None, *args, **kwargs): |
| 1335 """Returns a new LogcatMonitor associated with this device. |
| 1336 |
| 1337 Parameters passed to this function are passed directly to |
| 1338 |logcat_monitor.LogcatMonitor| and are documented there. |
| 1339 |
| 1340 Args: |
| 1341 timeout: timeout in seconds |
| 1342 retries: number of retries |
| 1343 """ |
| 1344 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) |
| 1345 |
1291 def __str__(self): | 1346 def __str__(self): |
1292 """Returns the device serial.""" | 1347 """Returns the device serial.""" |
1293 return self.adb.GetDeviceSerial() | 1348 return self.adb.GetDeviceSerial() |
1294 | 1349 |
1295 @classmethod | 1350 @classmethod |
1296 def parallel(cls, devices=None, async=False): | 1351 def parallel(cls, devices=None, async=False): |
1297 """Creates a Parallelizer to operate over the provided list of devices. | 1352 """Creates a Parallelizer to operate over the provided list of devices. |
1298 | 1353 |
1299 If |devices| is either |None| or an empty list, the Parallelizer will | 1354 If |devices| is either |None| or an empty list, the Parallelizer will |
1300 operate over all attached devices. | 1355 operate over all attached devices. |
1301 | 1356 |
1302 Args: | 1357 Args: |
1303 devices: A list of either DeviceUtils instances or objects from | 1358 devices: A list of either DeviceUtils instances or objects from |
1304 from which DeviceUtils instances can be constructed. If None, | 1359 from which DeviceUtils instances can be constructed. If None, |
1305 all attached devices will be used. | 1360 all attached devices will be used. |
1306 async: If true, returns a Parallelizer that runs operations | 1361 async: If true, returns a Parallelizer that runs operations |
1307 asynchronously. | 1362 asynchronously. |
1308 | 1363 |
1309 Returns: | 1364 Returns: |
1310 A Parallelizer operating over |devices|. | 1365 A Parallelizer operating over |devices|. |
1311 """ | 1366 """ |
1312 if not devices: | 1367 if not devices: |
1313 devices = adb_wrapper.AdbWrapper.GetDevices() | 1368 devices = adb_wrapper.AdbWrapper.GetDevices() |
1314 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1369 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1315 if async: | 1370 if async: |
1316 return parallelizer.Parallelizer(devices) | 1371 return parallelizer.Parallelizer(devices) |
1317 else: | 1372 else: |
1318 return parallelizer.SyncParallelizer(devices) | 1373 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |