diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index aeb49304e1..e8ec28d9bb 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -42,6 +42,7 @@ """ import datetime import difflib +import filecmp import glob import hashlib import inspect @@ -2385,6 +2386,9 @@ def copy_file(path, target_path, force_in_dry_run=False): target_exists = os.path.exists(target_path) if target_exists and path_exists and os.path.samefile(path, target_path): _log.debug("Not copying %s to %s since files are identical", path, target_path) + # Don't copy them if they are already considered the same + elif target_exists and path_exists and os.path.isfile(target_path) and filecmp.cmp(path, target_path): + _log.debug("Not copying %s to %s since file contents are identical", path, target_path) # if target file exists and is owned by someone else than the current user, # try using shutil.copyfile to just copy the file contents # since shutil.copy2 will fail when trying to copy over file metadata (since chown requires file ownership) diff --git a/test/framework/filetools.py b/test/framework/filetools.py index 99b9f35398..fd28f36544 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -1873,6 +1873,13 @@ def test_copy_file(self): self.assertExists(target_path) self.assertTrue(ft.read_file(toy_ec) == ft.read_file(target_path)) + # Test that we don't copy the same file twice + # (use the metadata timestamp to verify) + ctime = os.path.getctime(target_path) + ft.copy_file(toy_ec, target_path) + new_ctime = os.path.getctime(target_path) + self.assertEqual(ctime, new_ctime) + # 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)