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 logging | 13 import logging |
14 import optparse | 14 import optparse |
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_errors |
24 from pylib.device import device_utils | 25 from pylib.device import device_utils |
25 | 26 |
26 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | 27 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, |
27 'third_party', 'android_testrunner')) | 28 'third_party', 'android_testrunner')) |
28 import errors | 29 import errors |
29 | 30 |
30 def KillHostHeartbeat(): | 31 def KillHostHeartbeat(): |
31 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) | 32 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
32 stdout, _ = ps.communicate() | 33 stdout, _ = ps.communicate() |
33 matches = re.findall('\\n.*host_heartbeat.*', stdout) | 34 matches = re.findall('\\n.*host_heartbeat.*', stdout) |
(...skipping 18 matching lines...) Expand all Loading... |
52 Arguments: | 53 Arguments: |
53 devices: The list of serial numbers of the device to which the | 54 devices: The list of serial numbers of the device to which the |
54 adb_reboot binary should be pushed. | 55 adb_reboot binary should be pushed. |
55 target : The build target (example, Debug or Release) which helps in | 56 target : The build target (example, Debug or Release) which helps in |
56 locating the adb_reboot binary. | 57 locating the adb_reboot binary. |
57 """ | 58 """ |
58 for device_serial in devices: | 59 for device_serial in devices: |
59 print 'Will push and launch adb_reboot on %s' % device_serial | 60 print 'Will push and launch adb_reboot on %s' % device_serial |
60 device = device_utils.DeviceUtils(device_serial) | 61 device = device_utils.DeviceUtils(device_serial) |
61 # Kill if adb_reboot is already running. | 62 # Kill if adb_reboot is already running. |
62 device.old_interface.KillAllBlocking('adb_reboot', 2) | 63 try: |
| 64 # Don't try to kill adb_reboot more than once. We don't expect it to be |
| 65 # running at all. |
| 66 device.KillAll('adb_reboot', blocking=True, timeout=2, retries=0) |
| 67 except device_errors.CommandFailedError: |
| 68 # We can safely ignore the exception because we don't expect adb_reboot |
| 69 # to be running. |
| 70 pass |
63 # Push adb_reboot | 71 # Push adb_reboot |
64 print ' Pushing adb_reboot ...' | 72 print ' Pushing adb_reboot ...' |
65 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, | 73 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, |
66 'out/%s/adb_reboot' % target) | 74 'out/%s/adb_reboot' % target) |
67 device.old_interface.PushIfNeeded(adb_reboot, '/data/local/tmp/') | 75 device.old_interface.PushIfNeeded(adb_reboot, '/data/local/tmp/') |
68 # Launch adb_reboot | 76 # Launch adb_reboot |
69 print ' Launching adb_reboot ...' | 77 print ' Launching adb_reboot ...' |
70 device.old_interface.GetAndroidToolStatusAndOutput( | 78 device.old_interface.GetAndroidToolStatusAndOutput( |
71 '/data/local/tmp/adb_reboot') | 79 '/data/local/tmp/adb_reboot') |
72 LaunchHostHeartbeat() | 80 LaunchHostHeartbeat() |
73 | 81 |
74 | 82 |
75 def _ConfigureLocalProperties(device): | 83 def _ConfigureLocalProperties(device): |
76 """Set standard readonly testing device properties prior to reboot.""" | 84 """Set standard readonly testing device properties prior to reboot.""" |
77 local_props = [ | 85 local_props = [ |
78 'ro.monkey=1', | 86 'ro.monkey=1', |
79 'ro.test_harness=1', | 87 'ro.test_harness=1', |
80 'ro.audio.silent=1', | 88 'ro.audio.silent=1', |
81 'ro.setupwizard.mode=DISABLED', | 89 'ro.setupwizard.mode=DISABLED', |
82 ] | 90 ] |
83 device.old_interface.SetProtectedFileContents( | 91 device.old_interface.SetProtectedFileContents( |
84 constants.DEVICE_LOCAL_PROPERTIES_PATH, | 92 constants.DEVICE_LOCAL_PROPERTIES_PATH, |
85 '\n'.join(local_props)) | 93 '\n'.join(local_props)) |
86 # Android will not respect the local props file if it is world writable. | 94 # Android will not respect the local props file if it is world writable. |
87 device.RunShellCommand( | 95 device.RunShellCommand( |
88 'chmod 644 %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH, | 96 'chmod 644 %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH, |
89 root=True) | 97 as_root=True) |
90 | 98 |
91 # LOCAL_PROPERTIES_PATH = '/data/local.prop' | 99 # LOCAL_PROPERTIES_PATH = '/data/local.prop' |
92 | 100 |
93 | 101 |
94 def WipeDeviceData(device): | 102 def WipeDeviceData(device): |
95 """Wipes data from device, keeping only the adb_keys for authorization. | 103 """Wipes data from device, keeping only the adb_keys for authorization. |
96 | 104 |
97 After wiping data on a device that has been authorized, adb can still | 105 After wiping data on a device that has been authorized, adb can still |
98 communicate with the device, but after reboot the device will need to be | 106 communicate with the device, but after reboot the device will need to be |
99 re-authorized because the adb keys file is stored in /data/misc/adb/. | 107 re-authorized because the adb keys file is stored in /data/misc/adb/. |
100 Thus, adb_keys file is rewritten so the device does not need to be | 108 Thus, adb_keys file is rewritten so the device does not need to be |
101 re-authorized. | 109 re-authorized. |
102 | 110 |
103 Arguments: | 111 Arguments: |
104 device: the device to wipe | 112 device: the device to wipe |
105 """ | 113 """ |
106 device_authorized = device.old_interface.FileExistsOnDevice( | 114 device_authorized = device.old_interface.FileExistsOnDevice( |
107 constants.ADB_KEYS_FILE) | 115 constants.ADB_KEYS_FILE) |
108 if device_authorized: | 116 if device_authorized: |
109 adb_keys = device.RunShellCommand('cat %s' % constants.ADB_KEYS_FILE, | 117 adb_keys = device.RunShellCommand('cat %s' % constants.ADB_KEYS_FILE, |
110 root=True) | 118 as_root=True) |
111 device.RunShellCommand('wipe data', root=True) | 119 device.RunShellCommand('wipe data', as_root=True) |
112 if device_authorized: | 120 if device_authorized: |
113 path_list = constants.ADB_KEYS_FILE.split('/') | 121 path_list = constants.ADB_KEYS_FILE.split('/') |
114 dir_path = '/'.join(path_list[:len(path_list)-1]) | 122 dir_path = '/'.join(path_list[:len(path_list)-1]) |
115 device.RunShellCommand('mkdir -p %s' % dir_path, root=True) | 123 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) |
116 device.RunShellCommand('echo %s > %s' % | 124 device.RunShellCommand('echo %s > %s' % |
117 (adb_keys[0], constants.ADB_KEYS_FILE)) | 125 (adb_keys[0], constants.ADB_KEYS_FILE)) |
118 for adb_key in adb_keys[1:]: | 126 for adb_key in adb_keys[1:]: |
119 device.RunShellCommand( | 127 device.RunShellCommand( |
120 'echo %s >> %s' % (adb_key, constants.ADB_KEYS_FILE)) | 128 'echo %s >> %s' % (adb_key, constants.ADB_KEYS_FILE)) |
121 | 129 |
122 | 130 |
123 def ProvisionDevices(options): | 131 def ProvisionDevices(options): |
124 # TODO(jbudorick): Parallelize provisioning of all attached devices after | 132 # TODO(jbudorick): Parallelize provisioning of all attached devices after |
125 # switching from AndroidCommands. | 133 # switching from AndroidCommands. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 if not device.old_interface.IsDeviceCharging(): | 175 if not device.old_interface.IsDeviceCharging(): |
168 if device.old_interface.CanControlUsbCharging(): | 176 if device.old_interface.CanControlUsbCharging(): |
169 device.old_interface.EnableUsbCharging() | 177 device.old_interface.EnableUsbCharging() |
170 else: | 178 else: |
171 logging.error('Device is not charging') | 179 logging.error('Device is not charging') |
172 break | 180 break |
173 logging.info('Waiting for device to charge. Current level=%s', | 181 logging.info('Waiting for device to charge. Current level=%s', |
174 battery_info.get('level', 0)) | 182 battery_info.get('level', 0)) |
175 time.sleep(60) | 183 time.sleep(60) |
176 battery_info = device.old_interface.GetBatteryInfo() | 184 battery_info = device.old_interface.GetBatteryInfo() |
177 device.RunShellCommand('date -u %f' % time.time(), root=True) | 185 device.RunShellCommand('date -u %f' % time.time(), as_root=True) |
178 try: | 186 try: |
179 device_utils.DeviceUtils.parallel(devices).Reboot(True) | 187 device_utils.DeviceUtils.parallel(devices).Reboot(True) |
180 except errors.DeviceUnresponsiveError: | 188 except errors.DeviceUnresponsiveError: |
181 pass | 189 pass |
182 for device_serial in devices: | 190 for device_serial in devices: |
183 device = device_utils.DeviceUtils(device_serial) | 191 device = device_utils.DeviceUtils(device_serial) |
184 device.WaitUntilFullyBooted(timeout=90) | 192 device.WaitUntilFullyBooted(timeout=90) |
185 (_, props) = device.old_interface.GetShellCommandStatusAndOutput('getprop') | 193 (_, props) = device.old_interface.GetShellCommandStatusAndOutput('getprop') |
186 for prop in props: | 194 for prop in props: |
187 print prop | 195 print prop |
(...skipping 18 matching lines...) Expand all Loading... |
206 | 214 |
207 if args: | 215 if args: |
208 print >> sys.stderr, 'Unused args %s' % args | 216 print >> sys.stderr, 'Unused args %s' % args |
209 return 1 | 217 return 1 |
210 | 218 |
211 ProvisionDevices(options) | 219 ProvisionDevices(options) |
212 | 220 |
213 | 221 |
214 if __name__ == '__main__': | 222 if __name__ == '__main__': |
215 sys.exit(main(sys.argv)) | 223 sys.exit(main(sys.argv)) |
OLD | NEW |