| 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>] |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 118 |
| 119 def WriteAdbKeysFile(device, adb_keys_string): | 119 def WriteAdbKeysFile(device, adb_keys_string): |
| 120 dir_path = posixpath.dirname(constants.ADB_KEYS_FILE) | 120 dir_path = posixpath.dirname(constants.ADB_KEYS_FILE) |
| 121 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) | 121 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) |
| 122 device.RunShellCommand('restorecon %s' % dir_path, as_root=True) | 122 device.RunShellCommand('restorecon %s' % dir_path, as_root=True) |
| 123 device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) | 123 device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) |
| 124 device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, | 124 device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, |
| 125 as_root=True) | 125 as_root=True) |
| 126 | 126 |
| 127 | 127 |
| 128 def WipeDeviceData(device): | 128 def WipeDeviceData(device, options): |
| 129 """Wipes data from device, keeping only the adb_keys for authorization. | 129 """Wipes data from device, keeping only the adb_keys for authorization. |
| 130 | 130 |
| 131 After wiping data on a device that has been authorized, adb can still | 131 After wiping data on a device that has been authorized, adb can still |
| 132 communicate with the device, but after reboot the device will need to be | 132 communicate with the device, but after reboot the device will need to be |
| 133 re-authorized because the adb keys file is stored in /data/misc/adb/. | 133 re-authorized because the adb keys file is stored in /data/misc/adb/. |
| 134 Thus, adb_keys file is rewritten so the device does not need to be | 134 Thus, adb_keys file is rewritten so the device does not need to be |
| 135 re-authorized. | 135 re-authorized. |
| 136 | 136 |
| 137 Arguments: | 137 Arguments: |
| 138 device: the device to wipe | 138 device: the device to wipe |
| 139 """ | 139 """ |
| 140 device_authorized = device.FileExists(constants.ADB_KEYS_FILE) | 140 device_authorized = device.FileExists(constants.ADB_KEYS_FILE) |
| 141 if device_authorized: | 141 if device_authorized: |
| 142 adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, as_root=True) | 142 adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, |
| 143 as_root=True).splitlines() |
| 143 device.RunShellCommand('wipe data', as_root=True) | 144 device.RunShellCommand('wipe data', as_root=True) |
| 144 if device_authorized: | 145 if device_authorized: |
| 145 WriteAdbKeysFile(device, adb_keys) | 146 adb_keys_set = set(adb_keys) |
| 147 for adb_key_file in options.adb_key_files or []: |
| 148 with open(adb_key_file, 'r') as f: |
| 149 adb_public_keys = f.readlines() |
| 150 adb_keys_set.update(adb_public_keys) |
| 151 WriteAdbKeysFile(device, '\n'.join(adb_keys_set)) |
| 146 | 152 |
| 147 | 153 |
| 148 def WipeDeviceIfPossible(device, timeout): | 154 def WipeDeviceIfPossible(device, timeout, options): |
| 149 try: | 155 try: |
| 150 device.EnableRoot() | 156 device.EnableRoot() |
| 151 WipeDeviceData(device) | 157 WipeDeviceData(device, options) |
| 152 device.Reboot(True, timeout=timeout, retries=0) | 158 device.Reboot(True, timeout=timeout, retries=0) |
| 153 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): | 159 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): |
| 154 pass | 160 pass |
| 155 | 161 |
| 156 | 162 |
| 157 def ChargeDeviceToLevel(device, level): | 163 def ChargeDeviceToLevel(device, level): |
| 158 def device_charged(): | 164 def device_charged(): |
| 159 battery_level = device.GetBatteryInfo().get('level') | 165 battery_level = device.GetBatteryInfo().get('level') |
| 160 if battery_level is None: | 166 if battery_level is None: |
| 161 logging.warning('Unable to find current battery level.') | 167 logging.warning('Unable to find current battery level.') |
| 162 battery_level = 100 | 168 battery_level = 100 |
| 163 else: | 169 else: |
| 164 logging.info('current battery level: %d', battery_level) | 170 logging.info('current battery level: %d', battery_level) |
| 165 battery_level = int(battery_level) | 171 battery_level = int(battery_level) |
| 166 return battery_level >= level | 172 return battery_level >= level |
| 167 | 173 |
| 168 timeout_retry.WaitFor(device_charged, wait_period=60) | 174 timeout_retry.WaitFor(device_charged, wait_period=60) |
| 169 | 175 |
| 170 | 176 |
| 171 def ProvisionDevice(device, options): | 177 def ProvisionDevice(device, options): |
| 172 if options.reboot_timeout: | 178 if options.reboot_timeout: |
| 173 reboot_timeout = options.reboot_timeout | 179 reboot_timeout = options.reboot_timeout |
| 174 elif (device.build_version_sdk >= | 180 elif (device.build_version_sdk >= |
| 175 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 181 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
| 176 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP | 182 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
| 177 else: | 183 else: |
| 178 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP | 184 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
| 179 | 185 |
| 180 if options.adb_key_files: | |
| 181 adb_keys = set() | |
| 182 for adb_key_file in options.adb_key_files: | |
| 183 with open(adb_key_file, 'r') as f: | |
| 184 adb_public_keys = f.readlines() | |
| 185 adb_keys.update(adb_public_keys) | |
| 186 WriteAdbKeysFile(device, '\n'.join(adb_keys)) | |
| 187 | |
| 188 try: | 186 try: |
| 189 if not options.skip_wipe: | 187 if not options.skip_wipe: |
| 190 WipeDeviceIfPossible(device, reboot_timeout) | 188 WipeDeviceIfPossible(device, reboot_timeout, options) |
| 191 try: | 189 try: |
| 192 device.EnableRoot() | 190 device.EnableRoot() |
| 193 except device_errors.CommandFailedError as e: | 191 except device_errors.CommandFailedError as e: |
| 194 logging.warning(str(e)) | 192 logging.warning(str(e)) |
| 195 _ConfigureLocalProperties(device, options.enable_java_debug) | 193 _ConfigureLocalProperties(device, options.enable_java_debug) |
| 196 device_settings.ConfigureContentSettings( | 194 device_settings.ConfigureContentSettings( |
| 197 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) | 195 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |
| 198 if options.disable_location: | 196 if options.disable_location: |
| 199 device_settings.ConfigureContentSettings( | 197 device_settings.ConfigureContentSettings( |
| 200 device, device_settings.DISABLE_LOCATION_SETTINGS) | 198 device, device_settings.DISABLE_LOCATION_SETTINGS) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 parser.add_argument('--adb-key-files', type=str, nargs='+', | 292 parser.add_argument('--adb-key-files', type=str, nargs='+', |
| 295 help='list of adb keys to push to device') | 293 help='list of adb keys to push to device') |
| 296 args = parser.parse_args() | 294 args = parser.parse_args() |
| 297 constants.SetBuildType(args.target) | 295 constants.SetBuildType(args.target) |
| 298 | 296 |
| 299 return ProvisionDevices(args) | 297 return ProvisionDevices(args) |
| 300 | 298 |
| 301 | 299 |
| 302 if __name__ == '__main__': | 300 if __name__ == '__main__': |
| 303 sys.exit(main()) | 301 sys.exit(main()) |
| OLD | NEW |