Skip to content

Commit 42e065b

Browse files
Markus ObendraufMarkusObendrauf
authored andcommitted
Check if license cache exists after obtaining lock
Fix: #4273 Signed-off-by: Markus Obendrauf <[email protected]>
1 parent 6a9db8f commit 42e065b

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/licensedcode/cache.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,11 @@ def load_or_build(
103103
create_dir(idx_cache_dir)
104104
cache_file = os.path.join(idx_cache_dir, LICENSE_INDEX_FILENAME)
105105

106-
has_cache = os.path.exists(cache_file) and os.path.getsize(cache_file)
107-
108106
# bypass build if cache exists
109-
if has_cache and not force:
110-
try:
111-
# save the list of additional directories included in the cache, or None if the cache does not
112-
# include any additional directories
113-
return load_cache_file(cache_file)
114-
except Exception as e:
115-
# work around some rare Windows quirks
116-
import traceback
117-
print('Inconsistent License cache: rebuilding index.')
118-
print(str(e))
119-
print(traceback.format_exc())
107+
if not force:
108+
license_cache = _load_cache_file_if_exists(cache_file)
109+
if license_cache is not None:
110+
return license_cache
120111

121112
from licensedcode.models import licenses_data_dir as ldd
122113
from licensedcode.models import rules_data_dir as rdd
@@ -135,6 +126,10 @@ def load_or_build(
135126
try:
136127
# acquire lock and wait until timeout to get a lock or die
137128
with lockfile.FileLock(lock_file).locked(timeout=timeout):
129+
if not force:
130+
license_cache = _load_cache_file_if_exists(cache_file)
131+
if license_cache is not None:
132+
return license_cache
138133

139134
additional_directories = []
140135
if only_builtin:
@@ -207,6 +202,24 @@ def dump(self, cache_file):
207202
pickle.dump(self, fn, protocol=PICKLE_PROTOCOL)
208203

209204

205+
def _load_cache_file_if_exists(cache_file):
206+
"""
207+
Return a LicenseCache loaded from ``cache_file`` if it exists.
208+
"""
209+
if os.path.exists(cache_file) and os.path.getsize(cache_file):
210+
try:
211+
# save the list of additional directories included in the cache, or None if the cache does not
212+
# include any additional directories
213+
return load_cache_file(cache_file)
214+
except Exception as e:
215+
# work around some rare Windows quirks
216+
import traceback
217+
print('Inconsistent License cache: rebuilding index.')
218+
print(str(e))
219+
print(traceback.format_exc())
220+
return None
221+
222+
210223
def build_index(
211224
licenses_db=None,
212225
licenses_data_dir=None,

tests/licensedcode/test_zzzz_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_get_spdx_symbols_checks_duplicates_with_deprecated_on_live_db(self):
159159
def test_build_spdx_license_expression(self):
160160
from licensedcode.cache import build_spdx_license_expression
161161
assert build_spdx_license_expression("mit")
162-
162+
163163
def test_build_spdx_license_expression_fails_on_invalid_key_none(self):
164164
from licensedcode.cache import build_spdx_license_expression
165165
from licensedcode.cache import InvalidLicenseKeyError

0 commit comments

Comments
 (0)