Index: build/android/provision_devices.py |
diff --git a/build/android/provision_devices.py b/build/android/provision_devices.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..86d142e3bdfbfbf7c4a2e42a3a312a6550ea86d1 |
--- /dev/null |
+++ b/build/android/provision_devices.py |
@@ -0,0 +1,93 @@ |
+#!/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, adb_reboot): |
+ print 'Will push and launch adb_reboot on %s' % device |
+ android_cmd = android_commands.AndroidCommands(device) |
+ # Check if adb_reboot is running. If yes, stop it. |
+ dev_procs = android_cmd.RunShellCommand('ps') |
+ for dev_proc in dev_procs: |
+ if 'adb_reboot' in dev_proc: |
Isaac (away)
2013/03/25 23:50:03
I'd prefer a more structured parsing of ps.
Siva Chandra
2013/03/26 02:27:51
Like?
|
+ print ' adb_reboot running. Killing ...' |
+ pid = re.findall('(\d+)', dev_proc)[0] |
+ android_cmd.RunShellCommand('kill %s' % pid) |
+ # Check if the exe is on the device. If yes, delete it. |
+ if android_cmd.FileExistsOnDevice('/data/local/adb_reboot'): |
Isaac (away)
2013/03/25 23:50:03
why delete? Just run pushifneeded.
Siva Chandra
2013/03/26 02:27:51
Done.
|
+ print ' adb_reboot exists on device. Deleting ...' |
+ android_cmd.RunShellCommand('rm /data/local/adb_reboot') |
+ # Push adb_reboot |
+ print ' Pushing adb_reboot ...' |
+ android_cmd.PushIfNeeded('%s' % adb_reboot, '/data/local/') |
+ # Launch adb_reboot |
+ print ' Launching adb_reboot ...' |
+ p1 = subprocess.Popen(['echo', 'cd /data/local; ./adb_reboot; exit'], |
Isaac (away)
2013/03/25 23:50:03
don't need to echo and pipe. You can use subproce
Siva Chandra
2013/03/26 02:27:51
This does not work:
p = subprocess.Popen(['adb',
|
+ stdout=subprocess.PIPE) |
+ subprocess.call(['adb', '-s', device, 'shell'], stdin=p1.stdout) |
+ |
+ |
+def LaunchHostHeartbeat(): |
+ ps = subprocess.Popen(['ps', 'a'], stdout = subprocess.PIPE) |
+ stdout, _ = ps.communicate() |
+ for line in stdout.splitlines(): |
+ if 'host_heartbeat' in line: |
+ # Kill the previous host_heartbeat |
+ print 'Host heart beart already running ... will kill' |
+ pid = re.findall('(\d+)', line)[0] |
Isaac (away)
2013/03/25 23:50:03
You should use a regex in line 52 and get the grou
Siva Chandra
2013/03/26 02:27:51
Done.
|
+ subprocess.call(['kill', '%s' % pid]) |
Isaac (away)
2013/03/25 23:50:03
prefer str(pid)
Siva Chandra
2013/03/26 02:27:51
Done.
|
+ break |
+ # Launch a new host_heartbeat |
+ cmd = '%s/build/android/host_heartbeat.py' % constants.CHROME_DIR |
+ print 'Launching %s ...' % cmd |
+ subprocess.Popen([cmd]) |
Isaac (away)
2013/03/25 23:50:03
Are you expecting this to not die when the parent
Siva Chandra
2013/03/26 02:27:51
On my system, the process stays alive, dont know w
Siva Chandra
2013/03/27 01:27:13
I do not think we need to have a detached process.
|
+ |
+ |
+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.EnableAdbRoot() |
+ android_cmd.RunShellCommand('date -u %f' % time.time()) |
+ PushAndLaunchAdbReboot(device, options.adb_reboot) |
+ LaunchHostHeartbeat() |
+ |
+ |
+def main(argv): |
+ parser = optparse.OptionParser() |
+ parser.add_option('-d', '--device', dest='device', default=None, |
+ help='The serial number of the device to be provisioned') |
+ parser.add_option('-a', '--adb_reboot', dest='adb_reboot', default=None, |
+ help='Path to the adb_reboot binary') |
+ options, _ = parser.parse_args(argv) |
+ |
+ if not options.adb_reboot: |
+ print >> sys.stderr, 'Path to adb_reboot not specified' |
+ return -1 |
+ |
+ ProvisionDevices(options) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |