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..0da0a86d5ffdb843ffa1d5b0cc660392ab25ae92 |
--- /dev/null |
+++ b/build/android/provision_devices.py |
@@ -0,0 +1,87 @@ |
+#!/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): |
Isaac (away)
2013/03/28 22:37:44
it's unclear what type of data these ares are, cou
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ 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') |
Isaac (away)
2013/03/28 22:37:44
Let's use android_commands.KillAllBlocking instead
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ for dev_proc in dev_procs: |
+ if 'adb_reboot' in dev_proc: |
+ print ' adb_reboot running. Killing ...' |
+ pid = re.findall('(\d+)', dev_proc)[0] |
+ android_cmd.RunShellCommand('kill %s' % pid) |
+ # Push adb_reboot |
+ print ' Pushing adb_reboot ...' |
+ android_cmd.PushIfNeeded('%s' % adb_reboot, '/data/local/') |
Isaac (away)
2013/03/28 22:37:44
adb_reboot is already a string. Just PushIfNeeded
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ # Launch adb_reboot |
+ print ' Launching adb_reboot ...' |
+ p = subprocess.Popen(['echo', 'cd /data/local; ./adb_reboot; exit'], |
+ stdout=subprocess.PIPE) |
+ subprocess.call(['adb', '-s', device, 'shell'], stdin=p.stdout) |
Isaac (away)
2013/03/28 22:37:44
Try:
p = subprocess.Popen(['adb', '-s', device, 's
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ |
+ |
+def LaunchHostHeartbeat(): |
+ ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
Isaac (away)
2013/03/28 22:37:44
Use killall or pkill to kill by name.
Siva Chandra
2013/03/28 23:52:47
The name for the process we to want kill happens t
Isaac (away)
2013/03/29 00:18:32
I think there is a way to do this with pkill. I d
Siva Chandra
2013/03/29 01:47:06
Stackoverflow gives this: http://stackoverflow.com
Isaac (away)
2013/03/29 02:05:54
Maybe I'm missing something but I don't see where
|
+ 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', '%s' % str(pid)]) |
+ # Launch a new host_heartbeat |
+ cmd = '%s/build/android/host_heartbeat.py' % constants.CHROME_DIR |
Isaac (away)
2013/03/28 22:37:44
os.path.join(constants.CHROME_DIR, 'build/......he
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ print 'Launching %s ...' % cmd |
Isaac (away)
2013/03/28 22:37:44
Not sure we need full path here, maybe just
print
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ subprocess.Popen([cmd]) |
+ |
+ |
+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, |
Isaac (away)
2013/03/28 22:37:44
None is the default, you can remove that argument
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ help='The serial number of the device to be provisioned') |
+ parser.add_option('-a', '--adb_reboot', dest='adb_reboot', default=None, |
Isaac (away)
2013/03/28 22:37:44
use hyphens, not underscores for long arguments:
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ help='Path to the adb_reboot binary') |
+ options, _ = parser.parse_args(argv) |
Isaac (away)
2013/03/28 22:37:44
Let's check the args list for non-parsed arguments
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ |
+ if not options.adb_reboot: |
+ print >> sys.stderr, 'Path to adb_reboot not specified' |
+ return -1 |
Isaac (away)
2013/03/28 22:37:44
negative error codes have undefined behavior in ba
Siva Chandra
2013/03/28 23:52:47
Done.
|
+ |
+ ProvisionDevices(options) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |