~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-09 19:57:36 UTC
  • mto: This revision was merged to the branch mainline in revision 3564.
  • Revision ID: john@arbash-meinel.com-20080709195736-s9cg26gnym3lf2d0
cleanup a few imports to be lazily loaded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from cStringIO import StringIO
 
18
 
 
19
from bzrlib.lazy_import import lazy_import
 
20
lazy_import(globals(), """
18
21
import codecs
19
 
#import traceback
20
 
from warnings import warn
21
 
 
22
 
import bzrlib
23
 
from bzrlib.decorators import (needs_read_lock,
24
 
        needs_write_lock)
25
 
import bzrlib.errors as errors
26
 
from bzrlib.errors import BzrError
27
 
from bzrlib.osutils import file_iterator, safe_unicode
 
22
import warnings
 
23
 
 
24
from bzrlib import (
 
25
    errors,
 
26
    osutils,
 
27
    transactions,
 
28
    urlutils,
 
29
    )
 
30
""")
 
31
 
 
32
from bzrlib.decorators import (
 
33
    needs_read_lock,
 
34
    needs_write_lock,
 
35
    )
28
36
from bzrlib.symbol_versioning import (
29
37
    deprecated_in,
30
38
    deprecated_method,
31
39
    )
32
 
from bzrlib.trace import mutter, note
33
 
import bzrlib.transactions as transactions
34
 
import bzrlib.urlutils as urlutils
35
40
 
36
41
 
37
42
# XXX: The tracking here of lock counts and whether the lock is held is
108
113
        if self.is_locked():
109
114
            # do not automatically unlock; there should have been a
110
115
            # try/finally to unlock this.
111
 
            warn("%r was gc'd while locked" % self)
 
116
            warnings.warn("%r was gc'd while locked" % self)
112
117
 
113
118
    def break_lock(self):
114
119
        """Break the lock of this lockable files group if it is held.
122
127
            file_or_path = '/'.join(file_or_path)
123
128
        if file_or_path == '':
124
129
            return u''
125
 
        return urlutils.escape(safe_unicode(file_or_path))
 
130
        return urlutils.escape(osutils.safe_unicode(file_or_path))
126
131
 
127
132
    def _find_modes(self):
128
133
        """Determine the appropriate modes for files and directories.
241
246
        some other way, and need to synchronise this object's state with that
242
247
        fact.
243
248
        """
244
 
        # mutter("lock write: %s (%s)", self, self._lock_count)
245
249
        # TODO: Upgrade locking to support using a Transport,
246
250
        # and potentially a remote locking protocol
247
251
        if self._lock_mode:
252
256
            return self._token_from_lock
253
257
        else:
254
258
            token_from_lock = self._lock.lock_write(token=token)
255
 
            #note('write locking %s', self)
256
259
            #traceback.print_stack()
257
260
            self._lock_mode = 'w'
258
261
            self._lock_count = 1
261
264
            return token_from_lock
262
265
 
263
266
    def lock_read(self):
264
 
        # mutter("lock read: %s (%s)", self, self._lock_count)
265
267
        if self._lock_mode:
266
268
            if self._lock_mode not in ('r', 'w'):
267
269
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
268
270
            self._lock_count += 1
269
271
        else:
270
272
            self._lock.lock_read()
271
 
            #note('read locking %s', self)
272
273
            #traceback.print_stack()
273
274
            self._lock_mode = 'r'
274
275
            self._lock_count = 1
277
278
            self.get_transaction().set_cache_size(5000)
278
279
                        
279
280
    def unlock(self):
280
 
        # mutter("unlock: %s (%s)", self, self._lock_count)
281
281
        if not self._lock_mode:
282
282
            raise errors.LockNotHeld(self)
283
283
        if self._lock_count > 1:
284
284
            self._lock_count -= 1
285
285
        else:
286
 
            #note('unlocking %s', self)
287
286
            #traceback.print_stack()
288
287
            self._finish_transaction()
289
288
            try: