Ignore:
Timestamp:
Oct 24, 2008, 3:35:21 AM (16 years ago)
Author:
broder
Message:

Now that we're using Python 2.5, we can actually write with statements

Location:
trunk/packages/sipb-xen-base
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-base/debian/changelog

    r1009 r1197  
     1sipb-xen-base (8.23) unstable; urgency=low
     2
     3  * Now that we're using Python 2.5, we can actually write with statements
     4
     5 -- Evan Broder <broder@mit.edu>  Fri, 24 Oct 2008 03:32:00 -0400
     6
    17sipb-xen-base (8.22) unstable; urgency=low
    28
  • trunk/packages/sipb-xen-base/debian/control

    r945 r1197  
    44Maintainer: SIPB Xen Project <sipb-xen@mit.edu>
    55Build-Depends: cdbs (>= 0.4.23-1.1), debhelper (>= 4.1.0), python-support
    6 Standards-Version: 3.7.2
     6Standards-Version: 3.8.0
    77
    88Package: sipb-xen-base
  • trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/common.py

    r795 r1197  
     1from __future__ import with_statement
     2
    13import unittest
    24from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
     5import contextlib as clib
    36
    47class struct(object):
     
    2124        return x
    2225
    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
     27def 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)
    6338
    6439#
  • trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py

    r816 r1197  
     1from __future__ import with_statement
     2
    13import json
    24from invirt.common import *
    35from os import rename
    46from os.path import getmtime
     7from contextlib import closing
    58
    69default_src_path   = '/etc/invirt/master.yaml'
    710default_cache_path = '/var/lib/invirt/cache.json'
    8 lock_file          = '/var/lib/invirt/cache.lock'
     11lock_path          = '/var/lib/invirt/cache.lock'
    912
    1013def load(src_path = default_src_path,
     
    5457        # lock with other concurrent reads).  This isolation is accomplished
    5558        # 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())
    5862        except: do_refresh = True
    5963
     
    6973        except: loader = yaml.SafeLoader
    7074        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))
    7781                except: pass # silent failure
    7882                else: rename(cache_path + '.tmp', cache_path)
    7983        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)
    8286    return ns.cfg
    8387
Note: See TracChangeset for help on using the changeset viewer.