[Kimchi-devel] [kimchi-devel][PATCH 3/5] Add utils to support file lock
lvroyce at linux.vnet.ibm.com
lvroyce at linux.vnet.ibm.com
Wed Jan 7 09:01:07 UTC 2015
From: Royce Lv <lvroyce at 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 at 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 ""
+
+
+ at contextmanager
+def flock(fp, pos, len):
+ fp.lock('w|', len, pos)
+ yield
+ fp.lock('u', len, pos)
+
+
+ at contextmanager
+def f_open(path, mod):
+ file = posixfile.open(path, mod)
+ yield file
+ file.close()
--
1.9.3
More information about the Kimchi-devel
mailing list