OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 import json |
6 import os | 6 import os |
7 import threading | 7 import threading |
8 | 8 |
9 from pylib import constants | 9 from pylib import constants |
10 _BLACKLIST_JSON = os.path.join( | 10 |
| 11 # TODO(jbudorick): Remove this once the blacklist is optional. |
| 12 BLACKLIST_JSON = os.path.join( |
11 constants.DIR_SOURCE_ROOT, | 13 constants.DIR_SOURCE_ROOT, |
12 os.environ.get('CHROMIUM_OUT_DIR', 'out'), | 14 os.environ.get('CHROMIUM_OUT_DIR', 'out'), |
13 'bad_devices.json') | 15 'bad_devices.json') |
14 | 16 |
15 # Note that this only protects against concurrent accesses to the blacklist | 17 class Blacklist(object): |
16 # within a process. | 18 |
17 _blacklist_lock = threading.RLock() | 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 |
18 | 63 |
19 def ReadBlacklist(): | 64 def ReadBlacklist(): |
20 """Reads the blacklist from the _BLACKLIST_JSON file. | 65 # TODO(jbudorick): Phase out once all clients have migrated. |
21 | 66 return Blacklist(BLACKLIST_JSON).Read() |
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 | 67 |
32 | 68 |
33 def WriteBlacklist(blacklist): | 69 def WriteBlacklist(blacklist): |
34 """Writes the provided blacklist to the _BLACKLIST_JSON file. | 70 # TODO(jbudorick): Phase out once all clients have migrated. |
35 | 71 Blacklist(BLACKLIST_JSON).Write(blacklist) |
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 | 72 |
43 | 73 |
44 def ExtendBlacklist(devices): | 74 def ExtendBlacklist(devices): |
45 """Adds devices to _BLACKLIST_JSON file. | 75 # TODO(jbudorick): Phase out once all clients have migrated. |
46 | 76 Blacklist(BLACKLIST_JSON).Extend(devices) |
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 | 77 |
55 | 78 |
56 def ResetBlacklist(): | 79 def ResetBlacklist(): |
57 """Erases the _BLACKLIST_JSON file if it exists.""" | 80 # TODO(jbudorick): Phase out once all clients have migrated. |
58 with _blacklist_lock: | 81 Blacklist(BLACKLIST_JSON).Reset() |
59 if os.path.exists(_BLACKLIST_JSON): | |
60 os.remove(_BLACKLIST_JSON) | |
61 | 82 |
OLD | NEW |