Skip to content

Commit 429c932

Browse files
committed
RF: git-annex specific handling in to_filename + allow NIBABEL_REMOVE_BEFORE_TO_FILENAME env var
1 parent 99af2f4 commit 429c932

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

nibabel/filebasedimages.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
splitext_addext)
1818
from .openers import ImageOpener
1919

20+
# OS-specific path
21+
_GIT_ANNEX_OBJECTS_PATH = os.path.join('.git', 'annex', 'objects')
22+
23+
def _is_annex_symlink(path):
24+
"""Return True if file is a symlink to the git-annex objects"""
25+
return os.path.islink(path) and _GIT_ANNEX_OBJECTS_PATH in os.path.realpath(path)
26+
2027

2128
class ImageFileError(Exception):
2229
pass
@@ -349,10 +356,11 @@ def to_filename(self, filename):
349356
None
350357
'''
351358
self.file_map = file_map = self.filespec_to_file_map(filename)
359+
remove_env = os.environ.get('NIBABEL_REMOVE_BEFORE_TO_FILENAME', None)
352360
for _, fh in file_map.items():
353361
if not isinstance(fh, FileHolder):
354362
continue
355-
if os.path.exists(fh.filename):
363+
if os.path.lexists(fh.filename) and (remove_env or _is_annex_symlink(fh.filename)):
356364
# Remove previous file where new file would be saved
357365
# Necessary e.g. for cases where file is a symlink pointing
358366
# to some non-writable file (e.g. under git annex control)

0 commit comments

Comments
 (0)