Home | History | Annotate | Download | only in device
      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 
      5 import json
      6 import os
      7 import threading
      8 
      9 from pylib import constants
     10 _BLACKLIST_JSON = os.path.join(
     11     constants.DIR_SOURCE_ROOT,
     12     os.environ.get('CHROMIUM_OUT_DIR', 'out'),
     13     'bad_devices.json')
     14 
     15 # Note that this only protects against concurrent accesses to the blacklist
     16 # within a process.
     17 _blacklist_lock = threading.RLock()
     18 
     19 def ReadBlacklist():
     20   """Reads the blacklist from the _BLACKLIST_JSON file.
     21 
     22   Returns:
     23     A list containing bad devices.
     24   """
     25   with _blacklist_lock:
     26     if not os.path.exists(_BLACKLIST_JSON):
     27       return []
     28 
     29     with open(_BLACKLIST_JSON, 'r') as f:
     30       return json.load(f)
     31 
     32 
     33 def WriteBlacklist(blacklist):
     34   """Writes the provided blacklist to the _BLACKLIST_JSON file.
     35 
     36   Args:
     37     blacklist: list of bad devices to write to the _BLACKLIST_JSON file.
     38   """
     39   with _blacklist_lock:
     40     with open(_BLACKLIST_JSON, 'w') as f:
     41       json.dump(list(set(blacklist)), f)
     42 
     43 
     44 def ExtendBlacklist(devices):
     45   """Adds devices to _BLACKLIST_JSON file.
     46 
     47   Args:
     48     devices: list of bad devices to be added to the _BLACKLIST_JSON file.
     49   """
     50   with _blacklist_lock:
     51     blacklist = ReadBlacklist()
     52     blacklist.extend(devices)
     53     WriteBlacklist(blacklist)
     54 
     55 
     56 def ResetBlacklist():
     57   """Erases the _BLACKLIST_JSON file if it exists."""
     58   with _blacklist_lock:
     59     if os.path.exists(_BLACKLIST_JSON):
     60       os.remove(_BLACKLIST_JSON)
     61 
     62