U kg @sdZddddddddd d d d d g ZddlZddlZddlZddlZ zddl Z e j Z Wnpek rddlZddlZddZddZe je je je jhe jkoe je jkoe je jkZdGddZ YnXddlZddlm Z!ddlZddl"Z#ddl$Z$e$j%Z&e j'e j(Be j)BZ*e+e dr e*e j,OZ*e*Z-e+e dr:e-e j.OZ-e+e drNe j/Z/ndZ/dZ0e&Z1ddZ2ddZ3d d!Z4Gd"d#d#Z5d$d%Z6d&d'Z7da8d(d)Z9d*d+Z:d,d-Z;d.d/Zda?d2d Z@d3d ZAdHd4dZBdId5dZCd6e0dfd7dZDGd8d9d9ZEGd:d;d;ZFdJdd?d@dZGe jHdAksBejIdBkrHeGZJne+e dCaKdKdd?dDdZJGdEddZLGdFddeMZNdS)LaTemporary 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 TemporaryFileSpooledTemporaryFileTemporaryDirectorymkstempmkdtempmktempTMP_MAX gettempprefixtempdir gettempdirgettempprefixb gettempdirbNc CsJztj|rtdWn*tk rB|tjj|tYdSXg}zt|}Wn&tk r||tj|tYnX|D]}tj||}zt|j }Wntk rd}YnXt |rt ||qzt |Wqtk r |tj |tYqXqzt|Wn(tk rD|tj|tYnXdS)N%Cannot call rmtree on a symbolic linkr)_ospathislinkOSError_sysexc_infolistdirjoinlstatst_mode_statS_ISDIR_rmtree_unsafeunlinkrmdir)ronerrornamesnamefullnamemoder$/usr/lib/python3.8/tempfile.pyr2s6     rc Csg}zt|}Wn:tk rL}z||_|tj|tW5d}~XYnX|D]x}tj||}ztj||dd}|j }Wntk rd}YnXt |rztj |tj |d} Wn&tk r|tj |tYnXztj|t| rFt| ||ztj||dWn(tk rB|tj|tYnXn8z tdWn*tk r||tjj|tYnXW5t| XqRztj||dWqRtk r|tj|tYqRXqRdS)NF)dir_fdfollow_symlinksr)r&r)rrrfilenamerrrrstatrrropenO_RDONLYclosesamestatfstat_rmtree_safe_fdrrr) topfdrrr errr!r"orig_str#dirfdr$r$r%r/SsD$      r/Fc Csb|rdd}n|dkrdd}trTt|tr8t|}zt|}Wn(tk rn|tj|tYdSXzt |tj }Wn(tk r|tj|tYdSXztj |t|r t|||zt|Wn(tk r|tj|tYnXn8z tdWn*tk r@|tj j|tYnXW5t |Xn t||SdS)aRecursively delete a directory tree. If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is platform and implementation dependent; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised. cWsdSNr$argsr$r$r%rsz_rmtree..onerrorNcWsdSr4r$r5r$r$r%rsr)_use_fd_functions isinstancebytesrfsdecoder Exceptionrrr*r+r,rr-r.r/rrrr)r ignore_errorsrr2fdr$r$r%_rmtrees<      r>)Random O_NOFOLLOWO_BINARYi'ZtmpcCs.zt|Wntk r$YdSXdSdS)NFT)rrr)fnr$r$r%_existss rCcGs\d}|D]B}|dkrqt|tr6|tkr0tdt}q|tkrFtdt}q|dkrXtS|S)zBLook at the type of all args and divine their implied return type.Nz1Can't mix bytes and non-bytes in path components.)r8r9str TypeError)r6Z return_typeargr$r$r%_infer_return_types rGcCsdt|||}|dkr|}|dkr:|tkr0t}n tt}|dkrX|tkrRt}nt}||||fS)z9Common parameter processing for most APIs in this module.N)rGrDtemplaterfsencoder r )prefixsuffixdir output_typer$r$r%_sanitize_paramss  rNc@s0eZdZdZdZeddZddZddZd 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,t}|t|ddkr&t|_||_|jS)N_rng_pid)rgetpidgetattr_RandomZ_rngrP)selfZcur_pidr$r$r%rngs z_RandomNameSequence.rngcCs|Sr4r$rTr$r$r%__iter__sz_RandomNameSequence.__iter__cs0|j|jjfddtdD}d|S)Ncsg|] }qSr$r$).0ZdummycZchooser$r% sz0_RandomNameSequence.__next__..) charactersrUZchoiceranger)rTZlettersr$rYr%__next__sz_RandomNameSequence.__next__N) __name__ __module__ __qualname____doc__r^propertyrUrWr`r$r$r$r%rOs  rOc Csg}dD]}t|}|r||qtjdkrX|tjdtjdddddgn|d d d gz|tWn$t t fk r|tj YnX|S) z[Generate a list of candidate temporary directories which _get_default_tempdir will try.)ZTMPDIRZTEMPZTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmpz/tmpz/var/tmpz/usr/tmp) rgetenvappendr!extendr expanduser expandvarsgetcwdAttributeErrorrcurdir)dirlistZenvnamedirnamer$r$r%_candidate_tempdir_list!s&    rqc CsFt}t}|D]}|tjkr,tj|}tdD]}t|}tj||}zft |t d}zCode common to mkstemp, TemporaryFile, and NamedTemporaryFile.ztempfile.mkstemprsrfz#No usable temporary file name foundN)rr9maprrIr_rrwrrrauditr*r{r|r!r}r~rrvrEEXIST) rLZpreZsufflagsrMr rr!filer=r$r$r%_mkstemp_innerys*    rcGsH|tjkr ||f|ddin$tjdks6tj|sD||f|dS)Nr'Frf)rsupports_follow_symlinksr!rr)funcrr6r$r$r%_dont_follow_symlinkss rcCs>z tj}Wntk rYnXt||dttj|ddS)Nr)rchflagsrmrchmod)rrr$r$r% _resetpermss   rcCstS)z-The default prefix for temporary directories.)rHr$r$r$r%r scCs ttS)z6The default prefix for temporary directories as bytes.)rrIr r$r$r$r%r scCs2tdkr.tztdkr taW5tXtS)zAccessor for tempfile.tempdir.N)r rrrrr$r$r$r%r s  cCs ttS)z)A bytes version of tempfile.gettempdir().)rrIr r$r$r$r%r scCs2t|||\}}}}|rt}nt}t|||||S)aUser-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 not None, the file name will end with that suffix, otherwise there will be no suffix. If 'prefix' is not None, the file name will begin with that prefix, otherwise a default prefix is used. If 'dir' is not None, 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. If any of 'suffix', 'prefix' and 'dir' are not None, they must be the same type. If they are bytes, the returned name will be bytes; str otherwise. 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. )rN_text_openflagsrxr)rKrJrLtextrMrr$r$r%rs c Cst|||\}}}}t}|tkr.ttj|}ttD]}t|}tj ||||}t d|zt |dWnVtk rYq6Yn@tk rtjdkrtj |rt|tjrYq6nYnX|SttjddS)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. ztempfile.mkdtemprrfz(No usable temporary directory name foundN)rNrr9rrrIr_rrwrrrrmkdirr{r|r!r}r~rrr)rKrJrLrMr rr!rr$r$r%rs,     r]cCs`|dkrt}t}ttD]2}t|}tj||||}t|s|Sqt t j ddS)aUser-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 rr_rrwrrrrCr{rr)rKrJrLr rr!rr$r$r%rs  c@sLeZdZdZdZdZd ddZejdkr@ej fdd Z d d Z nd d Z dS)_TemporaryFileCloserzA separate object allowing proper closing of a temporary file's underlying file object, without adding a __del__ method to the temporary file.NFTcCs||_||_||_dSr4)rr!deleterTrr!rr$r$r%__init__9sz_TemporaryFileCloser.__init__rfcCs<|js8|jdk r8d|_z|jW5|jr6||jXdSNT) close_calledrrr!r,)rTrr$r$r%r,Hs z_TemporaryFileCloser.closecCs |dSr4)r,rVr$r$r%__del__Rsz_TemporaryFileCloser.__del__cCs|jsd|_|jdSr)rrr,rVr$r$r%r,Vs)T) rarbrcrdrrrrr!rr,rr$r$r$r%r1s   rc@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)_TemporaryFileWrapperzTemporary 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||||_dSr4)rr!rr_closerrr$r$r%rdsz_TemporaryFileWrapper.__init__cs^|jd}t||}t|drD|tfdd}|j|_|}t|tsZt||||S)Nr__call__cs ||Sr4r$)r6kwargsrr$r% func_wrapperrsz7_TemporaryFileWrapper.__getattr__..func_wrapper) __dict__rRhasattr _functoolswrapsrr8intsetattr)rTr!rarr$rr% __getattr__js     z!_TemporaryFileWrapper.__getattr__cCs|j|Sr4)r __enter__rVr$r$r%rs z_TemporaryFileWrapper.__enter__cCs|j|||}||Sr4)r__exit__r,)rTexcvaluetbresultr$r$r%rsz_TemporaryFileWrapper.__exit__cCs|jdS)zA Close the temporary file, possibly deleting it. N)rr,rVr$r$r%r,sz_TemporaryFileWrapper.closeccs|jD] }|VqdSr4)r)rTliner$r$r%rWs z_TemporaryFileWrapper.__iter__N)T) rarbrcrdrrrrr,rWr$r$r$r%r\s rw+bTerrorscCst|||\}}}} t} tjdkr0|r0| tjO} t|||| | \} } z$tj| |||||d} t| | |WSt k rt | t | YnXdS)aCreate 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). 'errors' -- the errors argument to io.open (default None) 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. rf bufferingnewlineencodingrN) rNrxrr!Z O_TEMPORARYrryr*r BaseExceptionrr,)r#rrrrKrJrLrrrMrr=r!rr$r$r%rs     posixcygwin O_TMPFILEc Cst|||\}}}}t} trz$| tjBtj@} t|| d} Wn*tk rXdaYnFtk rjYn4Xzt j| |||||dWSt | YnXt |||| |\} } z"t | t j| |||||dWSt | YnXdS)aCreate 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) 'errors' -- the errors 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. rsFrN) rNrx_O_TMPFILE_WORKSrrO_CREATr*IsADirectoryErrorrryr,rr) r#rrrrKrJrLrrMrZflags2r=r!r$r$r%rs<     c@seZdZdZdZd:dddd Zd d Zd d ZddZddZ ddZ ddZ e ddZ e ddZe ddZddZddZd d!Ze d"d#Ze d$d%Ze d&d'Zd(d)Zd*d+Zd,d-Zd.d/Ze d0d1Zd2d3Zd;d4d5Zd6d7Zd8d9ZdS)r)rrrclsr!r$r%rsz+TemporaryDirectory._rmtree..onerror)r)r>)rr!rr$rr%r>szTemporaryDirectory._rmtreecCs||t|tdSr4)r> _warningswarnResourceWarning)rr!rr$r$r%rs zTemporaryDirectory._cleanupcCsd|jj|jS)Nz <{} {!r}>)r __class__rar!rVr$r$r%__repr__szTemporaryDirectory.__repr__cCs|jSr4)r!rVr$r$r%rszTemporaryDirectory.__enter__cCs |dSr4)cleanuprr$r$r%rszTemporaryDirectory.__exit__cCs|jr||jdSr4)rrr>r!rVr$r$r%rs zTemporaryDirectory.cleanup)NNN) rarbrcrdr classmethodr>rrrrrr$r$r$r%rs   )FN)NNNF)NNN)rrNNNNNT)rrNNNNN)Ord__all__ functoolsrwarningsrioryosrZshutilZ_shutilZrmtreer> ImportErrorsysrr)rrr/r*rrsupports_dir_fdr supports_fdrr7errnorZrandomr?rSweakrefr_thread allocate_lockZ_allocate_lockO_RDWRrO_EXCLrrr@rxrArrHrrCrGrNrOrqrrrrrrr r r r r rrrrrrr!platformrrrobjectrr$r$r$r%s  !,  3       -   ' ( +? '  <