mercurial/lock.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Mon, 20 Feb 2006 01:09:40 +0100
changeset 1753 e6e70450edb9
parent 1559 59b3639df0a9
child 1787 e431344e604c
permissions -rw-r--r--
Raise a different exception when the lock is not available When the filesystem is read-only or if we have some other error, there is no need to wait. Raise a lock.LockUnavailable exception.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     1
# lock.py - simple locking scheme for mercurial
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     2
#
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     4
#
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
     7
1753
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
     8
import errno, os, time
422
10c43444a38e [PATCH] Enables lock work under the other 'OS'
mpm@selenic.com
parents: 161
diff changeset
     9
import util
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    10
1753
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    11
class LockException(Exception):
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    12
    pass
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    13
class LockHeld(LockException):
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    14
    pass
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    15
class LockUnavailable(LockException):
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    16
    pass
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    17
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1530
diff changeset
    18
class lock(object):
1530
abfab59fce79 add a releasefn keyword to lock.lock
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1062
diff changeset
    19
    def __init__(self, file, wait=1, releasefn=None):
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    20
        self.f = file
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    21
        self.held = 0
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    22
        self.wait = wait
1530
abfab59fce79 add a releasefn keyword to lock.lock
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1062
diff changeset
    23
        self.releasefn = releasefn
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    24
        self.lock()
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    25
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    26
    def __del__(self):
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    27
        self.release()
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    28
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    29
    def lock(self):
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    30
        while 1:
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    31
            try:
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    32
                self.trylock()
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    33
                return 1
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    34
            except LockHeld, inst:
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    35
                if self.wait:
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    36
                    time.sleep(1)
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    37
                    continue
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    38
                raise inst
515
03f27b1381f9 Whitespace cleanups
mpm@selenic.com
parents: 503
diff changeset
    39
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    40
    def trylock(self):
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    41
        pid = os.getpid()
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    42
        try:
422
10c43444a38e [PATCH] Enables lock work under the other 'OS'
mpm@selenic.com
parents: 161
diff changeset
    43
            util.makelock(str(pid), self.f)
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    44
            self.held = 1
1753
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    45
        except (OSError, IOError), why:
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    46
            if why.errno == errno.EEXIST:
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    47
                raise LockHeld(util.readlock(self.f))
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    48
            else:
e6e70450edb9 Raise a different exception when the lock is not available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1559
diff changeset
    49
                raise LockUnavailable(why)
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    50
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    51
    def release(self):
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    52
        if self.held:
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    53
            self.held = 0
1530
abfab59fce79 add a releasefn keyword to lock.lock
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1062
diff changeset
    54
            if self.releasefn:
abfab59fce79 add a releasefn keyword to lock.lock
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1062
diff changeset
    55
                self.releasefn()
503
c6a2e41c8c60 Fix troubles with clone and exception handling
mpm@selenic.com
parents: 429
diff changeset
    56
            try:
c6a2e41c8c60 Fix troubles with clone and exception handling
mpm@selenic.com
parents: 429
diff changeset
    57
                os.unlink(self.f)
c6a2e41c8c60 Fix troubles with clone and exception handling
mpm@selenic.com
parents: 429
diff changeset
    58
            except: pass
161
0b4c5cb953d9 Simply repository locking
mpm@selenic.com
parents:
diff changeset
    59