X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tools.py;fp=tools.py;h=f4496e13a4eff31802957c4e0acb6c0533e41328;hb=e4dadb3e4da34a59d17d79c865f55e34a575915f;hp=f41f981ce971bfed797ac6649e824a951d745842;hpb=2ce5a3c741a30b2ede4b6118446530450429af2f;p=nodemanager.git diff --git a/tools.py b/tools.py index f41f981..f4496e1 100644 --- a/tools.py +++ b/tools.py @@ -6,6 +6,7 @@ import os import pwd import tempfile import threading +import fcntl import logger @@ -88,3 +89,17 @@ def write_temp_file(do_write, mode=None, uidgid=None): try: do_write(f) finally: f.close() return temporary_filename + + +class NMLock: + def __init__(self, file): + self.fd = os.open(file, os.O_RDWR|os.O_CREAT, 0600) + flags = fcntl.fcntl(self.fd, fcntl.F_GETFD) + flags |= fcntl.FD_CLOEXEC + fcntl.fcntl(self.fd, fcntl.F_SETFD, flags) + def __del__(self): + os.close(self.fd) + def acquire(self): + fcntl.lockf(self.fd, fcntl.LOCK_EX) + def release(self): + fcntl.lockf(self.fd, fcntl.LOCK_UN)