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