Skip to content

Commit

Permalink
RF: git-annex specific handling in to_filename + allow NIBABEL_REMOVE…
Browse files Browse the repository at this point in the history
…_BEFORE_TO_FILENAME env var
  • Loading branch information
yarikoptic committed Aug 2, 2016
1 parent 99af2f4 commit cf564b6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion nibabel/filebasedimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
splitext_addext)
from .openers import ImageOpener

# OS-specific path
_GIT_ANNEX_OBJECTS_PATH = os.path.join('.git', 'annex', 'objects')


def _is_annex_symlink(path):
"""Return True if file is a symlink to the git-annex objects"""
return os.path.islink(path) and _GIT_ANNEX_OBJECTS_PATH in os.path.realpath(path)


class ImageFileError(Exception):
pass
Expand Down Expand Up @@ -349,10 +357,11 @@ def to_filename(self, filename):
None
'''
self.file_map = file_map = self.filespec_to_file_map(filename)
remove_env = os.environ.get('NIBABEL_REMOVE_BEFORE_TO_FILENAME', None)
for _, fh in file_map.items():
if not isinstance(fh, FileHolder):
continue
if os.path.exists(fh.filename):
if os.path.lexists(fh.filename) and (remove_env or _is_annex_symlink(fh.filename)):
# Remove previous file where new file would be saved
# Necessary e.g. for cases where file is a symlink pointing
# to some non-writable file (e.g. under git annex control)
Expand Down

0 comments on commit cf564b6

Please sign in to comment.