| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 # pylint: disable=unused-wildcard-import |
| 6 import os | 6 # pylint: disable=wildcard-import |
| 7 import threading | |
| 8 | 7 |
| 9 from pylib import constants | 8 from devil.android.device_blacklist import * |
| 10 | |
| 11 # TODO(jbudorick): Remove this once the blacklist is optional. | |
| 12 BLACKLIST_JSON = os.path.join( | |
| 13 constants.DIR_SOURCE_ROOT, | |
| 14 os.environ.get('CHROMIUM_OUT_DIR', 'out'), | |
| 15 'bad_devices.json') | |
| 16 | |
| 17 class Blacklist(object): | |
| 18 | |
| 19 def __init__(self, path): | |
| 20 self._blacklist_lock = threading.RLock() | |
| 21 self._path = path | |
| 22 | |
| 23 def Read(self): | |
| 24 """Reads the blacklist from the blacklist file. | |
| 25 | |
| 26 Returns: | |
| 27 A list containing bad devices. | |
| 28 """ | |
| 29 with self._blacklist_lock: | |
| 30 if not os.path.exists(self._path): | |
| 31 return [] | |
| 32 | |
| 33 with open(self._path, 'r') as f: | |
| 34 return json.load(f) | |
| 35 | |
| 36 def Write(self, blacklist): | |
| 37 """Writes the provided blacklist to the blacklist file. | |
| 38 | |
| 39 Args: | |
| 40 blacklist: list of bad devices to write to the blacklist file. | |
| 41 """ | |
| 42 with self._blacklist_lock: | |
| 43 with open(self._path, 'w') as f: | |
| 44 json.dump(list(set(blacklist)), f) | |
| 45 | |
| 46 def Extend(self, devices): | |
| 47 """Adds devices to blacklist file. | |
| 48 | |
| 49 Args: | |
| 50 devices: list of bad devices to be added to the blacklist file. | |
| 51 """ | |
| 52 with self._blacklist_lock: | |
| 53 blacklist = ReadBlacklist() | |
| 54 blacklist.extend(devices) | |
| 55 WriteBlacklist(blacklist) | |
| 56 | |
| 57 def Reset(self): | |
| 58 """Erases the blacklist file if it exists.""" | |
| 59 with self._blacklist_lock: | |
| 60 if os.path.exists(self._path): | |
| 61 os.remove(self._path) | |
| 62 | |
| 63 | |
| 64 def ReadBlacklist(): | |
| 65 # TODO(jbudorick): Phase out once all clients have migrated. | |
| 66 return Blacklist(BLACKLIST_JSON).Read() | |
| 67 | |
| 68 | |
| 69 def WriteBlacklist(blacklist): | |
| 70 # TODO(jbudorick): Phase out once all clients have migrated. | |
| 71 Blacklist(BLACKLIST_JSON).Write(blacklist) | |
| 72 | |
| 73 | |
| 74 def ExtendBlacklist(devices): | |
| 75 # TODO(jbudorick): Phase out once all clients have migrated. | |
| 76 Blacklist(BLACKLIST_JSON).Extend(devices) | |
| 77 | |
| 78 | |
| 79 def ResetBlacklist(): | |
| 80 # TODO(jbudorick): Phase out once all clients have migrated. | |
| 81 Blacklist(BLACKLIST_JSON).Reset() | |
| 82 | |
| OLD | NEW |