fix(rotation): lab7 replace read-only files on WSL mounts
This commit is contained in:
@@ -3,6 +3,7 @@ import fcntl
|
|||||||
import getpass
|
import getpass
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import stat
|
||||||
import ssl
|
import ssl
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -22,14 +23,30 @@ def compose(*args, **kwargs):
|
|||||||
|
|
||||||
def write_file(path, data, mode):
|
def write_file(path, data, mode):
|
||||||
with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as tmp:
|
with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as tmp:
|
||||||
|
staged = Path(tmp.name)
|
||||||
|
try:
|
||||||
|
with staged.open("wb") as stream:
|
||||||
|
stream.write(data)
|
||||||
|
stream.flush()
|
||||||
|
os.fchmod(stream.fileno(), mode)
|
||||||
|
os.fsync(stream.fileno())
|
||||||
try:
|
try:
|
||||||
tmp.write(data)
|
os.replace(staged, path)
|
||||||
tmp.flush()
|
except PermissionError:
|
||||||
os.fchmod(tmp.fileno(), mode)
|
previous = stat.S_IMODE(path.stat().st_mode)
|
||||||
os.fsync(tmp.fileno())
|
if previous & stat.S_IWUSR:
|
||||||
os.replace(tmp.name, path)
|
raise
|
||||||
finally:
|
path.chmod(previous | stat.S_IWUSR)
|
||||||
Path(tmp.name).unlink(missing_ok=True)
|
try:
|
||||||
|
os.replace(staged, path)
|
||||||
|
except OSError:
|
||||||
|
path.chmod(previous)
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
if staged.exists():
|
||||||
|
staged.chmod(0o600)
|
||||||
|
staged.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Operator:
|
class Operator:
|
||||||
|
|||||||
Reference in New Issue
Block a user