3131import stat
3232import sys
3333import tempfile
34- import uuid
3534
3635import SCons .Action
3736import SCons .Errors
37+ # import SCons.Node.FS # used for hash_chunksice, but causes import loop
3838import SCons .Warnings
3939import SCons .Util
4040
4949cache_force = False
5050cache_show = False
5151cache_readonly = False
52- cache_tmp_uuid = uuid .uuid4 ().hex
5352
5453def CacheRetrieveFunc (target , source , env ) -> int :
5554 t = target [0 ]
@@ -110,7 +109,6 @@ def CachePushFunc(target, source, env) -> None:
110109
111110 cd .CacheDebug ('CachePush(%s): pushing to %s\n ' , t , cachefile )
112111
113- tempfile = "%s.tmp%s" % (cachefile ,cache_tmp_uuid )
114112 errfmt = "Unable to copy %s to cache. Cache file is %s"
115113
116114 try :
@@ -119,11 +117,13 @@ def CachePushFunc(target, source, env) -> None:
119117 msg = errfmt % (str (target ), cachefile )
120118 raise SCons .Errors .SConsEnvironmentError (msg )
121119 try :
122- if fs .islink (t .get_internal_path ()):
123- fs .symlink (fs .readlink (t .get_internal_path ()), tempfile )
124- else :
125- cd .copy_to_cache (env , t .get_internal_path (), tempfile )
126- fs .rename (tempfile , cachefile )
120+ with tempfile .TemporaryDirectory (dir = cachedir ) as temp_dir :
121+ temp_file = os .path .join (temp_dir , os .path .basename (cachefile ))
122+ if fs .islink (t .get_internal_path ()):
123+ fs .symlink (fs .readlink (t .get_internal_path ()), temp_file )
124+ else :
125+ cd .copy_to_cache (env , t .get_internal_path (), temp_file )
126+ fs .rename (temp_file , cachefile )
127127
128128 except OSError :
129129 # It's possible someone else tried writing the file at the
0 commit comments