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 optparse | 13 import optparse |
14 import os | 14 import os |
15 import re | 15 import re |
16 import subprocess | 16 import subprocess |
17 import sys | 17 import sys |
18 import time | 18 import time |
19 | 19 |
20 from pylib import android_commands | 20 from pylib import android_commands |
21 from pylib import constants | 21 from pylib import constants |
22 | 22 |
23 | 23 |
24 def PushAndLaunchAdbReboot(device, target): | |
25 """Pushes and launches the adb_reboot binary on the device. | |
26 | |
27 Arguments: | |
28 device: The serial number of the device to which the | |
29 adb_reboot binary should be pushed. | |
30 target: The build target (example, Debug or Release) which helps in | |
31 locating the adb_reboot binary. | |
32 """ | |
33 print 'Will push and launch adb_reboot on %s' % device | |
34 android_cmd = android_commands.AndroidCommands(device) | |
35 # Kill if adb_reboot is already running. | |
36 android_cmd.KillAllBlocking('adb_reboot', 2) | |
37 # Push adb_reboot | |
38 print ' Pushing adb_reboot ...' | |
39 adb_reboot = os.path.join(constants.CHROME_DIR, | |
40 'out/%s/adb_reboot' % target) | |
41 android_cmd.PushIfNeeded(adb_reboot, '/data/local/') | |
42 # Launch adb_reboot | |
43 print ' Launching adb_reboot ...' | |
44 p = subprocess.Popen(['adb', '-s', device, 'shell'], stdin=subprocess.PIPE) | |
45 p.communicate('/data/local/adb_reboot; exit\n') | |
46 | |
47 | |
48 def LaunchHostHeartbeat(): | 24 def LaunchHostHeartbeat(): |
49 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) | 25 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
50 stdout, _ = ps.communicate() | 26 stdout, _ = ps.communicate() |
51 matches = re.findall('\\n.*host_heartbeat.*', stdout) | 27 matches = re.findall('\\n.*host_heartbeat.*', stdout) |
52 for match in matches: | 28 for match in matches: |
53 print 'An instance of host heart beart already running... will kill' | 29 print 'An instance of host heart beart already running... will kill' |
54 pid = re.findall('(\d+)', match)[1] | 30 pid = re.findall('(\d+)', match)[1] |
55 subprocess.call(['kill', str(pid)]) | 31 subprocess.call(['kill', str(pid)]) |
56 # Launch a new host_heartbeat | 32 # Launch a new host_heartbeat |
57 print 'Spawing host heartbeat...' | 33 print 'Spawning host heartbeat...' |
58 subprocess.Popen([os.path.join(constants.CHROME_DIR, | 34 subprocess.Popen([os.path.join(constants.CHROME_DIR, |
59 'build/android/host_heartbeat.py')]) | 35 'build/android/host_heartbeat.py')]) |
60 | 36 |
61 | 37 |
| 38 def PushAndLaunchAdbReboot(devices, target): |
| 39 """Pushes and launches the adb_reboot binary on the device. |
| 40 |
| 41 Arguments: |
| 42 devices: The list of serial numbers of the device to which the |
| 43 adb_reboot binary should be pushed. |
| 44 target : The build target (example, Debug or Release) which helps in |
| 45 locating the adb_reboot binary. |
| 46 """ |
| 47 for device in devices: |
| 48 print 'Will push and launch adb_reboot on %s' % device |
| 49 android_cmd = android_commands.AndroidCommands(device) |
| 50 # Kill if adb_reboot is already running. |
| 51 android_cmd.KillAllBlocking('adb_reboot', 2) |
| 52 # Push adb_reboot |
| 53 print ' Pushing adb_reboot ...' |
| 54 adb_reboot = os.path.join(constants.CHROME_DIR, |
| 55 'out/%s/adb_reboot' % target) |
| 56 android_cmd.PushIfNeeded(adb_reboot, '/data/local/') |
| 57 # Launch adb_reboot |
| 58 print ' Launching adb_reboot ...' |
| 59 p = subprocess.Popen(['adb', '-s', device, 'shell'], stdin=subprocess.PIPE) |
| 60 p.communicate('/data/local/adb_reboot; exit\n') |
| 61 LaunchHostHeartbeat() |
| 62 |
| 63 |
62 def ProvisionDevices(options): | 64 def ProvisionDevices(options): |
63 if options.device is not None: | 65 if options.device is not None: |
64 devices = [options.device] | 66 devices = [options.device] |
65 else: | 67 else: |
66 devices = android_commands.GetAttachedDevices() | 68 devices = android_commands.GetAttachedDevices() |
67 for device in devices: | 69 for device in devices: |
68 android_cmd = android_commands.AndroidCommands(device) | 70 android_cmd = android_commands.AndroidCommands(device) |
69 android_cmd.RunShellCommand('su -c date -u %f' % time.time()) | 71 android_cmd.RunShellCommand('su -c date -u %f' % time.time()) |
70 PushAndLaunchAdbReboot(device, options.target) | 72 if options.auto_reconnect: |
71 LaunchHostHeartbeat() | 73 PushAndLaunchAdbReboot(devices, options.target) |
72 | 74 |
73 | 75 |
74 def main(argv): | 76 def main(argv): |
75 parser = optparse.OptionParser() | 77 parser = optparse.OptionParser() |
76 parser.add_option('-d', '--device', | 78 parser.add_option('-d', '--device', |
77 help='The serial number of the device to be provisioned') | 79 help='The serial number of the device to be provisioned') |
78 parser.add_option('-t', '--target', | 80 parser.add_option('-t', '--target', default='Debug', help='The build target') |
79 help='Path to the adb_reboot binary') | 81 parser.add_option( |
| 82 '-r', '--auto-reconnect', action='store_true', |
| 83 help='Push binary which will reboot the device on adb disconnections.') |
80 options, args = parser.parse_args(argv[1:]) | 84 options, args = parser.parse_args(argv[1:]) |
81 | 85 |
82 if args: | 86 if args: |
83 print >> sys.stderr, 'Unused args %s' % args | 87 print >> sys.stderr, 'Unused args %s' % args |
84 return 1 | 88 return 1 |
85 | 89 |
86 if not options.target: | |
87 print >> sys.stderr, 'Build target not specified' | |
88 return 1 | |
89 | |
90 ProvisionDevices(options) | 90 ProvisionDevices(options) |
91 | 91 |
92 | 92 |
93 if __name__ == '__main__': | 93 if __name__ == '__main__': |
94 sys.exit(main(sys.argv)) | 94 sys.exit(main(sys.argv)) |
OLD | NEW |