| 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 52261ac549031b4d197cbde1c5a0a23918fc6976..65fc5fed30f6c07a5d4098979ea6b9a854e3b241 100755
 | 
| --- a/build/android/buildbot/bb_device_status_check.py
 | 
| +++ b/build/android/buildbot/bb_device_status_check.py
 | 
| @@ -5,9 +5,9 @@
 | 
|  # found in the LICENSE file.
 | 
|  
 | 
|  """A class to keep track of devices across builds and report state."""
 | 
| +import argparse
 | 
|  import json
 | 
|  import logging
 | 
| -import optparse
 | 
|  import os
 | 
|  import psutil
 | 
|  import re
 | 
| @@ -41,7 +41,7 @@ from pylib.utils import timeout_retry
 | 
|  
 | 
|  _RE_DEVICE_ID = re.compile('Device ID = (\d+)')
 | 
|  
 | 
| -def DeviceInfo(device, options):
 | 
| +def DeviceInfo(device, args):
 | 
|    """Gathers info on a device via various adb calls.
 | 
|  
 | 
|    Args:
 | 
| @@ -96,7 +96,7 @@ def DeviceInfo(device, options):
 | 
|        dev_good = False
 | 
|        if not battery.GetCharging():
 | 
|          battery.SetCharging(True)
 | 
| -    if not options.no_provisioning_check:
 | 
| +    if not args.no_provisioning_check:
 | 
|        setup_wizard_disabled = (
 | 
|            device.GetProp('ro.setupwizard.mode') == 'DISABLED')
 | 
|        if not setup_wizard_disabled and device.build_type != 'user':
 | 
| @@ -114,16 +114,16 @@ def DeviceInfo(device, options):
 | 
|    return (build_product, build_id, battery_level, errors, dev_good, json_data)
 | 
|  
 | 
|  
 | 
| -def CheckForMissingDevices(options, devices):
 | 
| +def CheckForMissingDevices(args, devices):
 | 
|    """Uses file of previous online devices to detect broken phones.
 | 
|  
 | 
|    Args:
 | 
| -    options: out_dir parameter of options argument is used as the base
 | 
| +    args: out_dir parameter of args argument is used as the base
 | 
|        directory to load and update the cache file.
 | 
|      devices: A list of DeviceUtils instance for the currently visible and
 | 
|        online attached devices.
 | 
|    """
 | 
| -  out_dir = os.path.abspath(options.out_dir)
 | 
| +  out_dir = os.path.abspath(args.out_dir)
 | 
|    device_serials = set(d.adb.GetDeviceSerial() for d in devices)
 | 
|  
 | 
|    # last_devices denotes all known devices prior to this run
 | 
| @@ -224,9 +224,9 @@ def KillAllAdb():
 | 
|        pass
 | 
|  
 | 
|  
 | 
| -def RecoverDevices(args):
 | 
| +def RecoverDevices(blacklist, output_directory):
 | 
|    # Remove the last build's "bad devices" before checking device statuses.
 | 
| -  device_blacklist.ResetBlacklist()
 | 
| +  blacklist.Reset()
 | 
|  
 | 
|    previous_devices = set(a.GetDeviceSerial()
 | 
|                           for a in adb_wrapper.AdbWrapper.Devices())
 | 
| @@ -236,7 +236,7 @@ def RecoverDevices(args):
 | 
|  
 | 
|    try:
 | 
|      expected_devices = set(device_list.GetPersistentDeviceList(
 | 
| -        os.path.join(args.out_dir, device_list.LAST_DEVICES_FILENAME)))
 | 
| +        os.path.join(output_directory, device_list.LAST_DEVICES_FILENAME)))
 | 
|    except IOError:
 | 
|      expected_devices = set()
 | 
|  
 | 
| @@ -249,15 +249,15 @@ def RecoverDevices(args):
 | 
|      except device_errors.CommandFailedError:
 | 
|        logging.exception('Failure while waiting for %s. Adding to blacklist.',
 | 
|                          str(device))
 | 
| -      device_blacklist.ExtendBlacklist([str(device)])
 | 
| +      blacklist.Extend([str(device)])
 | 
|      except device_errors.CommandTimeoutError:
 | 
|        logging.exception('Timed out while waiting for %s. Adding to blacklist.',
 | 
|                          str(device))
 | 
| -      device_blacklist.ExtendBlacklist([str(device)])
 | 
| +      blacklist.Extend([str(device)])
 | 
|  
 | 
|    device_utils.DeviceUtils.parallel(all_devices).pMap(blacklisting_recovery)
 | 
|  
 | 
| -  devices = device_utils.DeviceUtils.HealthyDevices()
 | 
| +  devices = device_utils.DeviceUtils.HealthyDevices(blacklist)
 | 
|    device_serials = set(d.adb.GetDeviceSerial() for d in devices)
 | 
|  
 | 
|    missing_devices = expected_devices.difference(device_serials)
 | 
| @@ -275,35 +275,41 @@ def RecoverDevices(args):
 | 
|  
 | 
|  
 | 
|  def main():
 | 
| -  parser = optparse.OptionParser()
 | 
| -  parser.add_option('', '--out-dir',
 | 
| -                    help='Directory where the device path is stored',
 | 
| -                    default=os.path.join(constants.DIR_SOURCE_ROOT, 'out'))
 | 
| -  parser.add_option('--no-provisioning-check', action='store_true',
 | 
| -                    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='DEPRECATED. '
 | 
| -                         'This script now always tries to reset USB.')
 | 
| -  parser.add_option('--json-output',
 | 
| -                    help='Output JSON information into a specified file.')
 | 
| -  parser.add_option('-v', '--verbose', action='count', default=1,
 | 
| -                    help='Log more information.')
 | 
| -
 | 
| -  options, args = parser.parse_args()
 | 
| -  if args:
 | 
| -    parser.error('Unknown options %s' % args)
 | 
| -
 | 
| -  run_tests_helper.SetLogLevel(options.verbose)
 | 
| -
 | 
| -  devices = RecoverDevices(options)
 | 
| +  parser = argparse.ArgumentParser()
 | 
| +  parser.add_argument('--out-dir',
 | 
| +                      help='Directory where the device path is stored',
 | 
| +                      default=os.path.join(constants.DIR_SOURCE_ROOT, 'out'))
 | 
| +  parser.add_argument('--no-provisioning-check', action='store_true',
 | 
| +                      help='Will not check if devices are provisioned '
 | 
| +                           'properly.')
 | 
| +  parser.add_argument('--device-status-dashboard', action='store_true',
 | 
| +                      help='Output device status data for dashboard.')
 | 
| +  parser.add_argument('--restart-usb', action='store_true',
 | 
| +                      help='DEPRECATED. '
 | 
| +                           'This script now always tries to reset USB.')
 | 
| +  parser.add_argument('--json-output',
 | 
| +                      help='Output JSON information into a specified file.')
 | 
| +  parser.add_argument('--blacklist-file', help='Device blacklist JSON file.')
 | 
| +  parser.add_argument('-v', '--verbose', action='count', default=1,
 | 
| +                      help='Log more information.')
 | 
| +
 | 
| +  args = parser.parse_args()
 | 
| +
 | 
| +  run_tests_helper.SetLogLevel(args.verbose)
 | 
| +
 | 
| +  if args.blacklist_file:
 | 
| +    blacklist = device_blacklist.Blacklist(args.blacklist_file)
 | 
| +  else:
 | 
| +    # TODO(jbudorick): Remove this once bots pass the blacklist file.
 | 
| +    blacklist = device_blacklist.Blacklist(device_blacklist.BLACKLIST_JSON)
 | 
| +
 | 
| +  devices = RecoverDevices(blacklist, args.out_dir)
 | 
|  
 | 
|    types, builds, batteries, errors, devices_ok, json_data = (
 | 
|        [], [], [], [], [], [])
 | 
|    if devices:
 | 
|      types, builds, batteries, errors, devices_ok, json_data = (
 | 
| -        zip(*[DeviceInfo(dev, options) for dev in devices]))
 | 
| +        zip(*[DeviceInfo(dev, args) for dev in devices]))
 | 
|  
 | 
|    # Write device info to file for buildbot info display.
 | 
|    if os.path.exists('/home/chrome-bot'):
 | 
| @@ -316,7 +322,7 @@ def main():
 | 
|          except Exception:
 | 
|            pass
 | 
|  
 | 
| -  err_msg = CheckForMissingDevices(options, devices) or []
 | 
| +  err_msg = CheckForMissingDevices(args, devices) or []
 | 
|  
 | 
|    unique_types = list(set(types))
 | 
|    unique_builds = list(set(builds))
 | 
| @@ -350,7 +356,7 @@ def main():
 | 
|      subject = 'Device status check errors on %s, %s.' % (slave_name, bot_name)
 | 
|      SendEmail(from_address, to_addresses, [], subject, '\n'.join(err_msg))
 | 
|  
 | 
| -  if options.device_status_dashboard:
 | 
| +  if args.device_status_dashboard:
 | 
|      offline_devices = [
 | 
|          device_utils.DeviceUtils(a)
 | 
|          for a in adb_wrapper.AdbWrapper.Devices(is_ready=False)
 | 
| @@ -366,15 +372,15 @@ def main():
 | 
|                                                  [battery], '%',
 | 
|                                                  'unimportant')
 | 
|  
 | 
| -  if options.json_output:
 | 
| -    with open(options.json_output, 'wb') as f:
 | 
| +  if args.json_output:
 | 
| +    with open(args.json_output, 'wb') as f:
 | 
|        f.write(json.dumps(json_data, indent=4))
 | 
|  
 | 
|    num_failed_devs = 0
 | 
|    for device_ok, device in zip(devices_ok, devices):
 | 
|      if not device_ok:
 | 
|        logging.warning('Blacklisting %s', str(device))
 | 
| -      device_blacklist.ExtendBlacklist([str(device)])
 | 
| +      blacklist.Extend([str(device)])
 | 
|        num_failed_devs += 1
 | 
|  
 | 
|    if num_failed_devs == len(devices):
 | 
| 
 |