
bc        
   @   s  d  Z  d d d d d d d d d	 d
 g
 Z d d l Z d d l Z d d l Z d d l m	 Z
 y d d l m Z Wn! e k
 r d d l m Z n Xy d d l Z Wn e k
 r d   Z n
 Xd   Z y d d l Z Wn e k
 r d d l Z n Xe j Z e j e j Be j BZ e e d  r;e e j OZ n  e e d  rZe e j OZ n  e Z e e d  re e j OZ n  e e d  re j  Z  n d Z  d Z! e   Z" e e d  re j# Z$ n$ e e d  re j% Z$ n	 d   Z$ d   Z& d d. d     YZ' d   Z( d   Z) d a+ d   Z, d   Z- d    Z. d a/ d!   Z0 d" e! d e1 d#  Z2 d" e! d d$  Z3 d" e! d d%  Z4 d& d/ d'     YZ5 d( d d" e! d e6 d)  Z7 e j8 d* k se j9 j: d+ k re7 Z; n d( d d" e! d d,  Z; d d0 d-     YZ< d S(1   s,  Temporary files.

This module provides generic, low- and high-level interfaces for
creating temporary files and directories.  All of the interfaces
provided by this module can be used without fear of race conditions
except for 'mktemp'.  'mktemp' is subject to race conditions and
should not be used; it is provided for backward compatibility only.

This module also provides some data items to the user:

  TMP_MAX  - maximum number of names that will be tried before
             giving up.
  template - the default prefix for all temporary names.
             You may change this to control the default prefix.
  tempdir  - If this is set to a string before the first use of
             any routine from this module, it will be considered as
             another candidate location to store temporary files.
t   NamedTemporaryFilet   TemporaryFilet   SpooledTemporaryFilet   mkstempt   mkdtempt   mktempt   TMP_MAXt   gettempprefixt   tempdirt
   gettempdiriN(   t   Random(   t   StringIOc         C   s   d  S(   N(    (   t   fd(    (    s   /usr/lib/python2.7/tempfile.pyt   _set_cloexec-   s    c         C   sW   y t  j |  t  j d  } Wn t k
 r/ n$ X| t  j O} t  j |  t  j |  d  S(   Ni    (   t   _fcntlt   fcntlt   F_GETFDt   IOErrort
   FD_CLOEXECt   F_SETFD(   R   t   flags(    (    s   /usr/lib/python2.7/tempfile.pyR   0   s    t   O_NOINHERITt
   O_NOFOLLOWt   O_BINARYi'  t   tmpt   lstatt   statc         C   s;   y t  |   } Wn t k
 r, t j  n X| j   d  S(   N(   t   openR   t   _ost   errort   close(   t   fnt   f(    (    s   /usr/lib/python2.7/tempfile.pyt   _stat]   s
    c         C   s.   y t  |   Wn t j k
 r% t SXt Sd  S(   N(   R!   R   R   t   Falset   True(   R   (    (    s   /usr/lib/python2.7/tempfile.pyt   _existsd   s
    t   _RandomNameSequencec           B   sF   e  Z d  Z d d d Z d   Z e d    Z d   Z d   Z RS(   s*  An instance of _RandomNameSequence generates an endless
    sequence of unpredictable strings which can safely be incorporated
    into file names.  Each string is six characters long.  Multiple
    threads can safely use the same instance at the same time.

    _RandomNameSequence is an iterator.t   abcdefghijklmnopqrstuvwxyzt   ABCDEFGHIJKLMNOPQRSTUVWXYZt   0123456789_c         C   s   t    |  _ t j j |  _ d  S(   N(   t   _allocate_lockt   mutexR   t   patht   normcase(   t   self(    (    s   /usr/lib/python2.7/tempfile.pyt   __init__x   s    c         C   sC   t  j   } | t |  d d   k r< t   |  _ | |  _ n  |  j S(   Nt   _rng_pid(   R   t   getpidt   getattrt   Nonet   _Randomt   _rngR/   (   R-   t   cur_pid(    (    s   /usr/lib/python2.7/tempfile.pyt   rng|   s
    c         C   s   |  S(   N(    (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyt   __iter__   s    c         C   so   |  j  } |  j } |  j j } | j   z# g  d D] } | |  ^ q2 } Wd  | j   X|  j d j |   S(   Nt   123456t    (   R*   t
   charactersR6   t   choicet   acquiret   releaseR,   t   join(   R-   t   mt   ct   chooset   dummyt   letters(    (    s   /usr/lib/python2.7/tempfile.pyt   next   s    		
#(	   t   __name__t
   __module__t   __doc__R:   R.   t   propertyR6   R7   RD   (    (    (    s   /usr/lib/python2.7/tempfile.pyR%   l   s   		c          C   s   g  }  x3 d D]+ } t  j |  } | r |  j |  q q Wt  j d k rs t  j d  } | r |  j |  q nA t  j d k r |  j d d d	 d
 g  n |  j d d d g  y |  j t  j    Wn* t t  j f k
 r |  j t  j  n X|  S(   s[   Generate a list of candidate temporary directories which
    _get_default_tempdir will try.t   TMPDIRt   TEMPt   TMPt   riscoss   Wimp$ScrapDirt   nts   c:\temps   c:\tmps   \temps   \tmps   /tmps   /var/tmps   /usr/tmp(   RI   RJ   RK   (	   R   t   getenvt   appendt   namet   extendt   getcwdt   AttributeErrorR   t   curdir(   t   dirlistt   envnamet   dirname(    (    s   /usr/lib/python2.7/tempfile.pyt   _candidate_tempdir_list   s"      c    
      C   s  t    }  t   } t } xv| D]n} | t j k rU t j j t j j |   } n  x5t d  D]'} |  j	   } t j j
 | |  } yu t j | | d  } zG z2 t j | d d t  } | j d  Wd QXWd t j |  XWd t j |  X| SWqb t t f k
 r}	 |	 j d t j k r2qb n  t j d k r|	 j d t j k rt j j |  rt j | t j  rqb n  Pqb Xqb Wq Wt t j d	 | f  d S(
   sq  Calculate the default directory to use for temporary files.
    This routine should be called exactly once.

    We determine whether or not a candidate temp dir is usable by
    trying to create and write to a file in that directory.  If this
    is successful, the test file is deleted.  To prevent denial of
    service, the name of the test file must be randomized.id   i  t   wbt   closefdt   blatNi    RM   s)   No usable temporary directory found in %s(   R%   RX   t   _text_openflagsR   RT   R+   R,   t   abspatht   xrangeRD   R>   R   t   _ioR"   t   writeR   t   unlinkt   OSErrorR   t   argst   _errnot   EEXISTRP   t   EACCESt   isdirt   accesst   W_OKt   ENOENT(
   t   namerRU   R   t   dirt   seqRP   t   filenameR   t   fpt   e(    (    s   /usr/lib/python2.7/tempfile.pyt   _get_default_tempdir   s6    			!%'	c           C   sG   t  d k rC t j   z t  d k r1 t   a  n  Wd t j   Xn  t  S(   s7   Common setup sequence for all user-callable interfaces.N(   t   _name_sequenceR2   t
   _once_lockR<   R%   R=   (    (    (    s   /usr/lib/python2.7/tempfile.pyt   _get_candidate_names   s    
c   
      C   s  t    } x t t  D] } | j   } t j j |  | | |  } y9 t j | | d  } t |  | t j j	 |  f SWq t
 k
 r }	 |	 j t j k r q n  t j d k r |	 j t j k r t j j |   r t j |  t j  r q n    q Xq Wt t j d f  d S(   s>   Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.i  RM   s#   No usable temporary file name foundN(   Rt   R^   R   RD   R   R+   R>   R   R   R]   Rb   t   errnoRd   Re   RP   Rf   Rg   Rh   Ri   R   (
   Rl   t   pret   sufR   t   namesRm   RP   t   fileR   Rp   (    (    s   /usr/lib/python2.7/tempfile.pyt   _mkstemp_inner   s     	
!'c           C   s   t  S(   s   Accessor for tempdir.template.(   t   template(    (    (    s   /usr/lib/python2.7/tempfile.pyR     s    c           C   sG   t  d k rC t j   z t  d k r1 t   a  n  Wd t j   Xn  t  S(   s   Accessor for tempfile.tempdir.N(   R   R2   Rs   R<   Rq   R=   (    (    (    s   /usr/lib/python2.7/tempfile.pyR	     s    
R9   c         C   s@   | d k r t   } n  | r' t } n t } t | | |  |  S(   s'  User-callable function to create and return a unique temporary
    file.  The return value is a pair (fd, name) where fd is the
    file descriptor returned by os.open, and name is the filename.

    If 'suffix' is specified, the file name will end with that suffix,
    otherwise there will be no suffix.

    If 'prefix' is specified, the file name will begin with that prefix,
    otherwise a default prefix is used.

    If 'dir' is specified, the file will be created in that directory,
    otherwise a default directory is used.

    If 'text' is specified and true, the file is opened in text
    mode.  Else (the default) the file is opened in binary mode.  On
    some operating systems, this makes no difference.

    The file is readable and writable only by the creating user ID.
    If the operating system uses permission bits to indicate whether a
    file is executable, the file is executable by no one. The file
    descriptor is not inherited by children of this process.

    Caller is responsible for deleting the file when done with it.
    N(   R2   R	   R\   t   _bin_openflagsRz   (   t   suffixt   prefixRl   t   textR   (    (    s   /usr/lib/python2.7/tempfile.pyR     s    	c         C   s  | d k r t   } n  t   } x t t  D] } | j   } t j j | | | |   } y t j	 | d  | SWq. t
 k
 r } | j t j k r q. n  t j d k r | j t j k r t j j |  r t j | t j  r q. n    q. Xq. Wt t j d f  d S(   s  User-callable function to create and return a unique temporary
    directory.  The return value is the pathname of the directory.

    Arguments are as for mkstemp, except that the 'text' argument is
    not accepted.

    The directory is readable, writable, and searchable only by the
    creating user.

    Caller is responsible for deleting the directory when done with it.
    i  RM   s(   No usable temporary directory name foundN(   R2   R	   Rt   R^   R   RD   R   R+   R>   t   mkdirRb   Ru   Rd   Re   RP   Rf   Rg   Rh   Ri   R   (   R}   R~   Rl   Rx   Rm   RP   Ry   Rp   (    (    s   /usr/lib/python2.7/tempfile.pyR   =  s"    	!'c         C   s   | d k r t   } n  t   } xM t t  D]? } | j   } t j j | | | |   } t	 |  s. | Sq. Wt
 t j d f  d S(   s  User-callable function to return a unique temporary file name.  The
    file is not created.

    Arguments are as for mkstemp, except that the 'text' argument is
    not accepted.

    This function is unsafe and should not be used.  The file name
    refers to a file that did not exist at some point, but by the time
    you get around to creating it, someone else may have beaten you to
    the punch.
    s"   No usable temporary filename foundN(   R2   R	   Rt   R^   R   RD   R   R+   R>   R$   R   Rd   Re   (   R}   R~   Rl   Rx   Rm   RP   Ry   (    (    s   /usr/lib/python2.7/tempfile.pyR   a  s    	t   _TemporaryFileWrapperc           B   sk   e  Z d  Z e d  Z d   Z d   Z e j d k r` e j	 Z	 d   Z
 d   Z d   Z n	 d   Z RS(	   s   Temporary file wrapper

    This class provides a wrapper around files opened for
    temporary use.  In particular, it seeks to automatically
    remove the file when it is no longer needed.
    c         C   s(   | |  _  | |  _ t |  _ | |  _ d  S(   N(   Ry   RP   R"   t   close_calledt   delete(   R-   Ry   RP   R   (    (    s   /usr/lib/python2.7/tempfile.pyR.     s    			c         C   sN   |  j  d } t | |  } t t |  t d   sJ t |  | |  n  | S(   NRy   i    (   t   __dict__R1   t
   issubclasst   typet   setattr(   R-   RP   Ry   t   a(    (    s   /usr/lib/python2.7/tempfile.pyt   __getattr__  s
    c         C   s   |  j  j   |  S(   N(   Ry   t	   __enter__(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR     s    RM   c         C   sJ   |  j  sF t |  _  z |  j j   Wd  |  j rB |  j |  j  n  Xn  d  S(   N(   R   R#   Ry   R   R   Ra   RP   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR     s    			c         C   s   |  j    d  S(   N(   R   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyt   __del__  s    c         C   s&   |  j  j | | |  } |  j   | S(   N(   Ry   t   __exit__R   (   R-   t   exct   valuet   tbt   result(    (    s   /usr/lib/python2.7/tempfile.pyR     s    
c         C   s   |  j  j | | |  d  S(   N(   Ry   R   (   R-   R   R   R   (    (    s   /usr/lib/python2.7/tempfile.pyR     s    (   RE   RF   RG   R#   R.   R   R   R   RP   Ra   R   R   R   (    (    (    s   /usr/lib/python2.7/tempfile.pyR     s   						s   w+bc   
      C   s   | d k r t   } n  d |  k r- t } n t } t j d k rX | rX | t j O} n  t | | | |  \ } } y) t j | |  |  }	 t	 |	 | |  SWn. t
 k
 r t j |  t j |    n Xd S(   sA  Create and return a temporary file.
    Arguments:
    'prefix', 'suffix', 'dir' -- as for mkstemp.
    'mode' -- the mode argument to os.fdopen (default "w+b").
    'bufsize' -- the buffer size argument to os.fdopen (default -1).
    'delete' -- whether the file is deleted on close (default True).
    The file is created as mkstemp() would do it.

    Returns an object with a file-like interface; the name of the file
    is accessible as its 'name' attribute.  The file will be automatically
    deleted when it is closed unless the 'delete' argument is set to False.
    t   bRM   N(   R2   R	   R|   R\   R   RP   t   O_TEMPORARYRz   t   fdopenR   t   BaseExceptionRa   R   (
   t   modet   bufsizeR}   R~   Rl   R   R   R   RP   Ry   (    (    s   /usr/lib/python2.7/tempfile.pyR      s    	t   posixt   cygwinc         C   s   | d k r t   } n  d |  k r- t } n t } t | | | |  \ } } y$ t j |  t j | |  |  SWn t j |    n Xd S(   s  Create and return a temporary file.
        Arguments:
        'prefix', 'suffix', 'dir' -- as for mkstemp.
        'mode' -- the mode argument to os.fdopen (default "w+b").
        'bufsize' -- the buffer size argument to os.fdopen (default -1).
        The file is created as mkstemp() would do it.

        Returns an object with a file-like interface.  The file has no
        name, and will cease to exist when it is closed.
        R   N(	   R2   R	   R|   R\   Rz   R   Ra   R   R   (   R   R   R}   R~   Rl   R   R   RP   (    (    s   /usr/lib/python2.7/tempfile.pyR     s    	c           B   s  e  Z d  Z e Z d d d d e d d  Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z e d    Z d   Z d   Z d   Z e d    Z e d    Z d   Z d   Z d   Z d   Z d   Z e d    Z d   Z d   Z d   Z d   Z d   Z RS(   s   Temporary file wrapper, specialized to switch from
    StringIO to a real file when it exceeds a certain size or
    when a fileno is needed.
    i    s   w+biR9   c         C   s:   t    |  _ | |  _ t |  _ | | | | | f |  _ d  S(   N(   t	   _StringIOt   _filet	   _max_sizeR"   t   _rolledt   _TemporaryFileArgs(   R-   t   max_sizeR   R   R}   R~   Rl   (    (    s   /usr/lib/python2.7/tempfile.pyR.     s    		c         C   s?   |  j  r d  S|  j } | r; | j   | k r; |  j   n  d  S(   N(   R   R   t   tellt   rollover(   R-   Ry   R   (    (    s   /usr/lib/python2.7/tempfile.pyt   _check  s
    	 	c         C   sh   |  j  r d  S|  j } t |  j   } |  _ |  ` | j | j    | j | j   d  t |  _  d  S(   Ni    (	   R   R   R   R   R`   t   getvaluet   seekR   R#   (   R-   Ry   t   newfile(    (    s   /usr/lib/python2.7/tempfile.pyR     s    	 	c         C   s   |  j  j r t d   n  |  S(   Ns%   Cannot enter context with closed file(   R   t   closedt
   ValueError(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   ,  s    c         C   s   |  j  j   d  S(   N(   R   R   (   R-   R   R   R   (    (    s   /usr/lib/python2.7/tempfile.pyR   1  s    c         C   s   |  j  j   S(   N(   R   R7   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR7   5  s    c         C   s   |  j  j   d  S(   N(   R   R   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   8  s    c         C   s
   |  j  j S(   N(   R   R   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   ;  s    c         C   s   |  j    |  j j   S(   N(   R   R   t   fileno(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   ?  s    
c         C   s   |  j  j   d  S(   N(   R   t   flush(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   C  s    c         C   s   |  j  j   S(   N(   R   t   isatty(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   F  s    c         C   s.   y |  j  j SWn t k
 r) |  j d SXd  S(   Ni    (   R   R   RS   R   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   I  s    c         C   s'   y |  j  j SWn t k
 r" d  SXd  S(   N(   R   RP   RS   R2   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyRP   P  s    c         C   s
   |  j  j S(   N(   R   RD   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyRD   W  s    c         G   s   |  j  j |   S(   N(   R   t   read(   R-   Rc   (    (    s   /usr/lib/python2.7/tempfile.pyR   Z  s    c         G   s   |  j  j |   S(   N(   R   t   readline(   R-   Rc   (    (    s   /usr/lib/python2.7/tempfile.pyR   ]  s    c         G   s   |  j  j |   S(   N(   R   t	   readlines(   R-   Rc   (    (    s   /usr/lib/python2.7/tempfile.pyR   `  s    c         G   s   |  j  j |   d  S(   N(   R   R   (   R-   Rc   (    (    s   /usr/lib/python2.7/tempfile.pyR   c  s    c         C   s
   |  j  j S(   N(   R   t	   softspace(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   f  s    c         C   s   |  j  j   S(   N(   R   R   (   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   j  s    c         C   s   |  j  j   d  S(   N(   R   t   truncate(   R-   (    (    s   /usr/lib/python2.7/tempfile.pyR   m  s    c         C   s)   |  j  } | j |  } |  j |  | S(   N(   R   R`   R   (   R-   t   sRy   t   rv(    (    s   /usr/lib/python2.7/tempfile.pyR`   p  s    	c         C   s)   |  j  } | j |  } |  j |  | S(   N(   R   t
   writelinesR   (   R-   t   iterableRy   R   (    (    s   /usr/lib/python2.7/tempfile.pyR   v  s    	c         G   s9   t  |  j d  r t |  j  St |  j j |    Sd  S(   Nt
   xreadlines(   t   hasattrR   t   iterR   (   R-   Rc   (    (    s   /usr/lib/python2.7/tempfile.pyR   |  s    N(    RE   RF   RG   R"   R   R{   R2   R.   R   R   R   R   R7   R   RH   R   R   R   R   R   RP   RD   R   R   R   R   R   R   R   R`   R   R   (    (    (    s   /usr/lib/python2.7/tempfile.pyR     s6   																			(    (    (    (=   RG   t   __all__t   ioR_   t   osR   Ru   Rd   t   randomR
   R3   t	   cStringIOR   R   t   ImportErrorR   R   R   t   threadt   _threadt   dummy_threadt   allocate_lockR)   t   O_RDWRt   O_CREATt   O_EXCLR\   R   R   R   R|   R   R   R{   Rs   R   R!   R   R$   R%   RX   Rq   R2   Rr   Rt   Rz   R   R   R	   R"   R   R   R   R   R#   R    RP   t   syst   platformR   R   (    (    (    s   /usr/lib/python2.7/tempfile.pyt   <module>   s~   					(		+				%$@	$!		