HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/filelock/__pycache__/_read_write.cpython-310.pyc
o

;jM=�@sUddlmZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZddlm
Z
mZddlmZddlmZddlmZe
rMdd	lmZe�d
�Ze�Zded<e��Zddd�Ze�e�dZ d dd�Z!Gdd�de"�Z#Gdd�de#d�Z$dS)!�)�annotationsN)�contextmanager�suppress)�
TYPE_CHECKING�Literal)�WeakValueDictionary�)�AcquireReturnProxy)�Timeout)�	GeneratorZfilelockzset[sqlite3.Connection]�_all_connections�return�Nonec
Cspt�,tt�D]}tt��|��Wd�n1swYqt��Wd�dS1s1wYdS�N)�_all_connections_lock�listrr�	Exception�close�clear)Zcon�r�H/opt/saltstack/salt/lib/python3.10/site-packages/filelock/_read_write.py�_cleanup_connectionss

��
"�ri��5w�timeout�float�blocking�bool�already_waited�intcCsx|durdS|dkrtS|dkrd}t|��|dkr!t||d�n|}t|d�}|tks1|dkr:t�d|t�tS|S)NFr���z+timeout must be a non-negative number or -1i�z7timeout %s is too large for SQLite, using %s ms instead)�_MAX_SQLITE_TIMEOUT_MS�
ValueError�maxr�_LOGGER�warning)rrr�msg�	remaining�
timeout_msrrr�timeout_for_sqlite(sr'cs@eZdZUdZded<ded<	dddd�d�fdd�Z�ZS)�_ReadWriteLockMetaa"
    Metaclass that handles singleton resolution when is_singleton=True.

    Singleton logic lives here rather than in ReadWriteLock.get_lock so that ``ReadWriteLock(path)`` transparently
    returns cached instances without a 2-arg ``super()`` call that type checkers cannot verify.

    �0WeakValueDictionary[pathlib.Path, ReadWriteLock]�
_instanceszthreading.Lock�_instances_lockrT�r�is_singleton�	lock_file�str | os.PathLike[str]rrrrr-r
�
ReadWriteLockc	s�|st�j||||d�St�|���}|j�B||jvr,t�j||||d�}||j|<n|j|}|j|ks;|j|krOd|j�d|j�d|�d|��}t	|��|Wd�S1s[wYdS)Nr,z$Singleton lock created with timeout=z, blocking=z, cannot be changed to timeout=)
�super�__call__�pathlib�Path�resolver+r*rrr )�clsr.rrr-�
normalized�instancer$��	__class__rrr2Gs$

���$�z_ReadWriteLockMeta.__call__�r)
r.r/rrrrr-rr
r0)�__name__�
__module__�__qualname__�__doc__�__annotations__r2�
__classcell__rrr9rr(;s
��r(c@s�eZdZUdZe�Zded<e��Z	e
	d9dd�d:dd��Z	d9ddd�d;dd�Zd<dd�Z
d=dd�Zd>d d!�Zd?d"d#�Zd@d$d%�Zd9dd�dAd&d'�Zd9dd�dAd(d)�Zd*d+�dBd-d.�ZedCd/d�dDd3d4��ZedCd/d�dDd5d6��ZdEd7d8�Zd/S)Fr0a�
    Cross-process read-write lock backed by SQLite.

    Allows concurrent shared readers or a single exclusive writer. The lock is reentrant within the same mode (multiple
    ``acquire_read`` calls nest, as do multiple ``acquire_write`` calls from the same thread), but upgrading from read
    to write or downgrading from write to read raises :class:`RuntimeError`. Write locks are pinned to the thread that
    acquired them.

    By default, ``is_singleton=True``: calling ``ReadWriteLock(path)`` with the same resolved path returns the same
    instance. The lock file must use a ``.db`` extension (SQLite database).

    :param lock_file: path to the SQLite database file used as the lock
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable
    :param is_singleton: if ``True``, reuse existing instances for the same resolved path

    .. versionadded:: 3.21.0

    r)r*rT�rr.r/rrrrr
cCs||||d�S)a	
        Return the singleton :class:`ReadWriteLock` for *lock_file*.

        :param lock_file: path to the SQLite database file used as the lock
        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: the singleton lock instance

        :raises ValueError: if an instance already exists for this path with different *timeout* or *blocking* values

        rBr)r6r.rrrrr�get_lock{szReadWriteLock.get_lockr,r-rcCs�t�|�|_||_||_t��|_t��|_d|_	d|_
d|_tj
|jdd�|_t�t�|j�Wd�dS1s<wYdS)NrF)Zcheck_same_thread)�os�fspathr.rr�	threading�Lock�_transaction_lock�_internal_lock�_lock_level�
_current_mode�_write_thread_id�sqlite3�connect�_conrr�add)�selfr.rrr-rrr�__init__�s

"�zReadWriteLock.__init__cCsP|s
|jjdd�}n|dkr|jjdd�}n|jjd|d�}|s&t|j�d�dS)NFrBrT�rr)rH�acquirer
r.)rQrrZacquiredrrr�_acquire_transaction_lock�s�z'ReadWriteLock._acquire_transaction_lock�mode�Literal['read', 'write']�opposite�str�	directionr	cCs�|j|krd|�d|j�dt|��d|�d|�d�}t|��|dkrAt��}|jkrAd|j�dt|��d	|�d
|j��}t|��|jd7_t|d�S)
NzCannot acquire z	 lock on � (lock id: z): already holding a z lock (z
 not allowed)�writezCannot acquire write lock on z) from thread z while it is held by thread r��lock)	rKr.�id�RuntimeErrorrF�	get_identrLrJr	)rQrVrXrZr$Zcurrrr�_validate_reentrant�s$
������
z!ReadWriteLock._validate_reentrant�
start_timec	Cs�t��|}t|||d�}|j�d|�d����|j�d���t��|}t|||d�}|kr>|j�d|�d����|dkrDdnd}|j�|���|dkr\|j�d	���dSdS)
N)rrzPRAGMA busy_timeout=�;zPRAGMA journal_mode=MEMORY;r\zBEGIN EXCLUSIVE TRANSACTION;zBEGIN TRANSACTION;�readz'SELECT name FROM sqlite_schema LIMIT 1;)�time�perf_counterr'rOZexecuter)	rQrVrrrcZwaitedr&Z
recomputedZstmtrrr�_configure_and_begin�s�z"ReadWriteLock._configure_and_beginc
Cs�|dkrdnd}|dkrdnd}|j�|jdkr'|�|||�Wd�SWd�n1s1wYt��}|j||d�z+z|j||||d�WW|j��St	j
yl}z
dt|�vrb�t|j
�d�d}~ww|j��w)	Nrer\�	downgrade�upgraderrS�rrczdatabase is locked)rIrJrbrfrgrU�_do_acquire_innerrH�releaserMZOperationalErrorrYr
r.)rQrVrrrXrZrc�excrrr�_acquire�s(
�����zReadWriteLock._acquirecCs�|dkrdnd}|dkrdnd}|j�|jdkr'|�|||�Wd�SWd�n1s1wY|j||||d�|j�||_d|_|dkrRt��|_Wd�n1s\wYt|d�S)	Nrer\rirjrrkrr])	rIrJrbrhrKrFrarLr	)rQrVrrrcrXrZrrrrl�s"
��
��
zReadWriteLock._do_acquire_innercC�|jd||d�S)a�
        Acquire a shared read lock.

        If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a
        read lock while holding a write lock raises :class:`RuntimeError` (downgrade not allowed).

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as a context manager to release the lock

        :raises RuntimeError: if a write lock is already held on this instance
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rerB�ro�rQrrrrr�acquire_read�szReadWriteLock.acquire_readcCrp)a�
        Acquire an exclusive write lock.

        If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant).
        Attempting to acquire a write lock while holding a read lock raises :class:`RuntimeError` (upgrade not allowed).
        Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raises
        :class:`RuntimeError`.

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as a context manager to release the lock

        :raises RuntimeError: if a read lock is already held, or a write lock is held by a different thread
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r\rBrqrrrrr�
acquire_writeszReadWriteLock.acquire_writeF��forcervcCs�d}|j�B|jdkr&|r	Wd�dSd|j�dt|��d�}t|��|r,d|_n|jd8_|jdkr@d|_d|_d}Wd�n1sJwY|rX|j��dSdS)	al
        Release one level of the current lock.

        When the lock level reaches zero the underlying SQLite transaction is rolled back, releasing the database lock.

        :param force: if ``True``, release the lock completely regardless of the current lock level

        :raises RuntimeError: if no lock is currently held and *force* is ``False``

        FrNzCannot release a lock on r[z) that is not heldrT)	rIrJr.r_r`rKrLrOZrollback)rQrvZshould_rollbackr$rrrrm#s(
�
���zReadWriteLock.releaseN�float | None�bool | None�Generator[None]cc�L�|dur|j}|dur|j}|j||d�z
dVW|��dS|��w)a}
        Context manager that acquires and releases a shared read lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        NrB)rrrsrmrrrrr�	read_lock@��zReadWriteLock.read_lockccrz)a�
        Context manager that acquires and releases an exclusive write lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        NrB)rrrtrmrrrrr�
write_lockUr|zReadWriteLock.write_lockcCsL|jdd�|j��t�t�|j�Wd�dS1swYdS)z�
        Release the lock (if held) and close the underlying SQLite connection.

        After calling this method, the lock instance is no longer usable.

        TruN)rmrOrrr�discard)rQrrrrjs

"�zReadWriteLock.closer;)r.r/rrrrr
r0)
r.r/rrrrr-rr
r)rrrrr
r)rVrWrXrYrZrYr
r	)
rVrWrrrrrcrr
r)rVrWrrrrr
r	)
rVrWrrrrrcrr
r	)rrrrr
r	)rvrr
rr)rrwrrxr
ry�r
r)r<r=r>r?rr*r@rFrGr+�classmethodrCrRrUrbrhrorlrsrtrmrr{r}rrrrrr0cs6
����





r0)�	metaclassr)rrrrrrr
r)%�
__future__r�atexit�loggingrDr3rMrFrf�
contextlibrr�typingrr�weakrefrZ_apir	�_errorr
�collections.abcr�	getLoggerr"�setrr@rGrr�registerrr'�typer(r0rrrr�<module>s0



(