dashboard: Fix file writes on Windows (#6013)

This commit is contained in:
J. Nick Koston 2023-12-24 03:56:31 -10:00 committed by GitHub
parent 6583026e14
commit fe15d993f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -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:

View file

@ -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: