OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Provisions Android devices with settings required for bots. |
| 8 |
| 9 Usage: |
| 10 ./provision_devices.py [-d <device serial number>] |
| 11 """ |
| 12 |
| 13 import optparse |
| 14 import os |
| 15 import re |
| 16 import subprocess |
| 17 import sys |
| 18 import time |
| 19 |
| 20 from pylib import android_commands |
| 21 from pylib import constants |
| 22 |
| 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(): |
| 49 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
| 50 stdout, _ = ps.communicate() |
| 51 matches = re.findall('\\n.*host_heartbeat.*', stdout) |
| 52 for match in matches: |
| 53 print 'An instance of host heart beart already running... will kill' |
| 54 pid = re.findall('(\d+)', match)[1] |
| 55 subprocess.call(['kill', str(pid)]) |
| 56 # Launch a new host_heartbeat |
| 57 print 'Spawing host heartbeat...' |
| 58 subprocess.Popen([os.path.join(constants.CHROME_DIR, |
| 59 'build/android/host_heartbeat.py')]) |
| 60 |
| 61 |
| 62 def ProvisionDevices(options): |
| 63 if options.device is not None: |
| 64 devices = [options.device] |
| 65 else: |
| 66 devices = android_commands.GetAttachedDevices() |
| 67 for device in devices: |
| 68 android_cmd = android_commands.AndroidCommands(device) |
| 69 android_cmd.RunShellCommand('su -c date -u %f' % time.time()) |
| 70 PushAndLaunchAdbReboot(device, options.target) |
| 71 LaunchHostHeartbeat() |
| 72 |
| 73 |
| 74 def main(argv): |
| 75 parser = optparse.OptionParser() |
| 76 parser.add_option('-d', '--device', |
| 77 help='The serial number of the device to be provisioned') |
| 78 parser.add_option('-t', '--target', |
| 79 help='Path to the adb_reboot binary') |
| 80 options, args = parser.parse_args(argv[1:]) |
| 81 |
| 82 if args: |
| 83 print >> sys.stderr, 'Unused args %s' % args |
| 84 return 1 |
| 85 |
| 86 if not options.target: |
| 87 print >> sys.stderr, 'Build target not specified' |
| 88 return 1 |
| 89 |
| 90 ProvisionDevices(options) |
| 91 |
| 92 |
| 93 if __name__ == '__main__': |
| 94 sys.exit(main(sys.argv)) |
OLD | NEW |