Spade
Mini Shell
| Directory:~$ /usr/lib/python2.7/site-packages/certbot/_internal/ |
| [Home] [System Details] [Kill Me] |
�
���_c@sdZddlZddlZddlmZddlmZddlmZddlm Z yddl
Z
Wn#ek
r�ddlZe
ZnXeZeje�Zd�Zdefd ��YZd
efd��YZdefd
��YZdefd��YZdS(sZImplements file
locks compatible with Linux and Windows for locking files and
directories.i����N(tOptional(terrors(t
filesystem(toscCsttjj|d��S(s3Place a lock file on the
directory at dir_path.
The lock file is placed in the root of dir_path with the name
.certbot.lock.
:param str dir_path: path to directory
:returns: the locked LockFile object
:rtype: LockFile
:raises errors.LockError: if unable to acquire the lock
s
.certbot.lock(tLockFileRtpathtjoin(tdir_path((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pytlock_dirsRcBs;eZdZd�Zd�Zd�Zd�Zd�ZRS(s�
Platform independent file lock system.
LockFile accepts a parameter, the path to a file acting as a lock. Once
the LockFile,
instance is created, the associated file is 'locked from the point
of view of the OS,
meaning that if another instance of Certbot try at the same time to
acquire the same lock,
it will raise an Exception. Calling release method will release the
lock, and make it
available to every other instance.
Upon exit, Certbot will also release all the locks.
This allows us to protect a file or directory from being concurrently
accessed
or modified by two Certbot instances.
LockFile is platform independent: it will proceed to the appropriate OS
lock mechanism
depending on Linux or Windows.
cCs8||_trtnt}||�|_|j�dS(s�
Create a LockFile instance on the given file path, and acquire
lock.
:param str path: the path to the file that will hold a lock
N(t_patht
POSIX_MODEt_UnixLockMechanismt_WindowsLockMechanismt_lock_mechanismtacquire(tselfRt mechanism((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyt__init__7s cCsBdj|jj|j�}|j�r4|d7}n
|d7}|S(Ns
{0}({1})
<s acquired>s released>(tformatt __class__t__name__R t is_locked(Rtrepr_str((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyt__repr__Cs
cCs|jj�dS(s�
Acquire the lock on the file, forbidding any other Certbot instance
to acquire it.
:raises errors.LockError: if unable to acquire the lock
N(R
R(R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRLscCs|jj�dS(sb
Release the lock on the file, allowing any other Certbot instance
to acquire it.
N(R
trelease(R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRTscCs
|jj�S(su
Check if the file is currently locked.
:return: True if the file is locked, False otherwise
(R
R(R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR[s(Rt
__module__t__doc__RRRRR(((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR)s
t_BaseLockMechanismcBs,eZd�Zd�Zd�Zd�ZRS(cCs||_d|_dS(sk
Create a lock file mechanism for Unix.
:param str path: the path to the lock file
N(R tNonet_fd(RR((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRes cCs
|jdk S(svCheck
if lock file is currently locked.
:return: True if the lock file is locked
:rtype: bool
N(RR(R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRnscCsdS(N((R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRvscCsdS(N((R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRys(RRRRRR(((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyRds RcBs2eZdZd�Zd�Zd�Zd�ZRS(s�
A UNIX lock file mechanism.
This lock file is released when the locked file is closed or the
process exits. It cannot be used to provide synchronization between
threads. It is based on the lock_file package by Martin Horcicka.
cCs�x�|jdkr�tj|jtjtjBd�}z,|j|�|j |�r_||_nWd|jdkr�tj
|�nXqWdS(sAcquire the
lock.i�N(RRRtopenR RtO_CREATtO_WRONLYt _try_lockt
_lock_successtclose(Rtfd((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR�s"
cCs{ytj|tjtjB�WnVtk
rv}|jtjtjfkrptj d|j
�tjd��n�nXdS(s�
Try to acquire the lock file without blocking.
:param int fd: file descriptor of the opened file to lock
s(A lock on %s is held by another process.s/Another instance of
Certbot is already
running.N(
tfcntltlockftLOCK_EXtLOCK_NBtIOErrorterrnotEACCEStEAGAINtloggertdebugR Rt LockError(RR$terr((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR!�scCs�ddlm}m}y||j�}Wn,tk
rW}|jtjkrQtS�nX||�}|j|jko�|j |j kS(s�
Did we successfully grab the lock?
Because this class deletes the locked file when the lock is
released, it is possible another process removed and recreated
the file between us opening the file and acquiring the lock.
:param int fd: file descriptor of the opened file to lock
:returns: True if the lock was successfully acquired
:rtype: bool
i����(tstattfstat(
RR1R2R tOSErrorR*tENOENTtFalsetst_devtst_ino(RR$R1R2tstat1R0tstat2((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR"�scCs[ztj|j�Wd|jdkr5td��nztj|j�Wdd|_XXdS(s)Remove,
close, and release the lock file.NsError, self._fd is
None.(RtremoveR RRt TypeErrorR#(R((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR�s(RRRRR!R"R(((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR}s
RcBs eZdZd�Zd�ZRS(s"
A Windows lock file mechanism.
By default on Windows, acquiring a file handler gives exclusive access
to the process
and results in an effective lock. However, it is possible to explicitly
acquire the
file handler in shared access in terms of read and write, and this is
done by os.open
and io.open in Python. So an explicit lock needs to be done through the
call of
msvcrt.locking, that will lock the first byte of the file. In theory,
it is also
possible to access a file in shared delete access, allowing other
processes to delete an
opened file. But this needs also to be done explicitly by all processes
using the Windows
low level APIs, and Python does not do it. As of Python 3.7 and below,
Python developers
state that deleting a file opened by a process from another process is
not possible with
os.open and io.open.
Consequently, mscvrt.locking is sufficient to obtain an effective lock,
and the race
condition encountered on Linux is not possible on Windows, leading to a
simpler workflow.
cCs�tjtjBtjB}d}y2tj|j|d�}tj |tj
d�Wnittfk
r�}|r}tj
|�n|jtjkr��ntjd|j�tjd��nX||_dS(sAcquire
the locki�is(A lock on %s is held by another process.s/Another instance
of Certbot is already
running.N(RtO_RDWRRtO_TRUNCRRRR tmsvcrttlockingtLK_NBLCKR)R3R#R*R+R-R.RR/R(Rt open_modeR$R0((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR�scCs{zjtj|jtjd�tj|j�ytj|j�Wn&tk
rh}t j
t|��nXWdd|_XdS(sRelease the
lock.iN(
R>R?RtLK_UNLCKRR#R:R R3R-R.tstrR(Rte((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR�s(RRRRR(((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyR�s (RR*tloggingtacme.magic_typingRtcertbotRtcertbot.compatRRR%tImportErrorR>R5R
tTruet getLoggerRR-RtobjectRRRR(((s:/usr/lib/python2.7/site-packages/certbot/_internal/lock.pyt<module>s$
;X