grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/20263 ¡ f„lã'@szdZdZdZdZdZdZddlmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZy ddlZWnek r�dZYnXy ddlZWnek r¶dZYnXeefZyeef7ZWnek râYnXd d d d d ddddddddddgZdZdZedZdZdZdZ 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/d+Z0d,Z1dZ2d-Z3d.Z4e3Z5e#e$e%e&e)e*e+e'e(e,e-e.f Z6e#e$e+e.fZ7e,e-e.fZ8dkZ9d/d0d5d6hZ:e;e;e;ed8k�rÜd9Z?ne j@ƒZ?d:d;„ZAdd?„ZCd@e5fdAdB„ZDdCdD„ZEdedfdEdF„ZFdGdH„ZGdIdJ„ZHGdKd „d eIƒZJGdLd„deJƒZKGdMd „d eJƒZLGdNd„deJƒZMGdOd„deJƒZNGdPd„deJƒZOGdQdR„dReOƒZPGdSdT„dTeOƒZQGdUdV„dVeOƒZRGdWdX„dXeOƒZSGdYdZ„dZeOƒZTGd[d\„d\ƒZUGd]d^„d^ƒZVGd_d`„d`eWƒZXGdadb„dbeWƒZYGdcdd„dde jZƒZ[Gded „d eWƒZ\Gdfd „d eWƒZ]dgd „Z^e]jZdhdi„Z_e`djk�rve_ƒdS)lz,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel (lars@gustaebel.de)z5$Date: 2011-02-25 17:42:01 +0200 (Fri, 25 Feb 2011) $z?$Id: tarfile.py 88586 2011-02-25 15:42:01Z marc-andre.lemburg $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Úgname)ZatimeZctimer(r)r*r'Úntzutf-8cCs(|j||ƒ}|d|…|t|ƒtS)z8Convert a string to a null-terminated bytes object. N)ÚencodeÚlenÚNUL)ÚsÚlengthÚencodingÚerrors©r5ú,/opt/alt/python36/lib64/python3.6/tarfile.pyÚstn s r7cCs*|jdƒ}|dkr|d|…}|j||ƒS)z8Convert a null-terminated bytes object to a string. rr#Néÿÿÿÿ)ÚfindÚdecode)r1r3r4Úpr5r5r6Únts¦s  r<c Csª|dd krfd}x0tt|ƒdƒD]}|dK}|||d7}q"W|ddkr¦dt|ƒd| }n@y"t|ddƒ}t|jƒp€d dƒ}Wntk r¤td ƒ‚YnX|S) z/Convert a number field to a python number. ré€éÿr#ééÚasciiÚstrictÚ0zinvalid header)r=r>)Úranger/r<ÚintÚstripÚ ValueErrorÚInvalidHeaderError)r1ÚnÚir5r5r6Únti®s   rKr?cCsÜt|ƒ}d|ko"d|dknrDtd|d|fdƒt}n”|tkrÐd|d |kopd|dknrÐ|dkrŠtdgƒ}ntdgƒ}d||}x6t|dƒD]}|jd|d@ƒ|dL}q®Wntd ƒ‚|S) z/Convert a python number to a number field. rr?r#z%0*orAr@r=r>zoverflow in number field)rEÚbytesr0rÚ bytearrayrDÚinsertrG)rIÚdigitsÚformatr1rJr5r5r6ÚitnÂs  2   rQcCs0dttjd|ƒƒ}dttjd|ƒƒ}||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. r@Z 148B8x356BZ 148b8x356b)ÚsumÚstructZ unpack_from)ÚbufZunsigned_chksumZ signed_chksumr5r5r6Ú calc_chksumsßs rUc Csª|pd}|dkrdS|dkr.tj|||ƒdSt||ƒ\}}x8t|ƒD],}|j|ƒ}t|ƒ|krh|dƒ‚|j|ƒqFW|dkr¦|j|ƒ}t|ƒ|krœ|dƒ‚|j|ƒdS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. éirNzunexpected end of datai@)ÚshutilÚ copyfileobjÚdivmodrDÚreadr/Úwrite) ÚsrcÚdstr2Ú exceptionÚbufsizeÚblocksÚ remainderÚbrTr5r5r6rXìs$     rXcCs ddl}|jdtdƒtj|ƒS)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)ÚwarningsÚwarnÚDeprecationWarningÚstatÚfilemode)Úmodercr5r5r6rgsrgcCs8ttjddƒ}|dk r(|j|dƒj|ƒ}t|dd�dS)Nr3Úbackslashreplaceú )Úend)ÚgetattrÚsysÚstdoutr.r:Úprint)r1r3r5r5r6Ú _safe_print srpc@seZdZdZdS)rzBase exception.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r5r5r5r6rsc@seZdZdZdS)r z%General exception for extract errors.N)rqrrrsrtr5r5r5r6r sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rqrrrsrtr5r5r5r6rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rqrrrsrtr5r5r5r6rsc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rqrrrsrtr5r5r5r6r sc@seZdZdZdS)r z!Base exception for header errors.N)rqrrrsrtr5r5r5r6r "sc@seZdZdZdS)ÚEmptyHeaderErrorzException for empty headers.N)rqrrrsrtr5r5r5r6ru%sruc@seZdZdZdS)ÚTruncatedHeaderErrorz Exception for truncated headers.N)rqrrrsrtr5r5r5r6rv(srvc@seZdZdZdS)ÚEOFHeaderErrorz"Exception for end of file headers.N)rqrrrsrtr5r5r5r6rw+srwc@seZdZdZdS)rHzException for invalid headers.N)rqrrrsrtr5r5r5r6rH.srHc@seZdZdZdS)ÚSubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rqrrrsrtr5r5r5r6rx1srxc@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}tj||dƒ|_dS)N)ÚrÚwÚO_BINARYi¶) ÚosÚO_RDONLYÚO_WRONLYÚO_CREATÚO_TRUNCÚhasattrr|rÚfd)ÚselfÚnamerhr5r5r6Ú__init__>s   z_LowLevelFile.__init__cCstj|jƒdS)N)r}Úcloserƒ)r„r5r5r6r‡Gsz_LowLevelFile.closecCstj|j|ƒS)N)r}rZrƒ)r„r'r5r5r6rZJsz_LowLevelFile.readcCstj|j|ƒdS)N)r}r[rƒ)r„r1r5r5r6r[Msz_LowLevelFile.writeN)rqrrrsrtr†r‡rZr[r5r5r5r6ry8s  ryc@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|ƒ}|jƒ}|pt|jƒ|jkrL|jj|jd|j…ƒ|j|jd…|_qWdS)z]Write string s to the stream if a whole new block is ready to be written. N)rTr/r_r”r[)r„r1r5r5r6Z__write½sz_Stream.__writec Cs²|jr dSd|_zŠ|jdkr:|jdkr:|j|jjƒ7_|jdkr˜|jr˜|jj|jƒd|_|jdkr˜|jjtj d|j ƒƒ|jjtj d|j d@ƒƒWd|j s¬|jj ƒXdS) z[Close the _Stream object. No operation should be done on it afterwards. NTr{r�r‹rŒzd|fg}d|_g|_d}|j}xT|D]L\}}||kr~|jj d||dfƒ|jj d||||fƒ||7}||}qZW||jkrÊ|jj d||jdfƒdS)Nrr…FT) r”Úoffsetr'Úpositionrlr…r–Ú map_indexÚmapr¸)r„r”rÂr'Z blockinfoZlastposZrealposr5r5r6r†js(   z_FileInFile.__init__cCsdS)Nr5)r„r5r5r6r®ƒsz_FileInFile.flushcCsdS)NTr5)r„r5r5r6Úreadable†sz_FileInFile.readablecCsdS)NFr5)r„r5r5r6Úwritable‰sz_FileInFile.writablecCs |jjƒS)N)r”Úseekable)r„r5r5r6rÈŒsz_FileInFile.seekablecCs|jS)z*Return the current file position. )rÃ)r„r5r5r6rµ�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_ENDrG)r„rÃÚwhencer5r5r6r¶”s   z_FileInFile.seekc Cs|dkr|j|j}nt||j|jƒ}d}xà|dk�r xZ|j|j\}}}}||jkob|knrlPq:|jd7_|jt|jƒkr:d|_q:Wt|||jƒ}|rè|jj||j|ƒ|jj|ƒ}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Är/r”r¶rZrr0) r„r'rTÚdataÚstartÚstoprÂr2rbr5r5r6rZ¤s.      z_FileInFile.readcCs&|jt|ƒƒ}||dt|ƒ…<t|ƒS)N)rZr/)r„rbrTr5r5r6ÚreadintoÃsz_FileInFile.readintocCs d|_dS)NT)r–)r„r5r5r6r‡Èsz_FileInFile.close)N)N)rqrrrsrtr†r®rÆrÇrÈrµrÉrÊr¶rZrÓr‡r5r5r5r6rÁds  rÁcseZdZ‡fdd„Z‡ZS)Ú ExFileObjectcs&t|j|j|j|jƒ}tƒj|ƒdS)N)rÁr”Ú offset_datar'ÚsparseÚsuperr†)r„ÚtarfileÚtarinfor”)Ú __class__r5r6r†Îs  zExFileObject.__init__)rqrrrsr†Ú __classcell__r5r5)rÚr6rÔÌsrÔc@sŽeZdZdZdiZdjdd„Zdd„Zdd„ZeeeƒZ dd„Z dd „Z ee e ƒZ d!d"„Z d#d$„Zeed%fd&d'„Zd(d)„Zd*d+„Zd,d-„Zed.d/„ƒZd0d1„Zed2d3„ƒZed4d5„ƒZed6d7„ƒZed8d9„ƒZed:d;„ƒZedd?„Zd@dA„Z dBdC„Z!dDdE„Z"dFdG„Z#dHdI„Z$dJdK„Z%dLdM„Z&dNdO„Z'dPdQ„Z(dRdS„Z)dTdU„Z*dVdW„Z+dXdY„Z,dZd[„Z-d\d]„Z.d^d_„Z/d`da„Z0dbdc„Z1ddde„Z2dfdg„Z3dhS)kraInformational 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…rhr)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…rhr)r*r'r(rÜÚREGTYPErÝrÞr+r,rßràrÂrÕrÖrá)r„r…r5r5r6r†ås"zTarInfo.__init__cCs|jS)N)r…)r„r5r5r6Ú_getpathÿszTarInfo._getpathcCs ||_dS)N)r…)r„r…r5r5r6Ú_setpathszTarInfo._setpathcCs|jS)N)rÞ)r„r5r5r6Ú _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)rÞ)r„rÞr5r5r6Ú _setlinkpathszTarInfo._setlinkpathcCsd|jj|jt|ƒfS)Nz<%s %r at %#x>)rÚrqr…Úid)r„r5r5r6Ú__repr__ szTarInfo.__repr__cCsn|j|jd@|j|j|j|j|j|j|j|j |j |j |j dœ }|dt krj|djdƒ rj|dd7<|S)z9Return the TarInfo's attributes as a dictionary. iÿ) r…rhr)r*r'r(rÜrÝrÞr+r,rßràrÝr…ú/)r…rhr)r*r'r(rÜrÝrÞr+r,rßràÚDIRTYPEr¬)r„Úinfor5r5r6Úget_infos  zTarInfo.get_infoÚsurrogateescapecCsT|jƒ}|tkr|j|||ƒS|tkr4|j|||ƒS|tkrH|j||ƒ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…||ƒ}|jtk�r†|j jdƒ�r†t|_|jtk�r6d}g}xrtdƒD]f} y0t|||d…ƒ} t||d|d…ƒ} Wntk �rðPYnX|j| | fƒ|d7}�q¤Wt|dƒ} t|dd…ƒ} || | f|_ |j!ƒ�rN|j j"dƒ|_ |�rp|jt#k�rp|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ï)$r/rur¼rvÚcountr0rwrKrUrHr<r…rhr)r*r'r(rÜrÝrÞr+r,rßràÚAREGTYPEr¬rìÚGNUTYPE_SPARSErDrGr¸ÚboolrâÚisdirÚrstripÚ GNU_TYPES)rrTr3r4rÜÚobjrõr•ÚstructsrJrÂÚnumbytesÚ isextendedÚorigsizer5r5r6ÚfrombufsZ       zTarInfo.frombufcCs8|jjtƒ}|j||j|jƒ}|jjƒt|_|j|ƒS)zOReturn the next TarInfo object from TarFile object tarfile. ) r”rZr¼r0r3r4rµrÂÚ _proc_member)rrØrTr+r5r5r6Ú fromtarfile?s zTarInfo.fromtarfilecCsT|jttfkr|j|ƒS|jtkr,|j|ƒS|jtttfkrF|j |ƒS|j |ƒSdS)zYChoose the right processing method depending on the type and call it. N) rÝrþrýÚ _proc_gnulongr&Ú _proc_sparser r ÚSOLARIS_XHDTYPEÚ _proc_paxÚ _proc_builtin)r„rØr5r5r6r1Ts    zTarInfo._proc_membercCsR|jjƒ|_|j}|jƒs$|jtkr4||j|jƒ7}||_|j |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ár3r4)r„rØrÂr5r5r6r7as zTarInfo._proc_builtinc Cs†|jj|j|jƒƒ}y|j|ƒ}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”rZr:r'r2r rxrÂrÝrþr<r3r4r…rýrÞ)r„rØrTÚnextr5r5r6r3rs  zTarInfo._proc_gnulongc CsÞ|j\}}}|`xœ|r¬|jjtƒ}d}xvtdƒD]j}y0t|||d…ƒ}t||d|d…ƒ} Wntk rzPYnX|r’| r’|j|| fƒ|d7}q0Wt|dƒ}qW||_ |jj ƒ|_ |j |j |j ƒ|_||_ |S)z8Process a GNU sparse header plus extra headers. rérr#iø)râr”rZr¼rDrKrGr¸r'rÖrµrÕr:r'rÂ) r„rØr,r.r/rTr•rJrÂr-r5r5r6r4ˆs(    zTarInfo._proc_sparsec Cs0|jj|j|jƒƒ}|jtkr&|j}n |jjƒ}tj d|ƒ}|dk rX|j dƒj dƒ|d<|j dƒ}|dkrr|j }nd}tjdƒ}d}x²|j||ƒ}|s˜P|jƒ\} } t| ƒ} | dkr¼td ƒ‚||jd ƒd|jdƒ| d…} |j| dd|jƒ} | tk�r|j| ||j |jƒ} n|j| dd|jƒ} | || <|| 7}q†Wy|j|ƒ} Wntk �rdtd ƒ‚YnXd |k�r~|j| |ƒnHd |k�r˜|j| ||ƒn.|j dƒdk�rÆ|j dƒdk�rÆ|j| ||ƒ|jttfk�r,| j ||j |jƒ|j!| _!d|k�r,| j"} | j#ƒ�s| jt$k�r&| | j| jƒ7} | |_!| S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNr#zutf-8Ú hdrcharsetZBINARYs(\d+) ([^=]+)=rzinvalid headerr$z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.majorÚ1zGNU.sparse.minorrCr')%r”rZr:r'rÝr rárÚreÚsearchÚgroupr:rr3ÚcompileÚmatchÚgroupsrErHrkrÑÚ_decode_pax_fieldr4ÚPAX_NAME_FIELDSr2r rxÚ_proc_gnusparse_01Ú_proc_gnusparse_00Ú_proc_gnusparse_10r r5r;rÂrÕr8r9)r„rØrTrárDr>r3Zregexr•r2rrr<rÂr5r5r6r6¤sd       $          zTarInfo._proc_paxcCspg}x(tjd|ƒD]}|jt|jdƒƒƒqWg}x(tjd|ƒD]}|jt|jdƒƒƒ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)r@Úfinditerr¸rErBÚlistÚziprÖ)r„r<rárTZoffsetsrDr-r5r5r6rI szTarInfo._proc_gnusparse_00cCs@dd„|djdƒDƒ}tt|ddd…|ddd…ƒƒ|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|ƒ‘qSr5)rE)Ú.0Úxr5r5r6ú sz.TarInfo._proc_gnusparse_01..zGNU.sparse.mapú,Nr$r#)rrLrMrÖ)r„r<rárÖr5r5r6rHszTarInfo._proc_gnusparse_01cCs¬d}g}|jjtƒ}|jddƒ\}}t|ƒ}xJt|ƒ|dkrvd|krV||jjtƒ7}|jddƒ\}}|jt|ƒƒq.W|jjƒ|_t t |ddd…|ddd…ƒƒ|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nrr#r$) r”rZr¼rrEr/r¸rµrÕrLrMrÖ)r„r<rárØZfieldsrÖrTZnumberr5r5r6rJs  zTarInfo._proc_gnusparse_10c CsÆx¶|jƒD]ª\}}|dkr(t|d|ƒq |dkrBt|dt|ƒƒq |dkr\t|dt|ƒƒq |tkr |tkr–yt||ƒ}Wntk r”d}YnX|dkr¨|jdƒ}t|||ƒq W|jƒ|_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ÚsetattrrEÚ PAX_FIELDSÚPAX_NUMBER_FIELDSrGr)rrá)r„rár3r4rrr5r5r6r;-s"  zTarInfo._apply_pax_infoc Cs.y |j|dƒStk r(|j||ƒSXdS)z1Decode a single field from a pax record. rBN)r:ÚUnicodeDecodeError)r„rr3Zfallback_encodingZfallback_errorsr5r5r6rFDs 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#)rYr¼)r„r$r`rar5r5r6r:LszTarInfo._blockcCs |jtkS)N)rÝÚ REGULAR_TYPES)r„r5r5r6r8Usz TarInfo.isregcCs|jƒS)N)r8)r„r5r5r6ÚisfileWszTarInfo.isfilecCs |jtkS)N)rÝrì)r„r5r5r6r(Ysz TarInfo.isdircCs |jtkS)N)rÝÚSYMTYPE)r„r5r5r6Úissym[sz TarInfo.issymcCs |jtkS)N)rÝÚLNKTYPE)r„r5r5r6Úislnk]sz TarInfo.islnkcCs |jtkS)N)rÝÚCHRTYPE)r„r5r5r6Úischr_sz TarInfo.ischrcCs |jtkS)N)rÝÚBLKTYPE)r„r5r5r6Úisblkasz TarInfo.isblkcCs |jtkS)N)rÝÚFIFOTYPE)r„r5r5r6ÚisfifocszTarInfo.isfifocCs |jdk S)N)rÖ)r„r5r5r6ÚissparseeszTarInfo.issparsecCs|jtttfkS)N)rÝr\r^r`)r„r5r5r6Úisdevgsz TarInfo.isdevN)r…rhr)r*r'r(rÜrÝrÞr+r,rßràrÂrÕrárÖrØrârã)rŠ)4rqrrrsrtÚ __slots__r†råræÚpropertyr%rçrèr&rêrîrr rórðrñròÚ classmethodrrùÚ staticmethodrúrrür r0r2r1r7r3r4r6rIrHrJr;rFr:r8rWr(rYr[r]r_rarbrcr5r5r5r6r×s`    1    3 ?  h  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#NrzrïcCs°dddddœ}||krtdƒ‚||_|||_|sj|jdkrVtjj|ƒ rVd|_d|_t||jƒ}d |_n@|d kr’t|d ƒr’t |j t t fƒr’|j }t|d ƒr¤|j|_d |_|rºtjj |ƒnd |_ ||_|d k rÔ||_|d k râ||_|d k rð||_|d k �r||_|d k �r||_| |_| d k �r4|jtk�r4| |_ni|_| d k �rJ| |_| d k �rZ| |_| |_d |_g|_d |_|jjƒ|_i|_�yþ|jdk�r¤d |_ |j!ƒ|_ |jdk�r8x†|jj"|jƒy|jj#|ƒ}|jj$|ƒWnTt%k �r|jj"|jƒPYn0t&k �r0}zt't |ƒƒ‚WYd d }~XnX�q²W|jdk�r‚d |_|j�r‚|jj(|jj)ƒƒ}|jj*|ƒ|jt+|ƒ7_Wn&|j�sž|jj,ƒ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ÚwbZxb)rzÚar{rOz!mode must be 'r', 'a', 'w' or 'x'rjr{FNr…rhTrzrO)rjr{rO)-rGrhÚ_moder}r%ÚexistsÚ bltn_openr�r‚rr…r rLÚabspathr”rPrÙÚ dereferenceÚ ignore_zerosr3r4rráÚdebugÚ errorlevelÚ copybufsizer–ÚmembersÚ_loadedrµrÂÚinodesÚ firstmemberr<r¶r2r¸rwr rrrr[r/r‡)r„r…rhr”rPrÙrorpr3r4rárqrrrsZmodesÚerTr5r5r6r†…sŠ             $   zTarFile.__init__c sº| r| rtdƒ‚|dkrª‡fdd„}xttˆj|d�D]b}tˆˆj|ƒ}|dk r\|jƒ} y||d|f|ŽSttfk r˜|dk r’|j| ƒw8Yq8Xq8Wtdƒ‚�nd |k�r |jd d ƒ\} }| pÊd} |pÒd }|ˆjkrðtˆˆj|ƒ}n td |ƒ‚||| |f|ŽSd |k�r’|jd d ƒ\} }| �p.d} |�p8d }| dk�rLtdƒ‚t || |||ƒ} yˆ|| | f|Ž} Wn| j ƒ‚YnXd| _ | S|dk�r®ˆ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 openrzúr:*csˆj|dkS)NÚtaropen)Ú OPEN_METH)r“)rr5r6Únot_compressedsz$TarFile.open..not_compressed)ÚkeyNz%file could not be opened successfullyú:r#r�zunknown compression type %rú|r{zmode must be 'r' or 'w'FrjrOzundiscernible mode)rzry)rzr{)rjr{rO) rGÚsortedr{rlrµrrr¶rrˆr‡r�rz) rr…rhr”r_Úkwargsr|r“ÚfuncZ saved_posrgÚstreamrºr5)rr6rôsP%             z TarFile.opencKs |dkrtdƒ‚||||f|ŽS)zCOpen uncompressed tar archive name for reading or writing. rzrjr{rOz!mode must be 'r', 'a', 'w' or 'x')rzrjr{rO)rG)rr…rhr”r�r5r5r6rzOszTarFile.taropenr¤cKsð|d krtdƒ‚yddl}|jWn ttfk rBtdƒ‚YnXy|j||d||ƒ}Wn.tk rŠ|dk r„|dkr„td ƒ‚‚YnXy|j|||f|Ž}WnBtk rÐ|j ƒ|dkrÊtd ƒ‚‚Yn|j ƒ‚YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. rzr{rOzmode must be 'r', 'w' or 'x'rNzgzip module is not availablerbznot a gzip fileF)rzr{rO) rGÚgzipZGzipFiler˜ÚAttributeErrorrr rrzr‡r�)rr…rhr”Ú compresslevelr�r„rºr5r5r6ÚgzopenWs2 zTarFile.gzopencKs¶|d krtdƒ‚y ddl}Wntk r8tdƒ‚YnX|j|pD|||d�}y|j|||f|Ž}WnFttfk r–|jƒ|dkr�t d ƒ‚‚Yn|jƒ‚YnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. rzr{rOzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)r†znot a bzip2 fileF)rzr{rO) rGr�r˜rZBZ2Filerzr ÚEOFErrorr‡rr�)rr…rhr”r†r�r�rºr5r5r6Úbz2openys(  zTarFile.bz2opencKs¸|d krtdƒ‚y ddl}Wntk r8tdƒ‚YnX|j|pD|||d�}y|j|||f|Ž}WnH|jtfk r˜|jƒ|dkr’t d ƒ‚‚Yn|jƒ‚YnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. rzr{rOzmode must be 'r', 'w' or 'x'rNzlzma module is not available)Úpresetznot an lzma fileF)rzr{rO) rGr¡r˜rZLZMAFilerzr¢rˆr‡rr�)rr…rhr”rŠr�r¡rºr5r5r6Úxzopen–s& zTarFile.xzopenrzr‡r‰r‹)r�rŒr�rŽc Csˆ|jr dSd|_z`|jdkrn|jjttdƒ|jtd7_t|jtƒ\}}|dkrn|jjtt|ƒWd|j s‚|jj ƒXdS) zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NTrjr{rOr$r)rjr{rO) r–rhr”r[r0r¼rÂrYÚ RECORDSIZEr�r‡)r„r`rar5r5r6r‡½s z TarFile.closecCs"|j|ƒ}|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Ùr5r5r6Ú getmemberÒs  zTarFile.getmembercCs|jƒ|js|jƒ|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. )Ú_checkruÚ_loadrt)r„r5r5r6Ú getmembersÝszTarFile.getmemberscCsdd„|jƒ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‘qSr5)r…)rNrÙr5r5r6rPësz$TarFile.getnames..)r’)r„r5r5r6ÚgetnamesçszTarFile.getnamesc Csp|jdƒ|dk r|j}|dkr$|}tjj|ƒ\}}|jtjdƒ}|jdƒ}|jƒ}||_ |dkrŒt tdƒr€|j r€tj |ƒ}qštj |ƒ}ntj|jƒƒ}d}|j}t j|ƒ�r|j|jf} |j rö|jdkrö| |jkrö||j| kröt} |j| }nt} | d�rx||j| <nht j|ƒ�r"t} nVt j|ƒ�r4t} nDt j|ƒ�rPt} tj|ƒ}n(t j|ƒ�rbt } nt j!|ƒ�rtt"} ndS||_||_#|j$|_%|j&|_'| tk�r¨|j(|_)nd|_)|j*|_+| |_,||_-t.�röyt.j/|j%ƒd|_0Wnt1k �rôYnXt2�r*yt2j3|j'ƒd|_4Wnt1k �r(YnX| t t"fk�rlt tdƒ�rlt td ƒ�rltj5|j6ƒ|_7tj8|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):r�r…r}r%Ú splitdriver¦ÚsepÚlstriprÙrØr‚ror•rfÚfstatÚfilenoÚst_modeÚS_ISREGÚst_inoÚst_devÚst_nlinkrvrZräÚS_ISDIRrìÚS_ISFIFOr`ÚS_ISLNKrXÚreadlinkÚS_ISCHRr\ÚS_ISBLKr^rhÚst_uidr)Úst_gidr*Úst_sizer'Úst_mtimer(rÝrÞÚpwdÚgetpwuidr+rŽÚgrpZgetgrgidr,r–Úst_rdevrßr—rà) r„r…Úarcnamer”ZdrvrÙZstatresrÞZstmdÚinoderÝr5r5r6Ú gettarinfoís~                 zTarFile.gettarinfoT)rtcCsþ|jƒ|dkr|}xä|D]Ü}|r¦ttj|jƒƒtd|jp@|j|jpJ|jfƒ|j ƒsb|j ƒr|tdd|j |j fƒntd|j ƒtdtj|jƒdd…ƒt|j|jƒr¸dnd ƒ|rð|jƒrÚtd |jƒ|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 )r�rprfrgrhr+r)r,r*r]r_rßràr'rªZ localtimer(r…r(rYrÞr[ro)r„ÚverbosertrÙr5r5r6rLQs* z TarFile.list)Úfilterc Csv|jdƒ|dkr|}|dk rPddl}|jdtdƒ||ƒrP|jdd|ƒdS|jdk r€tjj|ƒ|jkr€|jdd|ƒdS|jd|ƒ|j ||ƒ}|dkr´|jdd |ƒdS|dk rà||ƒ}|dkrà|jdd|ƒdS|j ƒ�rt |d ƒ�}|j ||ƒWdQRXnd|j ƒ�rh|j |ƒ|�rrxHtj|ƒD].}|jtjj||ƒtjj||ƒ|||d ��q4Wn |j |ƒ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. `exclude' is a function that should return True for each filename to be excluded. `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”Nrzuse the filter argument insteadr$ztarfile: Excluded %rztarfile: Skipped %rr#ztarfile: Unsupported type %rrh)r´)r�rcrdreÚ_dbgr…r}r%rnr²r8rmÚaddfiler(ÚlistdirÚaddr¹) r„r…r°Ú recursiveZexcluder´rcrÙÚfr5r5r6r¸qsB       z TarFile.addcCs¾|jdƒtj|ƒ}|j|j|j|jƒ}|jj|ƒ|jt |ƒ7_|j }|dk r®t ||j|j |d�t |j tƒ\}}|dkrœ|jjtt|ƒ|d7}|j|t7_|jj|ƒ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#)r�rrórPr3r4r”r[rÂr/rsrXr'rYr¼r0rtr¸)r„rÙr”rTr_r`rar5r5r6r¶®s   zTarFile.addfileÚ.)Ú numeric_ownercCsg}|dkr|}xF|D]>}|jƒr<|j|ƒtj|ƒ}d|_|j|||jƒ |d�qW|jdd„d�|jƒxŠ|D]‚}tjj ||j ƒ}y,|j |||d�|j ||ƒ|j ||ƒWqvtk rö}z$|jdkrÖ‚n|jdd |ƒWYdd}~XqvXqvWdS) 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…)rjr5r5r6Úßsz$TarFile.extractall..)r})r¼r#z tarfile: %s)r(r¸rrhÚextractÚsortÚreverser}r%r¹r…ÚchownÚutimeÚchmodr rrrµ)r„r%rtr¼Z directoriesrÙÚdirpathrxr5r5r6Ú extractallÇs,       zTarFile.extractallrŠcCs |jdƒt|tƒr |j|ƒ}n|}|jƒr>tjj||jƒ|_ y"|j |tjj||j ƒ||d�Wn¦t k rÆ}zJ|j dkr€‚n6|jdkrž|jdd|jƒn|jdd|j|jfƒWYdd}~XnBtk �r}z$|j dkræ‚n|jdd|ƒWYdd}~XnXdS)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. rz)r½r¼rNr#z tarfile: %sztarfile: %s %r)r�rr r�r[r}r%r¹rÞrãÚ_extract_memberr…r rrÚfilenamerµÚstrerrorr )r„Úmemberr%r½r¼rÙrxr5r5r6r¿ïs(      ( zTarFile.extractcCs‚|jdƒt|tƒr |j|ƒ}n|}|jƒs6|jtkrB|j||ƒS|jƒsR|j ƒrzt|j t ƒrht dƒ‚q~|j |j|ƒƒ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. rzz'cannot extract (sym)link as file objectN)r�rr r�r8rÝr9Ú fileobjectr[rYr”rˆr Ú extractfileÚ_find_link_target)r„rÊrÙr5r5r6rÌs      zTarFile.extractfilecCsV|jdƒ}|jdtjƒ}tjj|ƒ}|r@tjj|ƒ r@tj|ƒ|jƒsP|j ƒrj|j dd|j |j fƒn|j d|j ƒ|j ƒrŽ|j||ƒnŽ|jƒr¤|j||ƒnx|jƒrº|j||ƒnb|jƒsÊ|jƒrØ|j||ƒnD|jƒsè|j ƒrö|j||ƒn&|jtk�r|j||ƒn |j||ƒ|�rR|j|||ƒ|j ƒ�sR|j||ƒ|j||ƒdS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rër#z%s -> %sN)r)r¦r}r™r%ÚdirnamerlÚmakedirsr[rYrµr…rÞr8Úmakefiler(ÚmakedirraÚmakefifor]r_ÚmakedevÚmakelinkrÝr9Ú makeunknownrÂrÄrÃ)r„rÙÚ targetpathr½r¼Z upperdirsr5r5r6rÇ4s4       zTarFile._extract_memberc Cs*ytj|dƒWntk r$YnXdS)z,Make a directory called targetpath. iÀN)r}ÚmkdirÚFileExistsError)r„rÙrÖr5r5r6rÑeszTarFile.makedircCs’|j}|j|jƒ|j}t|dƒ�f}|jdk rrx,|jD]"\}}|j|ƒt|||t|ƒq6W|j|jƒ|j ƒnt|||jt|ƒWdQRXdS)z'Make a file called targetpath. riN) r”r¶rÕrsrmrÖrXrr'Útruncate)r„rÙrÖÚsourcer_ÚtargetrÂr'r5r5r6rÐos      zTarFile.makefilecCs"|j||ƒ|jdd|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Ör5r5r6rÕs zTarFile.makeunknowncCs"ttdƒrtj|ƒntdƒ‚dS)z'Make a fifo called targetpath. Úmkfifozfifo not supported by systemN)r‚r}rÜr )r„rÙrÖr5r5r6rÒ‡s  zTarFile.makefifocCsbttdƒ sttdƒ r tdƒ‚|j}|jƒr:|tjO}n |tjO}tj||tj |j |j ƒƒdS)zttdƒr:ytj||jƒWntk r8tdƒ‚YnXdS)zASet file permissions of targetpath according to tarinfo. rÄzcould not change modeN)r‚r}rÄrhr r )r„rÙrÖr5r5r6rÄÒs  z TarFile.chmodc CsHttdƒsdSytj||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Ör5r5r6rÃÛs  z TarFile.utimecCsæ|jdƒ|jdk r$|j}d|_|S|j|jjƒkrZ|jj|jdƒ|jjdƒsZtdƒ‚d}�xby|jj |ƒ}W�nHt k rÂ}z2|j r²|j dd|j|fƒ|jt 7_wbWYdd}~Xnütk �r,}zN|j �r|j dd|j|fƒ|jt 7_wbn|jdk�rtt|ƒƒ‚WYdd}~Xn’tk �rT|jdk�rPtdƒ‚Ynjtk �rŽ}z|jdk�r~tt|ƒƒ‚WYdd}~Xn0tk �r¼}ztt|ƒƒ‚WYdd}~XnXPqbW|dk �rÜ|jj|ƒ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)r�rwrÂr”rµr¶rZrrÙr2rwrprµr¼rHr rurvrxrtr¸ru)r„ÚmrÙrxr5r5r6r<æsL        z TarFile.nextcCsn|jƒ}|dk r"|d|j|ƒ…}|r2tjj|ƒ}x6t|ƒD]*}|rTtjj|jƒ}n|j}||kr<|SqzShow listing of a tarfile)Úmetavarrûz-ez --extractú+ú zExtract tarfile into target dir)Únargsrýrûz-cz--createúúzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validrz)rðz{!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.rŒrŽr�)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r{z{!r} file created.)rürÿ)rr)ÚargparseÚArgumentParserÚ add_argumentZadd_mutually_exclusive_groupÚ parse_argsZtestrrr’rormrñr³rPÚexitrLrr¿r/r}ÚcurdirZ format_helprÆZcreateÚpopr%Úsplitextr¸)rr÷ÚparserrBÚargsr\r�ZtfrróZtar_nameÚ_ZextZ compressionsZtar_modeZ tar_filesÚ file_namer5r5r6Úmain� sˆ                rÚ__main__)r%r&r'r(r)r*r+r,)artÚversionÚ __author__Z__date__Z __cvsid__Ú __credits__Úbuiltinsrrmrmr}rÉrWrfrªrSrr@r¬r˜r®r…ÚNotImplementedErrorrâr Ú NameErrorÚ__all__r0r¼rŒrûrörør÷rrär%rZrXr\r^rìr`ZCONTTYPErþrýr&r r r5r rrrr9rVr*rSrGrrErTr…r Úgetfilesystemencodingr7r<rKrQrUrXrgrpÚ Exceptionrr rrr r rurvrwrHrxryrˆÚobjectr‘rÁÚBufferedReaderrÔrrrrrqr5r5r5r6Úsú         wh , Z