/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lockdir.py

Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
import os
98
98
import time
99
 
from StringIO import StringIO
 
99
from cStringIO import StringIO
100
100
 
101
101
import bzrlib.config
102
102
from bzrlib.errors import (
111
111
        ResourceBusy,
112
112
        UnlockableTransport,
113
113
        )
114
 
from bzrlib.trace import mutter
 
114
from bzrlib.trace import mutter, note
115
115
from bzrlib.transport import Transport
116
 
from bzrlib.osutils import rand_chars
 
116
from bzrlib.osutils import rand_chars, format_delta
117
117
from bzrlib.rio import read_stanza, Stanza
118
118
 
 
119
 
119
120
# XXX: At the moment there is no consideration of thread safety on LockDir
120
121
# objects.  This should perhaps be updated - e.g. if two threads try to take a
121
122
# lock at the same time they should *both* get it.  But then that's unlikely
130
131
# TODO: Make sure to pass the right file and directory mode bits to all
131
132
# files/dirs created.
132
133
 
 
134
 
133
135
_DEFAULT_TIMEOUT_SECONDS = 300
134
 
_DEFAULT_POLL_SECONDS = 0.5
 
136
_DEFAULT_POLL_SECONDS = 1.0
 
137
 
135
138
 
136
139
class LockDir(object):
137
140
    """Write-lock guarding access to data."""
160
163
        self._dir_modebits = dir_modebits
161
164
        self.nonce = rand_chars(20)
162
165
 
 
166
        self._report_function = note
 
167
 
163
168
    def __repr__(self):
164
169
        return '%s(%s%s)' % (self.__class__.__name__,
165
170
                             self.transport.base,
243
248
        self._check_not_locked()
244
249
        holder_info = self.peek()
245
250
        if holder_info is not None:
246
 
            if bzrlib.ui.ui_factory.get_boolean(
247
 
                "Break lock %s held by %s@%s [process #%s]" % (
248
 
                    self.transport,
249
 
                    holder_info["user"],
250
 
                    holder_info["hostname"],
251
 
                    holder_info["pid"])):
 
251
            lock_info = '\n'.join(self._format_lock_info(holder_info))
 
252
            if bzrlib.ui.ui_factory.get_boolean("Break %s" % lock_info):
252
253
                self.force_break(holder_info)
253
254
        
254
255
    def force_break(self, dead_holder_info):
352
353
    def _parse_info(self, info_file):
353
354
        return read_stanza(info_file.readlines()).as_dict()
354
355
 
355
 
    def wait_lock(self, timeout=_DEFAULT_TIMEOUT_SECONDS,
356
 
                  poll=_DEFAULT_POLL_SECONDS):
 
356
    def wait_lock(self, timeout=None, poll=None):
357
357
        """Wait a certain period for a lock.
358
358
 
359
359
        If the lock can be acquired within the bounded time, it
362
362
        approximately `timeout` seconds.  (It may be a bit more if
363
363
        a transport operation takes a long time to complete.)
364
364
        """
 
365
        if timeout is None:
 
366
            timeout = _DEFAULT_TIMEOUT_SECONDS
 
367
        if poll is None:
 
368
            poll = _DEFAULT_POLL_SECONDS
 
369
 
365
370
        # XXX: the transport interface doesn't let us guard 
366
371
        # against operations there taking a long time.
367
372
        deadline = time.time() + timeout
 
373
        deadline_str = None
 
374
        last_info = None
368
375
        while True:
369
376
            try:
370
377
                self.attempt_lock()
371
378
                return
372
379
            except LockContention:
373
380
                pass
 
381
            new_info = self.peek()
 
382
            mutter('last_info: %s, new info: %s', last_info, new_info)
 
383
            if new_info is not None and new_info != last_info:
 
384
                if last_info is None:
 
385
                    start = 'Unable to obtain'
 
386
                else:
 
387
                    start = 'Lock owner changed for'
 
388
                last_info = new_info
 
389
                formatted_info = self._format_lock_info(new_info)
 
390
                if deadline_str is None:
 
391
                    deadline_str = time.strftime('%H:%M:%S',
 
392
                                                 time.localtime(deadline))
 
393
                self._report_function('%s %s\n'
 
394
                                      '%s\n' # held by
 
395
                                      '%s\n' # locked ... ago
 
396
                                      'Will continue to try until %s\n',
 
397
                                      start,
 
398
                                      formatted_info[0],
 
399
                                      formatted_info[1],
 
400
                                      formatted_info[2],
 
401
                                      deadline_str)
 
402
 
374
403
            if time.time() + poll < deadline:
375
404
                time.sleep(poll)
376
405
            else:
378
407
 
379
408
    def lock_write(self):
380
409
        """Wait for and acquire the lock."""
381
 
        self.attempt_lock()
 
410
        self.wait_lock()
382
411
 
383
412
    def lock_read(self):
384
413
        """Compatibility-mode shared lock.
408
437
            else:
409
438
                raise LockContention(self)
410
439
 
 
440
    def _format_lock_info(self, info):
 
441
        """Turn the contents of peek() into something for the user"""
 
442
        lock_url = self.transport.abspath(self.path)
 
443
        delta = time.time() - int(info['start_time'])
 
444
        return [
 
445
            'lock %s' % (lock_url,),
 
446
            'held by %(user)s on host %(hostname)s [process #%(pid)s]' % info,
 
447
            'locked %s' % (format_delta(delta),),
 
448
            ]
 
449