| 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 _blacklist_lock = threading.RLock() |
| 16 |
| 14 def ReadBlacklist(): | 17 def ReadBlacklist(): |
| 15 """Reads the blacklist from the _BLACKLIST_JSON file. | 18 """Reads the blacklist from the _BLACKLIST_JSON file. |
| 16 | 19 |
| 17 Returns: | 20 Returns: |
| 18 A list containing bad devices. | 21 A list containing bad devices. |
| 19 """ | 22 """ |
| 20 if not os.path.exists(_BLACKLIST_JSON): | 23 with _blacklist_lock: |
| 21 return [] | 24 if not os.path.exists(_BLACKLIST_JSON): |
| 25 return [] |
| 22 | 26 |
| 23 with open(_BLACKLIST_JSON, 'r') as f: | 27 with open(_BLACKLIST_JSON, 'r') as f: |
| 24 return json.load(f) | 28 return json.load(f) |
| 25 | 29 |
| 26 | 30 |
| 27 def WriteBlacklist(blacklist): | 31 def WriteBlacklist(blacklist): |
| 28 """Writes the provided blacklist to the _BLACKLIST_JSON file. | 32 """Writes the provided blacklist to the _BLACKLIST_JSON file. |
| 29 | 33 |
| 30 Args: | 34 Args: |
| 31 blacklist: list of bad devices to write to the _BLACKLIST_JSON file. | 35 blacklist: list of bad devices to write to the _BLACKLIST_JSON file. |
| 32 """ | 36 """ |
| 33 with open(_BLACKLIST_JSON, 'w') as f: | 37 with _blacklist_lock: |
| 34 json.dump(list(set(blacklist)), f) | 38 with open(_BLACKLIST_JSON, 'w') as f: |
| 39 json.dump(list(set(blacklist)), f) |
| 35 | 40 |
| 36 | 41 |
| 37 def ExtendBlacklist(devices): | 42 def ExtendBlacklist(devices): |
| 38 """Adds devices to _BLACKLIST_JSON file. | 43 """Adds devices to _BLACKLIST_JSON file. |
| 39 | 44 |
| 40 Args: | 45 Args: |
| 41 devices: list of bad devices to be added to the _BLACKLIST_JSON file. | 46 devices: list of bad devices to be added to the _BLACKLIST_JSON file. |
| 42 """ | 47 """ |
| 43 blacklist = ReadBlacklist() | 48 with _blacklist_lock: |
| 44 blacklist.extend(devices) | 49 blacklist = ReadBlacklist() |
| 45 WriteBlacklist(blacklist) | 50 blacklist.extend(devices) |
| 51 WriteBlacklist(blacklist) |
| 46 | 52 |
| 47 | 53 |
| 48 def ResetBlacklist(): | 54 def ResetBlacklist(): |
| 49 """Erases the _BLACKLIST_JSON file if it exists.""" | 55 """Erases the _BLACKLIST_JSON file if it exists.""" |
| 50 if os.path.exists(_BLACKLIST_JSON): | 56 with _blacklist_lock: |
| 51 os.remove(_BLACKLIST_JSON) | 57 if os.path.exists(_BLACKLIST_JSON): |
| 58 os.remove(_BLACKLIST_JSON) |
| 52 | 59 |
| OLD | NEW |