aurweb/aurweb/testing/filelock.py
Leonidas Spyropoulos 9c0f8f053e
chore: rename logging.py and redis.py to avoid circular imports
Signed-off-by: Leonidas Spyropoulos <artafinde@archlinux.org>
2022-10-22 18:51:38 +01:00

31 lines
844 B
Python

import hashlib
import os
from typing import Callable
from posix_ipc import O_CREAT, Semaphore
from aurweb import aur_logging
logger = aur_logging.get_logger(__name__)
def default_on_create(path):
logger.info(f"Filelock at {path} acquired.")
class FileLock:
def __init__(self, tmpdir, name: str):
self.root = tmpdir
self.path = str(self.root / name)
self._file = str(self.root / (f"{name}.1"))
def lock(self, on_create: Callable = default_on_create):
hash = hashlib.sha1(self.path.encode()).hexdigest()
with Semaphore(f"/{hash}-lock", flags=O_CREAT, initial_value=1):
retval = os.path.exists(self._file)
if not retval:
with open(self._file, "w") as f:
f.write("1")
on_create(self.path)
return retval