| 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 re |   17 import re | 
|   17 import subprocess |   18 import subprocess | 
|   18 import sys |   19 import sys | 
|   19 import time |   20 import time | 
|   20  |   21  | 
|   21 from pylib import android_commands |   22 from pylib import android_commands | 
|   22 from pylib import constants |   23 from pylib import constants | 
|   23 from pylib import device_settings |   24 from pylib import device_settings | 
|   24 from pylib.device import device_blacklist |   25 from pylib.device import device_blacklist | 
|   25 from pylib.device import device_errors |   26 from pylib.device import device_errors | 
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  106         '\n'.join(local_props), as_root=True) |  107         '\n'.join(local_props), as_root=True) | 
|  107     # Android will not respect the local props file if it is world writable. |  108     # Android will not respect the local props file if it is world writable. | 
|  108     device.RunShellCommand( |  109     device.RunShellCommand( | 
|  109         ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH], |  110         ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH], | 
|  110         as_root=True) |  111         as_root=True) | 
|  111   except device_errors.CommandFailedError as e: |  112   except device_errors.CommandFailedError as e: | 
|  112     logging.warning(str(e)) |  113     logging.warning(str(e)) | 
|  113  |  114  | 
|  114   # LOCAL_PROPERTIES_PATH = '/data/local.prop' |  115   # LOCAL_PROPERTIES_PATH = '/data/local.prop' | 
|  115  |  116  | 
 |  117 def WriteAdbKeysFile(device, adb_keys_string): | 
 |  118   dir_path = posixpath.dirname(constants.ADB_KEYS_FILE) | 
 |  119   device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) | 
 |  120   device.RunShellCommand('restorecon %s' % dir_path, as_root=True) | 
 |  121   device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) | 
 |  122   device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, | 
 |  123                          as_root=True) | 
 |  124  | 
|  116  |  125  | 
|  117 def WipeDeviceData(device): |  126 def WipeDeviceData(device): | 
|  118   """Wipes data from device, keeping only the adb_keys for authorization. |  127   """Wipes data from device, keeping only the adb_keys for authorization. | 
|  119  |  128  | 
|  120   After wiping data on a device that has been authorized, adb can still |  129   After wiping data on a device that has been authorized, adb can still | 
|  121   communicate with the device, but after reboot the device will need to be |  130   communicate with the device, but after reboot the device will need to be | 
|  122   re-authorized because the adb keys file is stored in /data/misc/adb/. |  131   re-authorized because the adb keys file is stored in /data/misc/adb/. | 
|  123   Thus, adb_keys file is rewritten so the device does not need to be |  132   Thus, adb_keys file is rewritten so the device does not need to be | 
|  124   re-authorized. |  133   re-authorized. | 
|  125  |  134  | 
|  126   Arguments: |  135   Arguments: | 
|  127     device: the device to wipe |  136     device: the device to wipe | 
|  128   """ |  137   """ | 
|  129   device_authorized = device.FileExists(constants.ADB_KEYS_FILE) |  138   device_authorized = device.FileExists(constants.ADB_KEYS_FILE) | 
|  130   if device_authorized: |  139   if device_authorized: | 
|  131     adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, as_root=True) |  140     adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, as_root=True) | 
|  132   device.RunShellCommand('wipe data', as_root=True) |  141   device.RunShellCommand('wipe data', as_root=True) | 
|  133   if device_authorized: |  142   if device_authorized: | 
|  134     path_list = constants.ADB_KEYS_FILE.split('/') |  143     WriteAdbKeysFile(device, adb_keys) | 
|  135     dir_path = '/'.join(path_list[:len(path_list)-1]) |  | 
|  136     device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) |  | 
|  137     device.RunShellCommand('restorecon %s' % dir_path, as_root=True) |  | 
|  138     device.WriteFile(constants.ADB_KEYS_FILE, adb_keys, as_root=True) |  | 
|  139     device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, |  | 
|  140                            as_root=True) |  | 
|  141  |  144  | 
|  142  |  145  | 
|  143 def WipeDeviceIfPossible(device, timeout): |  146 def WipeDeviceIfPossible(device, timeout): | 
|  144   try: |  147   try: | 
|  145     device.EnableRoot() |  148     device.EnableRoot() | 
|  146     WipeDeviceData(device) |  149     WipeDeviceData(device) | 
|  147     device.Reboot(True, timeout=timeout, retries=0) |  150     device.Reboot(True, timeout=timeout, retries=0) | 
|  148   except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): |  151   except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): | 
|  149     pass |  152     pass | 
|  150  |  153  | 
|  151  |  154  | 
|  152 def ProvisionDevice(device, options): |  155 def ProvisionDevice(device, options): | 
|  153   if options.reboot_timeout: |  156   if options.reboot_timeout: | 
|  154     reboot_timeout = options.reboot_timeout |  157     reboot_timeout = options.reboot_timeout | 
|  155   elif (device.build_version_sdk >= |  158   elif (device.build_version_sdk >= | 
|  156         constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |  159         constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 
|  157     reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |  160     reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP | 
|  158   else: |  161   else: | 
|  159     reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |  162     reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP | 
|  160  |  163  | 
 |  164   if options.adb_key_files: | 
 |  165     adb_keys = set() | 
 |  166     for adb_key_file in options.adb_key_files: | 
 |  167       with open(adb_key_file, 'r') as f: | 
 |  168         adb_public_keys = f.readlines() | 
 |  169       adb_keys.update(adb_public_keys) | 
 |  170     WriteAdbKeysFile(device, '\n'.join(adb_keys)) | 
 |  171  | 
|  161   try: |  172   try: | 
|  162     if not options.skip_wipe: |  173     if not options.skip_wipe: | 
|  163       WipeDeviceIfPossible(device, reboot_timeout) |  174       WipeDeviceIfPossible(device, reboot_timeout) | 
|  164     try: |  175     try: | 
|  165       device.EnableRoot() |  176       device.EnableRoot() | 
|  166     except device_errors.CommandFailedError as e: |  177     except device_errors.CommandFailedError as e: | 
|  167       logging.warning(str(e)) |  178       logging.warning(str(e)) | 
|  168     _ConfigureLocalProperties(device, options.enable_java_debug) |  179     _ConfigureLocalProperties(device, options.enable_java_debug) | 
|  169     device_settings.ConfigureContentSettings( |  180     device_settings.ConfigureContentSettings( | 
|  170         device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |  181         device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) | 
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  269   parser.add_argument('--disable-network', action='store_true', |  280   parser.add_argument('--disable-network', action='store_true', | 
|  270                       help='disable network access on devices') |  281                       help='disable network access on devices') | 
|  271   parser.add_argument('--disable-java-debug', action='store_false', |  282   parser.add_argument('--disable-java-debug', action='store_false', | 
|  272                       dest='enable_java_debug', default=True, |  283                       dest='enable_java_debug', default=True, | 
|  273                       help='disable Java property asserts and JNI checking') |  284                       help='disable Java property asserts and JNI checking') | 
|  274   parser.add_argument('-t', '--target', default='Debug', |  285   parser.add_argument('-t', '--target', default='Debug', | 
|  275                       help='the build target (default: %(default)s)') |  286                       help='the build target (default: %(default)s)') | 
|  276   parser.add_argument('-r', '--auto-reconnect', action='store_true', |  287   parser.add_argument('-r', '--auto-reconnect', action='store_true', | 
|  277                       help='push binary which will reboot the device on adb' |  288                       help='push binary which will reboot the device on adb' | 
|  278                       ' disconnections') |  289                       ' disconnections') | 
 |  290   parser.add_argument('--adb-key-files', type=str, nargs='+', | 
 |  291                       help='list of adb keys to push to device') | 
|  279   args = parser.parse_args() |  292   args = parser.parse_args() | 
|  280   constants.SetBuildType(args.target) |  293   constants.SetBuildType(args.target) | 
|  281  |  294  | 
|  282   return ProvisionDevices(args) |  295   return ProvisionDevices(args) | 
|  283  |  296  | 
|  284  |  297  | 
|  285 if __name__ == '__main__': |  298 if __name__ == '__main__': | 
|  286   sys.exit(main()) |  299   sys.exit(main()) | 
| OLD | NEW |