OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Provisions Android devices with settings required for bots. | 7 """Provisions Android devices with settings required for bots. |
8 | 8 |
9 Usage: | 9 Usage: |
10 ./provision_devices.py [-d <device serial number>] | 10 ./provision_devices.py [-d <device serial number>] |
11 """ | 11 """ |
12 | 12 |
13 import argparse | 13 import argparse |
14 import logging | 14 import logging |
15 import os | 15 import os |
16 import re | 16 import re |
17 import subprocess | 17 import subprocess |
18 import sys | 18 import sys |
19 import time | 19 import time |
20 | 20 |
21 from pylib import android_commands | 21 from pylib import android_commands |
22 from pylib import constants | 22 from pylib import constants |
23 from pylib import device_settings | 23 from pylib import device_settings |
24 from pylib.device import device_blacklist | 24 from pylib.device import device_blacklist |
25 from pylib.device import device_errors | 25 from pylib.device import device_errors |
26 from pylib.device import device_utils | 26 from pylib.device import device_utils |
27 from pylib.utils import run_tests_helper | 27 from pylib.utils import run_tests_helper |
| 28 from pylib.utils import timeout_retry |
28 | 29 |
29 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | 30 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, |
30 'third_party', 'android_testrunner')) | 31 'third_party', 'android_testrunner')) |
31 import errors | 32 import errors |
32 | 33 |
33 | 34 |
34 class _DEFAULT_TIMEOUTS(object): | 35 class _DEFAULT_TIMEOUTS(object): |
35 # L can take a while to reboot after a wipe. | 36 # L can take a while to reboot after a wipe. |
36 LOLLIPOP = 600 | 37 LOLLIPOP = 600 |
37 PRE_LOLLIPOP = 180 | 38 PRE_LOLLIPOP = 180 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 # We can safely ignore the exception because we don't expect adb_reboot | 78 # We can safely ignore the exception because we don't expect adb_reboot |
78 # to be running. | 79 # to be running. |
79 pass | 80 pass |
80 # Push adb_reboot | 81 # Push adb_reboot |
81 logging.info(' Pushing adb_reboot ...') | 82 logging.info(' Pushing adb_reboot ...') |
82 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, | 83 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, |
83 'out/%s/adb_reboot' % target) | 84 'out/%s/adb_reboot' % target) |
84 device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')]) | 85 device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')]) |
85 # Launch adb_reboot | 86 # Launch adb_reboot |
86 logging.info(' Launching adb_reboot ...') | 87 logging.info(' Launching adb_reboot ...') |
87 device.old_interface.GetAndroidToolStatusAndOutput( | 88 device.RunShellCommand([ |
88 '/data/local/tmp/adb_reboot') | 89 device.GetDevicePieWrapper(), |
| 90 '/data/local/tmp/adb_reboot']) |
89 | 91 |
90 | 92 |
91 def _ConfigureLocalProperties(device, java_debug=True): | 93 def _ConfigureLocalProperties(device, java_debug=True): |
92 """Set standard readonly testing device properties prior to reboot.""" | 94 """Set standard readonly testing device properties prior to reboot.""" |
93 local_props = [ | 95 local_props = [ |
94 'persist.sys.usb.config=adb', | 96 'persist.sys.usb.config=adb', |
95 'ro.monkey=1', | 97 'ro.monkey=1', |
96 'ro.test_harness=1', | 98 'ro.test_harness=1', |
97 'ro.audio.silent=1', | 99 'ro.audio.silent=1', |
98 'ro.setupwizard.mode=DISABLED', | 100 'ro.setupwizard.mode=DISABLED', |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 144 |
143 def WipeDeviceIfPossible(device, timeout): | 145 def WipeDeviceIfPossible(device, timeout): |
144 try: | 146 try: |
145 device.EnableRoot() | 147 device.EnableRoot() |
146 WipeDeviceData(device) | 148 WipeDeviceData(device) |
147 device.Reboot(True, timeout=timeout, retries=0) | 149 device.Reboot(True, timeout=timeout, retries=0) |
148 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): | 150 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): |
149 pass | 151 pass |
150 | 152 |
151 | 153 |
| 154 def ChargeDeviceToLevel(device, level): |
| 155 def device_charged(): |
| 156 battery_level = device.GetBatteryInfo().get('level') |
| 157 if battery_level is None: |
| 158 logging.warning('Unable to find current battery level.') |
| 159 battery_level = 100 |
| 160 else: |
| 161 logging.info('current battery level: %d', battery_level) |
| 162 battery_level = int(battery_level) |
| 163 return battery_level >= level |
| 164 |
| 165 timeout_retry.WaitFor(device_charged, wait_period=60) |
| 166 |
| 167 |
152 def ProvisionDevice(device, options): | 168 def ProvisionDevice(device, options): |
153 if options.reboot_timeout: | 169 if options.reboot_timeout: |
154 reboot_timeout = options.reboot_timeout | 170 reboot_timeout = options.reboot_timeout |
155 elif (device.build_version_sdk >= | 171 elif (device.build_version_sdk >= |
156 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 172 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
157 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP | 173 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
158 else: | 174 else: |
159 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP | 175 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
160 | 176 |
161 try: | 177 try: |
(...skipping 11 matching lines...) Expand all Loading... |
173 device, device_settings.DISABLE_LOCATION_SETTINGS) | 189 device, device_settings.DISABLE_LOCATION_SETTINGS) |
174 else: | 190 else: |
175 device_settings.ConfigureContentSettings( | 191 device_settings.ConfigureContentSettings( |
176 device, device_settings.ENABLE_LOCATION_SETTINGS) | 192 device, device_settings.ENABLE_LOCATION_SETTINGS) |
177 device_settings.SetLockScreenSettings(device) | 193 device_settings.SetLockScreenSettings(device) |
178 if options.disable_network: | 194 if options.disable_network: |
179 device_settings.ConfigureContentSettings( | 195 device_settings.ConfigureContentSettings( |
180 device, device_settings.NETWORK_DISABLED_SETTINGS) | 196 device, device_settings.NETWORK_DISABLED_SETTINGS) |
181 if options.min_battery_level is not None: | 197 if options.min_battery_level is not None: |
182 try: | 198 try: |
183 battery_info = device.old_interface.GetBatteryInfo() | 199 device.SetUsbCharging(True) |
184 except Exception as e: | 200 ChargeDeviceToLevel(device, options.min_battery_level) |
185 battery_info = {} | 201 except device_errors.CommandFailedError as e: |
186 logging.error('Unable to obtain battery info for %s, %s', | 202 logging.exception('Unable to charge device to specified level.') |
187 str(device), e) | |
188 | 203 |
189 while int(battery_info.get('level', 100)) < options.min_battery_level: | |
190 if not device.old_interface.IsDeviceCharging(): | |
191 if device.old_interface.CanControlUsbCharging(): | |
192 device.old_interface.EnableUsbCharging() | |
193 else: | |
194 logging.error('Device is not charging') | |
195 break | |
196 logging.info('Waiting for device to charge. Current level=%s', | |
197 battery_info.get('level', 0)) | |
198 time.sleep(60) | |
199 battery_info = device.old_interface.GetBatteryInfo() | |
200 if not options.skip_wipe: | 204 if not options.skip_wipe: |
201 device.Reboot(True, timeout=reboot_timeout, retries=0) | 205 device.Reboot(True, timeout=reboot_timeout, retries=0) |
202 device.RunShellCommand('date -s %s' % time.strftime('%Y%m%d.%H%M%S', | 206 device.RunShellCommand('date -s %s' % time.strftime('%Y%m%d.%H%M%S', |
203 time.gmtime()), | 207 time.gmtime()), |
204 as_root=True) | 208 as_root=True) |
205 props = device.RunShellCommand('getprop') | 209 props = device.RunShellCommand('getprop') |
206 for prop in props: | 210 for prop in props: |
207 logging.info(' %s' % prop) | 211 logging.info(' %s' % prop) |
208 if options.auto_reconnect: | 212 if options.auto_reconnect: |
209 PushAndLaunchAdbReboot(device, options.target) | 213 PushAndLaunchAdbReboot(device, options.target) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 help='push binary which will reboot the device on adb' | 281 help='push binary which will reboot the device on adb' |
278 ' disconnections') | 282 ' disconnections') |
279 args = parser.parse_args() | 283 args = parser.parse_args() |
280 constants.SetBuildType(args.target) | 284 constants.SetBuildType(args.target) |
281 | 285 |
282 return ProvisionDevices(args) | 286 return ProvisionDevices(args) |
283 | 287 |
284 | 288 |
285 if __name__ == '__main__': | 289 if __name__ == '__main__': |
286 sys.exit(main()) | 290 sys.exit(main()) |
OLD | NEW |