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
This commit is contained in:
Oleg Gelbukh 2016-05-17 21:49:22 +00:00
parent a40e4e4073
commit 969c90144e
5 changed files with 5 additions and 5 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

@ -445,7 +445,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)

View File

@ -26,7 +26,7 @@ def test_set_astute_password(mocker, mock_open, exc_on_apply):
mkstemp_mock = mocker.patch(
"tempfile.mkstemp",
return_value=(1, "/etc/fuel/.astute.yaml.bac"))
mock_copy = mocker.patch("shutil.copy")
mock_copy = mocker.patch("shutil.copy2")
mock_move = mocker.patch("shutil.move")
yaml_load = mocker.patch(
"yaml.load", return_value={"FUEL_ACCESS": {"password": "dump_pswd"}})

View File

@ -24,7 +24,7 @@ def set_astute_password(auth_context):
_, tmp_file_name = tempfile.mkstemp(
dir="/etc/fuel",
prefix=".astute.yaml.octane")
shutil.copy("/etc/fuel/astute.yaml", tmp_file_name)
shutil.copy2("/etc/fuel/astute.yaml", tmp_file_name)
try:
data = helpers.get_astute_dict()
data["FUEL_ACCESS"]["password"] = auth_context.password