@@ -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+
210223def build_index (
211224 licenses_db = None ,
212225 licenses_data_dir = None ,
0 commit comments