2525import os
2626import os .path
2727import pathlib
28+ import shutil
2829from typing import (
2930 cast ,
3031 TYPE_CHECKING ,
@@ -257,7 +258,13 @@ def parse_and_digest_groovy_content(
257258 # version of the software and its dependencies
258259 hreldir = h .copy ().hexdigest ()
259260
260- ro_cache_paths : "MutableSequence[pathlib.Path]" = []
261+ this_cache_path = cache_path / hreldir
262+ this_cache_path .mkdir (parents = True , exist_ok = True )
263+
264+ # The first path to be inspected must the read-write one
265+ # so no spurious backpropagation operations from read-only to
266+ # already existing read-write one happen
267+ ro_cache_paths : "MutableSequence[pathlib.Path]" = [this_cache_path ]
261268 if ro_cache_directories is not None :
262269 for ro_cache_directory in ro_cache_directories :
263270 if isinstance (ro_cache_directory , pathlib .Path ):
@@ -270,11 +277,6 @@ def parse_and_digest_groovy_content(
270277 if this_ro_cache_path .is_dir ():
271278 ro_cache_paths .append (this_ro_cache_path )
272279
273- this_cache_path = cache_path / hreldir
274- this_cache_path .mkdir (parents = True , exist_ok = True )
275-
276- ro_cache_paths .append (this_cache_path )
277-
278280 # Now, let's go for the content signature
279281 h .update (content .encode ("utf-8" ))
280282 rel_hashpath = h .hexdigest () + ".json.gz"
@@ -289,7 +291,25 @@ def parse_and_digest_groovy_content(
289291 ro_hashpath .as_posix (), mode = "rt" , encoding = "utf-8"
290292 ) as jH :
291293 t_tree = json .load (jH )
292- hashpath = None
294+
295+ # This is needed in order to propagate the cached
296+ # copy from the read-only cache
297+ try :
298+ assert hashpath is not None
299+ if not hashpath .samefile (ro_hashpath ):
300+ # Removing possible stale copy
301+ if hashpath .exists ():
302+ if hashpath .is_dir () and not hashpath .is_symlink ():
303+ shutil .rmtree (hashpath .as_posix ())
304+ else :
305+ hashpath .unlink ()
306+ # New copy
307+ shutil .copy2 (ro_hashpath .as_posix (), hashpath .as_posix ())
308+ hashpath = None
309+ except :
310+ # If it cannot be created for some reason, try again later
311+ pass
312+
293313 break
294314 except :
295315 # If it is unreadable, re-create
0 commit comments