Fix copy_file_if_changed src permissions copied too (#3161)

This commit is contained in:
Otto Winter 2022-02-07 21:26:16 +01:00 committed by GitHub
parent ab47e201c7
commit 253161d3d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import codecs
from contextlib import suppress
import logging
import os
@ -233,8 +234,20 @@ def copy_file_if_changed(src: os.PathLike, dst: os.PathLike) -> None:
return
mkdir_p(os.path.dirname(dst))
try:
shutil.copy(src, dst)
shutil.copyfile(src, dst)
except OSError as err:
if isinstance(err, PermissionError):
# Older esphome versions copied over the src file permissions too.
# So when the dst file had 444 permissions, the dst file would have those
# too and subsequent writes would fail
# -> delete file (it would be overwritten anyway), and try again
# if that fails, use normal error handler
with suppress(OSError):
os.unlink(dst)
shutil.copyfile(src, dst)
return
from esphome.core import EsphomeError
raise EsphomeError(f"Error copying file {src} to {dst}: {err}") from err