Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't try to overwrite an existing file if the contents are identical #4604

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"""
import datetime
import difflib
import filecmp
import glob
import hashlib
import inspect
Expand Down Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least we should also check whether file permissions are the same as well before skipping the actual copy operation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the permissions may have been changed later on by postinstallcmds or hooks or the normal EB change-permission step and thus permission differences should not be considered if the content is actually the same.

_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)
Expand Down
7 changes: 7 additions & 0 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading