From: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
Wrap posix file lock with contextlib so that file sections
can be protected by lock.
Signed-off-by: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
---
src/kimchi/model/utils.py | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/src/kimchi/model/utils.py b/src/kimchi/model/utils.py
index 9896289..1188b19 100644
--- a/src/kimchi/model/utils.py
+++ b/src/kimchi/model/utils.py
@@ -18,8 +18,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import libvirt
+import posixfile
import socket
import urlparse
+from contextlib import contextmanager
from lxml import etree, objectify
from lxml.builder import E, ElementMaker
@@ -161,3 +163,17 @@ def get_metadata_node(dom, tag, metadata_support,
mode="current"):
if node is not None:
return etree.tostring(node)
return ""
+
+
+@contextmanager
+def flock(fp, pos, len):
+ fp.lock('w|', len, pos)
+ yield
+ fp.lock('u', len, pos)
+
+
+@contextmanager
+def f_open(path, mod):
+ file = posixfile.open(path, mod)
+ yield file
+ file.close()
--
1.9.3