o bZ@sdZddlZddlZddlmZddlmZddZddlmZe s3ddlm Z m Z m Z mZd Zn0d Zz ddlZdd lmZWn eyLdZ Yn wd Zd ZddZ eZddZddZ ddZ GdddZddZddgZdS)z& Filesystem-based interprocess mutex. N)time)platformcCstttdS)Ni)strint _uniquefloatrr9/usr/lib/python3/dist-packages/twisted/python/lockfile.pyuniquesr )rename)killreadlinkremovesymlinkFT) OpenProcessWc Csjz tdd|Wt dtjy4}z|jdtkr"WYd}~dS|jdtkr/ttjdd}~ww)Nrz OpenProcess is required to fail.) r pywintypeserrorargsERROR_ACCESS_DENIEDERROR_INVALID_PARAMETEROSErrorerrnoESRCH RuntimeError)pidsignalerrrr 4s r cCs|dtd}tj|d}t|d}t||}|||Wdn1s0wYzt||WdSt yOt |t |w)z Write a file at C{filename} with the contents of C{value}. See the above comment block as to why this is needed. .z.newlinkrwN) r ospathjoinmkdir_openwriteflushr BaseExceptionr rmdir)valuefilename newlinkname newvalnamemodefrrrrEs       rc Csz ttj|dd}Wnty+}z|jtjks |jtjkr&t|jdd}~ww| |}Wd|S1s>wY|S)zq Read the contents of C{filename}. See the above comment block as to why this is needed. rrN) r$r r!r"rrENOENTEIOread)r*fObjrresultrrrr `s   r cCs"ttj|dt|dS)Nr)r r r!r"r()r*rrrrmlinkrsr5c@s0eZdZdZdZdZddZddZdd ZdS) FilesystemLocka A mutex. This relies on the filesystem property that creating a symlink is an atomic operation and that it will fail if the symlink already exists. Deleting the symlink will release the lock. @ivar name: The name of the file associated with this lock. @ivar clean: Indicates whether this lock was released cleanly by its last owner. Only meaningful after C{lock} has been called and returns True. @ivar locked: Indicates whether the lock is currently held by this object. NFcCs ||_dS)N)name)selfr7rrr__init__s zFilesystemLock.__init__cCsd} z ttt|jWnty}ztr)|jtjtj fvr)WYd}~dS|jtj krzt |j}Wn2tyh}z&|jtj krOWYd}~WYd}~qtrc|jtjkrcWYd}~WYd}~dSd}~wwz t durut t|dWnKty}z?|jtjkrzt|jWn#ty}z|jtj krWYd}~WYd}~WYd}~qd}~wwd}WYd}~WYd}~qd}~wwWYd}~dSd}~wwd|_||_dS)z Acquire this lock. @rtype: C{bool} @return: True if the lock is acquired, false otherwise. @raise OSError: Any exception L{os.symlink()} may raise, other than L{errno.EEXIST}. TNFr)rrr getpidr7r_windowsrEACCESr1EEXISTr r0r rrr5lockedclean)r8r?rrrrrlocksX       +zFilesystemLock.lockcCs@t|j}t|tkrtd|jdt|jd|_dS)z Release this lock. This deletes the directory with the given name. @raise OSError: Any exception L{os.readlink()} may raise. @raise ValueError: If the lock is not owned by this process. zLock z not owned by this processFN)r r7rr r: ValueErrorr5r>)r8rrrrunlocks   zFilesystemLock.unlock) __name__ __module__ __qualname____doc__r?r>r9r@rBrrrrr6ws =r6cCs:t|}d}z|}W|r|| S|r|ww)z Determine if the lock of the given name is held or not. @type name: C{str} @param name: The filesystem path to the lock to test @rtype: C{bool} @return: True if the lock is held, False otherwise. N)r6r@rB)r7lr4rrrisLockeds   rH)rFrr rrtwisted.python.runtimerr r isWindowsr r r r5rr;rwin32apir ImportErrorrropenr$r6rH__all__rrrrs6      f