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, 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.
| |
25 print 'Will push and launch adb_reboot on %s' % device | |
26 android_cmd = android_commands.AndroidCommands(device) | |
27 # Check if adb_reboot is running. If yes, stop it. | |
28 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.
| |
29 for dev_proc in dev_procs: | |
30 if 'adb_reboot' in dev_proc: | |
31 print ' adb_reboot running. Killing ...' | |
32 pid = re.findall('(\d+)', dev_proc)[0] | |
33 android_cmd.RunShellCommand('kill %s' % pid) | |
34 # Push adb_reboot | |
35 print ' Pushing adb_reboot ...' | |
36 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.
| |
37 # Launch adb_reboot | |
38 print ' Launching adb_reboot ...' | |
39 p = subprocess.Popen(['echo', 'cd /data/local; ./adb_reboot; exit'], | |
40 stdout=subprocess.PIPE) | |
41 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.
| |
42 | |
43 | |
44 def LaunchHostHeartbeat(): | |
45 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
| |
46 stdout, _ = ps.communicate() | |
47 matches = re.findall('\\n.*host_heartbeat.*', stdout) | |
48 for match in matches: | |
49 print 'An instance of host heart beart already running... will kill' | |
50 pid = re.findall('(\d+)', match)[1] | |
51 subprocess.call(['kill', '%s' % str(pid)]) | |
52 # Launch a new host_heartbeat | |
53 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.
| |
54 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.
| |
55 subprocess.Popen([cmd]) | |
56 | |
57 | |
58 def ProvisionDevices(options): | |
59 if options.device is not None: | |
60 devices = [options.device] | |
61 else: | |
62 devices = android_commands.GetAttachedDevices() | |
63 for device in devices: | |
64 android_cmd = android_commands.AndroidCommands(device) | |
65 android_cmd.EnableAdbRoot() | |
66 android_cmd.RunShellCommand('date -u %f' % time.time()) | |
67 PushAndLaunchAdbReboot(device, options.adb_reboot) | |
68 LaunchHostHeartbeat() | |
69 | |
70 | |
71 def main(argv): | |
72 parser = optparse.OptionParser() | |
73 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.
| |
74 help='The serial number of the device to be provisioned') | |
75 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.
| |
76 help='Path to the adb_reboot binary') | |
77 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.
| |
78 | |
79 if not options.adb_reboot: | |
80 print >> sys.stderr, 'Path to adb_reboot not specified' | |
81 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.
| |
82 | |
83 ProvisionDevices(options) | |
84 | |
85 | |
86 if __name__ == '__main__': | |
87 sys.exit(main(sys.argv)) | |
OLD | NEW |