B ‚Ž\]Éiã@srdZdZdZdZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZy ddlZWnek rˆdZYnXy ddlZWnek r®dZYnXeefZyeef7ZWnek rÚYnXddd d d d d ddddddddgZdZdZedZdZdZdZdZdZ dZ!dZ"dZ#dZ$d Z%d!Z&d"Z'd#Z(d$Z)d%Z*d&Z+d'Z,d(Z-d)Z.d*Z/dZ0d+Z1d,Z2e1Z3e!e"e#e$e'e(e)e%e&e*e+e,f Z4e!e"e)e,fZ5e*e+e,fZ6d-Z7d.d/d0d1hZ8e9e9e9e:e:e:d2œZ;ej¡Z=d5d6„Z?d7d8„Z@d9d:„ZAd;e3fdd?„ZCdedfd@dA„ZDdBdC„ZEdDdE„ZFGdFd „d eGƒZHGdGd„deHƒZIGdHd „d eHƒZJGdId „d eHƒZKGdJd „d eHƒZLGdKd„deHƒZMGdLdM„dMeMƒZNGdNdO„dOeMƒZOGdPdQ„dQeMƒZPGdRdS„dSeMƒZQGdTdU„dUeMƒZRGdVdW„dWƒZSGdXdY„dYƒZTGdZd[„d[eUƒZVGd\d]„d]eUƒZWGd^d_„d_e jXƒZYGd`d„deUƒZZGdad„deUƒZ[dbd „Z\e[jZdcdd„Z]e^dekrne]ƒdS)fz,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel (lars@gustaebel.de)u4Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.é)ÚopenNÚTarFileÚTarInfoÚ is_tarfileÚTarErrorÚ ReadErrorÚCompressionErrorÚ StreamErrorÚ ExtractErrorÚ HeaderErrorÚENCODINGÚ USTAR_FORMATÚ GNU_FORMATÚ PAX_FORMATÚDEFAULT_FORMATróiésustar sustar00édé›ó0ó1ó2ó3ó4ó5ó6ó7óLóKóSóxógóXéé)ÚpathÚlinkpathÚsizeÚmtimeÚuidÚgidÚunameÚgnamer%r&r+r,)ZatimeÚctimer(r)r*r'Úntzutf-8cCs(| ||¡}|d|…|t|ƒtS)z8Convert a string to a null-terminated bytes object. N)ÚencodeÚlenÚNUL)ÚsÚlengthÚencodingÚerrors©r6ú#/usr/local/lib/python3.7/tarfile.pyÚstnžs r8cCs*| d¡}|dkr|d|…}| ||¡S)z8Convert a null-terminated bytes object to a string. réÿÿÿÿN)ÚfindÚdecode)r2r4r5Úpr6r6r7Únts¤s  r=cCsª|ddkrfd}x0tt|ƒdƒD]}|dK}|||d7}q"W|ddkr¦dt|ƒd| }n@y"t|ddƒ}t| ¡p€d dƒ}Wntk r¤td ƒ‚YnX|S) z/Convert a number field to a python number. r)é€éÿr#ér?éÚasciiÚstrictÚ0zinvalid header)Úranger0r=ÚintÚstripÚ ValueErrorÚInvalidHeaderError)r2ÚnÚir6r6r7Únti¬s   rLr@cCsÜt|ƒ}d|kr$d|dkrDnntd|d|fdƒt}n”|tkrÐd|d |krrd|dkrÐnnZ|dkrŠtdgƒ}ntdgƒ}d||}x6t|dƒD]}| d|d@¡|dL}q®Wntd ƒ‚|S) z/Convert a python number to a number field. rr@r#z%0*orBrAr>r?zoverflow in number field)rFÚbytesr1rÚ bytearrayrEÚinsertrH)rJÚdigitsÚformatr2rKr6r6r7ÚitnÀs  2   rRcCs0dtt d|¡ƒ}dtt d|¡ƒ}||fS)a¨Calculate the checksum for a member's header by summing up all characters except for the chksum field which is treated as if it was filled with spaces. According to the GNU tar sources, some tars (Sun and NeXT) calculate chksum with signed char, which will be different if there are chars in the buffer with the high bit set. So we calculate two checksums, unsigned and signed. rAZ 148B8x356BZ 148b8x356b)ÚsumÚstructÚ unpack_from)ÚbufZunsigned_chksumZ signed_chksumr6r6r7Ú calc_chksumsÝs rWc Csª|pd}|dkrdS|dkr.t |||¡dSt||ƒ\}}x8t|ƒD],}| |¡}t|ƒ|krh|dƒ‚| |¡qFW|dkr¦| |¡}t|ƒ|krœ|dƒ‚| |¡dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. i@rNzunexpected end of data)ÚshutilÚ copyfileobjÚdivmodrEÚreadr0Úwrite) ÚsrcÚdstr3Ú exceptionÚbufsizeÚblocksÚ remainderÚbrVr6r6r7rYês$     rYcCs ddl}| dtd¡t |¡S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)ÚwarningsÚwarnÚDeprecationWarningÚstatÚfilemode)Úmoderdr6r6r7rhsrhcCs8ttjddƒ}|dk r(| |d¡ |¡}t|dddS)Nr4Úbackslashreplaceú )Úend)ÚgetattrÚsysÚstdoutr/r;Úprint)r2r4r6r6r7Ú _safe_print srqc@seZdZdZdS)rzBase exception.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r6r6r6r7rsc@seZdZdZdS)r z%General exception for extract errors.N)rrrsrtrur6r6r6r7r sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rrrsrtrur6r6r6r7rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rrrsrtrur6r6r6r7rsc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rrrsrtrur6r6r6r7r sc@seZdZdZdS)r z!Base exception for header errors.N)rrrsrtrur6r6r6r7r sc@seZdZdZdS)ÚEmptyHeaderErrorzException for empty headers.N)rrrsrtrur6r6r6r7rv#srvc@seZdZdZdS)ÚTruncatedHeaderErrorz Exception for truncated headers.N)rrrsrtrur6r6r6r7rw&srwc@seZdZdZdS)ÚEOFHeaderErrorz"Exception for end of file headers.N)rrrsrtrur6r6r6r7rx)srxc@seZdZdZdS)rIzException for invalid headers.N)rrrsrtrur6r6r6r7rI,srIc@seZdZdZdS)ÚSubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rrrsrtrur6r6r6r7ry/sryc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú _LowLevelFilez‰Low-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsFtjtjtjBtjBdœ|}ttdƒr2|tjO}t ||d¡|_dS)N)ÚrÚwÚO_BINARYi¶) ÚosÚO_RDONLYÚO_WRONLYÚO_CREATÚO_TRUNCÚhasattrr}rÚfd)ÚselfÚnamerir6r6r7Ú__init__<s   z_LowLevelFile.__init__cCst |j¡dS)N)r~Úcloser„)r…r6r6r7rˆEsz_LowLevelFile.closecCst |j|¡S)N)r~r[r„)r…r'r6r6r7r[Hsz_LowLevelFile.readcCst |j|¡dS)N)r~r\r„)r…r2r6r6r7r\Ksz_LowLevelFile.writeN)rrrsrtrur‡rˆr[r\r6r6r6r7rz6s  rzc@steZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z ddd„Z ddd„Z dd„Zdd„ZdS)Ú_Streama˜Class that serves as an adapter between TarFile and a stream-like object. The stream-like object only needs to have a read() or write() method and is accessed blockwise. Use of gzip or bzip2 compression is possible. A stream-like object could be for example: sys.stdin, sys.stdout, a socket, a tape device etc. _Stream is intended to be used only internally. c Csäd|_|dkrt||ƒ}d|_|dkr6t|ƒ}| ¡}|pt|jƒ|jkrL|j |jd|j…¡|j|jd…|_qWdS)z]Write string s to the stream if a whole new block is ready to be written. N)rVr0r`r•r\)r…r2r6r6r7Z__write»sz_Stream.__writecCs²|jr dSd|_zŠ|jdkr:|jdkr:|j|j ¡7_|jdkr˜|jr˜|j |j¡d|_|jdkr˜|j t  d|j ¡¡|j t  d|j d@¡¡Wd|j s¬|j  ¡XdS) z[Close the _Stream object. No operation should be done on it afterwards. NTr|rrŒrzd|fg}d|_g|_d}|j}xT|D]L\}}||kr~|j  d||df¡|j  d||||f¡||7}||}qZW||jkrÊ|j  d||jdf¡dS)Nrr†FT) r•Úoffsetr'Úpositionrmr†r—Ú map_indexÚmaprÃ)r…r•rÍr'Z blockinfoZlastposZrealposr6r6r7r‡hs(   z_FileInFile.__init__cCsdS)Nr6)r…r6r6r7r¶sz_FileInFile.flushcCsdS)NTr6)r…r6r6r7Úreadable„sz_FileInFile.readablecCsdS)NFr6)r…r6r6r7Úwritable‡sz_FileInFile.writablecCs |j ¡S)N)r•Úseekable)r…r6r6r7rÓŠsz_FileInFile.seekablecCs|jS)z*Return the current file position. )rÎ)r…r6r6r7rÀsz_FileInFile.tellcCs|tjkr tt|dƒ|jƒ|_nj|tjkr\|dkrFt|j|dƒ|_qŠt|j||jƒ|_n.|tjkr‚tt|j||jƒdƒ|_ntdƒ‚|jS)z(Seek to a position in the file. rzInvalid argument) ÚioÚSEEK_SETÚminÚmaxr'rÎÚSEEK_CURÚSEEK_ENDrH)r…rÎÚwhencer6r6r7rÁ’s   z_FileInFile.seekc Cs|dkr|j|j}nt||j|jƒ}d}xà|dkr xZ|j|j\}}}}||jkrd|krlnnPq:|jd7_|jt|jƒkr:d|_q:Wt|||jƒ}|rè|j ||j|¡|j |¡}t|ƒ|krÞt dƒ‚||7}n |t |7}||8}|j|7_q.W|S)z!Read data from the file. NrŒrr#zunexpected end of data) r'rÎrÖrÐrÏr0r•rÁr[rr1) r…r'rVÚdataÚstartÚstoprÍr3rcr6r6r7r[¢s.      z_FileInFile.readcCs&| t|ƒ¡}||dt|ƒ…<t|ƒS)N)r[r0)r…rcrVr6r6r7ÚreadintoÁsz_FileInFile.readintocCs d|_dS)NT)r—)r…r6r6r7rˆÆsz_FileInFile.close)N)N)rrrsrtrur‡r¶rÑrÒrÓrÀrÔrÕrÁr[rÞrˆr6r6r6r7rÌbs  rÌcseZdZ‡fdd„Z‡ZS)Ú ExFileObjectcs&t|j|j|j|jƒ}tƒ |¡dS)N)rÌr•Ú offset_datar'ÚsparseÚsuperr‡)r…ÚtarfileÚtarinfor•)Ú __class__r6r7r‡Ìs  zExFileObject.__init__)rrrsrtr‡Ú __classcell__r6r6)rår7rßÊsrßc@sŽeZdZdZdZdTdd„Zedd„ƒZejdd„ƒZed d „ƒZ e jd d „ƒZ d d „Z dd„Z e e dfdd„Zdd„Zdd„Zdd„Zedd„ƒZdd„Zedd„ƒZedd „ƒZed!d"„ƒZed#d$„ƒZed%d&„ƒZed'd(„ƒZd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Z d3d4„Z!d5d6„Z"d7d8„Z#d9d:„Z$d;d<„Z%d=d>„Z&d?d@„Z'dAdB„Z(dCdD„Z)dEdF„Z*dGdH„Z+dIdJ„Z,dKdL„Z-dMdN„Z.dOdP„Z/dQdR„Z0dSS)UraInformational class which holds the details about an archive member given by a tar header block. TarInfo objects are returned by TarFile.getmember(), TarFile.getmembers() and TarFile.gettarinfo() and are usually created internally. )r†rir)r*r'r(ÚchksumÚtypeÚlinknamer+r,ÚdevmajorÚdevminorrÍràÚ pax_headersrárãÚ_sparse_structsÚ _link_targetr‹cCsj||_d|_d|_d|_d|_d|_d|_t|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_i|_dS)zXConstruct a TarInfo object. name is the optional name of the member. i¤rr‹N)r†rir)r*r'r(rçÚREGTYPErèrér+r,rêrërÍràrárì)r…r†r6r6r7r‡ãs"zTarInfo.__init__cCs|jS)N)r†)r…r6r6r7r%ýsz TarInfo.pathcCs ||_dS)N)r†)r…r†r6r6r7r%scCs|jS)N)ré)r…r6r6r7r&szTarInfo.linkpathcCs ||_dS)N)ré)r…rér6r6r7r& scCsd|jj|jt|ƒfS)Nz<%s %r at %#x>)rårrr†Úid)r…r6r6r7Ú__repr__ szTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j dœ }|dt krh|d d¡sh|dd7<|S)z9Return the TarInfo's attributes as a dictionary. iÿ) r†rir)r*r'r(rçrèrér+r,rêrërèr†ú/)r†rir)r*r'r(rçrèrér+r,rêrëÚDIRTYPEr³)r…Úinfor6r6r7Úget_infos  zTarInfo.get_infoÚsurrogateescapecCsT| ¡}|tkr| |||¡S|tkr4| |||¡S|tkrH| ||¡Stdƒ‚dS)ztdƒ‚t|dd…ƒ}|t|ƒkrbt dƒ‚|ƒ}t |dd…||ƒ|_ t|dd …ƒ|_ t|d d …ƒ|_ t|d d …ƒ|_t|d d …ƒ|_t|d d…ƒ|_||_|dd …|_t |d d…||ƒ|_t |dd…||ƒ|_t |dd…||ƒ|_t|dd…ƒ|_t|dd…ƒ|_t |dd…||ƒ}|jtkr†|j  d¡r†t|_|jtkr6d}g}xrtdƒD]f} y0t|||d…ƒ} t||d|d…ƒ} Wntk rðPYnX| | | f¡|d7}q¤Wt|dƒ} t|dd…ƒ} || | f|_ | !¡rN|j  "d¡|_ |rp|jt#krp|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headeré”éœz bad checksumrélété|éˆéii i)iIiQiYiôròi‚r¹réiâiãiï)$r0rvrÈrwÚcountr1rxrLrWrIr=r†rir)r*r'r(rçrèrér+r,rêrëÚAREGTYPEr³róÚGNUTYPE_SPARSErErHrÃÚboolríÚisdirÚrstripÚ GNU_TYPES)rrVr4r5rçÚobjrür–ÚstructsrKrÍÚnumbytesÚ isextendedÚorigsizer6r6r7ÚfrombufsZ       zTarInfo.frombufcCs8|j t¡}| ||j|j¡}|j ¡t|_| |¡S)zOReturn the next TarInfo object from TarFile object tarfile. ) r•r[rÈr7r4r5rÀrÍÚ _proc_member)rrãrVr2r6r6r7Ú fromtarfileAs zTarInfo.fromtarfilecCsT|jttfkr| |¡S|jtkr,| |¡S|jtttfkrF|  |¡S|  |¡SdS)zYChoose the right processing method depending on the type and call it. N) rèrrÚ _proc_gnulongr-Ú _proc_sparserrÚSOLARIS_XHDTYPEÚ _proc_paxÚ _proc_builtin)r…rãr6r6r7r8Vs    zTarInfo._proc_membercCsR|j ¡|_|j}| ¡s$|jtkr4|| |j¡7}||_|  |j |j |j ¡|S)zfProcess a builtin type or an unknown type which will be treated as a regular file. ) r•rÀràÚisregrèÚSUPPORTED_TYPESÚ_blockr'rÍÚ_apply_pax_inforìr4r5)r…rãrÍr6r6r7r>cs zTarInfo._proc_builtincCs†|j | |j¡¡}y| |¡}Wntk r>tdƒ‚YnX|j|_|jt krft ||j |j ƒ|_ n|jtkr‚t ||j |j ƒ|_|S)zSProcess the blocks that hold a GNU longname or longlink member. z missing or bad subsequent header)r•r[rAr'r9r ryrÍrèrr=r4r5r†rré)r…rãrVÚnextr6r6r7r:ts  zTarInfo._proc_gnulongc CsÞ|j\}}}|`xœ|r¬|j t¡}d}xvtdƒD]j}y0t|||d…ƒ}t||d|d…ƒ} Wntk rzPYnX|r’| r’| || f¡|d7}q0Wt|dƒ}qW||_ |j  ¡|_ |j |  |j ¡|_||_ |S)z8Process a GNU sparse header plus extra headers. rérr*iø)rír•r[rÈrErLrHrÃr.rárÀràrAr'rÍ) r…rãr3r5r6rVr–rKrÍr4r6r6r7r;Šs(    zTarInfo._proc_sparsecCs |j | |j¡¡}|jtkr&|j}n |j ¡}t  d|¡}|dk rX|  d¡  d¡|d<|  d¡}|dkrr|j }nd}t d¡}d}x¢| ||¡}|s˜P| ¡\} } t| ƒ} || d ¡d| d¡| d…} | | dd|j¡} | tkr| | ||j |j¡} n| | dd|j¡} | || <|| 7}q†Wy| |¡} Wntk rTtd ƒ‚YnXd |krn| | |¡nHd |krˆ| | ||¡n.|  d ¡dkr¶|  d¡dkr¶| | ||¡|jttfkr|  ||j |j¡|j | _ d|kr| j!} |  "¡s| jt#kr| |  | j¡7} | |_ | S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNr#zutf-8Ú hdrcharsetZBINARYs(\d+) ([^=]+)=rr$z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.majorÚ1zGNU.sparse.minorrDr')$r•r[rAr'rèrrìrÚreÚsearchÚgroupr;rr4ÚcompileÚmatchÚgroupsrFrlrÜÚ_decode_pax_fieldr5ÚPAX_NAME_FIELDSr9r ryÚ_proc_gnusparse_01Ú_proc_gnusparse_00Ú_proc_gnusparse_10rr<rBrÍràr?r@)r…rãrVrìrKrEr4Zregexr–r3rr rCrÍr6r6r7r=¦s`       $          zTarInfo._proc_paxcCspg}x(t d|¡D]}| t| d¡ƒ¡qWg}x(t d|¡D]}| t| d¡ƒ¡q@Wtt||ƒƒ|_dS)z?Process a GNU tar extended sparse header, version 0.0. s\d+ GNU.sparse.offset=(\d+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\nN)rGÚfinditerrÃrFrIÚlistÚziprá)r…rCrìrVZoffsetsrKr4r6r6r7rP szTarInfo._proc_gnusparse_00cCs@dd„|d d¡Dƒ}tt|ddd…|ddd…ƒƒ|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|ƒ‘qSr6)rF)Ú.0Úxr6r6r7ú sz.TarInfo._proc_gnusparse_01..zGNU.sparse.mapú,Nr$r#)rrSrTrá)r…rCrìrár6r6r7rOszTarInfo._proc_gnusparse_01cCs¬d}g}|j t¡}| dd¡\}}t|ƒ}xJt|ƒ|dkrvd|krV||j t¡7}| dd¡\}}| t|ƒ¡q.W|j ¡|_t t |ddd…|ddd…ƒƒ|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nrr#r$) r•r[rÈrrFr0rÃrÀràrSrTrá)r…rCrìrãÚfieldsrárVÚnumberr6r6r7rQs  zTarInfo._proc_gnusparse_10c CsÆx¶| ¡D]ª\}}|dkr(t|d|ƒq |dkrBt|dt|ƒƒq |dkr\t|dt|ƒƒq |tkr |tkr–yt||ƒ}Wntk r”d}YnX|dkr¨| d¡}t|||ƒq W| ¡|_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namer%zGNU.sparse.sizer'zGNU.sparse.realsizerròN) rÚsetattrrFÚ PAX_FIELDSÚPAX_NUMBER_FIELDSrHr0rrì)r…rìr4r5rr r6r6r7rB-s"  zTarInfo._apply_pax_infocCs.y | |d¡Stk r(| ||¡SXdS)z1Decode a single field from a pax record. rCN)r;ÚUnicodeDecodeError)r…r r4Zfallback_encodingZfallback_errorsr6r6r7rMDs zTarInfo._decode_pax_fieldcCs"t|tƒ\}}|r|d7}|tS)z_Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024. r#)rZrÈ)r…r+rarbr6r6r7rALszTarInfo._blockcCs |jtkS)N)rèÚ REGULAR_TYPES)r…r6r6r7r?Usz TarInfo.isregcCs| ¡S)N)r?)r…r6r6r7ÚisfileWszTarInfo.isfilecCs |jtkS)N)rèró)r…r6r6r7r/Ysz TarInfo.isdircCs |jtkS)N)rèÚSYMTYPE)r…r6r6r7Úissym[sz TarInfo.issymcCs |jtkS)N)rèÚLNKTYPE)r…r6r6r7Úislnk]sz TarInfo.islnkcCs |jtkS)N)rèÚCHRTYPE)r…r6r6r7Úischr_sz TarInfo.ischrcCs |jtkS)N)rèÚBLKTYPE)r…r6r6r7Úisblkasz TarInfo.isblkcCs |jtkS)N)rèÚFIFOTYPE)r…r6r6r7ÚisfifocszTarInfo.isfifocCs |jdk S)N)rá)r…r6r6r7ÚissparseeszTarInfo.issparsecCs|jtttfkS)N)rèrergri)r…r6r6r7Úisdevgsz TarInfo.isdevN)r‹)1rrrsrtruÚ __slots__r‡Úpropertyr%Úsetterr&rñrõrr rúr÷rørùÚ classmethodrrÚ staticmethodrrrr r7r9r8r>r:r;r=rPrOrQrBrMrAr?r`r/rbrdrfrhrjrkrlr6r6r6r7rÕsT    1    3 ?  f  c @s´eZdZdZdZdZdZdZeZ e Z dZ e ZeZd\dd „Zedddefd d „ƒZed]d d „ƒZed^dd„ƒZed_dd„ƒZed`dd„ƒZdddddœZdd„Zdd„Zdd„Zd d!„Zdad"d#„Zdbdd%œd&d'„Zdcdd(œd)d*„Z ddd+d,„Z!dedd.œd/d0„Z"dfdd.œd2d3„Z#d4d5„Z$dgd6d7„Z%d8d9„Z&d:d;„Z'dd?„Z)d@dA„Z*dBdC„Z+dDdE„Z,dFdG„Z-dHdI„Z.dJdK„Z/dhdLdM„Z0dNdO„Z1didPdQ„Z2dRdS„Z3dTdU„Z4dVdW„Z5dXdY„Z6dZd[„Z7dS)jrz=The TarFile Class provides an interface to tar archives. rFr#Nr{röc Csªdddddœ}||krtdƒ‚||_|||_|sh|jdkrTtj |¡sTd|_d|_t||jƒ}d |_n@|d krt|d ƒrt |j t t fƒr|j }t|d ƒr¢|j|_d |_|r¸tj  |¡nd |_ ||_|d k rÒ||_|d k rà||_|d k rî||_|d k rü||_|d k r ||_| |_| d k r0|jtkr0| |_ni|_| d k rF| |_| d k rV| |_| |_d |_g|_d |_|j ¡|_i|_yþ|jdkržd |_ | !¡|_ |jdkr2x†|j "|j¡y|j #|¡}|j $|¡WnTt%k rü|j "|j¡PYn0t&k r*}zt't |ƒƒ‚Wd d }~XYnXq¬W|jdkr|d |_|jr||j (|j )¡¡}|j *|¡|jt+|ƒ7_Wn&|js˜|j ,¡d |_‚YnXd S)aÖOpen an (uncompressed) tar archive `name'. `mode' is either 'r' to read from an existing archive, 'a' to append data to an existing file or 'w' to create a new file overwriting an existing one. `mode' defaults to 'r'. If `fileobj' is given, it is used for reading or writing data. If it can be determined, `mode' is overridden by `fileobj's mode. `fileobj' is not closed, when TarFile is closed. Úrbzr+bÚwbÚxb)r{Úar|rVz!mode must be 'r', 'a', 'w' or 'x'rur|FNr†riTr{)rur|rV)-rHriÚ_moder~r%ÚexistsÚ bltn_openr‘rƒr r†r rMÚabspathr•rQräÚ dereferenceÚ ignore_zerosr4r5rrìÚdebugÚ errorlevelÚ copybufsizer—ÚmembersÚ_loadedrÀrÍÚinodesÚ firstmemberrCrÁr9rÃrxr rrrr\r0rˆ)r…r†rir•rQrärzr{r4r5rìr|r}r~ÚmodesÚerVr6r6r7r‡…sŠ            $   zTarFile.__init__c s¶|s|stdƒ‚|dkr¦‡fdd„}xttˆj|dD]b}tˆˆj|ƒ}|dk rX| ¡} y||d|f|ŽSttfk r”|dk rŽ| | ¡w4Yq4Xq4Wtdƒ‚nd |kr| d d ¡\} }| pÆd} |pÎd }|ˆjkrìtˆˆj|ƒ}n td |ƒ‚||| |f|ŽSd |krŽ| d d ¡\} }| p*d} |p4d }| dkrHtdƒ‚t || |||ƒ} yˆ|| | f|Ž} Wn|   ¡‚YnXd| _ | S|dkrªˆj |||f|ŽStdƒ‚dS)a¥Open a tar archive for reading, writing or appending. Return an appropriate TarFile class. mode: 'r' or 'r:*' open for reading with transparent compression 'r:' open for reading exclusively uncompressed 'r:gz' open for reading with gzip compression 'r:bz2' open for reading with bzip2 compression 'r:xz' open for reading with lzma compression 'a' or 'a:' open for appending, creating the file if necessary 'w' or 'w:' open for writing without compression 'w:gz' open for writing with gzip compression 'w:bz2' open for writing with bzip2 compression 'w:xz' open for writing with lzma compression 'x' or 'x:' create a tarfile exclusively without compression, raise an exception if the file is already created 'x:gz' create a gzip compressed tarfile, raise an exception if the file is already created 'x:bz2' create a bzip2 compressed tarfile, raise an exception if the file is already created 'x:xz' create an lzma compressed tarfile, raise an exception if the file is already created 'r|*' open a stream of tar blocks with transparent compression 'r|' open an uncompressed stream of tar blocks for reading 'r|gz' open a gzip compressed stream of tar blocks 'r|bz2' open a bzip2 compressed stream of tar blocks 'r|xz' open an lzma compressed stream of tar blocks 'w|' open an uncompressed stream for writing 'w|gz' open a gzip compressed stream for writing 'w|bz2' open a bzip2 compressed stream for writing 'w|xz' open an lzma compressed stream for writing znothing to open)r{zr:*csˆj|dkS)NÚtaropen)Ú OPEN_METH)r”)rr6r7Únot_compressedsz$TarFile.open..not_compressed)ÚkeyNr{z%file could not be opened successfullyú:r#rzunknown compression type %rú|)r{r|zmode must be 'r' or 'w'F)rur|rVzundiscernible mode) rHÚsortedr†rmrÀrrrÁrr‰rˆr‘r…) rr†rir•r`Úkwargsr‡r”ÚfuncZ saved_posrhÚstreamrÅr6)rr7rôsP%            z TarFile.opencKs |dkrtdƒ‚||||f|ŽS)zCOpen uncompressed tar archive name for reading or writing. )r{rur|rVz!mode must be 'r', 'a', 'w' or 'x')rH)rr†rir•rŒr6r6r7r…OszTarFile.taropenr©c Ksð|dkrtdƒ‚yddl}|jWn ttfk rBtdƒ‚YnXy| ||d||¡}Wn.tk rŠ|dk r„|dkr„tdƒ‚‚YnXy|j|||f|Ž}WnBtk rÐ|  ¡|dkrÊtdƒ‚‚Yn|  ¡‚YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. )r{r|rVzmode must be 'r', 'w' or 'x'rNzgzip module is not availablercr{znot a gzip fileF) rHÚgzipZGzipFiler™ÚAttributeErrorrr¢rr…rˆr‘)rr†rir•Ú compresslevelrŒrrÅr6r6r7ÚgzopenWs2 zTarFile.gzopenc Ks¶|dkrtdƒ‚y ddl}Wntk r8tdƒ‚YnX|j|pD|||d}y|j|||f|Ž}WnFttfk r–| ¡|dkrt dƒ‚‚Yn| ¡‚YnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. )r{r|rVzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)r‘r{znot a bzip2 fileF) rHrŽr™rÚBZ2Filer…r¢ÚEOFErrorrˆrr‘)rr†rir•r‘rŒrŽrÅr6r6r7Úbz2openys(  zTarFile.bz2openc Ks¸|dkrtdƒ‚y ddl}Wntk r8tdƒ‚YnX|j|pD|||d}y|j|||f|Ž}WnH|jtfk r˜| ¡|dkr’t dƒ‚‚Yn| ¡‚YnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. )r{r|rVzmode must be 'r', 'w' or 'x'rNzlzma module is not available)Úpresetr{znot an lzma fileF) rHr¤r™rÚLZMAFiler…r¦r”rˆrr‘)rr†rir•r–rŒr¤rÅr6r6r7Úxzopen–s& zTarFile.xzopenr…r’r•r˜)rrrŽrcCsˆ|jr dSd|_z`|jdkrn|j ttd¡|jtd7_t|jtƒ\}}|dkrn|j tt|¡Wd|j s‚|j  ¡XdS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NT)rur|rVr$r) r—rir•r\r1rÈrÍrZÚ RECORDSIZEr‘rˆ)r…rarbr6r6r7rˆ½s z TarFile.closecCs"| |¡}|dkrtd|ƒ‚|S)aReturn a TarInfo object for member `name'. If `name' can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version. Nzfilename %r not found)Ú _getmemberÚKeyError)r…r†rär6r6r7Ú getmemberÒs  zTarFile.getmembercCs| ¡|js| ¡|jS)zŽReturn the members of the archive as a list of TarInfo objects. The list has the same order as the members in the archive. )Ú_checkr€Ú_loadr)r…r6r6r7Ú getmembersÝszTarFile.getmemberscCsdd„| ¡DƒS)z‹Return the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|] }|j‘qSr6)r†)rUrär6r6r7rWësz$TarFile.getnames..)rŸ)r…r6r6r7ÚgetnamesçszTarFile.getnamesc Csl| d¡|dk r|j}|dkr$|}tj |¡\}}| tjd¡}| d¡}| ¡}||_ |dkrŠt tdƒr~|j s~t  |¡}q˜t  |¡}nt | ¡¡}d}|j}t  |¡r |j|jf} |j sò|jdkrò| |jkrò||j| kròt} |j| }nt} | drt||j| <nht  |¡rt} nVt  |¡r0t} nDt  |¡rLt} t |¡}n(t  |¡r^t } nt  !|¡rpt"} ndS||_||_#|j$|_%|j&|_'| tkr¤|j(|_)nd|_)|j*|_+| |_,||_-t.ròyt. /|j%¡d|_0Wnt1k rðYnXt2r&yt2 3|j'¡d|_4Wnt1k r$YnX| t t"fkrht tdƒrht td ƒrht 5|j6¡|_7t 8|j6¡|_9|S) aÒCreate a TarInfo object from the result of os.stat or equivalent on an existing file. The file is either named by `name', or specified as a file object `fileobj' with a file descriptor. If given, `arcname' specifies an alternative name for the file in the archive, otherwise, the name is taken from the 'name' attribute of 'fileobj', or the 'name' argument. The name should be a text string. ÚawxNròÚlstatr‹r#rÚmajorÚminor):rr†r~r%Ú splitdriver«ÚsepÚlstriprärãrƒrzr¢rgÚfstatÚfilenoÚst_modeÚS_ISREGÚst_inoÚst_devÚst_nlinkrrcrïÚS_ISDIRróÚS_ISFIFOriÚS_ISLNKraÚreadlinkÚS_ISCHRreÚS_ISBLKrgriÚst_uidr)Úst_gidr*Úst_sizer'Úst_mtimer(rèréÚpwdÚgetpwuidr+r›ÚgrpÚgetgrgidr,r£Úst_rdevrêr¤rë) r…r†Úarcnamer•ZdrvräZstatresréZstmdÚinoderèr6r6r7Ú gettarinfoís~                 zTarFile.gettarinfoT)rcCsþ| ¡|dkr|}xä|D]Ü}|r¦tt |j¡ƒtd|jp@|j|jpJ|jfƒ|  ¡sb|  ¡r|tdd|j |j fƒntd|j ƒtdt |j¡dd…ƒt|j| ¡r¸dnd ƒ|rð| ¡rÚtd |jƒ| ¡rðtd |jƒtƒqWdS) aPrint a table of contents to sys.stdout. If `verbose' is False, only the names of the members are printed. If it is True, an `ls -l'-like output is produced. `members' is optional and must be a subset of the list returned by getmembers(). Nz%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02dr¸ròr‹z-> zlink to )rrqrgrhrir+r)r,r*rfrhrêrër'r±Ú localtimer(r†r/rbrérdrp)r…Úverboserrär6r6r7rSQs* z TarFile.list)Úfilterc Cs:| d¡|dkr|}|jdk rFtj |¡|jkrF| dd|¡dS| d|¡| ||¡}|dkrz| dd|¡dS|dk r¦||ƒ}|dkr¦| dd|¡dS| ¡rÒt|dƒ}|  ||¡WdQRXnd|  ¡r,|  |¡|r6xHt t  |¡ƒD]*}|j tj ||¡tj ||¡||d qüWn |  |¡dS) a!Add the file `name' to the archive. `name' may be any type of file (directory, fifo, symbolic link, etc.). If given, `arcname' specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting `recursive' to False. `filter' is a function that expects a TarInfo object argument and returns the changed TarInfo object, if it returns None the TarInfo object will be excluded from the archive. r¡Nr$ztarfile: Skipped %rr#ztarfile: Unsupported type %rztarfile: Excluded %rrr)rÃ)rr†r~r%ryÚ_dbgrÀr?rxÚaddfiler/r‹ÚlistdirÚaddrÄ)r…r†r¾Ú recursiverÃräÚfr6r6r7rÇqs4      z TarFile.addcCs¾| d¡t |¡}| |j|j|j¡}|j |¡|jt |ƒ7_|j }|dk r®t ||j|j |dt |j tƒ\}}|dkrœ|j tt|¡|d7}|j|t7_|j |¡dS)aAdd the TarInfo object `tarinfo' to the archive. If `fileobj' is given, it should be a binary file, and tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects directly, or by using gettarinfo(). r¡N)r`rr#)rrrúrQr4r5r•r\rÍr0r~rYr'rZrÈr1rrÃ)r…rär•rVr`rarbr6r6r7rŤs   zTarFile.addfileÚ.)Ú numeric_ownerc Csg}|dkr|}xF|D]>}| ¡r<| |¡t |¡}d|_|j||| ¡ |dqW|jdd„d| ¡xŠ|D]‚}tj  ||j ¡}y,|j |||d|  ||¡|  ||¡Wqvtk rö}z"|jdkrÖ‚n| dd |¡Wdd}~XYqvXqvWdS) a¶Extract all members from the archive to the current working directory and set owner, modification time and permissions on directories afterwards. `path' specifies a different directory to extract to. `members' is optional and must be a subset of the list returned by getmembers(). If `numeric_owner` is True, only the numbers for user/group names are used and not the names. NiÀ)Ú set_attrsrËcSs|jS)N)r†)rur6r6r7ÚÕrŒz$TarFile.extractall..)rˆ)rËr#z tarfile: %s)r/rÃrriÚextractÚsortÚreverser~r%rÄr†ÚchownÚutimeÚchmodr r}rÄ)r…r%rrËZ directoriesräÚdirpathr„r6r6r7Ú extractall½s,       zTarFile.extractallr‹c Cs | d¡t|tƒr | |¡}n|}| ¡r>tj ||j¡|_ y"|j |tj ||j ¡||dWn¦t k rÆ}zH|j dkr€‚n6|jdkrž| dd|j¡n| dd|j|jf¡Wdd}~XYnBtk r}z"|j dkræ‚n| dd|¡Wdd}~XYnXdS)aìExtract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a TarInfo object. You can specify a different directory using `path'. File attributes (owner, mtime, mode) are set unless `set_attrs' is False. If `numeric_owner` is True, only the numbers for user/group names are used and not the names. r{)rÌrËrNr#z tarfile: %sztarfile: %s %r)rr r rœrdr~r%rÄrérîÚ_extract_memberr†r¢r}ÚfilenamerÄÚstrerrorr )r…Úmemberr%rÌrËrär„r6r6r7rÎås(      ( zTarFile.extractcCs‚| d¡t|tƒr | |¡}n|}| ¡s6|jtkrB| ||¡S| ¡sR|  ¡rzt|j t ƒrht dƒ‚q~|  | |¡¡SndSdS)züExtract a member from the archive as a file object. `member' may be a filename or a TarInfo object. If `member' is a regular file or a link, an io.BufferedReader object is returned. Otherwise, None is returned. r{z'cannot extract (sym)link as file objectN)rr r rœr?rèr@Ú fileobjectrdrbr•r‰r Ú extractfileÚ_find_link_target)r…rÙrär6r6r7rÛ s      zTarFile.extractfilecCsT| d¡}| dtj¡}tj |¡}|r>tj |¡s>t |¡| ¡sN|  ¡rh|  dd|j |j f¡n|  d|j ¡|  ¡rŒ| ||¡nŽ| ¡r¢| ||¡nx| ¡r¸| ||¡nb| ¡sÈ| ¡rÖ| ||¡nD| ¡sæ|  ¡rô| ||¡n&|jtkr| ||¡n | ||¡|rP| |||¡|  ¡sP| ||¡| ||¡dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. ròr#z%s -> %sN)r0r«r~r¦r%ÚdirnamerwÚmakedirsrdrbrÄr†rér?Úmakefiler/ÚmakedirrjÚmakefiforfrhÚmakedevÚmakelinkrèr@Ú makeunknownrÑrÓrÒ)r…räÚ targetpathrÌrËÚ upperdirsr6r6r7rÖ*s4       zTarFile._extract_membercCs*yt |d¡Wntk r$YnXdS)z,Make a directory called targetpath. iÀN)r~ÚmkdirÚFileExistsError)r…rärår6r6r7rà[szTarFile.makedirc Cs’|j}| |j¡|j}t|dƒf}|jdk rrx,|jD]"\}}| |¡t|||t|ƒq6W| |j¡|  ¡nt|||jt|ƒWdQRXdS)z'Make a file called targetpath. rsN) r•rÁràr~rxrárYrr'Útruncate)r…räråÚsourcer`ÚtargetrÍr'r6r6r7rßes      zTarFile.makefilecCs"| ||¡| dd|j¡dS)zYMake a file from a TarInfo object with an unknown type at targetpath. r#z9tarfile: Unknown file type %r, extracted as regular file.N)rßrÄrè)r…rärår6r6r7räus zTarFile.makeunknowncCs"ttdƒrt |¡ntdƒ‚dS)z'Make a fifo called targetpath. Úmkfifozfifo not supported by systemN)rƒr~rìr )r…rärår6r6r7rá}s  zTarFile.makefifocCs^ttdƒrttdƒstdƒ‚|j}| ¡r6|tjO}n |tjO}t ||t  |j |j ¡¡dS)zttdƒr:yt ||j¡Wntk r8tdƒ‚YnXdS)zASet file permissions of targetpath according to tarinfo. rÓzcould not change modeN)rƒr~rÓrir¢r )r…rärår6r6r7rÓÈs  z TarFile.chmodcCsHttdƒsdSyt ||j|jf¡Wntk rBtdƒ‚YnXdS)zBSet modification time of targetpath according to tarinfo. rÒNz"could not change modification time)rƒr~rÒr(r¢r )r…rärår6r6r7rÒÑs  z TarFile.utimec Csæ| d¡|jdk r$|j}d|_|S|j|j ¡krZ|j |jd¡|j d¡sZtdƒ‚d}xby|j  |¡}WnHt k rÂ}z0|j r²|  dd|j|f¡|jt 7_wbWdd}~XYnütk r,}zL|j r|  dd|j|f¡|jt 7_wbn|jdkrtt|ƒƒ‚Wdd}~XYn’tk rT|jdkrPtdƒ‚Ynjtk rŽ}z|jdkr~tt|ƒƒ‚Wdd}~XYn0tk r¼}ztt|ƒƒ‚Wdd}~XYnXPqbW|dk rÜ|j |¡nd |_|S) z¨Return the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available. ZraNr#zunexpected end of datar$z0x%X: %srz empty fileT)rr‚rÍr•rÀrÁr[rrär9rxr{rÄrÈrIr rvrwryrrÃr€)r…Úmrär„r6r6r7rCÜsL        z TarFile.nextcCsn| ¡}|dk r"|d| |¡…}|r2tj |¡}x6t|ƒD]*}|rTtj |j¡}n|j}||kr<|SqzShow listing of a tarfile)Úmetavarr z-ez --extractú+)z z zExtract tarfile into target dir)Únargsrr z-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr{)rz{!r} is a tar archive.r#z{!r} is not a tar archive. zr:*)rÂr$)r%rÊz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrrŽ)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r|z{!r} file created.)ÚargparseÚArgumentParserÚ add_argumentÚadd_mutually_exclusive_groupÚ parse_argsÚtestrrrŸrprnrrÂrQÚexitrSrrÎr0r~ÚcurdirÚ format_helprÕÚcreateÚpopr%ÚsplitextrÇ)rrÚparserrIÚargsr]rZtfrrZtar_nameÚ_ÚextZ compressionsZtar_modeZ tar_filesÚ file_namer6r6r7Úmain“ s†                     r"Ú__main__)_ruÚversionÚ __author__Ú __credits__Úbuiltinsrrxrnr~rÔrXrgr±rTrrGr¹r™r»rÚNotImplementedErrorròr¢Ú NameErrorÚ__all__r1rÈr™rrýrÿrþrrïr,rcrarergróriZCONTTYPErrr-rrr<r rrrr@r_r1r\rNr rFr]r†r Úgetfilesystemencodingr8r=rLrRrWrYrhrqÚ Exceptionrr rrr r rvrwrxrIryrzr‰Úobjectr’rÌÚBufferedReaderrßrrrr"rrr6r6r6r7Úsô         wh " W