Back to index

enigmail  1.4.3
Classes | Functions
utils Namespace Reference

Classes

class  LockFile
class  pushback_iter

Functions

def lockFile

Function Documentation

def utils.lockFile (   lockfile,
  max_wait = 600 
)
Create and hold a lockfile of the given name, with the given timeout.

To release the lock, delete the returned object.

Definition at line 70 of file utils.py.

00070 
00071 def lockFile(lockfile, max_wait = 600):
00072   '''Create and hold a lockfile of the given name, with the given timeout.
00073 
00074   To release the lock, delete the returned object.
00075   '''
00076   while True:
00077     try:
00078       fd = os.open(lockfile, os.O_EXCL | os.O_RDWR | os.O_CREAT)
00079       # we created the lockfile, so we're the owner
00080       break
00081     except OSError, e:
00082       if e.errno == errno.EEXIST or \
00083          (sys.platform == "win32" and e.errno == errno.EACCES):
00084         pass
00085       else:
00086         # should not occur
00087         raise
00088   
00089     try:
00090       # the lock file exists, try to stat it to get its age
00091       # and read its contents to report the owner PID
00092       f = open(lockfile, "r")
00093       s = os.stat(lockfile)
00094     except EnvironmentError, e:
00095       if e.errno == errno.ENOENT or e.errno == errno.EACCES:
00096         # we didn't create the lockfile, so it did exist, but it's
00097         # gone now. Just try again
00098         continue
00099       sys.exit("%s exists but stat() failed: %s" %
00100                (lockfile, e.strerror))
00101   
00102     # we didn't create the lockfile and it's still there, check
00103     # its age
00104     now = int(time.time())
00105     if now - s[stat.ST_MTIME] > max_wait:
00106       pid = f.readline().rstrip()
00107       sys.exit("%s has been locked for more than " \
00108                "%d seconds (PID %s)" % (lockfile, max_wait,
00109                                         pid))
00110   
00111     # it's not been locked too long, wait a while and retry
00112     f.close()
00113     time.sleep(1)
00114   
00115   # if we get here. we have the lockfile. Convert the os.open file
00116   # descriptor into a Python file object and record our PID in it
00117   
00118   f = os.fdopen(fd, "w")
00119   f.write("%d\n" % os.getpid())
00120   f.close()
00121   return LockFile(lockfile)

Here is the caller graph for this function: