Skip to content

Commit

Permalink
util.write_atomic(): use UUID temp filename
Browse files Browse the repository at this point in the history
Network filesystems can be wonky with O_EXCL
  • Loading branch information
mlin committed Jun 5, 2021
1 parent 26758b9 commit 1adde09
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions WDL/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fcntl
import shutil
import hashlib
import uuid
from time import sleep
from datetime import datetime
from contextlib import contextmanager, AbstractContextManager
Expand Down Expand Up @@ -161,13 +162,12 @@ def topsort(adj: AdjM[T]) -> List[T]:


@export
def write_atomic(contents: str, filename: str, end: str = "\n", attempt: int = 0) -> None:
tn = filename + ".tmp" + (str(int(time.time() * 1e6)) if attempt > 0 else "")
try:
with open(tn, "x") as outfile:
print(contents, file=outfile, end=end)
except FileExistsError:
write_atomic(contents, filename, end=end, attempt=attempt + 1)
def write_atomic(contents: str, filename: str, end: str = "\n") -> None:
# 04-JUN-2021 changed to use UUID filename instead of relying on open(tn, "x") in case network
# filesystem is wonky with O_EXCL.
tn = filename + ".tmp." + str(uuid.uuid1())
with open(tn, "w") as outfile:
print(contents, file=outfile, end=end)
os.rename(tn, filename)


Expand Down

0 comments on commit 1adde09

Please sign in to comment.