diff --git a/lib/entropy/locks.py b/lib/entropy/locks.py index f5998dc64..e78b8a60a 100644 --- a/lib/entropy/locks.py +++ b/lib/entropy/locks.py @@ -9,6 +9,7 @@ B{Entropy resources lock functions module}. """ +import contextlib import errno import fcntl import threading @@ -292,6 +293,17 @@ class ResourceLock(object): self.acquire_exclusive() return True + @contextlib.contextmanager + def exclusive(self): + """ + Acquire an exclusive file lock for this repository (context manager). + """ + self.wait_exclusive() + try: + yield + finally: + self.release() + def acquire_exclusive(self): """ Acquire the resource in exclusive blocking mode. @@ -313,6 +325,17 @@ class ResourceLock(object): """ return self._wait_resource(False) + @contextlib.contextmanager + def shared(self): + """ + Acquire a shared file lock for this repository (context manager). + """ + self.wait_shared() + try: + yield + finally: + self.release() + def acquire_shared(self): """ Acquire the resource in shared blocking mode.