provision_devices.py revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 9Usage: 10 ./provision_devices.py [-d <device serial number>] 11""" 12 13import optparse 14import os 15import re 16import subprocess 17import sys 18import time 19 20from pylib import android_commands 21from pylib import constants 22 23 24def LaunchHostHeartbeat(): 25 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) 26 stdout, _ = ps.communicate() 27 matches = re.findall('\\n.*host_heartbeat.*', stdout) 28 for match in matches: 29 print 'An instance of host heart beart already running... will kill' 30 pid = re.findall('(\d+)', match)[1] 31 subprocess.call(['kill', str(pid)]) 32 # Launch a new host_heartbeat 33 print 'Spawning host heartbeat...' 34 subprocess.Popen([os.path.join(constants.CHROME_DIR, 35 'build/android/host_heartbeat.py')]) 36 37 38def 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 64def ProvisionDevices(options): 65 if options.device is not None: 66 devices = [options.device] 67 else: 68 devices = android_commands.GetAttachedDevices() 69 for device in devices: 70 android_cmd = android_commands.AndroidCommands(device) 71 android_cmd.RunShellCommand('su -c date -u %f' % time.time()) 72 if options.auto_reconnect: 73 PushAndLaunchAdbReboot(devices, options.target) 74 75 76def main(argv): 77 parser = optparse.OptionParser() 78 parser.add_option('-d', '--device', 79 help='The serial number of the device to be provisioned') 80 parser.add_option('-t', '--target', default='Debug', help='The build target') 81 parser.add_option( 82 '-r', '--auto-reconnect', action='store_true', 83 help='Push binary which will reboot the device on adb disconnections.') 84 options, args = parser.parse_args(argv[1:]) 85 86 if args: 87 print >> sys.stderr, 'Unused args %s' % args 88 return 1 89 90 ProvisionDevices(options) 91 92 93if __name__ == '__main__': 94 sys.exit(main(sys.argv)) 95