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 b43ee0a99b9f189d3343639cdd8015bc4b9782a6..a021bfc892b1136e13951f486db1d70ac02802ad 100755 |
--- a/build/android/buildbot/bb_device_status_check.py |
+++ b/build/android/buildbot/bb_device_status_check.py |
@@ -9,11 +9,12 @@ import logging |
import optparse |
import os |
import psutil |
+import re |
import signal |
import smtplib |
import subprocess |
import sys |
-import re |
+import time |
import urllib |
import bb_annotations |
@@ -111,6 +112,26 @@ def DeviceInfo(serial, options): |
return device_type, device_build, battery_level, full_report, errors, True |
+def GetLastDevices(out_dir): |
+ """Returns a list of devices that have been seen on the bot. |
+ |
+ Args: |
+ options: out_dir parameter of options argument is used as the base |
+ directory to load and update the cache file. |
+ |
+ Returns: List of device serial numbers that were on the bot. |
+ """ |
+ devices_path = os.path.join(out_dir, '.last_devices') |
+ devices = [] |
+ try: |
+ with open(devices_path) as f: |
+ devices = f.read().splitlines() |
+ except IOError: |
+ # Ignore error, file might not exist |
+ pass |
+ return devices |
+ |
+ |
def CheckForMissingDevices(options, adb_online_devs): |
"""Uses file of previous online devices to detect broken phones. |
@@ -127,17 +148,6 @@ def CheckForMissingDevices(options, adb_online_devs): |
out_dir = os.path.abspath(options.out_dir) |
- def ReadDeviceList(file_name): |
- devices_path = os.path.join(out_dir, file_name) |
- devices = [] |
- try: |
- with open(devices_path) as f: |
- devices = f.read().splitlines() |
- except IOError: |
- # Ignore error, file might not exist |
- pass |
- return devices |
- |
def WriteDeviceList(file_name, device_list): |
path = os.path.join(out_dir, file_name) |
if not os.path.exists(out_dir): |
@@ -147,7 +157,7 @@ def CheckForMissingDevices(options, adb_online_devs): |
f.write('\n'.join(set(device_list))) |
last_devices_path = os.path.join(out_dir, '.last_devices') |
- last_devices = ReadDeviceList('.last_devices') |
+ last_devices = GetLastDevices(out_dir) |
missing_devs = list(set(last_devices) - set(adb_online_devs)) |
all_known_devices = list(set(adb_online_devs) | set(last_devices)) |
@@ -277,10 +287,20 @@ def main(): |
parser.error('Unknown options %s' % args) |
if options.restart_usb: |
- KillAllAdb() |
- rc = RestartUsb() |
- if rc: |
- return 1 |
+ expected_devices = GetLastDevices(os.path.abspath(options.out_dir)) |
+ devices = android_commands.GetAttachedDevices() |
+ # Only restart usb if devices are missing |
+ if set(expected_devices) != set(devices): |
+ KillAllAdb() |
+ if RestartUsb(): |
+ return 1 |
+ retries = 5 |
+ while retries: |
+ time.sleep(1) |
+ devices = android_commands.GetAttachedDevices() |
+ if set(expected_devices) == set(devices): |
+ break |
+ retries -= 1 |
devices = android_commands.GetAttachedDevices() |
# TODO(navabi): Test to make sure this fails and then fix call |