Skip to content

Commit ddecb7c

Browse files
authored
follow symlink in LocalContext downloading (#201)
* follow symlink in LocalContext downloading to prevent broken symbolic links are downloaded * add tests * use copyfile for symlinks * remove realpath * fix typo * add tests for common_file
1 parent 0edfa7c commit ddecb7c

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

dpdispatcher/local_context.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ def download(self,
230230
pass
231231
elif (os.path.exists(rfile)) and (not os.path.exists(lfile)) :
232232
# trivial case, download happily
233-
shutil.move(rfile, lfile)
233+
# for links, copy instead of moving (default behavior of copyfile is following symlinks)
234+
if not os.path.islink(rfile):
235+
shutil.move(rfile, lfile)
236+
else:
237+
shutil.copyfile(rfile, lfile)
234238
elif (os.path.exists(rfile)) and (os.path.exists(lfile)) :
235239
# both exists, replace!
236240
dlog.info('find existing %s, replacing by %s' % (lfile, rfile))
@@ -276,7 +280,10 @@ def download(self,
276280
pass
277281
elif (os.path.exists(rfile)) and (not os.path.exists(lfile)) :
278282
# trivial case, download happily
279-
shutil.move(rfile, lfile)
283+
if not os.path.islink(rfile):
284+
shutil.move(rfile, lfile)
285+
else:
286+
shutil.copyfile(rfile, lfile)
280287
elif (os.path.exists(rfile)) and (os.path.exists(lfile)) :
281288
dlog.info(f"both exist rfile:{rfile}; lfile:{lfile}")
282289
# both exists, replace!

tests/test_local_context.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,33 @@ def test_download_replace_old_files(self):
273273
md5_new = get_file_md5(target_file)
274274
self.assertNotEqual(md5_old, md5_new)
275275

276+
def test_download_symlink(self):
277+
task1 = MagicMock(
278+
task_work_path='bct-1/',
279+
backward_files=['input.lammps.symlink']
280+
)
281+
submission = MagicMock(work_base='0_md/',
282+
belonging_tasks=[task1],
283+
backward_common_files=['graph.pb.symlink'],
284+
submission_hash='0_md/')
285+
os.symlink(os.path.abspath(os.path.join(self.tmp_remote_root, "0_md", "bct-1", "input.lammps")), os.path.join(self.tmp_remote_root, "0_md", "bct-1", "input.lammps.symlink"))
286+
os.symlink(os.path.abspath(os.path.join(self.tmp_remote_root, "0_md", "graph.pb")), os.path.join(self.tmp_remote_root, "0_md", "graph.pb.symlink"))
287+
288+
self.local_context.bind_submission(submission)
289+
self.local_context.download(
290+
submission)
291+
self.local_context.clean()
292+
task_file = os.path.join(
293+
self.tmp_local_root,
294+
'0_md',
295+
'bct-1',
296+
'input.lammps.symlink',
297+
)
298+
common_file = os.path.join(
299+
self.tmp_local_root,
300+
'0_md',
301+
'graph.pb.symlink',
302+
)
303+
self.assertTrue(os.path.isfile(task_file))
304+
self.assertTrue(os.path.isfile(common_file))
305+

0 commit comments

Comments
 (0)