Skip to content

Commit f757ce2

Browse files
committed
feat: Check skeleton for S3 XML error bodies before returning
1 parent 4f939b9 commit f757ce2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

templateflow/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ def get(template, raise_empty=False, **kwargs):
159159
if raise_empty and not out_file:
160160
raise Exception('No results found')
161161

162+
# Truncate possible S3 error files from previous attempts
163+
_truncate_s3_errors(out_file)
164+
162165
# Try DataLad first
163166
dl_missing = [p for p in out_file if not p.is_file()]
164167
if TF_USE_DATALAD and dl_missing:
@@ -395,3 +398,20 @@ def _normalize_ext(value):
395398
if isinstance(value, str):
396399
return f'{"" if value.startswith(".") else "."}{value}'
397400
return [_normalize_ext(v) for v in value]
401+
402+
403+
def _truncate_s3_errors(filepaths):
404+
"""
405+
Truncate XML error bodies saved by previous versions of TemplateFlow.
406+
407+
Parameters
408+
----------
409+
filepaths : list of Path
410+
List of file paths to check and truncate if necessary.
411+
"""
412+
for filepath in filepaths:
413+
if filepath.is_file(follow_symlinks=False) and 0 < filepath.stat().st_size < 1024:
414+
with open(filepath, 'rb') as f:
415+
content = f.read(100)
416+
if content.startswith(b'<?xml') and b'<Error><Code>' in content:
417+
filepath.write_bytes(b'') # Truncate file to zero bytes

0 commit comments

Comments
 (0)