/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/lockdir.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
 
84
84
Example usage:
85
85
 
86
 
>>> from bzrlib.transport.memory import MemoryTransport
 
86
>>> from breezy.transport.memory import MemoryTransport
87
87
>>> # typically will be obtained from a BzrDir, Branch, etc
88
88
>>> t = MemoryTransport()
89
89
>>> l = LockDir(t, 'sample-lock')
112
112
import os
113
113
import time
114
114
 
115
 
from bzrlib import (
 
115
from . import (
116
116
    config,
117
117
    debug,
118
118
    errors,
121
121
    ui,
122
122
    urlutils,
123
123
    )
124
 
from bzrlib.decorators import only_raises
125
 
from bzrlib.errors import (
 
124
from .decorators import only_raises
 
125
from .errors import (
126
126
        DirectoryNotEmpty,
127
127
        FileExists,
128
128
        LockBreakMismatch,
136
136
        ResourceBusy,
137
137
        TransportError,
138
138
        )
139
 
from bzrlib.trace import mutter, note
140
 
from bzrlib.osutils import format_delta, rand_chars, get_host_name
141
 
from bzrlib.i18n import gettext
 
139
from .trace import mutter, note
 
140
from .osutils import format_delta, rand_chars, get_host_name
 
141
from .i18n import gettext
142
142
 
143
 
from bzrlib.lazy_import import lazy_import
 
143
from .lazy_import import lazy_import
144
144
lazy_import(globals(), """
145
 
from bzrlib import rio
 
145
from breezy import rio
146
146
""")
147
147
 
148
148
# XXX: At the moment there is no consideration of thread safety on LockDir
170
170
 
171
171
    __INFO_NAME = '/info'
172
172
 
173
 
    def __init__(self, transport, path, file_modebits=0644, dir_modebits=0755,
 
173
    def __init__(self, transport, path, file_modebits=0o644, dir_modebits=0o755,
174
174
        extra_holder_info=None):
175
175
        """Create a new LockDir object.
176
176
 
214
214
        self._trace("create lock directory")
215
215
        try:
216
216
            self.transport.mkdir(self.path, mode=mode)
217
 
        except (TransportError, PathError), e:
 
217
        except (TransportError, PathError) as e:
218
218
            raise LockFailed(self, e)
219
219
 
220
220
    def _attempt_lock(self):
236
236
        start_time = time.time()
237
237
        try:
238
238
            tmpname = self._create_pending_dir()
239
 
        except (errors.TransportError, PathError), e:
 
239
        except (errors.TransportError, PathError) as e:
240
240
            self._trace("... failed to create pending dir, %s", e)
241
241
            raise LockFailed(self, e)
242
242
        while True:
244
244
                self.transport.rename(tmpname, self._held_dir)
245
245
                break
246
246
            except (errors.TransportError, PathError, DirectoryNotEmpty,
247
 
                    FileExists, ResourceBusy), e:
 
247
                    FileExists, ResourceBusy) as e:
248
248
                self._trace("... contention, %s", e)
249
249
                other_holder = self.peek()
250
250
                self._trace("other holder is %r" % other_holder)
253
253
                except:
254
254
                    self._remove_pending_dir(tmpname)
255
255
                    raise
256
 
            except Exception, e:
 
256
            except Exception as e:
257
257
                self._trace("... lock failed, %s", e)
258
258
                self._remove_pending_dir(tmpname)
259
259
                raise
317
317
        try:
318
318
            self.transport.delete(tmpname + self.__INFO_NAME)
319
319
            self.transport.rmdir(tmpname)
320
 
        except PathError, e:
 
320
        except PathError as e:
321
321
            note(gettext("error removing pending lock: %s"), e)
322
322
 
323
323
    def _create_pending_dir(self):
368
368
            self.transport.delete(tmpname + self.__INFO_NAME)
369
369
            try:
370
370
                self.transport.rmdir(tmpname)
371
 
            except DirectoryNotEmpty, e:
 
371
            except DirectoryNotEmpty as e:
372
372
                # There might have been junk left over by a rename that moved
373
373
                # another locker within the 'held' directory.  do a slower
374
374
                # deletion where we list the directory and remove everything
399
399
        self._check_not_locked()
400
400
        try:
401
401
            holder_info = self.peek()
402
 
        except LockCorrupt, e:
 
402
        except LockCorrupt as e:
403
403
            # The lock info is corrupt.
404
404
            if ui.ui_factory.get_boolean(u"Break (corrupt %r)" % (self,)):
405
405
                self.force_break_corrupt(e.file_data)
407
407
        if holder_info is not None:
408
408
            if ui.ui_factory.confirm_action(
409
409
                u"Break %(lock_info)s",
410
 
                'bzrlib.lockdir.break',
 
410
                'breezy.lockdir.break',
411
411
                dict(lock_info=unicode(holder_info))):
412
412
                result = self.force_break(holder_info)
413
413
                ui.ui_factory.show_message(
531
531
            info = self._read_info_file(self._held_info_path)
532
532
            self._trace("peek -> held")
533
533
            return info
534
 
        except NoSuchFile, e:
 
534
        except NoSuchFile as e:
535
535
            self._trace("peek -> not held")
536
536
 
537
537
    def _prepare_info(self):
626
626
                    msg += '\n' + gettext(
627
627
                             'Will continue to try until %s, unless '
628
628
                             'you press Ctrl-C.') % deadline_str
629
 
                msg += '\n' + gettext('See "bzr help break-lock" for more.')
 
629
                msg += '\n' + gettext('See "brz help break-lock" for more.')
630
630
                self._report_function(msg)
631
631
            if (max_attempts is not None) and (attempt_count >= max_attempts):
632
632
                self._trace("exceeded %d attempts")
792
792
        lines = osutils.split_lines(info_file_bytes)
793
793
        try:
794
794
            stanza = rio.read_stanza(lines)
795
 
        except ValueError, e:
 
795
        except ValueError as e:
796
796
            mutter('Corrupt lock info file: %r', lines)
797
797
            raise LockCorrupt("could not parse lock info file: " + str(e),
798
798
                lines)