/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

  • Committer: Ian Clatworthy
  • Date: 2008-12-15 06:18:29 UTC
  • mfrom: (3905 +trunk)
  • mto: (3586.1.23 views-ui)
  • mto: This revision was merged to the branch mainline in revision 4030.
  • Revision ID: ian.clatworthy@canonical.com-20081215061829-c8qwa93g71u9fsh5
merge bzr.dev 3905

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
105
105
 
106
106
import os
107
107
import time
108
 
from cStringIO import StringIO
109
108
 
110
109
from bzrlib import (
111
110
    debug,
112
111
    errors,
 
112
    lock,
113
113
    )
114
114
import bzrlib.config
115
115
from bzrlib.errors import (
124
124
        PathError,
125
125
        ResourceBusy,
126
126
        TransportError,
127
 
        UnlockableTransport,
128
127
        )
 
128
from bzrlib.hooks import Hooks
129
129
from bzrlib.trace import mutter, note
130
 
from bzrlib.transport import Transport
131
 
from bzrlib.osutils import rand_chars, format_delta
132
 
from bzrlib.rio import read_stanza, Stanza
 
130
from bzrlib.osutils import format_delta, rand_chars, get_host_name
133
131
import bzrlib.ui
134
132
 
 
133
from bzrlib.lazy_import import lazy_import
 
134
lazy_import(globals(), """
 
135
from bzrlib import rio
 
136
""")
135
137
 
136
138
# XXX: At the moment there is no consideration of thread safety on LockDir
137
139
# objects.  This should perhaps be updated - e.g. if two threads try to take a
152
154
_DEFAULT_POLL_SECONDS = 1.0
153
155
 
154
156
 
155
 
class LockDir(object):
156
 
    """Write-lock guarding access to data."""
 
157
class LockDir(lock.Lock):
 
158
    """Write-lock guarding access to data.
 
159
    """
157
160
 
158
161
    __INFO_NAME = '/info'
159
162
 
296
299
            self._locked_via_token = False
297
300
            self._lock_held = False
298
301
        else:
 
302
            old_nonce = self.nonce
299
303
            # rename before deleting, because we can't atomically remove the
300
304
            # whole tree
301
305
            start_time = time.time()
321
325
                self.transport.delete_tree(tmpname)
322
326
            self._trace("... unlock succeeded after %dms",
323
327
                    (time.time() - start_time) * 1000)
 
328
            result = lock.LockResult(self.transport.abspath(self.path),
 
329
                old_nonce)
 
330
            for hook in self.hooks['lock_released']:
 
331
                hook(result)
324
332
 
325
333
    def break_lock(self):
326
334
        """Break a lock not held by this instance of LockDir.
422
430
    def _prepare_info(self):
423
431
        """Write information about a pending lock to a temporary file.
424
432
        """
425
 
        import socket
426
433
        # XXX: is creating this here inefficient?
427
434
        config = bzrlib.config.GlobalConfig()
428
435
        try:
429
436
            user = config.user_email()
430
437
        except errors.NoEmailInUsername:
431
438
            user = config.username()
432
 
        s = Stanza(hostname=socket.gethostname(),
 
439
        s = rio.Stanza(hostname=get_host_name(),
433
440
                   pid=str(os.getpid()),
434
441
                   start_time=str(int(time.time())),
435
442
                   nonce=self.nonce,
438
445
        return s.to_string()
439
446
 
440
447
    def _parse_info(self, info_file):
441
 
        return read_stanza(info_file.readlines()).as_dict()
 
448
        return rio.read_stanza(info_file.readlines()).as_dict()
442
449
 
443
450
    def attempt_lock(self):
444
451
        """Take the lock; fail if it's already held.
451
458
        """
452
459
        if self._fake_read_lock:
453
460
            raise LockContention(self)
454
 
        return self._attempt_lock()
 
461
        result = self._attempt_lock()
 
462
        hook_result = lock.LockResult(self.transport.abspath(self.path),
 
463
                self.nonce)
 
464
        for hook in self.hooks['lock_acquired']:
 
465
            hook(hook_result)
 
466
        return result
455
467
 
456
468
    def wait_lock(self, timeout=None, poll=None, max_attempts=None):
457
469
        """Wait a certain period for a lock.