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 import codecs
from contextlib import suppress
import logging import logging
import os import os
@ -233,8 +234,20 @@ def copy_file_if_changed(src: os.PathLike, dst: os.PathLike) -> None:
return return
mkdir_p(os.path.dirname(dst)) mkdir_p(os.path.dirname(dst))
try: try:
shutil.copy(src, dst) shutil.copyfile(src, dst)
except OSError as err: 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 from esphome.core import EsphomeError
raise EsphomeError(f"Error copying file {src} to {dst}: {err}") from err raise EsphomeError(f"Error copying file {src} to {dst}: {err}") from err