1
  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þZi ¬Ü¢#á�a	HçÉ„­:ÏÈ_ŠI|¥�+Wé3ž¯ÀÇ ?÷     # Copyright (C) 2005-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Locking using OS file locks or file existence.

Note: This method of locking is generally deprecated in favour of LockDir, but
is used to lock local WorkingTrees, and by some old formats.  It's accessed
through Transport.lock_read(), etc.

This module causes two methods, lock() and unlock() to be defined in
any way that works on the current platform.

It is not specified whether these locks are reentrant (i.e. can be
taken repeatedly by a single process) or whether they exclude
different threads in a single process.  That reentrancy is provided by
LockableFiles.

This defines two classes: ReadLock and WriteLock, which can be
implemented in different ways on different platforms.  Both have an
unlock() method.
"""

import contextlib
import errno
import os
import sys
from typing import Dict, Set, List, Tuple, Optional, Any
import warnings

from . import (
    debug,
    errors,
    osutils,
    trace,
    )
from .hooks import Hooks
from .i18n import gettext
from .transport import Transport


LockToken = bytes


class LockHooks(Hooks):

    def __init__(self):
        Hooks.__init__(self, "breezy.lock", "Lock.hooks")
        self.add_hook(
            'lock_acquired',
            "Called with a breezy.lock.LockResult when a physical lock is "
            "acquired.", (1, 8))
        self.add_hook(
            'lock_released',
            "Called with a breezy.lock.LockResult when a physical lock is "
            "released.", (1, 8))
        self.add_hook(
            'lock_broken',
            "Called with a breezy.lock.LockResult when a physical lock is "
            "broken.", (1, 15))


class Lock:
    """Base class for locks.

    :cvar hooks: Hook dictionary for operations on locks.
    """

    hooks = LockHooks()

    def __init__(self, transport: Transport, path: str, file_modebits: int,
                 dir_modebits: int) -> None: ...

    def create(self, mode: int): ...

    def break_lock(self) -> None: ...

    def leave_in_place(self) -> None: ...

    def dont_leave_in_place(self) -> None: ...

    def validate_token(self, token: Optional[LockToken]) -> None: ...

    def lock_write(self, token: Optional[LockToken]) -> Optional[LockToken]: ...

    def lock_read(self) -> None: ...

    def unlock(self) -> None: ...

    def peek(self) -> LockToken:
        raise NotImplementedError(self.peek)


class LockResult:
    """Result of an operation on a lock; passed to a hook"""

    def __init__(self, lock_url, details=None):
        """Create a lock result for lock with optional details about the lock."""
        self.lock_url = lock_url
        self.details = details

    def __eq__(self, other):
        return self.lock_url == other.lock_url and self.details == other.details

    def __repr__(self):
        return '{}({}, {})'.format(self.__class__.__name__,
                               self.lock_url, self.details)


class LogicalLockResult:
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.

    :ivar unlock: A callable which will unlock the lock.
    """

    def __init__(self, unlock, token=None):
        self.unlock = unlock
        self.token = token

    def __repr__(self):
        return "LogicalLockResult(%s)" % (self.unlock)

    def __enter__(self):
     