~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Martin Pool
  • Date: 2006-04-12 04:45:32 UTC
  • mfrom: (1608.2.13 bzr.mbp.escape-stores)
  • mto: This revision was merged to the branch mainline in revision 1657.
  • Revision ID: mbp@sourcefrog.net-20060412044532-fc8c5c9408aae88b
[merge][wip] Storage escaping

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from cStringIO import StringIO
18
18
import codecs
 
19
#import traceback
19
20
 
20
21
import bzrlib
21
22
from bzrlib.decorators import *
23
24
from bzrlib.errors import LockError, ReadOnlyError
24
25
from bzrlib.osutils import file_iterator, safe_unicode
25
26
from bzrlib.symbol_versioning import *
26
 
from bzrlib.trace import mutter
 
27
from bzrlib.symbol_versioning import deprecated_method, zero_eight
 
28
from bzrlib.trace import mutter, note
27
29
import bzrlib.transactions as transactions
28
30
 
29
31
# XXX: The tracking here of lock counts and whether the lock is held is
80
82
        self._lock_mode = None
81
83
        self._lock_count = 0
82
84
        esc_name = self._escape(lock_name)
83
 
        self._lock = lock_class(transport, esc_name, 
 
85
        self._lock = lock_class(transport, esc_name,
84
86
                                file_modebits=self._file_mode,
85
87
                                dir_modebits=self._dir_mode)
86
88
 
95
97
    def __repr__(self):
96
98
        return '%s(%r)' % (self.__class__.__name__,
97
99
                           self._transport)
 
100
    def __str__(self):
 
101
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
 
102
 
 
103
    def __del__(self):
 
104
        if self.is_locked():
 
105
            # XXX: This should show something every time, and be suitable for
 
106
            # headless operation and embedding
 
107
            from warnings import warn
 
108
            warn("file group %r was not explicitly unlocked" % self)
 
109
            self._lock.unlock()
98
110
 
99
111
    def _escape(self, file_or_path):
100
112
        if not isinstance(file_or_path, basestring):
201
213
            self._lock_count += 1
202
214
        else:
203
215
            self._lock.lock_write()
 
216
            #note('write locking %s', self)
 
217
            #traceback.print_stack()
204
218
            self._lock_mode = 'w'
205
219
            self._lock_count = 1
206
220
            self._set_transaction(transactions.WriteTransaction())
213
227
            self._lock_count += 1
214
228
        else:
215
229
            self._lock.lock_read()
 
230
            #note('read locking %s', self)
 
231
            #traceback.print_stack()
216
232
            self._lock_mode = 'r'
217
233
            self._lock_count = 1
218
234
            self._set_transaction(transactions.ReadOnlyTransaction())
226
242
        if self._lock_count > 1:
227
243
            self._lock_count -= 1
228
244
        else:
 
245
            #note('unlocking %s', self)
 
246
            #traceback.print_stack()
229
247
            self._finish_transaction()
230
248
            self._lock.unlock()
231
249
            self._lock_mode = self._lock_count = None