1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import json 6import os 7 8from pylib import constants 9_BLACKLIST_JSON = os.path.join( 10 constants.DIR_SOURCE_ROOT, 11 os.environ.get('CHROMIUM_OUT_DIR', 'out'), 12 'bad_devices.json') 13 14def ReadBlacklist(): 15 """Reads the blacklist from the _BLACKLIST_JSON file. 16 17 Returns: 18 A list containing bad devices. 19 """ 20 if not os.path.exists(_BLACKLIST_JSON): 21 return [] 22 23 with open(_BLACKLIST_JSON, 'r') as f: 24 return json.load(f) 25 26 27def WriteBlacklist(blacklist): 28 """Writes the provided blacklist to the _BLACKLIST_JSON file. 29 30 Args: 31 blacklist: list of bad devices to write to the _BLACKLIST_JSON file. 32 """ 33 with open(_BLACKLIST_JSON, 'w') as f: 34 json.dump(list(set(blacklist)), f) 35 36 37def ExtendBlacklist(devices): 38 """Adds devices to _BLACKLIST_JSON file. 39 40 Args: 41 devices: list of bad devices to be added to the _BLACKLIST_JSON file. 42 """ 43 blacklist = ReadBlacklist() 44 blacklist.extend(devices) 45 WriteBlacklist(blacklist) 46 47 48def ResetBlacklist(): 49 """Erases the _BLACKLIST_JSON file if it exists.""" 50 if os.path.exists(_BLACKLIST_JSON): 51 os.remove(_BLACKLIST_JSON) 52 53