U ��UV� � @ st d Z ddlmZ ddlZddlZddlZddlmZmZm Z m Z mZmZ G dd� de�Z dd � Zd d� Zdd � ZdS )z8 Lockfile behaviour implemented via Unix PID files. � )�absolute_importN� )�LockBase� AlreadyLocked� LockFailed� NotLocked� NotMyLock�LockTimeoutc @ sL e Zd ZdZddd�Zdd� Zdd � Zd d� Zddd �Zdd� Z dd� 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 C s t �| |d|� | j| _d S )NF)r �__init__�pathZunique_name)�selfr Zthreaded�timeout� r �/pidlockfile.pyr $ s zPIDLockFile.__init__c C s t | j�S )z- Get the PID from the lock file. )�read_pid_from_pidfiler �r r r r �read_pid* s zPIDLockFile.read_pidc C s 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 r r r � is_locked/ s zPIDLockFile.is_lockedc C s | � � ot�� | �� kS )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 r r r �i_am_locking7 s zPIDLockFile.i_am_lockingc C s� |dk r|n| j }t�� }|dk r2|dkr2||7 }zt| j� W n� tk r� } zt|jtjkr�t�� |kr�|dk r�|dkr�td| j ��ntd| j ��t� |dk r�|d p�d� nt d| j ��W 5 d}~X Y q2X dS q2dS )z� Acquire the lock. Creates the PID file for this lock, or raises an error if the lock could not be acquired. Nr z&Timeout waiting to acquire lock for %sz%s is already locked� g�������?zfailed to create %s)r �time�write_pid_to_pidfiler �OSError�errnoZEEXISTr r �sleepr )r r Zend_time�excr r r �acquire? s&