~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Martin Pool
  • Date: 2006-03-03 07:35:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1593.
  • Revision ID: mbp@sourcefrog.net-20060303073547-e406d0326abae2fd
cleanup LockableFiles

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.errors import LockError, ReadOnlyError
24
24
from bzrlib.osutils import file_iterator, safe_unicode
25
25
from bzrlib.symbol_versioning import *
26
 
from bzrlib.symbol_versioning import deprecated_method, zero_eight
27
26
from bzrlib.trace import mutter
28
27
import bzrlib.transactions as transactions
29
28
 
54
53
    OSLocks for the local filesystem.
55
54
    """
56
55
 
57
 
    _lock_mode = None               # None, or 'r' or 'w'
 
56
    # _lock_mode: None, or 'r' or 'w'
58
57
 
59
 
    # If _lock_mode is true, a positive count of the number of times the
60
 
    # lock has been taken *by this process*.  Others may have compatible 
61
 
    # read locks.
62
 
    _lock_count = None
63
 
 
 
58
    # _lock_count: If _lock_mode is true, a positive count of the number of
 
59
    # times the lock has been taken *by this process*.   
 
60
    
64
61
    # If set to False (by a plugin, etc) BzrBranch will not set the
65
62
    # mode on created files or directories
66
63
    _set_file_mode = True
67
64
    _set_dir_mode = True
68
65
 
69
 
    def __init__(self, transport, lock_name, lock_strategy_class=None):
 
66
    def __init__(self, transport, lock_name, lock_class=None):
70
67
        """Create a LockableFiles group
71
68
 
72
69
        :param transport: Transport pointing to the directory holding the 
73
70
            control files and lock.
74
71
        :param lock_name: Name of the lock guarding these files.
75
 
        :param lock_strategy_class: Class of lock strategy to use.
 
72
        :param lock_class: Class of lock strategy to use: typically
 
73
            either LockDir or TransportLock.
76
74
        """
77
75
        object.__init__(self)
78
76
        self._transport = transport
79
77
        self.lock_name = lock_name
80
78
        self._transaction = None
81
79
        self._find_modes()
82
 
        # TODO: remove this and make the parameter mandatory
83
 
        if lock_strategy_class is None:
84
 
            lock_strategy_class = TransportLock
 
80
        self._lock_mode = None
 
81
        self._lock_count = 0
85
82
        esc_name = self._escape(lock_name)
86
 
        self._lock_strategy = lock_strategy_class(transport, esc_name)
 
83
        if lock_class is None:
 
84
            warn("LockableFiles: lock_class is now a mandatory parameter",
 
85
                 DeprecationWarning, stacklevel=2)
 
86
            lock_class = TransportLock
 
87
        self._lock = lock_class(transport, esc_name)
87
88
 
88
89
    def _escape(self, file_or_path):
89
90
        if not isinstance(file_or_path, basestring):
189
190
                raise ReadOnlyError(self)
190
191
            self._lock_count += 1
191
192
        else:
192
 
            self._lock_strategy.lock_write()
 
193
            self._lock.lock_write()
193
194
            self._lock_mode = 'w'
194
195
            self._lock_count = 1
195
196
            self._set_transaction(transactions.PassThroughTransaction())
201
202
                   "invalid lock mode %r" % self._lock_mode
202
203
            self._lock_count += 1
203
204
        else:
204
 
            self._lock_strategy.lock_read()
 
205
            self._lock.lock_read()
205
206
            self._lock_mode = 'r'
206
207
            self._lock_count = 1
207
208
            self._set_transaction(transactions.ReadOnlyTransaction())
216
217
            self._lock_count -= 1
217
218
        else:
218
219
            self._finish_transaction()
219
 
            self._lock_strategy.unlock()
 
220
            self._lock.unlock()
220
221
            self._lock_mode = self._lock_count = None
221
222
 
222
223
    def is_locked(self):