3 �L�j�}�@s�dZddddddddd d d d d g ZddlZddlZddlZddlZ ddl Z ddl Z ddlmZddlmZddlZy ddlZWnek r�ddlZYnXejZe je jBe jBZee d�r�ee j OZeZ!ee d�r�e!e j"OZ!ee d�r�e j#Z#ndZ#dZ$e�Z%ee d��re j&Z'nee d��r(e jZ'ndd�Z'dd�Z(dd�Z)dd�Z*Gd d!�d!�Z+d"d#�Z,d$d%�Z-da.d&d'�Z/d(d)�Z0d*d+�Z1d,d-�Z2d.d/�Z3e j4e j5e jhe j6k�o�e j4e j7kZ8d0d1�Z9ye j:e j;Be j BZd4d �Z?d5d �Z@daAd6d �ZBd7d �ZCdKd9d�ZDdLd:d�ZEd;e$dfd�d>�ZGGd?d@�d@�ZHdNdDd�ZIe jJdEk�s�e jKjLdFk�r�eIZMnee dG�aNdPdHd�ZMGdId�d�ZOGdJd�deP�ZQdS)Qa�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. The default path names are returned as str. If you supply bytes as input, all return values will be in bytes. Ex: >>> tempfile.mkstemp() (4, '/tmp/tmptpu9nin8') >>> tempfile.mkdtemp(suffix=b'') b'/tmp/tmppbi8f0hy' This module also provides some data items to the user: TMP_MAX - maximum number of names that will be tried before giving up. 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. �NamedTemporaryFile� TemporaryFile�SpooledTemporaryFile�TemporaryDirectory�mkstemp�mkdtemp�mktemp�TMP_MAX� gettempprefix�tempdir� gettempdir�gettempprefixb� gettempdirb�N)�Random)�S_ISLNK� O_NOFOLLOW�O_BINARYi'Ztmp�lstat�statcCstj|tj�}tj|�dS)N)�_os�open�O_RDONLY�close)�fn�fd�r� /usr/lib64/python3.6/tempfile.py�_statUsrc Cs*y t|�Wntk r dSXdSdS)NFT)r�OSError)rrrr�_existsYs  rcGs`d}xJ|D]B}|dkrq t|t�r8|tkr2td��t}q |tkrHtd��t}q W|dkr\tS|S)zBLook at the type of all args and divine their implied return type.Nz1Can't mix bytes and non-bytes in path components.)� isinstance�bytes�str� TypeError)�argsZ return_type�argrrr�_infer_return_typebs  r&cCsdt|||�}|dkr|�}|dkr:|tkr0t}n tjt�}|dkrX|tkrRt�}nt�}||||fS)z9Common parameter processing for most APIs in this module.N)r&r"�templater�fsencoder r )�prefix�suffix�dir� output_typerrr�_sanitize_paramsws  r-c@s0eZdZdZdZedd��Zdd�Zdd�Zd S) �_RandomNameSequencea,An instance of _RandomNameSequence generates an endless sequence of unpredictable strings which can safely be incorporated into file names. Each string is eight characters long. Multiple threads can safely use the same instance at the same time. _RandomNameSequence is an iterator.Z%abcdefghijklmnopqrstuvwxyz0123456789_cCs,tj�}|t|dd�kr&t�|_||_|jS)N�_rng_pid)r�getpid�getattr�_RandomZ_rngr/)�selfZcur_pidrrr�rng�s z_RandomNameSequence.rngcCs|S)Nr)r3rrr�__iter__�sz_RandomNameSequence.__iter__cs0|j�|jj���fdd�td�D�}dj|�S)Ncsg|] }����qSrr)�.0Zdummy)�c�chooserr� �sz0_RandomNameSequence.__next__..��)� charactersr4Zchoice�range�join)r3Zlettersr)r7r8r�__next__�sz_RandomNameSequence.__next__N) �__name__� __module__� __qualname__�__doc__r<�propertyr4r5r?rrrrr.�s  r.c Cs�g}x$dD]}tj|�}|r |j|�q Wtjdkr\|jtjjd�tjjd�ddd d g�n|jd d d g�y|jtj��Wn$t t fk r�|jtj �YnX|S)z[Generate a list of candidate temporary directories which _get_default_tempdir will try.�TMPDIR�TEMP�TMP�ntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmpz/tmpz/var/tmpz/usr/tmp)rErFrG) r�getenv�append�name�extend�path� expanduser� expandvars�getcwd�AttributeErrorr�curdir)�dirlistZenvname�dirnamerrr�_candidate_tempdir_list�s    rUcCs4t�}t�}�x|D�]}|tjkr0tjj|�}x�td�D]�}t|�}tjj||�}y\tj |t d�}zr�_bin_openflags�_io�writer�unlink�FileExistsError�PermissionErrorrK�isdir�access�W_OKr�FileNotFoundError�_errno�ENOENT)ZnamerrSr+�seqrK�filenamer�fprrr�_get_default_tempdir�s:    rjc Cs2tdkr.tj�ztdkr t�aWdtj�XtS)z7Common setup sequence for all user-callable interfaces.N)�_name_sequence� _once_lock�acquirer.�releaserrrr�_get_candidate_names�s  roc Cs�t�}|tkrttj|�}x�tt�D]�}t|�}tjj ||||�}ytj ||d�} WnRt k rnw$Yn>t k r�tj dkr�tjj|�r�tj|tj�r�w$n�YnX| tjj|�fSWt tjd��dS)z>Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.i�rHz#No usable temporary file name foundN)ror!�maprr(r=rrZrMr>rr_r`rKrarbrcrYre�EEXIST) r+ZpreZsuf�flagsr,�namesrgrK�filerrrr�_mkstemp_inner�s$ rucGsJ|tjkr ||f|�ddi�n&tjdks8tjj|� rF||f|��dS)N�follow_symlinksFrH)r�supports_follow_symlinksrKrM�islink)�funcrMr$rrr�_dont_follow_symlinkss rzc Cs0y tj}Wntk rYnXt||d�dS)Nr)r�chflagsrQrz)rMr{rrr� _resetflags s  r|cCst|�ttj|d�dS)Ni�)r|rzr�chmod)rMrrr� _resetperms(sr~cCs,|dkrt|�dSt|�tj|d�dS)Ni�)r~r|rr})�dir_fdrMrrr�_resetperms_fd3s r�cCs�|dkrt|�dSt|�tjtjkrt k r�tjdkr�tj j|�r�tj|tj�r�w8n�YnX|SWt tjd��dS)aUser-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�rHz(No usable temporary directory name foundN)r-ror!rprr(r=rrZrMr>�mkdirr_r`rKrarbrcrerq)r*r)r+r,rsrgrKrtrrrr�s&  r;cCs`|dkrt�}t�}x:tt�D].}t|�}tjj||||�}t|�s|SqWt t j d��dS)a�User-callable function to return a unique temporary file name. The file is not created. Arguments are similar to mkstemp, except that the 'text' argument is not accepted, and suffix=None, prefix=None and bytes file names are not supported. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may refer 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. Nz"No usable temporary filename found) r ror=rrZrrMr>rr_rerq)r*r)r+rsrgrKrtrrrr�sc@sLeZdZdZdZdZd dd�Zejdkr@ej fdd �Z d d �Z nd d �Z dS)�_TemporaryFileCloserz�A separate object allowing proper closing of a temporary file's underlying file object, without adding a __del__ method to the temporary file.NFTcCs||_||_||_dS)N)rtrK�delete)r3rtrKr�rrr�__init__�sz_TemporaryFileCloser.__init__rHc Cs>|j r:|jdk r:d|_z|jj�Wd|jr8||j�XdS)NT)� close_calledrtrr�rK)r3r^rrrrs z_TemporaryFileCloser.closecCs |j�dS)N)r)r3rrr�__del__ sz_TemporaryFileCloser.__del__cCs|jsd|_|jj�dS)NT)r�rtr)r3rrrrs)T) r@rArBrCrtr�r�rrKr^rr�rrrrr��s   r�c@sBeZdZdZddd�Zdd�Zdd�Zd d �Zd d �Zd d�Z dS)�_TemporaryFileWrapperz�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. TcCs$||_||_||_t|||�|_dS)N)rtrKr�r��_closer)r3rtrKr�rrrr�sz_TemporaryFileWrapper.__init__cs^|jd}t||�}t|d�rD|�tj���fdd��}|j|_|}t|t�sZt|||�|S)Nrt�__call__cs �||�S)Nr)r$�kwargs)ryrr� func_wrapper+sz7_TemporaryFileWrapper.__getattr__..func_wrapper) �__dict__r1�hasattr� _functools�wrapsr�r �int�setattr)r3rKrt�ar�r)ryr� __getattr__#s     z!_TemporaryFileWrapper.__getattr__cCs|jj�|S)N)rt� __enter__)r3rrrr�8s z_TemporaryFileWrapper.__enter__cCs|jj|||�}|j�|S)N)rt�__exit__r)r3�exc�value�tb�resultrrrr�>sz_TemporaryFileWrapper.__exit__cCs|jj�dS)zA Close the temporary file, possibly deleting it. N)r�r)r3rrrrCsz_TemporaryFileWrapper.closeccsx|jD] }|VqWdS)N)rt)r3�linerrrr5Js z_TemporaryFileWrapper.__iter__N)T) r@rArBrCr�r�r�r�rr5rrrrr�s r��w+b�Tc Cs�t|||�\}}}}t} tjdkr0|r0| tjO} t|||| |�\} } y tj| ||||d�} t| | |�St k r�tj | �tj | ��YnXdS)a�Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) '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. rH)� buffering�newline�encodingN) r-r[rrKZ O_TEMPORARYrur\rr�� BaseExceptionr^r) �moder�r�r�r*r)r+r�r,rrrrKrtrrrrTs      �posix�cygwin� O_TMPFILEc Cs�t|||�\}}}}t}tr�y$|tjBtj@} tj|| d�} Wn*tk rXdaYnBtk rjYn0Xyt j| ||||d�Stj | ��YnXt |||||�\} } ytj | �t j| ||||d�Stj | ��YnXdS)a>Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 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. i�F)r�r�r�N) r-r[�_O_TMPFILE_WORKSrr��O_CREATr�IsADirectoryErrorrr\rrur^) r�r�r�r�r*r)r+r,rrZflags2rrKrrrr�s0       c@s�eZdZdZdZd8dd�Zd d �Zd d �Zd d�Zdd�Z dd�Z dd�Z e dd��Z e dd��Zdd�Zdd�Zdd�Ze dd ��Ze d!d"��Ze d#d$��Zd%d&�Zd'd(�Zd)d*�Zd+d,�Ze d-d.��Zd/d0�Zd9d1d2�Zd3d4�Zd5d6�ZdS):rz�Temporary file wrapper, specialized to switch from BytesIO or StringIO to a real file when it exceeds a certain size or when a fileno is needed. Fr�w+br�Nc CsHd|krtj�|_ntjdd�|_||_d|_|||||||d�|_dS)N�b� )r�F)r�r�r*r)r�r�r+)r\�BytesIO�_file�StringIO� _max_size�_rolled�_TemporaryFileArgs) r3�max_sizer�r�r�r�r*r)r+rrrr��s zSpooledTemporaryFile.__init__cCs,|jr dS|j}|r(|j�|kr(|j�dS)N)r�r��tell�rollover)r3rtr�rrr�_check�s zSpooledTemporaryFile._checkcCsN|jr dS|j}tf|j�}|_|`|j|j��|j|j�d�d|_dS)NrT)r�r�rr�r]�getvalue�seekr�)r3rtZnewfilerrrr��szSpooledTemporaryFile.rollovercCs|jjrtd��|S)Nz%Cannot enter context with closed file)r��closed� ValueError)r3rrrr��szSpooledTemporaryFile.__enter__cCs|jj�dS)N)r�r)r3r�r�r�rrrr��szSpooledTemporaryFile.__exit__cCs |jj�S)N)r�r5)r3rrrr5�szSpooledTemporaryFile.__iter__cCs|jj�dS)N)r�r)r3rrrr�szSpooledTemporaryFile.closecCs|jjS)N)r�r�)r3rrrr��szSpooledTemporaryFile.closedc Cs8y|jjStk r2d|jdkr(�|jdSXdS)Nr�r�r�)r�r�rQr�)r3rrrr�s zSpooledTemporaryFile.encodingcCs|j�|jj�S)N)r�r��fileno)r3rrrr� szSpooledTemporaryFile.filenocCs|jj�dS)N)r��flush)r3rrrr�szSpooledTemporaryFile.flushcCs |jj�S)N)r��isatty)r3rrrr�szSpooledTemporaryFile.isattyc Cs(y|jjStk r"|jdSXdS)Nr�)r�r�rQr�)r3rrrr�szSpooledTemporaryFile.modec Cs"y|jjStk rdSXdS)N)r�rKrQ)r3rrrrKszSpooledTemporaryFile.namec Cs8y|jjStk r2d|jdkr(�|jdSXdS)Nr�r�r�)r��newlinesrQr�)r3rrrr�"s zSpooledTemporaryFile.newlinescGs |jj|�S)N)r��read)r3r$rrrr�+szSpooledTemporaryFile.readcGs |jj|�S)N)r��readline)r3r$rrrr�.szSpooledTemporaryFile.readlinecGs |jj|�S)N)r�� readlines)r3r$rrrr�1szSpooledTemporaryFile.readlinescGs|jj|�dS)N)r�r�)r3r$rrrr�4szSpooledTemporaryFile.seekcCs|jjS)N)r�� softspace)r3rrrr�7szSpooledTemporaryFile.softspacecCs |jj�S)N)r�r�)r3rrrr�;szSpooledTemporaryFile.tellcCs6|dkr|jj�n||jkr&|j�|jj|�dS)N)r��truncater�r�)r3�sizerrrr�>s   zSpooledTemporaryFile.truncatecCs|j}|j|�}|j|�|S)N)r�r]r�)r3�srt�rvrrrr]Fs  zSpooledTemporaryFile.writecCs|j}|j|�}|j|�|S)N)r�� writelinesr�)r3�iterablertr�rrrr�Ls  zSpooledTemporaryFile.writelines�����)rr�r�NNNNN)N)r@rArBrCr�r�r�r�r�r�r5rrDr�r�r�r�r�r�rKr�r�r�r�r�r�r�r�r]r�rrrrr�s8        c@sTeZdZdZddd�Zeddd��Zedd ��Zd d �Zd d �Z dd�Z dd�Z dS)ra+Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example: with TemporaryDirectory() as tmpdir: ... Upon exiting the context, the directory and everything contained in it are removed. NcCs0t|||�|_tj||j|jdj|�d�|_dS)NzImplicitly cleaning up {!r})� warn_message)rrK�_weakref�finalize�_cleanup�format� _finalizer)r3r*r)r+rrrr�_s zTemporaryDirectory.__init__Fcs8�dkr ��d�����fdd� }tj�||dd�dS)Ncsjt|dt��rL�r&|�kr&�r$dS��|t��d�}|dksHt rT|d}}n|dkrb|}n|}y�|�kr�t|tjj|��t|||�ytj ||d�Wnnt k r��j |�|�k||d�YnDtk �rtjj |�s�r�dS��j |�|�k||d�YnXWn6t k �r(Yn"tk �rH��rBdS�YnXnt|dt ��r^n��sf�dS)Nr)r)� ignore_errors�repeatedr�fullname)� issubclassr`�len�_rmtree_use_dir_fdr�rrMrTr�r^r��_rmtreerardr)ryrM�exc_infoZ entry_namerZfullpath�base)�clsr�r�rKr�rr�onerrorksN     z+TemporaryDirectory._rmtree..onerrorT)r�rZ _onexc_kwargs)NN)�_shutilZrmtree)r�rKr�r�rr�r�r)r�r�r�rKr�rr�es ; zTemporaryDirectory._rmtreecCs|j|�tj|t�dS)N)r�� _warnings�warn�ResourceWarning)r�rKr�rrrr��s zTemporaryDirectory._cleanupcCsdj|jj|j�S)Nz <{} {!r}>)r�� __class__r@rK)r3rrr�__repr__�szTemporaryDirectory.__repr__cCs|jS)N)rK)r3rrrr��szTemporaryDirectory.__enter__cCs |j�dS)N)�cleanup)r3r�r�r�rrrr��szTemporaryDirectory.__exit__cCs|jj�r|j|j�dS)N)r��detachr�rK)r3rrrr��s zTemporaryDirectory.cleanup)NNN)FFNN) r@rArBrCr�� classmethodr�r�r�r�r�r�rrrrrSs   B )NNNF)NNNr�)r�r�NNNNNTr�)r�r�NNNNN)RrC�__all__� functoolsr��warningsr��ior\�osrZshutilr��errnoreZrandomrr2rrr��weakrefr��_thread� ImportErrorZ _dummy_thread� allocate_lockZ_allocate_lock�O_RDWRr��O_EXCLr�r�rr[rrr'rlrrrr&r-r.rUrjrkrorurzr|r~r}r^�supports_dir_fd� supports_fdr�r�r� O_NONBLOCKr�rQr�r r r r r rrrr�r�rrK�sys�platformrr�r�objectrrrrr�s�           -  !  ( ' +? $  8