X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tools.py;h=f4496e13a4eff31802957c4e0acb6c0533e41328;hb=04a528370f6e237c08e26de5272e3ac418bb8057;hp=f41f981ce971bfed797ac6649e824a951d745842;hpb=8043da14ad0a8f76c20c8ff4e5ce2d8e1c2131df;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)