Skip to content

Check if license cache exists after obtaining lock #4274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/licensedcode/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,11 @@ def load_or_build(
create_dir(idx_cache_dir)
cache_file = os.path.join(idx_cache_dir, LICENSE_INDEX_FILENAME)

has_cache = os.path.exists(cache_file) and os.path.getsize(cache_file)

# bypass build if cache exists
if has_cache and not force:
try:
# save the list of additional directories included in the cache, or None if the cache does not
# include any additional directories
return load_cache_file(cache_file)
except Exception as e:
# work around some rare Windows quirks
import traceback
print('Inconsistent License cache: rebuilding index.')
print(str(e))
print(traceback.format_exc())
if not force:
license_cache = _load_cache_file_if_exists(cache_file)
if license_cache is not None:
return license_cache

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

additional_directories = []
if only_builtin:
Expand Down Expand Up @@ -207,6 +202,24 @@ def dump(self, cache_file):
pickle.dump(self, fn, protocol=PICKLE_PROTOCOL)


def _load_cache_file_if_exists(cache_file):
"""
Return a LicenseCache loaded from ``cache_file`` if it exists.
"""
if os.path.exists(cache_file) and os.path.getsize(cache_file):
try:
# save the list of additional directories included in the cache, or None if the cache does not
# include any additional directories
return load_cache_file(cache_file)
except Exception as e:
# work around some rare Windows quirks
import traceback
print('Inconsistent License cache: rebuilding index.')
print(str(e))
print(traceback.format_exc())
return None


def build_index(
licenses_db=None,
licenses_data_dir=None,
Expand Down
2 changes: 1 addition & 1 deletion tests/licensedcode/test_zzzz_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_get_spdx_symbols_checks_duplicates_with_deprecated_on_live_db(self):
def test_build_spdx_license_expression(self):
from licensedcode.cache import build_spdx_license_expression
assert build_spdx_license_expression("mit")

def test_build_spdx_license_expression_fails_on_invalid_key_none(self):
from licensedcode.cache import build_spdx_license_expression
from licensedcode.cache import InvalidLicenseKeyError
Expand Down
Loading