blob: 5fb586f6de4c2a5896c51e389ffb3dce7071dfa0 [file] [log] [blame]
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""A module to keep track of devices across builds."""
import json
import logging
import os
import six
logger = logging.getLogger(__name__)
def GetPersistentDeviceList(file_name):
"""Returns a list of devices.
file_name: the file name containing a list of devices.
Returns: List of device serial numbers that were on the bot.
if not os.path.isfile(file_name):
logger.warning("Device file %s doesn't exist.", file_name)
return []
with open(file_name) as f:
devices = json.load(f)
if not isinstance(devices, list) or not all(
isinstance(d, six.string_types) for d in devices):
logger.warning('Unrecognized device file format: %s', devices)
return []
return [d for d in devices if d != '(error)']
except ValueError:
'Error reading device file %s. Falling back to old format.', file_name)
# TODO(bpastene) Remove support for old unstructured file format.
with open(file_name) as f:
return [d for d in if d != '(error)']
def WritePersistentDeviceList(file_name, device_list):
path = os.path.dirname(file_name)
assert isinstance(device_list, list)
# If there is a problem with ADB "(error)" can be added to the device list.
# These should be removed before saving.
device_list = [d for d in device_list if d != '(error)']
if not os.path.exists(path):
with open(file_name, 'w') as f:
json.dump(device_list, f)