~ubuntu-branches/ubuntu/lucid/bzr/lucid-proposed

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2006-03-20 08:31:00 UTC
  • mfrom: (1.1.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060320083100-ovdi2ssuw0epcx8s
Tags: 0.8~200603200831-0ubuntu1
* Snapshot uploaded to Dapper at Martin Pool's request.

* Disable testsuite for upload.  Fakeroot and the testsuite don't
  play along.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
"""Locking wrappers.
 
18
"""Locking using OS file locks or file existence.
19
19
 
20
 
This only does local locking using OS locks for now.
 
20
Note: This method of locking is generally deprecated in favour of LockDir, but
 
21
is used to lock local WorkingTrees, and by some old formats.  It's accessed
 
22
through Transport.lock_read(), etc.
21
23
 
22
24
This module causes two methods, lock() and unlock() to be defined in
23
25
any way that works on the current platform.
24
26
 
25
27
It is not specified whether these locks are reentrant (i.e. can be
26
28
taken repeatedly by a single process) or whether they exclude
27
 
different threads in a single process.  
28
 
 
29
 
Eventually we may need to use some kind of lock representation that
30
 
will work on a dumb filesystem without actual locking primitives.
 
29
different threads in a single process.  That reentrancy is provided by 
 
30
LockableFiles.
31
31
 
32
32
This defines two classes: ReadLock and WriteLock, which can be
33
33
implemented in different ways on different platforms.  Both have an
34
34
unlock() method.
35
35
"""
36
36
 
37
 
 
 
37
import errno
 
38
import os
38
39
import sys
39
 
import os
40
40
 
41
41
from bzrlib.trace import mutter, note, warning
42
42
from bzrlib.errors import LockError
43
43
 
44
44
class _base_Lock(object):
45
45
    def _open(self, filename, filemode):
46
 
        import errno
47
46
        try:
48
47
            self.f = open(filename, filemode)
49
48
            return self.f
54
53
            # maybe this is an old branch (before may 2005)
55
54
            mutter("trying to create missing branch lock %r", filename)
56
55
            
57
 
            self.f = open(filename, 'wb')
 
56
            self.f = open(filename, 'wb+')
58
57
            return self.f
59
58
 
60
 
 
61
59
    def __del__(self):
62
60
        if self.f:
63
61
            from warnings import warn
64
62
            warn("lock on %r not released" % self.f)
65
63
            self.unlock()
66
64
            
67
 
 
68
65
    def unlock(self):
69
66
        raise NotImplementedError()
70
67
 
88
85
            self.f.close()
89
86
            del self.f 
90
87
 
91
 
 
92
88
    class _fcntl_WriteLock(_fcntl_FileLock):
93
89
        def __init__(self, filename):
 
90
            # standard IO errors get exposed directly.
 
91
            self._open(filename, 'wb')
94
92
            try:
95
 
                fcntl.lockf(self._open(filename, 'wb'), fcntl.LOCK_EX)
96
 
            except Exception, e:
 
93
                fcntl.lockf(self.f, fcntl.LOCK_EX)
 
94
            except IOError, e:
 
95
                # we should be more precise about whats a locking
 
96
                # error and whats a random-other error
97
97
                raise LockError(e)
98
98
 
99
 
 
100
99
    class _fcntl_ReadLock(_fcntl_FileLock):
 
100
 
101
101
        def __init__(self, filename):
 
102
            # standard IO errors get exposed directly.
 
103
            self._open(filename, 'rb')
102
104
            try:
103
 
                fcntl.lockf(self._open(filename, 'rb'), fcntl.LOCK_SH)
104
 
            except Exception, e:
 
105
                fcntl.lockf(self.f, fcntl.LOCK_SH)
 
106
            except IOError, e:
 
107
                # we should be more precise about whats a locking
 
108
                # error and whats a random-other error
105
109
                raise LockError(e)
106
110
 
107
111
    WriteLock = _fcntl_WriteLock
108
112
    ReadLock = _fcntl_ReadLock
109
113
 
 
114
 
110
115
except ImportError:
111
116
    try:
112
117
        import win32con, win32file, pywintypes
245
250
        except ImportError:
246
251
            raise NotImplementedError("please write a locking method "
247
252
                                      "for platform %r" % sys.platform)
248
 
 
249
 
 
250
 
 
251
 
 
252
 
 
253
 
 
254