def CheckForMissingDevices(options, devices):
    """Uses file of previous online devices to detect broken phones.

  Args:
    options: out_dir parameter of options 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)
    device_serials = set(d.adb.GetDeviceSerial() for d in devices)

    # last_devices denotes all known devices prior to this run
    last_devices_path = os.path.join(out_dir,
                                     device_list.LAST_DEVICES_FILENAME)
    last_missing_devices_path = os.path.join(
        out_dir, device_list.LAST_MISSING_DEVICES_FILENAME)
    try:
        last_devices = device_list.GetPersistentDeviceList(last_devices_path)
    except IOError:
        # Ignore error, file might not exist
        last_devices = []

    try:
        last_missing_devices = device_list.GetPersistentDeviceList(
            last_missing_devices_path)
    except IOError:
        last_missing_devices = []

    missing_devs = list(set(last_devices) - device_serials)
    new_missing_devs = list(set(missing_devs) - set(last_missing_devices))

    if new_missing_devs and os.environ.get('BUILDBOT_SLAVENAME'):
        logging.info('new_missing_devs %s' % new_missing_devs)
        devices_missing_msg = '%d devices not detected.' % len(missing_devs)
        bb_annotations.PrintSummaryText(devices_missing_msg)

        from_address = '*****@*****.**'
        to_addresses = [
            '*****@*****.**',
            '*****@*****.**'
        ]
        cc_addresses = ['*****@*****.**']
        subject = 'Devices offline on %s, %s, %s' % (
            os.environ.get('BUILDBOT_SLAVENAME'),
            os.environ.get('BUILDBOT_BUILDERNAME'),
            os.environ.get('BUILDBOT_BUILDNUMBER'))
        msg = ('Please reboot the following devices:\n%s' %
               '\n'.join(map(str, new_missing_devs)))
        SendEmail(from_address, to_addresses, cc_addresses, subject, msg)

    all_known_devices = list(device_serials | set(last_devices))
    device_list.WritePersistentDeviceList(last_devices_path, all_known_devices)
    device_list.WritePersistentDeviceList(last_missing_devices_path,
                                          missing_devs)

    if not all_known_devices:
        # This can happen if for some reason the .last_devices file is not
        # present or if it was empty.
        return ['No online devices. Have any devices been plugged in?']
    if missing_devs:
        devices_missing_msg = '%d devices not detected.' % len(missing_devs)
        bb_annotations.PrintSummaryText(devices_missing_msg)
        return [
            'Current online devices: %s' % ', '.join(d
                                                     for d in device_serials),
            '%s are no longer visible. Were they removed?' % missing_devs
        ]
    else:
        new_devs = device_serials - set(last_devices)
        if new_devs and os.path.exists(last_devices_path):
            bb_annotations.PrintWarning()
            bb_annotations.PrintSummaryText('%d new devices detected' %
                                            len(new_devs))
            logging.info('New devices detected:')
            for d in new_devs:
                logging.info('  %s', d)
Esempio n. 2
0
def CheckForMissingDevices(options, adb_online_devs):
    """Uses file of previous online devices to detect broken phones.

  Args:
    options: out_dir parameter of options argument is used as the base
             directory to load and update the cache file.
    adb_online_devs: A list of serial numbers of the currently visible
                     and online attached devices.
  """
    # TODO(navabi): remove this once the bug that causes different number
    # of devices to be detected between calls is fixed.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    out_dir = os.path.abspath(options.out_dir)

    # last_devices denotes all known devices prior to this run
    last_devices_path = os.path.join(out_dir,
                                     device_list.LAST_DEVICES_FILENAME)
    last_missing_devices_path = os.path.join(
        out_dir, device_list.LAST_MISSING_DEVICES_FILENAME)
    try:
        last_devices = device_list.GetPersistentDeviceList(last_devices_path)
    except IOError:
        # Ignore error, file might not exist
        last_devices = []

    try:
        last_missing_devices = device_list.GetPersistentDeviceList(
            last_missing_devices_path)
    except IOError:
        last_missing_devices = []

    missing_devs = list(set(last_devices) - set(adb_online_devs))
    new_missing_devs = list(set(missing_devs) - set(last_missing_devices))

    if new_missing_devs and os.environ.get('BUILDBOT_SLAVENAME'):
        logging.info('new_missing_devs %s' % new_missing_devs)
        devices_missing_msg = '%d devices not detected.' % len(missing_devs)
        bb_annotations.PrintSummaryText(devices_missing_msg)

        from_address = '*****@*****.**'
        to_addresses = [
            '*****@*****.**',
            '*****@*****.**'
        ]
        cc_addresses = ['*****@*****.**']
        subject = 'Devices offline on %s, %s, %s' % (
            os.environ.get('BUILDBOT_SLAVENAME'),
            os.environ.get('BUILDBOT_BUILDERNAME'),
            os.environ.get('BUILDBOT_BUILDNUMBER'))
        msg = ('Please reboot the following devices:\n%s' %
               '\n'.join(map(str, new_missing_devs)))
        SendEmail(from_address, to_addresses, cc_addresses, subject, msg)

    all_known_devices = list(set(adb_online_devs) | set(last_devices))
    device_list.WritePersistentDeviceList(last_devices_path, all_known_devices)
    device_list.WritePersistentDeviceList(last_missing_devices_path,
                                          missing_devs)

    if not all_known_devices:
        # This can happen if for some reason the .last_devices file is not
        # present or if it was empty.
        return ['No online devices. Have any devices been plugged in?']
    if missing_devs:
        devices_missing_msg = '%d devices not detected.' % len(missing_devs)
        bb_annotations.PrintSummaryText(devices_missing_msg)

        # TODO(navabi): Debug by printing both output from GetCmdOutput and
        # GetAttachedDevices to compare results.
        crbug_link = (
            'https://code.google.com/p/chromium/issues/entry?summary='
            '%s&comment=%s&labels=Restrict-View-Google,OS-Android,Infra' %
            (urllib.quote('Device Offline'),
             urllib.quote('Buildbot: %s %s\n'
                          'Build: %s\n'
                          '(please don\'t change any labels)' %
                          (os.environ.get('BUILDBOT_BUILDERNAME'),
                           os.environ.get('BUILDBOT_SLAVENAME'),
                           os.environ.get('BUILDBOT_BUILDNUMBER')))))
        return [
            'Current online devices: %s' % adb_online_devs,
            '%s are no longer visible. Were they removed?\n' % missing_devs,
            'SHERIFF:\n',
            '@@@STEP_LINK@Click here to file a bug@%s@@@\n' % crbug_link,
            'Cache file: %s\n\n' % last_devices_path,
            'adb devices: %s' % GetCmdOutput(['adb', 'devices']),
            'adb devices(GetAttachedDevices): %s' % adb_online_devs
        ]
    else:
        new_devs = set(adb_online_devs) - set(last_devices)
        if new_devs and os.path.exists(last_devices_path):
            bb_annotations.PrintWarning()
            bb_annotations.PrintSummaryText('%d new devices detected' %
                                            len(new_devs))
            print(
                'New devices detected %s. And now back to your '
                'regularly scheduled program.' % list(new_devs))