mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
dashboard: Fix file writes on Windows (#6013)
This commit is contained in:
parent
6583026e14
commit
fe15d993f9
2 changed files with 10 additions and 2 deletions
|
@ -30,6 +30,7 @@ def write_file(
|
|||
"""
|
||||
|
||||
tmp_filename = ""
|
||||
missing_fchmod = False
|
||||
try:
|
||||
# Modern versions of Python tempfile create this file with mode 0o600
|
||||
with tempfile.NamedTemporaryFile(
|
||||
|
@ -38,8 +39,15 @@ def write_file(
|
|||
fdesc.write(utf8_data)
|
||||
tmp_filename = fdesc.name
|
||||
if not private:
|
||||
os.fchmod(fdesc.fileno(), 0o644)
|
||||
try:
|
||||
os.fchmod(fdesc.fileno(), 0o644)
|
||||
except AttributeError:
|
||||
# os.fchmod is not available on Windows
|
||||
missing_fchmod = True
|
||||
|
||||
os.replace(tmp_filename, filename)
|
||||
if missing_fchmod:
|
||||
os.chmod(filename, 0o644)
|
||||
finally:
|
||||
if os.path.exists(tmp_filename):
|
||||
try:
|
||||
|
|
|
@ -13,7 +13,7 @@ def test_write_utf8_file(tmp_path: Path) -> None:
|
|||
assert tmp_path.joinpath("foo.txt").read_text() == "foo"
|
||||
|
||||
with pytest.raises(OSError):
|
||||
write_utf8_file(Path("/not-writable"), "bar")
|
||||
write_utf8_file(Path("/dev/not-writable"), "bar")
|
||||
|
||||
|
||||
def test_write_file(tmp_path: Path) -> None:
|
||||
|
|
Loading…
Reference in a new issue