Skip to content

Commit d1211be

Browse files
committed
Merge pull request #1388 from effigies/link_chain
FIX: Use realpath to determine hard link source
2 parents beec4be + 7c26e16 commit d1211be

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Next release
2121
* API: Default model level for the bedpostx workflow has been set to "2" following FSL 5.0.9 lead
2222
* ENH: New interfaces for interacting with AWS S3: S3DataSink and S3DataGrabber (https://github.com/nipy/nipype/pull/1201)
2323
* ENH: Interfaces for MINC tools (https://github.com/nipy/nipype/pull/1304)
24+
* FIX: Use realpath to determine hard link source (https://github.com/nipy/nipype/pull/1388)
2425

2526
Release 0.11.0 (September 15, 2015)
2627
============

nipype/utils/filemanip.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def nipype_hardlink_wrapper(raw_src, raw_dst):
4242
If the hardlink fails, then fall back to using
4343
a standard copy.
4444
"""
45-
src = os.path.normpath(raw_src)
45+
# Use realpath to avoid hardlinking symlinks
46+
src = os.path.realpath(raw_src)
47+
# Use normpath, in case destination is a symlink
4648
dst = os.path.normpath(raw_dst)
4749
del raw_src
4850
del raw_dst
@@ -283,12 +285,15 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
283285
matofile = originalfile[:-4] + ".mat"
284286
if os.path.exists(matofile):
285287
matnfile = newfile[:-4] + ".mat"
286-
copyfile(matofile, matnfile, copy)
287-
copyfile(hdrofile, hdrnfile, copy)
288+
copyfile(matofile, matnfile, copy, create_new, hashmethod,
289+
use_hardlink)
290+
copyfile(hdrofile, hdrnfile, copy, create_new, hashmethod,
291+
use_hardlink)
288292
elif originalfile.endswith(".BRIK"):
289293
hdrofile = originalfile[:-5] + ".HEAD"
290294
hdrnfile = newfile[:-5] + ".HEAD"
291-
copyfile(hdrofile, hdrnfile, copy)
295+
copyfile(hdrofile, hdrnfile, copy, create_new, hashmethod,
296+
use_hardlink)
292297

293298
return newfile
294299

nipype/utils/tests/test_filemanip.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ def test_copyfiles():
132132
os.unlink(new_hdr2)
133133

134134

135+
def test_linkchain():
136+
if os.name is not 'posix':
137+
return
138+
orig_img, orig_hdr = _temp_analyze_files()
139+
pth, fname = os.path.split(orig_img)
140+
new_img1 = os.path.join(pth, 'newfile1.img')
141+
new_hdr1 = os.path.join(pth, 'newfile1.hdr')
142+
new_img2 = os.path.join(pth, 'newfile2.img')
143+
new_hdr2 = os.path.join(pth, 'newfile2.hdr')
144+
new_img3 = os.path.join(pth, 'newfile3.img')
145+
new_hdr3 = os.path.join(pth, 'newfile3.hdr')
146+
copyfile(orig_img, new_img1)
147+
yield assert_true, os.path.islink(new_img1)
148+
yield assert_true, os.path.islink(new_hdr1)
149+
copyfile(new_img1, new_img2, copy=True)
150+
yield assert_false, os.path.islink(new_img2)
151+
yield assert_false, os.path.islink(new_hdr2)
152+
yield assert_false, os.path.samefile(orig_img, new_img2)
153+
yield assert_false, os.path.samefile(orig_hdr, new_hdr2)
154+
copyfile(new_img1, new_img3, copy=True, use_hardlink=True)
155+
yield assert_false, os.path.islink(new_img3)
156+
yield assert_false, os.path.islink(new_hdr3)
157+
yield assert_true, os.path.samefile(orig_img, new_img3)
158+
yield assert_true, os.path.samefile(orig_hdr, new_hdr3)
159+
os.unlink(new_img1)
160+
os.unlink(new_hdr1)
161+
os.unlink(new_img2)
162+
os.unlink(new_hdr2)
163+
os.unlink(new_img3)
164+
os.unlink(new_hdr3)
165+
# final cleanup
166+
os.unlink(orig_img)
167+
os.unlink(orig_hdr)
168+
169+
135170
def test_filename_to_list():
136171
x = filename_to_list('foo.nii')
137172
yield assert_equal, x, ['foo.nii']

0 commit comments

Comments
 (0)