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