Package dimer :: Module filelock
[hide private]
[frames] | no frames]

Source Code for Module dimer.filelock

  1  # Copyright (c) 2009, Evan Fosmark 
  2  # All rights reserved. 
  3  #  
  4  # Redistribution and use in source and binary forms, with or without 
  5  # modification, are permitted provided that the following conditions are met:  
  6  #  
  7  # 1. Redistributions of source code must retain the above copyright notice, this 
  8  #    list of conditions and the following disclaimer.  
  9  # 2. Redistributions in binary form must reproduce the above copyright notice, 
 10  #    this list of conditions and the following disclaimer in the documentation 
 11  #    and/or other materials provided with the distribution.  
 12  #  
 13  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 14  # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 15  # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 16  # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
 17  # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 18  # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 19  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 20  # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 21  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 22  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 23  #  
 24  # The views and conclusions contained in the software and documentation are those 
 25  # of the authors and should not be interpreted as representing official policies,  
 26  # either expressed or implied, of the FreeBSD Project. 
 27    
 28  import os 
 29  import time 
 30  import errno 
 31    
32 -class FileLockException(Exception):
33 pass
34
35 -class FileLock(object):
36 """ A file locking mechanism that has context-manager support so 37 you can use it in a with statement. This should be relatively cross 38 compatible as it doesn't rely on msvcrt or fcntl for the locking. 39 """ 40
41 - def __init__(self, file_name, timeout=10, delay=.05):
42 """ Prepare the file locker. Specify the file to lock and optionally 43 the maximum timeout and the delay between each attempt to lock. 44 """ 45 self.is_locked = False 46 self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name) 47 self.file_name = file_name 48 self.timeout = timeout 49 self.delay = delay
50 51
52 - def acquire(self):
53 """ Acquire the lock, if possible. If the lock is in use, it check again 54 every `wait` seconds. It does this until it either gets the lock or 55 exceeds `timeout` number of seconds, in which case it throws 56 an exception. 57 """ 58 start_time = time.time() 59 while True: 60 try: 61 self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR) 62 break; 63 except OSError as e: 64 if e.errno != errno.EEXIST: 65 raise 66 if (time.time() - start_time) >= self.timeout: 67 raise FileLockException("Timeout occured.") 68 time.sleep(self.delay) 69 self.is_locked = True
70 71
72 - def release(self):
73 """ Get rid of the lock by deleting the lockfile. 74 When working in a `with` statement, this gets automatically 75 called at the end. 76 """ 77 if self.is_locked: 78 os.close(self.fd) 79 os.unlink(self.lockfile) 80 self.is_locked = False
81 82
83 - def __enter__(self):
84 """ Activated when used in the with statement. 85 Should automatically acquire a lock to be used in the with block. 86 """ 87 if not self.is_locked: 88 self.acquire() 89 return self
90 91
92 - def __exit__(self, type, value, traceback):
93 """ Activated at the end of the with statement. 94 It automatically releases the lock if it isn't locked. 95 """ 96 if self.is_locked: 97 self.release()
98 99
100 - def __del__(self):
101 """ Make sure that the FileLock instance doesn't leave a lockfile 102 lying around. 103 """ 104 self.release()
105