@@ -81,27 +81,9 @@ def load_or_build(
8181 cached_directories_file = os .path .join (idx_cache_dir , CACHED_DIRECTORIES_FILENAME )
8282
8383 has_cache = os .path .exists (cache_file ) and os .path .getsize (cache_file )
84- has_cached_directories = os .path .exists (cached_directories_file ) and os .path .getsize (cached_directories_file )
85- should_rebuild_cache = False
86-
87- if has_cached_directories :
88- # if we have cached additional directories of licenses, check if those licenses are equal to the additional
89- # directories passed in
90- with open (cached_directories_file , 'rb' ) as file :
91- cached_additional_directories = pickle .load (file )
92-
93- # we need to rebuild the cache if the list of additional directories we passed in is different than
94- # the set of additional directories current included in the index cache
95- should_rebuild_cache = additional_directories is not None \
96- and sorted (additional_directories ) != sorted (cached_additional_directories )
97- else :
98- # otherwise, we don't have a file of cached directories. If there are additional directories passed in,
99- # we know we need to make a new cache file.
100- if additional_directories :
101- should_rebuild_cache = True
10284
10385 # bypass build if cache exists
104- if has_cache and not force and not should_rebuild_cache :
86+ if has_cache and not force :
10587 try :
10688 return load_cache_file (cache_file )
10789 except Exception as e :
@@ -163,7 +145,8 @@ def load_or_build(
163145 with open (cache_file , 'wb' ) as fn :
164146 pickle .dump (license_cache , fn , protocol = PICKLE_PROTOCOL )
165147
166- # save the list of additional directories included in the cache
148+ # save the list of additional directories included in the cache, or None if the cache does not
149+ # include any additional directories
167150 with open (cached_directories_file , 'wb' ) as file :
168151 pickle .dump (additional_directories , file , protocol = PICKLE_PROTOCOL )
169152
@@ -376,6 +359,8 @@ def populate_cache(force=False, index_all_languages=False, additional_directorie
376359 Load or build and cache a LicenseCache. Return None.
377360 """
378361 global _LICENSE_CACHE
362+ if need_cache_rebuild (additional_directories ):
363+ force = True
379364 if force or not _LICENSE_CACHE :
380365 _LICENSE_CACHE = LicenseCache .load_or_build (
381366 licensedcode_cache_dir = licensedcode_cache_dir ,
@@ -388,6 +373,34 @@ def populate_cache(force=False, index_all_languages=False, additional_directorie
388373 )
389374
390375
376+ def need_cache_rebuild (additional_directories ):
377+ """
378+ Return true if we need to rebuild the index cache.
379+ """
380+ idx_cache_dir = os .path .join (licensedcode_cache_dir , LICENSE_INDEX_DIR )
381+ cached_directories_file = os .path .join (idx_cache_dir , CACHED_DIRECTORIES_FILENAME )
382+
383+ has_cached_directories = os .path .exists (cached_directories_file )
384+ should_rebuild_cache = False
385+
386+ if has_cached_directories :
387+ # if we have cached additional directories of licenses, check if those licenses are equal to the additional
388+ # directories passed in
389+ with open (cached_directories_file , 'rb' ) as file :
390+ cached_additional_directories = pickle .load (file )
391+
392+ # we need to rebuild the cache if the list of additional directories we passed in is not a subset of
393+ # the set of additional directories currently included in the index cache
394+ should_rebuild_cache = additional_directories is not None and cached_additional_directories is not None \
395+ and not set (additional_directories ).issubset (set (cached_additional_directories ))
396+ else :
397+ # otherwise, we don't have a file of cached directories. If there are additional directories passed in,
398+ # we know we need to make a new cache file.
399+ if additional_directories :
400+ should_rebuild_cache = True
401+ return should_rebuild_cache
402+
403+
391404def load_cache_file (cache_file ):
392405 """
393406 Return a LicenseCache loaded from ``cache_file``.
0 commit comments