Reimplement duplicated run detection as a file lock

This commit is contained in:
DevilXD committed 2023-08-28 23:47:15 +02:00
1 parent bb87b2793e
commit e03ea755bb
5 files changed
+63 -45

No files matched your search

+24
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import io
import os
import sys
import json
@@ -77,6 +78,29 @@ def format_traceback(exc: BaseException, **kwargs: Any) -> str:
return ''.join(traceback.format_exception(type(exc), exc, **kwargs))
def lock_file(path: Path) -> tuple[bool, io.TextIOWrapper]:
file = path.open('w', encoding="utf8")
file.write('ツ')
file.flush()
if sys.platform == "win32":
import msvcrt
try:
# we need to lock at least one byte for this to work
msvcrt.locking(file.fileno(), msvcrt.LK_NBLCK, max(path.stat().st_size, 1))
except Exception:
return False, file
return True, file
if sys.platform == "linux":
import fcntl
try:
fcntl.lockf(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
except Exception:
return False, file
return True, file
# for unsupported systems, just always return True
return True, file
def json_minify(data: JsonType | list[JsonType]) -> str:
"""
Returns minified JSON for payload usage.