ó
®â0_c           @   s  d  Z  d d l Z d Z d „  Z e j d	 k r—d d l Z d d
 l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z d d l m Z m Z m Z d Z d Z d Z e e ƒ e e	 ƒ k rË e
 Z n e Z e	 Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e ƒ Z e j j Z e e _ e e e e e e g e _  e j j! Z! e e! _ e e e e e g e! _  d „  Z" d „  Z# nw y+ d d l$ Z$ e$ j Z e$ j Z e$ j Z Wn7 e% e& f k
 rûd Z Z Z d „  Z" d „  Z# n Xd „  Z" d „  Z# d S(   su  
Portable file locking utilities.

Based partially on an example by Jonathan Feignberg in the Python
Cookbook [1] (licensed under the Python Software License) and a ctypes port by
Anatoly Techtonik for Roundup [2] (license [3]).

[1] http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
[2] http://sourceforge.net/p/roundup/code/ci/default/tree/roundup/backends/portalocker.py
[3] http://sourceforge.net/p/roundup/code/ci/default/tree/COPYING.txt

Example Usage::

    >>> from django.core.files import locks
    >>> with open('./file', 'wb') as f:
    ...     locks.lock(f, locks.LOCK_EX)
    ...     f.write('Django')
iÿÿÿÿNt   LOCK_EXt   LOCK_SHt   LOCK_NBt   lockt   unlockc         C   s   t  |  d ƒ r |  j ƒ  S|  S(   sC   Get a filedescriptor from something which could be a file or an fd.t   fileno(   t   hasattrR   (   t   f(    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyt   _fd   s    t   nt(	   t   sizeoft   c_ulongt   c_void_pt   c_int64t	   Structuret   Uniont   POINTERt   windllt   byref(   t   BOOLt   DWORDt   HANDLEi    i   i   t   _OFFSETc           B   s    e  Z d  e f d e f g Z RS(   t   Offsett
   OffsetHigh(   t   __name__t
   __module__R   t   _fields_(    (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   0   s   	t   _OFFSET_UNIONc           B   s)   e  Z d  g Z d  e f d e f g Z RS(   t   _offsett   Pointer(   R   R   t   _anonymous_R   t   PVOIDR   (    (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   5   s   		t
   OVERLAPPEDc           B   s;   e  Z d  g Z d e f d e f d  e f d e f g Z RS(   t   _offset_uniont   Internalt   InternalHight   hEvent(   R   R   R   t	   ULONG_PTRR   R   R   (    (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR!   ;   s
   				c         C   sI   t  j t |  ƒ ƒ } t ƒ  } t | | d d d t | ƒ ƒ } t | ƒ S(   Ni    I  ÿÿ    (   t   msvcrtt   get_osfhandleR   R!   t
   LockFileExR   t   bool(   R   t   flagst   hfilet
   overlappedt   ret(    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   M   s    	!c         C   sF   t  j t |  ƒ ƒ } t ƒ  } t | d d d t | ƒ ƒ } t | ƒ S(   Ni    I  ÿÿ    (   R'   R(   R   R!   t   UnlockFileExR   R*   (   R   R,   R-   R.   (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   S   s    	c         C   s   t  S(   N(   t   False(   R   R+   (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   c   s    c         C   s   t  S(   N(   t   True(   R   (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   g   s    c         C   s"   t  j t |  ƒ | ƒ } | d k S(   Ni    (   t   fcntlt   flockR   (   R   R+   R.   (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   k   s    c         C   s%   t  j t |  ƒ t  j ƒ } | d k S(   Ni    (   R2   R3   R   t   LOCK_UN(   R   R.   (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyR   o   s    (   R    R   R   R   R   ('   t   __doc__t   ost   __all__R   t   nameR'   t   ctypesR
   R   R   R   R   R   R   R   R   t   ctypes.wintypesR   R   R   R   R   R    R&   R    R   R   R!   t   LPOVERLAPPEDt   kernel32R)   t   restypet   argtypesR/   R   R   R2   t   ImportErrort   AttributeError(    (    (    s9   /tmp/pip-unpacked-wheel-BAJOf3/django/core/files/locks.pyt   <module>   sJ   	@								