projects
/
invirt/packages/invirt-base.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
use yaml.CSafeDumper and yaml.CSafeLoader everywhere
[invirt/packages/invirt-base.git]
/
files
/
usr
/
share
/
python-support
/
sipb-xen-base
/
invirt
/
common.py
diff --git
a/files/usr/share/python-support/sipb-xen-base/invirt/common.py
b/files/usr/share/python-support/sipb-xen-base/invirt/common.py
index
ca90714
..
d986196
100644
(file)
--- a/
files/usr/share/python-support/sipb-xen-base/invirt/common.py
+++ b/
files/usr/share/python-support/sipb-xen-base/invirt/common.py
@@
-1,6
+1,5
@@
import unittest
import unittest
-from fcntl import flock, LOCK_EX, LOCK_UN
-from os import remove
+from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
class struct(object):
'A simple namespace object.'
class struct(object):
'A simple namespace object.'
@@
-26,13
+25,21
@@
def dicts2struct(x):
#
def with_closing(rsrc):
#
def with_closing(rsrc):
- "Utility to emulate Python 2.5's `with closing(rsrc)` context manager."
+ """
+ Utility to emulate Python 2.5's `with closing(rsrc)` context manager.
+
+ E.g.,
+ @with_closing(file('/tmp/foo'))
+ def contents(f):
+ return f.read()
+ # now 'contents' is the contents of /tmp/foo
+ """
def wrapper(func):
try: return func(rsrc)
finally: rsrc.close()
return wrapper
def wrapper(func):
try: return func(rsrc)
finally: rsrc.close()
return wrapper
-def with_lock_file(path):
+def with_lock_file(path, exclusive = True):
"""
Context manager for lock files. Example:
"""
Context manager for lock files. Example:
@@
-40,16
+47,17
@@
def with_lock_file(path):
def input():
print 'locked'
return raw_input()
def input():
print 'locked'
return raw_input()
- # decorator is executed immediately
- print input # prints the input text
+ # prints 'locked'
+ print input # prints what raw_input() returned
"""
def wrapper(func):
@with_closing(file(path, 'w'))
def g(f):
"""
def wrapper(func):
@with_closing(file(path, 'w'))
def g(f):
- flock(f, LOCK_EX)
+ if exclusive: locktype = LOCK_EX
+ else: locktype = LOCK_SH
+ flock(f, locktype)
try: return func()
finally: flock(f, LOCK_UN)
try: return func()
finally: flock(f, LOCK_UN)
- remove(path)
return g
return wrapper
return g
return wrapper