Use shutil.copy2 to preserve file stats in backup

To preserve file ownership and permissions in backup and
some other places, move from using shutil.copy to shutil.copy2.

Change-Id: I8f00834ab598267ccc54f18cea70d6db415ba69d
Closes-bug: 1572922
(cherry picked from commit 969c90144e)
This commit is contained in:
Oleg Gelbukh 2016-05-17 21:49:22 +00:00
parent ecc0550a34
commit 1eb1efec34
3 changed files with 3 additions and 3 deletions

View File

@ -21,7 +21,7 @@ from octane.util import archivate
def update_centos_bootstrap():
with archivate.update_cpio(magic_consts.BOOTSTRAP_INITRAMFS) as tmp_dir:
shutil.copy(
shutil.copy2(
"/root/.ssh/authorized_keys",
os.path.join(tmp_dir, "root/.ssh/authorized_keys"))

View File

@ -109,7 +109,7 @@ class AstuteArchivator(base.PathArchivator):
",".join(not_found_keys)))
old_path_name = "{0}.old".format(self.path)
new_path_name = "{0}.new".format(self.path)
shutil.copy(self.path, old_path_name)
shutil.copy2(self.path, old_path_name)
with open(new_path_name, "w") as new:
yaml.safe_dump(current_yaml, new, default_flow_style=False)
shutil.move(new_path_name, self.path)

View File

@ -430,7 +430,7 @@ def test_astute_restore(mocker, mock_open, keys_in_dump_file, restored):
mocker.patch("yaml.load", side_effect=[dump_dict, current_dict])
safe_dump = mocker.patch("yaml.safe_dump")
copy_mock = mocker.patch("shutil.copy")
copy_mock = mocker.patch("shutil.copy2")
move_mock = mocker.patch("shutil.move")
cls = astute.AstuteArchivator
archive = TestArchive([member], cls)