Index: build/android/buildbot/bb_device_status_check.py |
diff --git a/build/android/buildbot/bb_device_status_check.py b/build/android/buildbot/bb_device_status_check.py |
index a5ef54b34ce8c9a435c6fbb7d31569e38c64a2d7..ed8c436e0f7b4573337dda199bd3624b273a16d6 100755 |
--- a/build/android/buildbot/bb_device_status_check.py |
+++ b/build/android/buildbot/bb_device_status_check.py |
@@ -9,10 +9,12 @@ import logging |
import optparse |
import os |
import smtplib |
+import subprocess |
import sys |
import re |
import urllib |
+import bb_utils |
tonyg
2013/10/10 01:56:11
nit: alphabetize below bb_annotations
navabi
2013/10/10 02:10:21
Done.
|
import bb_annotations |
sys.path.append(os.path.join(os.path.dirname(__file__), |
@@ -203,6 +205,37 @@ def SendDeviceStatusAlert(msg): |
print 'Failed to send alert email. Error: %s' % e |
+def restart_usb(): |
tonyg
2013/10/10 01:56:11
style nit: RestartUsb()
navabi
2013/10/10 02:10:21
Done.
|
+ # Expects restart_usb to be installed (BUG=305769) |
tonyg
2013/10/10 01:56:11
This comment doesn't add value, I'd drop it.
navabi
2013/10/10 02:10:21
Done.
|
+ if not os.path.isfile('/usr/bin/restart_usb'): |
+ print ('WARNING: Could not restart usb. /usr/bin/restart_usb not installed ' |
+ 'on host (see BUG=305769).') |
+ return |
+ |
+ lsusb_proc = bb_utils.SpawnCmd(['lsusb'], stdout=subprocess.PIPE) |
+ lsusb_output, _ = lsusb_proc.communicate() |
+ if lsusb_proc.returncode: |
+ print ('WARNING: Could not restart usb. Error getting list of USB ports.') |
+ return |
+ |
+ usb_devices = [re.findall('Bus (\d\d\d) Device (\d\d\d)', lsusb_line)[0] |
+ for lsusb_line in lsusb_output.strip().split('\n')] |
+ |
+ # Walk USB devices from leaves up (i.e reverse sorted) restarting the |
+ # connection. If a parent node (e.g. usb hub) is restarted before the |
+ # devices connected to it, the (bus, dev) for the hub can change, making the |
+ # output we have wrong. This way we restart the devices before the hub. |
+ for (bus, dev) in reversed(sorted(usb_devices)): |
+ # Can not restart root usb connections |
+ if dev != '001': |
+ proc = bb_utils.SpawnCmd(['/usr/bin/restart_usb', bus, dev]) |
tonyg
2013/10/10 01:56:11
Do you want RunCmd instead of SpawnCmd+communicate
navabi
2013/10/10 02:10:21
Done.
|
+ proc.communicate() |
+ if proc.returncode: |
+ print 'Error restarting USB device /dev/bus/usb/%s/%s' % (bus, dev) |
+ else: |
+ print 'Restarted USB device /dev/bus/usb/%s/%s' % (bus, dev) |
+ |
+ |
def main(): |
parser = optparse.OptionParser() |
parser.add_option('', '--out-dir', |
@@ -212,9 +245,15 @@ def main(): |
help='Will not check if devices are provisioned properly.') |
parser.add_option('--device-status-dashboard', action='store_true', |
help='Output device status data for dashboard.') |
+ parser.add_option('--restart-usb', action='store_true', |
+ help='Restart USB ports before running device check.') |
options, args = parser.parse_args() |
if args: |
parser.error('Unknown options %s' % args) |
+ |
+ if options.restart_usb: |
+ restart_usb() |
+ |
devices = android_commands.GetAttachedDevices() |
# TODO(navabi): Test to make sure this fails and then fix call |
offline_devices = android_commands.GetAttachedDevices(hardware=False, |