f0dZddlmZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z mZmZmZmZddlmZmZmZdd lmZdd lmZdd lmZdd lmZe r,ddlZdd lm Z ddl!m"Z"ejFdk\rddl m$Z$nddl%m$Z$ejLdZ'e GddeZ(Gdde(e Z)GddZ*GddeZ+Gddee+Z,Gddee,Z-Gdd ee,Z.Gd!d"ee,Z/gd#Z0y)$z1An asyncio-based implementation of the file lock.) annotationsN) dataclass)local) TYPE_CHECKINGAnyCallableNoReturncast) BaseFileLockFileLockContext FileLockMeta)Timeout) SoftFileLock) UnixFileLock)WindowsFileLock)futures) TracebackType) )Selffilelockc<eZdZUdZdZded<dZded<dZded <y) AsyncFileLockContextzGA dataclass which holds the context for a ``BaseAsyncFileLock`` object.Tboolrun_in_executorNfutures.Executor | Noneexecutor asyncio.AbstractEventLoop | Noneloop)__name__ __module__ __qualname____doc__r__annotations__rr Y/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/filelock/asyncio.pyrr"s+Q!OT )-H%,.2D *1r'rceZdZdZy)AsyncThreadLocalFileContextz8A thread local version of the ``FileLockContext`` class.Nr!r"r#r$r&r'r(r*r*0sBr'r*c8eZdZdZddZddZ ddZy) AsyncAcquireReturnProxyzDA context-aware object that will release the lock file when exiting.c||_yNlock)selfr1s r(__init__z AsyncAcquireReturnProxy.__init__7s  r'c"K|jSwr/r0r2s r( __aenter__z"AsyncAcquireReturnProxy.__aenter__:syys cTK|jjd{y7wr/)r1releaser2exc_type exc_value tracebacks r( __aexit__z!AsyncAcquireReturnProxy.__aexit__=s ii!!!s (&(N)r1BaseAsyncFileLockreturnNone)r?r>r:ztype[BaseException] | Noner;zBaseException | Noner<zTracebackType | Noner?r@)r!r"r#r$r3r6r=r&r'r(r-r-4s:N","("( "  "r'r-c\eZdZ ddddddd dfdZxZS)AsyncFileLockMetaFTNblocking is_singletonr rrc v|r|r d} t| t | |||||||||  } tt| S)Nz:run_in_executor is not supported when thread_local is True) lock_filetimeoutmode thread_localrErFr rr) ValueErrorsuper__call__r r>) clsrHrIrJrKrErFr rrmsginstance __class__s r(rNzAsyncFileLockMeta.__call__GsX ONCS/ !7#%%+$  %x00r'iF)rHstr | os.PathLike[str]rIfloatrJintrKrrErrFrr rrrrrr?r>)r!r"r#rN __classcell__)rRs@r(rCrCFs" 1"15 $,01)11 1  111/11*1 11r'rCceZdZdZ ddddddd ddZeddZeddZejdd Zedd Z ddd  dd Z ddd Z ddZ ddZ ddZ d dZd!dZy)"r>z'Base class for asynchronous file locks.FTNrDc||_||_tj||||||| d} |rtnt di| |_y)a" Create a new lock object. :param lock_file: path to the file :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock. :param mode: file permissions for the lockfile :param thread_local: Whether this object's internal context should be thread local or not. If this is set to ``False`` then the lock will be reentrant across threads. :param blocking: whether the lock should be blocking or not :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around. :param loop: The event loop to use. If not specified, the running event loop will be used. :param run_in_executor: If this is set to ``True`` then the lock will be acquired in an executor. :param executor: The executor to use. If not specified, the default executor will be used. )rHrIrJrEr rrNr&)_is_thread_local _is_singletonosfspathr*r_context) r2rHrIrJrKrErFr rrkwargss r(r3zBaseAsyncFileLock.__init__hs[@!-) 9- . " 0ul/J`t/ /  r'c.|jjS)z"::return: whether run in executor.)r_rr5s r(rz!BaseAsyncFileLock.run_in_executors}},,,r'c.|jjS)z::return: the executor.r_rr5s r(rzBaseAsyncFileLock.executors}}%%%r'c&||j_y)z Change the executor. :param value: the new executor or ``None`` :type value: futures.Executor | None Nrc)r2values r(rzBaseAsyncFileLock.executors"' r'c.|jjS)z::return: the event loop.)r_r r5s r(r zBaseAsyncFileLock.loops}}!!!r')rEcK||jj}||jj}|jxjdz c_t |}|j }t j} |js:tjd|||j|jd{|jrtjd||n|dur"tjd||t|d|cxkrt j|z kr%nn"tjd||t|d }tj||||tj|d{ t%| S77#t $r4t#d|jjdz |j_wxYww) aW Try to acquire the file lock. :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~BaseFileLock.timeout` is and if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired :param poll_interval: interval of trying to acquire the lock file :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. :raises Timeout: if fails to acquire lock within the timeout period :return: a context object that will unlock the file when the context is exited .. code-block:: python # You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct: lock.acquire() try: pass finally: lock.release() Nr z#Attempting to acquire lock %s on %szLock %s acquired on %sFz+Failed to immediately acquire lock %s on %srz"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r0)r_rIrE lock_counteridrHtime perf_counter is_locked_LOGGERdebug_run_internal_method_acquirerasynciosleep BaseExceptionmaxr-)r2rI poll_intervalrElock_id lock_filename start_timerPs r(acquirezBaseAsyncFileLock.acquiresD ?mm++G  }}--H ""a'"T( &&(  ~~MM"GR_`33DMMBBB>>MM":G]Su$MM"OQXZgh!-00B$"3"3"5 "BBMM"FQ^_!-00J c7M=ImmM222 'D11!C3 ),Q 0J0JQ0N)ODMM &  sDA;G>AFFCFFF GFF=GGcK|jr|jxjdzc_|jjdk(s|rzt||j}}t j d|||j|jd{d|j_t j d||yyy7/w)a+ Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also note, that the lock file itself is not automatically deleted. :param force: If true, the lock counter is ignored and the lock is released in every case/ r rz#Attempting to release lock %s on %sNzLock %s released on %s) rlr_rhrirHrmrnro_release)r2forcervrws r(r8zBaseAsyncFileLock.releases >> MM & &! + &}}))Q.%)+D4>> CWm\// >>>-. * 6O 38 ?sBCC0CcKtj|r|d{y|jrG|jxstj}|j|j |d{y|y7`7wr/)rqiscoroutinefunctionrr get_running_loopr)r2methodr s r(roz&BaseAsyncFileLock._run_internal_method sg  & &v .(NN  ! !99: 8 8 :D&&t}}f= = = H  >s" BBAB5B6BBcd}t|)z Replace old __enter__ method to avoid using it. NOTE: DO NOT USE `with` FOR ASYNCIO LOCKS, USE `async with` INSTEAD. :return: none :rtype: NoReturn z>Do not use `with` for asyncio locks, use `async with` instead.)NotImplementedError)r2rPs r( __enter__zBaseAsyncFileLock.__enter__sO!#&&r'cBK|jd{|S7w)zF Acquire the lock. :return: the lock object N)ryr5s r(r6zBaseAsyncFileLock.__aenter__ s!lln  s c@K|jd{y7w)z Release the lock. :param exc_type: the exception type if raised :param exc_value: the exception value if raised :param traceback: the exception traceback if raised N)r8r9s r(r=zBaseAsyncFileLock.__aexit__*sllns cJtjt5|jxst j }|j s"|j|jdn!|j|jddddy#1swYyxYw)z'Called when the lock object is deleted.T)r|N) contextlibsuppress RuntimeErrorr rqr is_runningrun_until_completer8 create_task)r2r s r(__del__zBaseAsyncFileLock.__del__:sx   . ;99: 8 8 :D??$'' 4 (@A  D!9:  ; ; ;s A6BB"rS)rHrUrIrVrJrWrKrrErrFrr rrrrrr?r@)r?r)r?r)rerr?r@)r?r)Ng?)rIz float | NonerurVrEz bool | Noner?r-)F)r|rr?r@)rzCallable[[], Any]r?r@)r?r )r?rrA)r?r@)r!r"r#r$r3propertyrrsetterr ryr8rorr6r=rr&r'r(r>r>esk1 " 0 "15 $,00 )0 0  0  0 0 0 /0 0 *0  0 d--&&__''"" !%#B2 !% B2B2B2  B2 ! B2HP& ',((    ;r'r>) metaclassceZdZdZy)AsyncSoftFileLockz.Simply watches the existence of the lock file.Nr+r&r'r(rrDs8r'rceZdZdZy)AsyncUnixFileLockzHUses the :func:`fcntl.flock` to hard lock the lock file on unix systems.Nr+r&r'r(rrHsRr'rceZdZdZy)AsyncWindowsFileLockzNUses the :func:`msvcrt.locking` to hard lock the lock file on windows systems.Nr+r&r'r(rrLsXr'r)r-rrrr>)1r$ __future__rrqrloggingr]rj dataclassesr threadingrtypingrrrr r _apir r r_errorr_softr_unixr_windowsrsys concurrentrtypesr version_infortyping_extensions getLoggerrmrr*r-rCr>rrr__all__r&r'r(rs7" !??==%"# 7"* '  J '  2? 2  2C"6C""$1 1>\; 0A\;~9 &79S &7SY?,=Y r'