/n | d Z ddlmZ ddlZddlZddlZddlmZmZm Z m Z mZmZ G d de Z d Zd Zd ZdS )z8 Lockfile behaviour implemented via Unix PID files. )absolute_importN )LockBase AlreadyLocked LockFailed NotLocked NotMyLockLockTimeoutc @ e Zd ZdZddZd Zd Zd ZddZd Z d Z dS ) PIDLockFileaA Lockfile implemented as a Unix PID file. The lock file is a normal file named by the attribute `path`. A lock's PID file contains a single line of text, containing the process ID (PID) of the process that acquired the lock. >>> lock = PIDLockFile('somefile') >>> lock = PIDLockFile('somefile') FNc L t j | |d| | j | _ d S )NF)r __init__pathunique_name)selfr threadedtimeouts t/builddir/build/BUILD/imunify360-venv-2.6.2/opt/imunify360/venv/lib/python3.11/site-packages/lockfile/pidlockfile.pyr zPIDLockFile.__init__$ s+ $eW5559 c * t | j S )z- Get the PID from the lock file. )read_pid_from_pidfiler r s r read_pidzPIDLockFile.read_pid* s %TY///r c J t j | j S )zv Test if the lock is currently held. The lock is held if the PID file for this lock exists. )osr existsr s r is_lockedzPIDLockFile.is_locked/ s w~~di(((r c | | o(t j | k S )z Test if the lock is held by the current process. Returns ``True`` if the current process ID matches the number stored in the PID file. )r r getpidr r s r i_am_lockingzPIDLockFile.i_am_locking7 s, ~~BBIKK4==??$BBr c ||n| j }t j }||dk r||z } t | j dS # t $ r}|j t j k rkt j |k r6||dk rt d| j z t d| j z t j |dur|dz pd nt d| j z Y d}~nd}~ww xY w) z Acquire the lock. Creates the PID file for this lock, or raises an error if the lock could not be acquired. Nr Tz&Timeout waiting to acquire lock for %sz%s is already locked g?zfailed to create %s)r timewrite_pid_to_pidfiler OSErrorerrnoEEXISTr r sleepr )r r end_timeexcs r acquirezPIDLockFile.acquire? s@ %0''dl9;;7Q;;H $TY/// H H H9,,y{{X--".7Q;;"- /=.2i/8 #9 #9 9 #00F04 1: #; #; ;Jwd2Cw|JsKKKK$%:TY%FGGG LKKKK H s A C/BC**C/c | st d| j z | st d| j z t | j dS )z Release the lock. Removes the PID file to release the lock, or raises an error if the current process does not hold the lock. z%s is not lockedz%s is locked, but not by meN)r r r r r remove_existing_pidfiler s r releasezPIDLockFile.release_ sm ~~ <.:;;; "" G9DIEFFF *****r c . t | j dS )z Break an existing lock. Removes the PID file if it already exists, otherwise does nothing. N)r- r r s r break_lockzPIDLockFile.break_lockl s *****r )FN)N)__name__ __module____qualname____doc__r r r r r+ r. r0 r r r r s % % % %0 0 0 ) ) )C C C @+ + ++ + + + +r r c d} t | d }| } t | }n# t $ r Y nw xY w| n# t $ r Y nw xY w|S )z Read the PID recorded in the named PID file. Read and return the numeric PID recorded as text in the named PID file. If the PID file cannot be read, or if the content is not a valid PID, return ``None``. Nr)openreadlinestripint ValueErrorcloseIOError)pidfile_pathpidpidfilelines r r r v s C|S)) !!'')) d))CC D % ( Js" A0 A AA0 A=<A=c * t j t j z t j z }d}t j | || }t j |d }t j }| d|z | dS )u Write the PID in the named PID file. Get the numeric process ID (“PID”) of the current process and write it to the named file as a line of text. i wz%s N) r O_CREATO_EXCLO_WRONLYr8 fdopenr writer= )r? open_flags open_mode pidfile_fdrA r@ s r r$ r$ sv *ry(2;6JIz9==Ji C((G )++CMM&3,MMOOOOOr c t j | dS # t $ r"}|j t j k rn Y d}~dS d}~ww xY w)z Remove the named PID file if it exists. Removing a PID file that doesn't already exist puts us in the desired state, so we ignore the condition if the file does not exist. N)r remover% r&