##// END OF EJS Templates
Regenerate hg.1.gendoc.txt if commands.py changed.
Regenerate hg.1.gendoc.txt if commands.py changed.

File last commit:

r1787:e431344e default
r1819:d657bfdc default
Show More
lock.py
62 lines | 1.5 KiB | text/x-python | PythonLexer
mpm@selenic.com
Simply repository locking...
r161 # lock.py - simple locking scheme for mercurial
#
# Copyright 2005 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
Benoit Boissinot
Raise a different exception when the lock is not available...
r1753 import errno, os, time
mpm@selenic.com
[PATCH] Enables lock work under the other 'OS'...
r422 import util
mpm@selenic.com
Simply repository locking...
r161
Benoit Boissinot
Raise a different exception when the lock is not available...
r1753 class LockException(Exception):
pass
class LockHeld(LockException):
pass
class LockUnavailable(LockException):
mpm@selenic.com
Simply repository locking...
r161 pass
Eric Hopper
Convert all classes to new-style classes by deriving them from object.
r1559 class lock(object):
Benoit Boissinot
add a timeout when a lock is held (default 1024 sec)...
r1787 def __init__(self, file, timeout=-1, releasefn=None):
mpm@selenic.com
Simply repository locking...
r161 self.f = file
self.held = 0
Benoit Boissinot
add a timeout when a lock is held (default 1024 sec)...
r1787 self.timeout = timeout
Benoit Boissinot
add a releasefn keyword to lock.lock...
r1530 self.releasefn = releasefn
mpm@selenic.com
Simply repository locking...
r161 self.lock()
def __del__(self):
self.release()
def lock(self):
Benoit Boissinot
add a timeout when a lock is held (default 1024 sec)...
r1787 timeout = self.timeout
mpm@selenic.com
Simply repository locking...
r161 while 1:
try:
self.trylock()
return 1
except LockHeld, inst:
Benoit Boissinot
add a timeout when a lock is held (default 1024 sec)...
r1787 if timeout != 0:
mpm@selenic.com
Simply repository locking...
r161 time.sleep(1)
Benoit Boissinot
add a timeout when a lock is held (default 1024 sec)...
r1787 if timeout > 0:
timeout -= 1
mpm@selenic.com
Simply repository locking...
r161 continue
raise inst
mpm@selenic.com
Whitespace cleanups...
r515
mpm@selenic.com
Simply repository locking...
r161 def trylock(self):
pid = os.getpid()
try:
mpm@selenic.com
[PATCH] Enables lock work under the other 'OS'...
r422 util.makelock(str(pid), self.f)
mpm@selenic.com
Simply repository locking...
r161 self.held = 1
Benoit Boissinot
Raise a different exception when the lock is not available...
r1753 except (OSError, IOError), why:
if why.errno == errno.EEXIST:
raise LockHeld(util.readlock(self.f))
else:
raise LockUnavailable(why)
mpm@selenic.com
Simply repository locking...
r161
def release(self):
if self.held:
self.held = 0
Benoit Boissinot
add a releasefn keyword to lock.lock...
r1530 if self.releasefn:
self.releasefn()
mpm@selenic.com
Fix troubles with clone and exception handling...
r503 try:
os.unlink(self.f)
except: pass
mpm@selenic.com
Simply repository locking...
r161