/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 breezy/lockdir.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
    )
125
125
from .decorators import only_raises
126
126
from .errors import (
127
 
    DirectoryNotEmpty,
128
 
    FileExists,
129
 
    LockBreakMismatch,
130
 
    LockBroken,
131
 
    LockContention,
132
 
    LockCorrupt,
133
 
    LockFailed,
134
 
    LockNotHeld,
135
 
    NoSuchFile,
136
 
    PathError,
137
 
    ResourceBusy,
138
 
    TransportError,
139
 
    )
 
127
        DirectoryNotEmpty,
 
128
        FileExists,
 
129
        LockBreakMismatch,
 
130
        LockBroken,
 
131
        LockContention,
 
132
        LockCorrupt,
 
133
        LockFailed,
 
134
        LockNotHeld,
 
135
        NoSuchFile,
 
136
        PathError,
 
137
        ResourceBusy,
 
138
        TransportError,
 
139
        )
140
140
from .i18n import gettext
141
141
from .osutils import format_delta, rand_chars, get_host_name
142
142
from .sixish import (
171
171
 
172
172
    __INFO_NAME = '/info'
173
173
 
174
 
    def __init__(self, transport, path, file_modebits=0o644,
175
 
                 dir_modebits=0o755, extra_holder_info=None):
 
174
    def __init__(self, transport, path, file_modebits=0o644, dir_modebits=0o755,
 
175
        extra_holder_info=None):
176
176
        """Create a new LockDir object.
177
177
 
178
178
        The LockDir is initially unlocked - this just creates the object.
251
251
                self._trace("other holder is %r" % other_holder)
252
252
                try:
253
253
                    self._handle_lock_contention(other_holder)
254
 
                except BaseException:
 
254
                except:
255
255
                    self._remove_pending_dir(tmpname)
256
256
                    raise
257
257
            except Exception as e:
273
273
        self._trace("after locking, info=%r", info)
274
274
        if info is None:
275
275
            raise LockFailed(self, "lock was renamed into place, but "
276
 
                             "now is missing!")
 
276
                "now is missing!")
277
277
        if info.get('nonce') != self.nonce:
278
278
            self._trace("rename succeeded, "
279
 
                        "but lock is still held by someone else")
 
279
                "but lock is still held by someone else")
280
280
            raise LockContention(self)
281
281
        self._lock_held = True
282
282
        self._trace("... lock succeeded after %dms",
283
 
                    (time.time() - start_time) * 1000)
 
283
                (time.time() - start_time) * 1000)
284
284
        return self.nonce
285
285
 
286
286
    def _handle_lock_contention(self, other_holder):
340
340
        # We'll rename the whole directory into place to get atomic
341
341
        # properties
342
342
        self.transport.put_bytes_non_atomic(tmpname + self.__INFO_NAME,
343
 
                                            info.to_bytes())
 
343
            info.to_bytes())
344
344
        return tmpname
345
345
 
346
346
    @only_raises(LockNotHeld, LockBroken)
369
369
            self.transport.delete(tmpname + self.__INFO_NAME)
370
370
            try:
371
371
                self.transport.rmdir(tmpname)
372
 
            except DirectoryNotEmpty:
 
372
            except DirectoryNotEmpty as e:
373
373
                # There might have been junk left over by a rename that moved
374
374
                # another locker within the 'held' directory.  do a slower
375
375
                # deletion where we list the directory and remove everything
376
376
                # within it.
 
377
                #
 
378
                # Maybe this should be broader to allow for ftp servers with
 
379
                # non-specific error messages?
377
380
                self._trace("doing recursive deletion of non-empty directory "
378
 
                            "%s", tmpname)
 
381
                        "%s", tmpname)
379
382
                self.transport.delete_tree(tmpname)
380
383
            self._trace("... unlock succeeded after %dms",
381
 
                        (time.time() - start_time) * 1000)
 
384
                    (time.time() - start_time) * 1000)
382
385
            result = lock.LockResult(self.transport.abspath(self.path),
383
386
                                     old_nonce)
384
387
            for hook in self.hooks['lock_released']:
406
409
            if ui.ui_factory.confirm_action(
407
410
                u"Break %(lock_info)s",
408
411
                'breezy.lockdir.break',
409
 
                    dict(lock_info=text_type(holder_info))):
 
412
                dict(lock_info=text_type(holder_info))):
410
413
                result = self.force_break(holder_info)
411
414
                ui.ui_factory.show_message(
412
415
                    "Broke lock %s" % result.lock_url)
529
532
            info = self._read_info_file(self._held_info_path)
530
533
            self._trace("peek -> held")
531
534
            return info
532
 
        except NoSuchFile:
 
535
        except NoSuchFile as e:
533
536
            self._trace("peek -> not held")
534
537
 
535
538
    def _prepare_info(self):
549
552
            raise LockContention(self)
550
553
        result = self._attempt_lock()
551
554
        hook_result = lock.LockResult(self.transport.abspath(self.path),
552
 
                                      self.nonce)
 
555
                self.nonce)
553
556
        for hook in self.hooks['lock_acquired']:
554
557
            hook(hook_result)
555
558
        return result
616
619
                    start = gettext('Lock owner changed for')
617
620
                last_info = new_info
618
621
                msg = gettext('{0} lock {1} {2}.').format(start, lock_url,
619
 
                                                          new_info)
 
622
                                                                    new_info)
620
623
                if deadline_str is None:
621
624
                    deadline_str = time.strftime('%H:%M:%S',
622
 
                                                 time.localtime(deadline))
 
625
                                                    time.localtime(deadline))
623
626
                if timeout > 0:
624
627
                    msg += '\n' + gettext(
625
 
                        'Will continue to try until %s, unless '
626
 
                        'you press Ctrl-C.') % deadline_str
 
628
                             'Will continue to try until %s, unless '
 
629
                             'you press Ctrl-C.') % deadline_str
627
630
                msg += '\n' + gettext('See "brz help break-lock" for more.')
628
631
                self._report_function(msg)
629
632
            if (max_attempts is not None) and (attempt_count >= max_attempts):
681
684
        # we can't rely on that remotely.  Once this is cleaned up,
682
685
        # reenable this warning to prevent it coming back in
683
686
        # -- mbp 20060303
684
 
        # warn("LockDir.lock_read falls back to write lock")
 
687
        ## warn("LockDir.lock_read falls back to write lock")
685
688
        if self._lock_held or self._fake_read_lock:
686
689
            raise LockContention(self)
687
690
        self._fake_read_lock = True
733
736
    def __unicode__(self):
734
737
        """Return a user-oriented description of this object."""
735
738
        d = self.to_readable_dict()
736
 
        return (gettext(
 
739
        return ( gettext(
737
740
            u'held by %(user)s on %(hostname)s (process #%(pid)s), '
738
741
            u'acquired %(time_ago)s') % d)
739
742
 
775
778
        info = dict(
776
779
            hostname=get_host_name(),
777
780
            pid=str(os.getpid()),
778
 
            nonce=rand_chars(20).encode('ascii'),
 
781
            nonce=rand_chars(20),
779
782
            start_time=str(int(time.time())),
780
783
            user=get_username_for_lock_info(),
781
784
            )
796
799
        except ValueError as e:
797
800
            mutter('Corrupt lock info file: %r', lines)
798
801
            raise LockCorrupt("could not parse lock info file: " + str(e),
799
 
                              lines)
 
802
                lines)
800
803
        if stanza is None:
801
804
            # see bug 185013; we fairly often end up with the info file being
802
805
            # empty after an interruption; we could log a message here but
803
806
            # there may not be much we can say
804
807
            return cls({})
805
808
        else:
806
 
            ret = stanza.as_dict()
807
 
            ret['nonce'] = ret['nonce'].encode('ascii')
808
 
            return cls(ret)
 
809
            return cls(stanza.as_dict())
809
810
 
810
811
    def __hash__(self):
811
812
        return id(self)
855
856
            pid = int(pid_str)
856
857
        except ValueError:
857
858
            mutter("can't parse pid %r from %r"
858
 
                   % (pid_str, self))
 
859
                % (pid_str, self))
859
860
            return False
860
861
        return osutils.is_local_pid_dead(pid)
861
862
 
868
869
    """
869
870
    try:
870
871
        return config.GlobalStack().get('email')
871
 
    except errors.NoWhoami:
 
872
    except config.NoWhoami:
872
873
        return osutils.getuser_unicode()