Rei@sRddlmZdZdZdZdZdZdZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZyddlZddlZWnek rdZZYnXeefZyeef7ZWnek rYnXd d d d gZejdd kr5ddlZn ddlZejZdZdZedZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1d Z2d!Z3d"Z4dZ5d#Z6d$Z7e6Z8e&e'e(e)e,e-e.e*e+e/e0e1f Z9e&e'e.e1fZ:e/e0e1fZ;d%d&d'd(d)d*d+d,fZ<e=d%d&d+d,fZ>d-e?d.e?d(e?d)e@d*e@d'e@iZAd/ZBd0ZCd1ZDd2ZEd3ZFd4ZGd5ZHd6ZIdZJd7ZKd8ZLd9ZMd:ZNd;ZOd<ZPd=ZQd$ZRd#ZSe jTd>d?fkr%d@ZUn ejVZUdAdBZWdCdDZXdEdFZYd<e8dGdHZZdIdJZ[ddKdLZ\eBdMfeCdNfeDdOfeEdPfeFdQfeGdRffeKdSffeLdTffeMeHBdUfeHdVfeMdWffeNdSffeOdTffePeIBdUfeIdVfePdWffeQdSffeRdTffeSeJBdXfeJdYfeSdWfff Z]dZd[Z^Gd\d d e_Z`Gd]d^d^e`ZaGd_d`d`e`ZbGdadbdbe`ZcGdcdddde`ZdGdedfdfe`ZeGdgdhdheeZfGdidjdjeeZgGdkdldleeZhGdmdndneeZiGdodpdpeeZjGdqdrdrekZlGdsdtdtekZmGdudvdvekZnGdwdxdxekZoGdydzdzekZpGd{d|d|ekZqGd}d d ekZrGd~d d ekZsGdddekZtdd ZueZvesjZdS))print_functionz $Revision$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.NTarFileTarInfo is_tarfileTarErrorsisustar sustar00d01234567LKSxgXpathlinkpathsizemtimeuidgidunamegnameatimectimeiii`i@i iii@ ntcezutf-8cCs2|j||}|d||t|tS)z8Convert a string to a null-terminated bytes object. N)encodelenNUL)slengthencodingerrorsr5/builddir/build/BUILDROOT/alt-python35-pip-20.2.4-5.el8.x86_64/opt/alt/python35/lib/python3.5/site-packages/pip/_vendor/distlib/_backport/tarfile.pystnsr7cCs;|jd}|dkr+|d|}|j||S)z8Convert a null-terminated bytes object to a string. srN)finddecode)r1r3r4pr5r5r6ntss r<c Cs|dtdkr_y%tt|ddp1dd}Wqtk r[tdYqXnId}x@tt|dD](}|dK}|t||d7}q|W|S) z/Convert a number field to a python number. rr&asciistrict0r*zinvalid headerr)chrintr< ValueErrorInvalidHeaderErrorranger/ord)r1nir5r5r6ntis%  rHcCsd|kod|dknrHd|d|fjdt}n|tksh|d|dkrttd|dkrtjdtjd |d}t}x6t|dD]$}|j d|d @|dL}qW|j dd |S) z/Convert a python number to a number field. rr*rz%0*or=r%zoverflow in number fieldLlr&) r.r0 GNU_FORMATrBstructunpackpack bytearrayrDinsert)rFdigitsformatr1rGr5r5r6itns $$   " rTcCsdttjd|ddtjd|dd}dttjd|ddtjd |dd}||fS) aCalculate 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%Z148BNZ356BiZ148bZ356b)sumrMrN)bufunsigned_chksum signed_chksumr5r5r6 calc_chksumss @@r[cCs|dkrdS|dkrJx'|jd}|s5P|j|qWdSd}t||\}}xNt|D]@}|j|}t||krtd|j|qrW|dkr|j|}t||krtd|j|dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. rNr)izend of file reachedi@i@)readwritedivmodrDr/IOError)srcdstr2rXBUFSIZEblocks remainderbr5r5r6 copyfileobjs,      rfrJ-redcr;rwr1SxtTcCsfg}xPtD]H}x?|D]*\}}||@|kr|j|PqW|jdq Wdj|S)zcConvert a file's mode to a string of the form -rwxrwxrwx. Used by TarFile.list() rg)filemode_tableappendjoin)modeZpermtablebitcharr5r5r6filemode8s  rxc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r5r5r5r6rGs c@seZdZdZdS) ExtractErrorz%General exception for extract errors.N)ryrzr{r|r5r5r5r6r}Js r}c@seZdZdZdS) ReadErrorz&Exception for unreadable tar archives.N)ryrzr{r|r5r5r5r6r~Ms r~c@seZdZdZdS)CompressionErrorz.Exception for unavailable compression methods.N)ryrzr{r|r5r5r5r6rPs rc@seZdZdZdS) StreamErrorz=Exception for unsupported operations on stream-like TarFiles.N)ryrzr{r|r5r5r5r6rSs rc@seZdZdZdS) HeaderErrorz!Base exception for header errors.N)ryrzr{r|r5r5r5r6rVs rc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)ryrzr{r|r5r5r5r6rYs rc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)ryrzr{r|r5r5r5r6r\s rc@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)ryrzr{r|r5r5r5r6r_s rc@seZdZdZdS)rCzException for invalid headers.N)ryrzr{r|r5r5r5r6rCbs rCc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)ryrzr{r|r5r5r5r6res rc@sFeZdZdZddZddZddZdd Zd S) _LowLevelFilezLow-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsbdtjdtjtjBtjBi|}ttdrF|tjO}tj||d|_dS)NrjrkO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrropenfd)selfnamertr5r5r6__init__rs   z_LowLevelFile.__init__cCstj|jdS)N)rcloser)rr5r5r6r{sz_LowLevelFile.closecCstj|j|S)N)rr\r)rrr5r5r6r\~sz_LowLevelFile.readcCstj|j|dS)N)rr]r)rr1r5r5r6r]sz_LowLevelFile.writeN)ryrzr{r|rrr\r]r5r5r5r6rls   rc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dddZ dddZ ddZddZdS)_StreamaClass 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. cCsd|_|dkr-t||}d|_|dkrQt|}|j}|pZd|_||_||_||_||_d|_ d|_ d|_ y|dkryddl }Wnt k rtd YnX||_ |jd|_|d kr|jn |j|d kryddl}Wnt k rYtd YnX|d krd|_|j|_n|j|_Wn*|js|jjd|_ YnXdS) z$Construct a _Stream object. TNF*rprgzzzlib module is not availablerjbz2zbz2 module is not available) _extfileobjr _StreamProxy getcomptyperrtcomptypefileobjbufsizerXposclosedzlib ImportErrorrcrc32crc _init_read_gz_init_write_gzrdbufBZ2Decompressorcmp BZ2Compressorr)rrrtrrrrrr5r5r6rsP                          z_Stream.__init__cCs't|dr#|j r#|jdS)Nr)rrr)rr5r5r6__del__sz_Stream.__del__cCs|jjd|jj|jj |jjd|_tjdtt j }|j d|d|j j dr|j dd |_ |j |j j d d tdS) z6Initialize for writing with gzip compression. rzd?Zd@dAZ dBdCZ!dDdEZ"dFdGZ#dHdIZ$dJdKZ%dLdMZ&dNdOZ'dPdQZ(dRdSZ)dTdUZ*dVdWZ+dXdYZ,dZd[Z-d\d]Z.d^d_Z/d`daZ0dbdcZ1dddeZ2dfdgZ3dhS)jraInformational 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. rrtrr rrchksumtypelinknamer!r"devmajordevminorrr pax_headersrr_sparse_structs _link_targetrpcCs||_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. irrpN)rrtrr rrrREGTYPErrr!r"rrrrrr)rrr5r5r6rs"                zTarInfo.__init__cCs|jS)N)r)rr5r5r6_getpathszTarInfo._getpathcCs ||_dS)N)r)rrr5r5r6_setpathszTarInfo._setpathcCs|jS)N)r)rr5r5r6 _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)r)rrr5r5r6 _setlinkpathszTarInfo._setlinkpathcCs d|jj|jt|fS)Nz<%s %r at %#x>) __class__ryrid)rr5r5r6__repr__szTarInfo.__repr__cCsd|jd|jd@d|jd|jd|jd|jd|jd |jd |jd |j d |j d |j d|j i }|d t kr|djd r|dd7<|S)z9Return the TarInfo's attributes as a dictionary. rrtirr rrrrrr!r"rr/)rrtrr rrrrrr!r"rrDIRTYPEr)rinfor5r5r6get_infos             $zTarInfo.get_infosurrogateescapecCsv|j}|tkr+|j|||S|tkrJ|j|||S|tkrf|j||StddS)zy||jd d Wn#tk r||||YnXt|||kr>||||WxdD]}\}}||krd||rr*r r rrrrr)r!r!r()r"r"r(rr*r r*rrrr)rrrr)r rcopyr r r.UnicodeEncodeErrorr/ isinstancefloatstr_create_pax_generic_headerXHDTYPErr) rrr3rrhnamer2rRvalrXr5r5r6rs4      4zTarInfo.create_pax_headercCs|j|tdS)zAReturn the object as a pax global header block sequence. utf8)rXGLTYPE)clsrr5r5r6create_pax_global_headerDsz TarInfo.create_pax_global_headercCs|dtd}x*|r@|ddkr@|dd}qW|t|d}|dd}| st|tkrtd||fS)zUSplit a name longer than 100 chars into a prefix and a name part. Nrrzname is too longr8r8r8) LENGTH_PREFIXr/r rB)rrr r5r5r6r Js zTarInfo._posix_split_namecCst|jddd||t|jddd@d|t|jddd|t|jd dd|t|jd dd |t|jd dd |d |jdtt|jddd|||jdtt|jddd||t|jddd||t|jddd|t|jddd|t|jddd||g}tjdtdj|}t |t dd}|ddd|j d|d d}|S)!zReturn a header block. info is a dictionary with file information, format must be one of the *_FORMAT constants. rrpr rtrir*rr rrrs rrrr!r(r"rrr r z%dsrNilz%06or=ieii) r7getrTrr rMrOrrsr[r.)rrSr3r4partsrXrr5r5r6rYs&$/zTarInfo._create_headercCs=tt|t\}}|dkr9|t|t7}|S)zdReturn the string payload filled with zero bytes up to the next 512 byte border. r)r^r/rr0)payloadrcrdr5r5r6_create_payloadus zTarInfo._create_payloadcCsm|j||t}i}d|d<||dTrs21 hdrcharset=BINARY rrrr= =s z././@PaxHeaderrrrrr) itemsr.rr/rbytesr rrr)) r#rrr3binarykeywordvaluerecordsrJrFr;rr5r5r6rs<      1   z"TarInfo._create_pax_generic_headerc CsNt|dkrtdt|tkr<td|jttkr]tdt|dd}|t|krt 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 jdr't|_|jtkrd}g}xtdD]u} y<t|||d} t||d|d} Wntk rPYnX|j| | f|d7}qOWt|d} t|dd} || | f|_ |j!r!|j j"d|_ |rJ|jt#krJ|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headerrUrVz bad checksumr lt|ii i)iIiQiYirir+riii)$r/rrrcountr0rrHr[rCr<rrtrr rrrrrr!r"rrAREGTYPErrGNUTYPE_SPARSErDrBrrboolrisdirrstrip GNU_TYPES)r#rXr3r4robjr rstructsrGrnumbytes isextendedorigsizer5r5r6frombufsZ      ! "  zTarInfo.frombufcCsP|jjt}|j||j|j}|jjt|_|j|S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr\rrDr3r4rr _proc_member)r#rrXr?r5r5r6 fromtarfileszTarInfo.fromtarfilecCst|jttfkr"|j|S|jtkr>|j|S|jtttfkrc|j |S|j |SdS)zYChoose the right processing method depending on the type and call it. N) rrr _proc_gnulongr: _proc_sparserr"SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr5r5r6rEs   zTarInfo._proc_membercCsu|jj|_|j}|js6|jtkrL||j|j7}||_|j |j |j |j |S)zfProcess a builtin type or an unknown type which will be treated as a regular file. ) rrrisregrSUPPORTED_TYPES_blockrr_apply_pax_inforr3r4)rrrr5r5r6rK$s  zTarInfo._proc_builtinc Cs|jj|j|j}y|j|}Wntk rQtdYnX|j|_|jt krt ||j |j |_ n*|jtkrt ||j |j |_|S)zSProcess the blocks that hold a GNU longname or longlink member. z missing or bad subsequent header)rr\rNrrFrrrrrr<r3r4rrr)rrrXnextr5r5r6rG5s  zTarInfo._proc_gnulongc Cs#|j\}}}|`x|r|jjt}d}xtdD]}y<t|||d}t||d|d} Wntk rPYnX|r| r|j|| f|d7}qFWt|d}qW||_ |jj |_ |j |j |j |_||_ |S)z8Process a GNU sparse header plus extra headers. rrr7i)rrr\rrDrHrBrrr;rrrrNrr) rrr@rBrCrXrrGrrAr5r5r6rHKs( "    zTarInfo._proc_sparsec Cs|jj|j|j}|jtkr9|j}n|jj}tj d|}|dk r|j dj d|d<|j d}|dkr|j }nd}tjd}d}x|j||}|sP|j\} } t| } ||jd d|jd| d} |j| dd|j} | tkrt|j| ||j |j} n|j| dd|j} | || <|| 7}qWy|j|} Wntk rtd YnXd |kr|j| |n_d |kr|j| ||n=|j d dkrY|j ddkrY|j| |||jttfkr| j||j |j|j | _ d|kr| j!} | j"s| jt#kr| | j| j7} | |_ | S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNrr! hdrcharsetBINARYs(\d+) ([^=]+)=rrz missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorr?r)$rr\rNrrr"rrresearchgroupr:r&r3compilematchgroupsrAendr_decode_pax_fieldr4PAX_NAME_FIELDSrFrr_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rrIrOrrrLrM)rrrXrrYrRr3regexrr2r/r0rPrr5r5r6rJgs`     .        *    zTarInfo._proc_paxcCsg}x6tjd|D]"}|jt|jdqWg}x6tjd|D]"}|jt|jdqXWtt|||_dS)z?Process a GNU tar extended sparse header, version 0.0. s\d+ GNU.sparse.offset=(\d+)\nrs\d+ GNU.sparse.numbytes=(\d+)\nN)rUfinditerrrrArWlistzipr)rrPrrXoffsetsrYrAr5r5r6r_s  zTarInfo._proc_gnusparse_00cCsVdd|djdD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|]}t|qSr5)rA).0rmr5r5r6 s z.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nrr)splitrcrdr)rrPrrr5r5r6r^s zTarInfo._proc_gnusparse_01cCsd}g}|jjt}|jdd\}}t|}xgt||dkrd|kr}||jjt7}|jdd\}}|jt|qEW|jj|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Ns rr) rr\rrirAr/rrrrrcrdr)rrPrrfieldsrrXnumberr5r5r6r`s  zTarInfo._proc_gnusparse_10c Cs x|jD]\}}|dkr8t|d|q |dkr]t|dt|q |dkrt|dt|q |tkr |tkryt||}Wntk rd}YnX|dkr|jd}t|||q W|j|_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namerzGNU.sparse.sizerzGNU.sparse.realsizerrN) r,setattrrA PAX_FIELDSPAX_NUMBER_FIELDSrBr=rr)rrr3r4r/r0r5r5r6rOs"        zTarInfo._apply_pax_infoc Cs=y|j|dSWn"tk r8|j||SYnXdS)z1Decode a single field from a pax record. r>N)r:UnicodeDecodeError)rr0r3fallback_encodingfallback_errorsr5r5r6r\s 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)r^r)rr8rcrdr5r5r6rN s zTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr5r5r6rLsz TarInfo.isregcCs |jS)N)rL)rr5r5r6isfileszTarInfo.isfilecCs |jtkS)N)rr)rr5r5r6r<sz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr5r5r6issymsz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr5r5r6islnksz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr5r5r6ischr sz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr5r5r6isblk"sz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr5r5r6isfifo$szTarInfo.isfifocCs |jdk S)N)r)rr5r5r6issparse&szTarInfo.issparsecCs|jtttfkS)N)rrxrzr|)rr5r5r6isdev(sz TarInfo.isdevN)rrtrr rrrrrr!r"rrrrrrrrr)4ryrzr{r| __slots__rrrpropertyrrrrrrDEFAULT_FORMATENCODINGrrrr classmethodr$r  staticmethodrr)rrrDrFrErKrGrHrJr_r^r`rOr\rNrLrsr<rurwryr{r}r~rr5r5r5r6rs`         1  3?    f             c@seZdZdZdZdZdZdZeZ e Z dZ e ZeZdddddddddddddd Zeddded d Zeddd d ZedddddZedddddZddddddiZddZddZddZdd Zdddd!d"Zd#d$d%Zdd#ddd&d'Zdd(d)Z d*dd+d,Z!d-d#d.d/Z"d0d1Z#d#d2d3Z$d4d5Z%d6d7Z&d8d9Z'd:d;Z(d<d=Z)d>d?Z*d@dAZ+dBdCZ,dDdEZ-dFdGZ.dddHdIZ/dJdKZ0ddLdMZ1dNdOZ2dPdQZ3dRdSZ4dTdUZ5dVdWZ6dS)Xrz=The TarFile Class provides an interface to tar archives. rFrNrjrc Cst|dks|dkr*td||_dddddd i||_|s|jdkrtjj| rd|_d |_t||j}d |_nH|d krt |d r|j }t |d r|j|_d|_|r tjj |nd |_ ||_ |d k r1||_ |d k rF||_|d k r[||_|d k rp||_|d k r||_| |_| d k r|j tkr| |_n i|_| d k r| |_| d k r| |_d |_g|_d |_|j j|_i|_y:|jdkrHd |_|j|_|jdkrx|j j|jy&|jj |}|jj!|WqZt"k r|j j|jPYqZt#k r} zt$t%| WYd d } ~ XqZXqZW|jdkrWd|_|jrW|jj&|jj'}|j j(||jt|7_Wn*|jst|j j)d|_YnXd S)aOpen 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. rrzmode must be 'r', 'a' or 'w'rjrbazr+brkwbFNrrtTaw)*r/rBrt_moderrexists bltn_openrrrabspathrrSr dereference ignore_zerosr3r4rrdebug errorlevelrmembers_loadedrrinodes firstmemberrPrrFrrrrr~rr$rr]r)rrrtrrSrrrr3r4rrrerXr5r5r6rFs   "      !                        )     zTarFile.__init__c Ks;| r| rtd|dkrx|jD]}t||j|}|dk rd|j}y||d||SWq0ttfk r} z!|dk r|j|w0WYdd} ~ Xq0Xq0WtdnSd|kr_|jdd\} }| pd} |pd}||jkr<t||j|}ntd |||| ||Sd |kr |jd d\} }| pd} |pd}| d krtd t|| |||} y||| | |} Wn| j YnXd | _ | S|dkr+|j ||||StddS)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 '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 '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 '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 znothing to openrjr:*Nz%file could not be opened successfully:rrzunknown compression type %r|rwzmode must be 'r' or 'w'Frzundiscernible mode)rjr) rB OPEN_METHrrr~rrrirrrtaropen) r#rrtrrkwargsrfunc saved_posrrxstreamrnr5r5r6rsN                 z TarFile.opencKs=t|dks|dkr*td|||||S)zCOpen uncompressed tar archive name for reading or writing. rrzmode must be 'r', 'a' or 'w')r/rB)r#rrtrrr5r5r6rs zTarFile.taropenrc Ks-t|dks|dkr*tdyddl}|jWn$ttfk rgtdYnX|dk }y8|j||d||}|j||||}Wnqtk r| r|dk r|j |dkrt dYn(| r|dk r|j YnX||_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. rrzmode must be 'r' or 'w'rNzgzip module is not availablereznot a gzip file) r/rBgzipGzipFilerAttributeErrorrrr_rr~r) r#rrtr compresslevelrrZ extfileobjrnr5r5r6gzopens.         zTarFile.gzopencKst|dks|dkr*tdyddl}Wntk rZtdYnX|dk ryt||}n|j||d|}y|j||||}Wn.tt fk r|j t dYnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. rrzmode must be 'r' or 'w'.rNzbz2 module is not availablerznot a bzip2 fileF) r/rBrrrrBZ2Filerr_EOFErrorrr~r)r#rrtrrrrrnr5r5r6bz2open$s      zTarFile.bz2openrrrrrrcCs|jr dS|jdkr|jjttd|jtd7_t|jt\}}|dkr|jjtt||j s|jj d|_dS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NrrrT) rrtrr]r0rrr^ RECORDSIZErr)rrcrdr5r5r6rHs    z TarFile.closecCs/|j|}|dkr+td||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) _getmemberKeyError)rrrr5r5r6 getmember\s zTarFile.getmembercCs$|j|js|j|jS)zReturn 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)rr5r5r6 getmembersgs   zTarFile.getmemberscCsdd|jDS)zReturn the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|]}|jqSr5)r)rfrr5r5r6rgus z$TarFile.getnames..)r)rr5r5r6getnamesqszTarFile.getnamesc CsI|jd|dk r"|j}|dkr4|}tjj|\}}|jtjd}|jd}|j}||_ |dkrt tdr|j rtj |}qtj |}ntj|j}d}|j}t j|r|j|jf} |j rd|jdkrd| |jkrd||j| krdt} |j| }qt} | dr||j| zlink to)rprintrxrtr!rr"r ryr{rrrr localtimerrr<rurrw)rverboserr5r5r6rcs&   $)  z TarFile.listc Cs|jd|dkr|}|dk rnddl}|jdtd||rn|jdd|dS|jdk rtjj||jkr|jdd|dS|jd||j ||}|dkr|jdd |dS|dk r2||}|dkr2|jdd|dS|j rjt |d }|j |||j n|jr|j ||rxatj|D]@}|jtjj||tjj||||d |qWn |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. rNrzuse the filter argument insteadrztarfile: Excluded %rztarfile: Skipped %rrztarfile: Unsupported type %rrfilter)rwarningswarnDeprecationWarning_dbgrrrrrrLraddfilerr<listdiraddrs) rrr recursiveexcluderrrfr5r5r6rsD       *        *z TarFile.addcCs|jdtj|}|j|j|j|j}|jj||jt |7_|dk rt ||j|j t |j t \}}|dkr|jjtt ||d7}|j|t 7_|jj|dS)a]Add the TarInfo object `tarinfo' to the archive. If `fileobj' is given, tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects using gettarinfo(). On Windows platforms, `fileobj' should always be opened with mode 'rb' to avoid irritation about the file size. rNrr)rrrrSr3r4rr]rr/rfrr^rr0rrr)rrrrXrcrdr5r5r6r4s    zTarFile.addfile.cCsIg}|dkr|}x\|D]T}|jrV|j|tj|}d|_|j||d|j qW|jddd|jx|D]}tjj ||j }y4|j |||j |||j ||Wqtk r@}z.|jdkrn|jdd|WYdd}~XqXqWdS) aMExtract 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(). Ni set_attrskeycSs|jS)N)r)rr5r5r6dsz$TarFile.extractall..rz tarfile: %s)r<rrrrtextractsortreverserrrsrchownutimechmodr}rr)rrr directoriesrdirpathrr5r5r6 extractallNs*     !  zTarFile.extractallrpcCs^|jdt|tr.|j|}n|}|jr[tjj||j|_ y,|j |tjj||j d|Wnt k r }zc|j dkrnI|jdkr|jdd|jn |jdd|j|jfWYdd}~XnNtk rY}z.|j dkr3n|jdd|WYdd}~XnXdS)axExtract 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. rjrrNrz tarfile: %sztarfile: %s %r)rrrrrwrrrsrr_extract_memberrEnvironmentErrorrfilenamerstrerrorr})rmemberrrrrr5r5r6rts&  ! 2zTarFile.extractcCs|jdt|tr.|j|}n|}|jrP|j||S|jtkro|j||S|js|j rt|j t rt dq|j |j|SndSdS)aExtract a member from the archive as a file object. `member' may be a filename or a TarInfo object. If `member' is a regular file, a file-like object is returned. If `member' is a link, a file-like object is constructed from the link's target. If `member' is none of the above, None is returned. The file-like object is read-only and provides the following methods: read(), readline(), readlines(), seek() and tell() rjz'cannot extract (sym)link as file objectN)rrrrrL fileobjectrrMrwrurrr extractfile_find_link_target)rrrr5r5r6rs  zTarFile.extractfilecCs|jd}|jdtj}tjj|}|r\tjj| r\tj||jst|j r|j dd|j |j fn|j d|j |j r|j||n|jr|j||n|jr|j||n|js|jr2|j||n]|jsJ|j r]|j||n2|jtkr|j||n|j|||r|j|||j s|j|||j||dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rrz%s -> %sN)r=rrrrdirnamermakedirsrwrurrrrLmakefiler<makedirr}makefiforyr{makedevmakelinkrrM makeunknownrrr)rr targetpathr upperdirsr5r5r6rs4 #    zTarFile._extract_membercCsUytj|dWn:tk rP}z|jtjkr>WYdd}~XnXdS)z,Make a directory called targetpath. iN)rmkdirrerrnoEEXIST)rrrrr5r5r6rs zTarFile.makedircCs|j}|j|jt|d}|jdk rqxJ|jD])\}}|j|t|||qAWnt|||j|j|j|j|jdS)z'Make a file called targetpath. rN) rrrrrrfrtruncater)rrrsourcetargetrrr5r5r6rs   zTarFile.makefilecCs+|j|||jdd|jdS)zYMake a file from a TarInfo object with an unknown type at targetpath. rz9tarfile: Unknown file type %r, extracted as regular file.N)rrr)rrrr5r5r6r s zTarFile.makeunknowncCs/ttdrtj|n tddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rrrr})rrrr5r5r6r szTarFile.makefifocCsttd s ttd r,td|j}|jrQ|tjO}n |tjO}tj||tj |j |j dS)z&# &0       1     c@s@eZdZdZddZddZddZeZdS) r zMIterator Class. for tarinfo in TarFile(...): suite... cCs||_d|_dS)z$Construct a TarIter object. rN)rr)rrr5r5r6r s zTarIter.__init__cCs|S)z Return iterator object. r5)rr5r5r6r szTarIter.__iter__c Cs{|jjs6|jj}|shd|j_tn2y|jj|j}Wntk rgtYnX|jd7_|S)zReturn the next item using TarFile's next() method. When all members have been read, set TarFile as _loaded. Tr)rrrP StopIterationrr IndexError)rrr5r5r6__next__ s     zTarIter.__next__N)ryrzr{r|rrr*rPr5r5r5r6r  s    r c Cs;yt|}|jdSWntk r6dSYnXdS)zfReturn True if name points to a tar archive that we are able to handle, else return False. TFN)rrr)rrnr5r5r6r# s    )w __future__r __version__version __author____date__ __cvsid__ __credits__rrrrrrMrrUrrrrNotImplementedErrorrZ WindowsError NameError__all__ version_info __builtin__builtinsr_openr0rrrr r r r%rr9rvrtrxrzrr|CONTTYPErrr:rr"rIrrLrrrMrrr>rmsetr]rrArnS_IFLNKS_IFREGr S_IFDIRr S_IFIFOZTSUIDZTSGIDZTSVTXZTUREADZTUWRITEZTUEXECZTGREADZTGWRITEZTGEXECZTOREADZTOWRITEZTOEXECrrgetfilesystemencodingr7r<rHrTr[rfrqrx Exceptionrr}r~rrrrrrrCrobjectrrrrrrrrr rrr5r5r5r6s,                                                 ?K*