- Timestamp:
- Oct 24, 2008, 3:35:21 AM (16 years ago)
- Location:
- trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/common.py
r795 r1197 1 from __future__ import with_statement 2 1 3 import unittest 2 4 from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN 5 import contextlib as clib 3 6 4 7 class struct(object): … … 21 24 return x 22 25 23 # 24 # Hacks to work around lack of Python 2.5's `with` statement. 25 # 26 27 def with_closing(rsrc): 28 """ 29 Utility to emulate Python 2.5's `with closing(rsrc)` context manager. 30 31 E.g., 32 @with_closing(file('/tmp/foo')) 33 def contents(f): 34 return f.read() 35 # now 'contents' is the contents of /tmp/foo 36 """ 37 def wrapper(func): 38 try: return func(rsrc) 39 finally: rsrc.close() 40 return wrapper 41 42 def with_lock_file(path, exclusive = True): 43 """ 44 Context manager for lock files. Example: 45 46 @with_lock_file('/tmp/mylock') 47 def input(): 48 print 'locked' 49 return raw_input() 50 # prints 'locked' 51 print input # prints what raw_input() returned 52 """ 53 def wrapper(func): 54 @with_closing(file(path, 'w')) 55 def g(f): 56 if exclusive: locktype = LOCK_EX 57 else: locktype = LOCK_SH 58 flock(f, locktype) 59 try: return func() 60 finally: flock(f, LOCK_UN) 61 return g 62 return wrapper 26 @clib.contextmanager 27 def lock_file(path, exclusive = True): 28 with clib.closing(file(path, 'w')) as f: 29 if exclusive: 30 locktype = LOCK_EX 31 else: 32 locktype = LOCK_SH 33 flock(f, locktype) 34 try: 35 yield 36 finally: 37 flock(f, LOCK_UN) 63 38 64 39 # -
trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py
r816 r1197 1 from __future__ import with_statement 2 1 3 import json 2 4 from invirt.common import * 3 5 from os import rename 4 6 from os.path import getmtime 7 from contextlib import closing 5 8 6 9 default_src_path = '/etc/invirt/master.yaml' 7 10 default_cache_path = '/var/lib/invirt/cache.json' 8 lock_ file= '/var/lib/invirt/cache.lock'11 lock_path = '/var/lib/invirt/cache.lock' 9 12 10 13 def load(src_path = default_src_path, … … 54 57 # lock with other concurrent reads). This isolation is accomplished 55 58 # using an atomic filesystem rename in the refreshing stage. 56 try: ns.cfg = with_closing(file(cache_path)) ( 57 lambda f: json.read(f.read())) 59 try: 60 with closing(file(cache_path)) as f: 61 ns.cfg = json.read(f.read()) 58 62 except: do_refresh = True 59 63 … … 69 73 except: loader = yaml.SafeLoader 70 74 try: 71 @with_lock_file(lock_file)72 def refresh_cache():73 ns.cfg = with_closing(file(src_path)) (74 lambda f: yaml.load(f, loader))75 try: with_closing(file(cache_path + '.tmp', 'w')) (76 lambda f: f.write(json.write(ns.cfg)))75 with lock_file(lock_path): 76 with closing(file(src_path)) as f: 77 ns.cfg = yaml.load(f, loader) 78 try: 79 with closing(file(cache_path + '.tmp', 'w')) as f: 80 f.write(json.write(ns.cfg)) 77 81 except: pass # silent failure 78 82 else: rename(cache_path + '.tmp', cache_path) 79 83 except IOError: 80 ns.cfg = with_closing(file(src_path)) (81 lambda f: yaml.load(f, loader))84 with closing(file(src_path)) as f: 85 ns.cfg = yaml.load(f, loader) 82 86 return ns.cfg 83 87
Note: See TracChangeset
for help on using the changeset viewer.