~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: abentley
  • Date: 2006-04-20 23:47:53 UTC
  • mfrom: (1681 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1683.
  • Revision ID: abentley@lappy-20060420234753-6a6874b76f09f86d
Merge bzr.dev

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.trace import mutter, note
27
28
import bzrlib.transactions as transactions
28
29
 
29
30
# XXX: The tracking here of lock counts and whether the lock is held is
80
81
        self._lock_mode = None
81
82
        self._lock_count = 0
82
83
        esc_name = self._escape(lock_name)
83
 
        self._lock = lock_class(transport, esc_name, 
 
84
        self._lock = lock_class(transport, esc_name,
84
85
                                file_modebits=self._file_mode,
85
86
                                dir_modebits=self._dir_mode)
86
87
 
90
91
        This should normally be called only when the LockableFiles directory
91
92
        is first created on disk.
92
93
        """
93
 
        self._lock.create()
 
94
        self._lock.create(mode=self._dir_mode)
94
95
 
95
96
    def __repr__(self):
96
97
        return '%s(%r)' % (self.__class__.__name__,
97
98
                           self._transport)
 
99
    def __str__(self):
 
100
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
 
101
 
 
102
    def __del__(self):
 
103
        if self.is_locked():
 
104
            # XXX: This should show something every time, and be suitable for
 
105
            # headless operation and embedding
 
106
            from warnings import warn
 
107
            warn("file group %r was not explicitly unlocked" % self)
 
108
            self._lock.unlock()
98
109
 
99
110
    def _escape(self, file_or_path):
100
111
        if not isinstance(file_or_path, basestring):
201
212
            self._lock_count += 1
202
213
        else:
203
214
            self._lock.lock_write()
 
215
            #note('write locking %s', self)
 
216
            #traceback.print_stack()
204
217
            self._lock_mode = 'w'
205
218
            self._lock_count = 1
206
219
            self._set_transaction(transactions.WriteTransaction())
213
226
            self._lock_count += 1
214
227
        else:
215
228
            self._lock.lock_read()
 
229
            #note('read locking %s', self)
 
230
            #traceback.print_stack()
216
231
            self._lock_mode = 'r'
217
232
            self._lock_count = 1
218
233
            self._set_transaction(transactions.ReadOnlyTransaction())
226
241
        if self._lock_count > 1:
227
242
            self._lock_count -= 1
228
243
        else:
 
244
            #note('unlocking %s', self)
 
245
            #traceback.print_stack()
229
246
            self._finish_transaction()
230
247
            self._lock.unlock()
231
248
            self._lock_mode = self._lock_count = None
289
306
        self._lock.unlock()
290
307
        self._lock = None
291
308
 
292
 
    def create(self):
 
309
    def create(self, mode=None):
293
310
        """Create lock mechanism"""
294
311
        # for old-style locks, create the file now
295
312
        self._transport.put(self._escaped_name, StringIO(),