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 posixpath | 16 import posixpath |
17 import re | 17 import re |
18 import subprocess | 18 import subprocess |
19 import sys | 19 import sys |
20 import time | 20 import time |
21 | 21 |
22 from pylib import android_commands | 22 from pylib import android_commands |
23 from pylib import constants | 23 from pylib import constants |
24 from pylib import device_settings | 24 from pylib import device_settings |
| 25 from pylib.device import battery_utils |
25 from pylib.device import device_blacklist | 26 from pylib.device import device_blacklist |
26 from pylib.device import device_errors | 27 from pylib.device import device_errors |
27 from pylib.device import device_utils | 28 from pylib.device import device_utils |
28 from pylib.utils import run_tests_helper | 29 from pylib.utils import run_tests_helper |
29 from pylib.utils import timeout_retry | 30 from pylib.utils import timeout_retry |
30 | 31 |
31 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | 32 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, |
32 'third_party', 'android_testrunner')) | 33 'third_party', 'android_testrunner')) |
33 import errors | 34 import errors |
34 | 35 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 157 |
157 def WipeDeviceIfPossible(device, timeout, options): | 158 def WipeDeviceIfPossible(device, timeout, options): |
158 try: | 159 try: |
159 device.EnableRoot() | 160 device.EnableRoot() |
160 WipeDeviceData(device, options) | 161 WipeDeviceData(device, options) |
161 device.Reboot(True, timeout=timeout, retries=0) | 162 device.Reboot(True, timeout=timeout, retries=0) |
162 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): | 163 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): |
163 pass | 164 pass |
164 | 165 |
165 | 166 |
166 def ChargeDeviceToLevel(device, level): | |
167 def device_charged(): | |
168 battery_level = device.GetBatteryInfo().get('level') | |
169 if battery_level is None: | |
170 logging.warning('Unable to find current battery level.') | |
171 battery_level = 100 | |
172 else: | |
173 logging.info('current battery level: %d', battery_level) | |
174 battery_level = int(battery_level) | |
175 return battery_level >= level | |
176 | |
177 timeout_retry.WaitFor(device_charged, wait_period=60) | |
178 | |
179 | |
180 def ProvisionDevice(device, options): | 167 def ProvisionDevice(device, options): |
181 if options.reboot_timeout: | 168 if options.reboot_timeout: |
182 reboot_timeout = options.reboot_timeout | 169 reboot_timeout = options.reboot_timeout |
183 elif (device.build_version_sdk >= | 170 elif (device.build_version_sdk >= |
184 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 171 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
185 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP | 172 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
186 else: | 173 else: |
187 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP | 174 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
188 | 175 |
189 try: | 176 try: |
(...skipping 11 matching lines...) Expand all Loading... |
201 device, device_settings.DISABLE_LOCATION_SETTINGS) | 188 device, device_settings.DISABLE_LOCATION_SETTINGS) |
202 else: | 189 else: |
203 device_settings.ConfigureContentSettings( | 190 device_settings.ConfigureContentSettings( |
204 device, device_settings.ENABLE_LOCATION_SETTINGS) | 191 device, device_settings.ENABLE_LOCATION_SETTINGS) |
205 device_settings.SetLockScreenSettings(device) | 192 device_settings.SetLockScreenSettings(device) |
206 if options.disable_network: | 193 if options.disable_network: |
207 device_settings.ConfigureContentSettings( | 194 device_settings.ConfigureContentSettings( |
208 device, device_settings.NETWORK_DISABLED_SETTINGS) | 195 device, device_settings.NETWORK_DISABLED_SETTINGS) |
209 if options.min_battery_level is not None: | 196 if options.min_battery_level is not None: |
210 try: | 197 try: |
211 device.SetCharging(True) | 198 battery = battery_utils.BatteryUtils(device) |
212 ChargeDeviceToLevel(device, options.min_battery_level) | 199 battery.ChargeDeviceToLevel(options.min_battery_level) |
213 except device_errors.CommandFailedError as e: | 200 except device_errors.CommandFailedError as e: |
214 logging.exception('Unable to charge device to specified level.') | 201 logging.exception('Unable to charge device to specified level.') |
215 | 202 |
216 if not options.skip_wipe: | 203 if not options.skip_wipe: |
217 device.Reboot(True, timeout=reboot_timeout, retries=0) | 204 device.Reboot(True, timeout=reboot_timeout, retries=0) |
218 device.RunShellCommand('date -s %s' % time.strftime('%Y%m%d.%H%M%S', | 205 device.RunShellCommand('date -s %s' % time.strftime('%Y%m%d.%H%M%S', |
219 time.gmtime()), | 206 time.gmtime()), |
220 as_root=True) | 207 as_root=True) |
221 props = device.RunShellCommand('getprop') | 208 props = device.RunShellCommand('getprop') |
222 for prop in props: | 209 for prop in props: |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 parser.add_argument('--adb-key-files', type=str, nargs='+', | 282 parser.add_argument('--adb-key-files', type=str, nargs='+', |
296 help='list of adb keys to push to device') | 283 help='list of adb keys to push to device') |
297 args = parser.parse_args() | 284 args = parser.parse_args() |
298 constants.SetBuildType(args.target) | 285 constants.SetBuildType(args.target) |
299 | 286 |
300 return ProvisionDevices(args) | 287 return ProvisionDevices(args) |
301 | 288 |
302 | 289 |
303 if __name__ == '__main__': | 290 if __name__ == '__main__': |
304 sys.exit(main()) | 291 sys.exit(main()) |
OLD | NEW |