Skip to content

Commit d762763

Browse files
committed
Update Cachedir file copy error handling to handle the expected exceptions shutil.SameFileError, IOError for the default copy shutil.copy(). Move notice that file has been retrieved until after the file has actually been copied and didn't thrown exception
1 parent a4e7c4e commit d762763

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
3636
- Fix occasional test failures caused by not being able to find a file or directory fixture
3737
when running multiple tests with multiple jobs.
3838

39+
From Raven Kopelman (@ravenAtSafe on GitHub):
40+
- Prevent files from being partially read from the cache. If there's an error while copying the file
41+
is now removed.
42+
3943
From Joachim Kuebart:
4044
- Suppress missing SConscript deprecation warning if `must_exist=False`
4145
is used.
@@ -48,6 +52,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
4852
From Daniel Moody:
4953
- Fix issue where java parsed a class incorrectly from lambdas used after a new.
5054

55+
5156
From Mats Wichmann:
5257
- Complete tests for Dictionary, env.keys() and env.values() for
5358
OverrideEnvironment. Enable env.setdefault() method, add tests.

SCons/CacheDir.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import os
3030
import stat
3131
import sys
32+
import shutil
3233

3334
import SCons.Action
3435
import SCons.Errors
@@ -50,31 +51,28 @@ def CacheRetrieveFunc(target, source, env):
5051
cd.CacheDebug('CacheRetrieve(%s): %s not in cache\n', t, cachefile)
5152
return 1
5253
cd.hits += 1
53-
cd.CacheDebug('CacheRetrieve(%s): retrieving from %s\n', t, cachefile)
5454
if SCons.Action.execute_actions:
5555
if fs.islink(cachefile):
5656
fs.symlink(fs.readlink(cachefile), t.get_internal_path())
5757
else:
5858
try:
5959
env.copy_from_cache(cachefile, t.get_internal_path())
60-
except:
61-
try:
60+
except (shutil.SameFileError, IOError) as e:
6261
# In case file was partially retrieved (and now corrupt)
6362
# delete it to avoid poisoning commands like 'ar' that
6463
# read from the initial state of the file they are writing
6564
# to.
6665
t.fs.unlink(t.get_internal_path())
67-
except:
68-
pass
69-
70-
raise
66+
cd.CacheDebug('CacheRetrieve(%s): Error while retrieving from %s deleting %s\n', t, cachefile)
67+
raise e
7168

7269
try:
7370
os.utime(cachefile, None)
7471
except OSError:
7572
pass
7673
st = fs.stat(cachefile)
7774
fs.chmod(t.get_internal_path(), stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
75+
cd.CacheDebug('CacheRetrieve(%s): retrieved from %s\n', t, cachefile)
7876
return 0
7977

8078
def CacheRetrieveString(target, source, env):
@@ -83,7 +81,7 @@ def CacheRetrieveString(target, source, env):
8381
cd = env.get_CacheDir()
8482
cachedir, cachefile = cd.cachepath(t)
8583
if t.fs.exists(cachefile):
86-
return "Retrieving `%s' from cache" % t.get_internal_path()
84+
return "Retrieved `%s' from cache" % t.get_internal_path()
8785
return None
8886

8987
CacheRetrieve = SCons.Action.Action(CacheRetrieveFunc, CacheRetrieveString)

test/CacheDir/debug.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# __COPYRIGHT__
3+
# Copyright The SCons Foundation
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining
66
# a copy of this software and associated documentation files (the
@@ -22,7 +22,6 @@
2222
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
#
2424

25-
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
2625

2726
"""
2827
Test the --cache-debug option to see if it prints the expected messages.
@@ -145,16 +144,16 @@ def cat(env, source, target):
145144

146145
expect = \
147146
r"""Retrieved `aaa.out' from cache
148-
CacheRetrieve\(aaa.out\): retrieving from [0-9a-fA-F]+
147+
CacheRetrieve\(aaa.out\): retrieved from [0-9a-fA-F]+
149148
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
150149
Retrieved `bbb.out' from cache
151-
CacheRetrieve\(bbb.out\): retrieving from [0-9a-fA-F]+
150+
CacheRetrieve\(bbb.out\): retrieved from [0-9a-fA-F]+
152151
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
153152
Retrieved `ccc.out' from cache
154-
CacheRetrieve\(ccc.out\): retrieving from [0-9a-fA-F]+
153+
CacheRetrieve\(ccc.out\): retrieved from [0-9a-fA-F]+
155154
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
156155
Retrieved `all' from cache
157-
CacheRetrieve\(all\): retrieving from [0-9a-fA-F]+
156+
CacheRetrieve\(all\): retrieved from [0-9a-fA-F]+
158157
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
159158
"""
160159

@@ -179,13 +178,13 @@ def cat(env, source, target):
179178
stdout=expect)
180179

181180
expect = \
182-
r"""CacheRetrieve\(aaa.out\): retrieving from [0-9a-fA-F]+
181+
r"""CacheRetrieve\(aaa.out\): retrieved from [0-9a-fA-F]+
183182
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
184-
CacheRetrieve\(bbb.out\): retrieving from [0-9a-fA-F]+
183+
CacheRetrieve\(bbb.out\): retrieved from [0-9a-fA-F]+
185184
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
186-
CacheRetrieve\(ccc.out\): retrieving from [0-9a-fA-F]+
185+
CacheRetrieve\(ccc.out\): retrieved from [0-9a-fA-F]+
187186
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
188-
CacheRetrieve\(all\): retrieving from [0-9a-fA-F]+
187+
CacheRetrieve\(all\): retrieved from [0-9a-fA-F]+
189188
requests: [0-9]+, hits: [0-9]+, misses: [0-9]+, hit rate: [0-9]+\.[0-9]{2,}%
190189
"""
191190

0 commit comments

Comments
 (0)