Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2290,10 +2290,13 @@ def copy_file(path, target_path, force_in_dry_run=False):
else:
mkdir(os.path.dirname(target_path), parents=True)
if os.path.islink(path):
if os.path.isdir(target_path):
target_path = os.path.join(target_path, os.path.basename(path))
_log.info("target_path changed to %s", target_path)
# special care for copying broken symlinks
link_target = os.readlink(path)
symlink(link_target, target_path)
_log.info("created symlink from %s to %s", path, target_path)
symlink(link_target, target_path, use_abspath_source=False)
_log.info("created symlink %s to %s", link_target, target_path)
else:
shutil.copy2(path, target_path)
_log.info("%s copied to %s", path, target_path)
Expand Down
25 changes: 18 additions & 7 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,14 +1602,25 @@ def test_apply_patch(self):
def test_copy_file(self):
"""Test copy_file function."""
testdir = os.path.dirname(os.path.abspath(__file__))
to_copy = os.path.join(testdir, 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb')
toy_ec = os.path.join(testdir, 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb')
target_path = os.path.join(self.test_prefix, 'toy.eb')
ft.copy_file(to_copy, target_path)
ft.copy_file(toy_ec, target_path)
self.assertTrue(os.path.exists(target_path))
self.assertTrue(ft.read_file(to_copy) == ft.read_file(target_path))
self.assertTrue(ft.read_file(toy_ec) == ft.read_file(target_path))

# Make sure it doesn't fail if path is a symlink and target_path is a dir
toy_link_fn = 'toy-link-0.0.eb'
toy_link = os.path.join(self.test_prefix, toy_link_fn)
ft.symlink(toy_ec, toy_link)
dir_target_path = os.path.join(self.test_prefix, 'subdir')
ft.mkdir(dir_target_path)
ft.copy_file(toy_link, dir_target_path)
self.assertTrue(os.path.islink(os.path.join(dir_target_path, toy_link_fn)))
self.assertEqual(os.readlink(os.path.join(dir_target_path, toy_link_fn)), os.readlink(toy_link))
os.remove(os.path.join(dir_target_path, toy_link))

# clean error when trying to copy a directory with copy_file
src, target = os.path.dirname(to_copy), os.path.join(self.test_prefix, 'toy')
src, target = os.path.dirname(toy_ec), os.path.join(self.test_prefix, 'toy')
# error message was changed in Python 3.9.7 to "FileNotFoundError: Directory does not exist"
error_pattern = "Failed to copy file.*(Is a directory|Directory does not exist)"
self.assertErrorRegex(EasyBuildError, error_pattern, ft.copy_file, src, target)
Expand Down Expand Up @@ -1646,7 +1657,7 @@ def test_copy_file(self):
os.remove(target_path)

self.mock_stdout(True)
ft.copy_file(to_copy, target_path)
ft.copy_file(toy_ec, target_path)
txt = self.get_stdout()
self.mock_stdout(False)

Expand All @@ -1655,12 +1666,12 @@ def test_copy_file(self):

# forced copy, even in dry run mode
self.mock_stdout(True)
ft.copy_file(to_copy, target_path, force_in_dry_run=True)
ft.copy_file(toy_ec, target_path, force_in_dry_run=True)
txt = self.get_stdout()
self.mock_stdout(False)

self.assertTrue(os.path.exists(target_path))
self.assertTrue(ft.read_file(to_copy) == ft.read_file(target_path))
self.assertTrue(ft.read_file(toy_ec) == ft.read_file(target_path))
self.assertEqual(txt, '')

# Test that a non-existing file raises an exception
Expand Down