Provision Android devices after the 'device_status_check' step.


BUG=169338


Review URL: https://chromiumcodereview.appspot.com/12733012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191315 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/build/android/buildbot/bb_device_steps.py b/build/android/buildbot/bb_device_steps.py
index 0f2afc6..a0d0d19 100755
--- a/build/android/buildbot/bb_device_steps.py
+++ b/build/android/buildbot/bb_device_steps.py
@@ -271,6 +271,12 @@
   buildbot_report.PrintNamedStep('device_status_check')
   RunCmd(['build/android/device_status_check.py'])
 
+  # Provision devices
+  if options.auto_reconnect:
+    buildbot_report.PrintNamedStep('provision_devices')
+    target = options.factory_properties.get('target', 'Debug')
+    RunCmd(['build/android/provision_devices.py', '-t', target])
+
   if options.install:
     test_obj = INSTRUMENTATION_TESTS[options.install]
     InstallApk(options, test_obj, print_step=True)
@@ -334,6 +340,9 @@
                     help='Reboot devices before running tests')
   parser.add_option('--upload-to-flakiness-server', action='store_true',
                     help='Upload the results to the flakiness dashboard.')
+  parser.add_option(
+      '--auto-reconnect', action='store_true',
+      help='Push script to device which restarts adbd on disconnections.')
   options, args = parser.parse_args(argv[1:])
 
   def ParserError(msg):
diff --git a/build/android/buildbot/bb_run_bot.py b/build/android/buildbot/bb_run_bot.py
index 4f69106..e3a85e67 100755
--- a/build/android/buildbot/bb_run_bot.py
+++ b/build/android/buildbot/bb_run_bot.py
@@ -113,7 +113,8 @@
         ['bb_compile', 'bb_compile_experimental', 'bb_zip_build'], None, None),
       B('fyi-tests', std_test_steps,
         T(std_tests, ['--experimental', flakiness_server]), None),
-      B('perf-tests-rel', std_test_steps, T([], ['--install=ContentShell']),
+      B('perf-tests-rel', std_test_steps,
+        T([], ['--install=ContentShell', '--auto-reconnect']),
         None),
       B('webkit-latest-webkit-tests', std_test_steps,
         T(['webkit_layout', 'webkit']), None),
diff --git a/build/android/host_heartbeat.py b/build/android/host_heartbeat.py
new file mode 100755
index 0000000..8321a77
--- /dev/null
+++ b/build/android/host_heartbeat.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Sends a heart beat pulse to the currently online Android devices.
+This heart beat lets the devices know that they are connected to a host.
+"""
+
+import os
+import sys
+import time
+
+from pylib import android_commands
+
+PULSE_PERIOD = 20
+
+def main():
+  while True:
+    try:
+      devices = android_commands.GetAttachedDevices()
+      for device in devices:
+        android_cmd = android_commands.AndroidCommands(device)
+        android_cmd.RunShellCommand('touch /sdcard/host_heartbeat')
+    except:
+      # Keep the heatbeat running bypassing all errors.
+      pass
+    time.sleep(PULSE_PERIOD)
+
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/build/android/provision_devices.py b/build/android/provision_devices.py
new file mode 100755
index 0000000..6ca53b1
--- /dev/null
+++ b/build/android/provision_devices.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Provisions Android devices with settings required for bots.
+
+Usage:
+  ./provision_devices.py [-d <device serial number>]
+"""
+
+import optparse
+import os
+import re
+import subprocess
+import sys
+import time
+
+from pylib import android_commands
+from pylib import constants
+
+
+def PushAndLaunchAdbReboot(device, target):
+  """Pushes and launches the adb_reboot binary on the device.
+
+  Arguments:
+    device: The serial number of the device to which the
+            adb_reboot binary should be pushed.
+    target: The build target (example, Debug or Release) which helps in
+            locating the adb_reboot binary.
+  """
+  print 'Will push and launch adb_reboot on %s' % device
+  android_cmd = android_commands.AndroidCommands(device)
+  # Kill if adb_reboot is already running.
+  android_cmd.KillAllBlocking('adb_reboot', 2)
+  # Push adb_reboot
+  print '  Pushing adb_reboot ...'
+  adb_reboot = os.path.join(constants.CHROME_DIR,
+                            'out/%s/adb_reboot' % target)
+  android_cmd.PushIfNeeded(adb_reboot, '/data/local/')
+  # Launch adb_reboot
+  print '  Launching adb_reboot ...'
+  p = subprocess.Popen(['adb', '-s', device, 'shell'], stdin=subprocess.PIPE)
+  p.communicate('/data/local/adb_reboot; exit\n')
+
+
+def LaunchHostHeartbeat():
+  ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE)
+  stdout, _ = ps.communicate()
+  matches = re.findall('\\n.*host_heartbeat.*', stdout)
+  for match in matches:
+    print 'An instance of host heart beart already running... will kill'
+    pid = re.findall('(\d+)', match)[1]
+    subprocess.call(['kill', str(pid)])
+  # Launch a new host_heartbeat
+  print 'Spawing host heartbeat...'
+  subprocess.Popen([os.path.join(constants.CHROME_DIR,
+                                 'build/android/host_heartbeat.py')])
+
+
+def ProvisionDevices(options):
+  if options.device is not None:
+    devices = [options.device]
+  else:
+    devices = android_commands.GetAttachedDevices()
+  for device in devices:
+    android_cmd = android_commands.AndroidCommands(device)
+    android_cmd.RunShellCommand('su -c date -u %f' % time.time())
+    PushAndLaunchAdbReboot(device, options.target)
+  LaunchHostHeartbeat()
+
+
+def main(argv):
+  parser = optparse.OptionParser()
+  parser.add_option('-d', '--device',
+                    help='The serial number of the device to be provisioned')
+  parser.add_option('-t', '--target',
+                    help='Path to the adb_reboot binary')
+  options, args = parser.parse_args(argv[1:])
+
+  if args:
+    print >> sys.stderr, 'Unused args %s' % args
+    return 1
+
+  if not options.target:
+    print >> sys.stderr, 'Build target not specified'
+    return 1
+
+  ProvisionDevices(options)
+
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv))