3 Rei@sNddlmZdZdZdZdZdZdZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZyddlZddlZWnek rdZZYnXeefZyeef7ZWnek rYnXd d d d gZejdd krddlZnddlZejZdZdZedZ 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;dZe?e?e?e@e@e@d-ZAd.ZBd/ZCd0ZDd1ZEd2ZFd3ZGd4ZHd5ZIdZJd6ZKd7ZLd8ZMd9ZNd:ZOd;ZPdr&zoverflow in number fieldLlr') r/r1 GNU_FORMATrCstructunpackpack bytearrayrEinsert)rGdigitsformatr2rHr6r6r7itns   rUcCshdttjd|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)sumrNrO)bufunsigned_chksum signed_chksumr6r6r7 calc_chksumss 00r\cCs|dkr dS|dkr8x|jd}|s&P|j|qWdSd}t||\}}x8t|D],}|j|}t||krvtd|j|qTW|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@)readwritedivmodrEr0IOError)srcdstr3rYZBUFSIZEblocks remainderbr6r6r7 copyfileobjs,      rfrK-redcr<rwr2SxtTcCsPg}x@tD]8}x2|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)modeZpermtablebitcharr6r6r7filemode8s   rxc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r6r6r6r7rGsc@seZdZdZdS) ExtractErrorz%General exception for extract errors.N)ryrzr{r|r6r6r6r7r}Jsr}c@seZdZdZdS) ReadErrorz&Exception for unreadable tar archives.N)ryrzr{r|r6r6r6r7r~Msr~c@seZdZdZdS)CompressionErrorz.Exception for unavailable compression methods.N)ryrzr{r|r6r6r6r7rPsrc@seZdZdZdS) StreamErrorz=Exception for unsupported operations on stream-like TarFiles.N)ryrzr{r|r6r6r6r7rSsrc@seZdZdZdS) HeaderErrorz!Base exception for header errors.N)ryrzr{r|r6r6r6r7rVsrc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)ryrzr{r|r6r6r6r7rYsrc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)ryrzr{r|r6r6r6r7r\src@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)ryrzr{r|r6r6r6r7r_src@seZdZdZdS)rDzException for invalid headers.N)ryrzr{r|r6r6r6r7rDbsrDc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)ryrzr{r|r6r6r6r7resrc@s0eZdZdZddZddZddZdd Zd S) _LowLevelFilezLow-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsFtjtjtjBtjBd|}ttdr2|tjO}tj||d|_dS)N)rjrkO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrropenfd)selfnamertr6r6r7__init__rs   z_LowLevelFile.__init__cCstj|jdS)N)rcloser)rr6r6r7r{sz_LowLevelFile.closecCstj|j|S)N)rr]r)rrr6r6r7r]~sz_LowLevelFile.readcCstj|j|dS)N)rr^r)rr2r6r6r7r^sz_LowLevelFile.writeN)ryrzr{r|rrr]r^r6r6r6r7rls  rc@steZdZdZddZddZddZdd 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. cCsRd|_|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)rYr0rrr^)rr2r6r6r7__writesz_Stream.__writecCs|jr dS|jdkr2|jdkr2|j|jj7_|jdkr|jr|jj|jd|_|jdkr|jjtj d|j d@|jjtj d|j d@|j s|jj d|_dS) z[Close the _Stream object. No operation should be done on it afterwards. Nrkrrrzd?Zd@dAZ dBdCZ!dDdEZ"dFdGZ#dHdIZ$dJdKZ%dLdMZ&dNdOZ'dPdQZ(dRdSZ)dTdUZ*dVdWZ+dXdYZ,dZd[Z-d\d]Z.d^d_Z/d`daZ0dbdcZ1dddeZ2dfdgZ3dhS)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. rrtr r!rrchksumtypelinknamer"r#devmajordevminorrr pax_headersrr_sparse_structs _link_targetrpcCsj||_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)rrtr r!rrrREGTYPErrr"r#rrrrrr)rrr6r6r7rs"zTarInfo.__init__cCs|jS)N)r)rr6r6r7_getpathszTarInfo._getpathcCs ||_dS)N)r)rrr6r6r7_setpathszTarInfo._setpathcCs|jS)N)r)rr6r6r7 _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)r)rrr6r6r7 _setlinkpathszTarInfo._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>) __class__ryrid)rr6r6r7__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) rrtr r!rrrrrr"r#rrrr/)rrtr r!rrrrrr"r#rrDIRTYPEr)rinfor6r6r7get_infos  zTarInfo.get_infosurrogateescapecCsT|j}|tkr|j|||S|tkr4|j|||S|tkrH|j||StddS)zr?r r+r!r rrrrr)r"r"r))r#r#r)r r+r!r+rrrr)rrrr)r rcopyrr r/UnicodeEncodeErrorr0 isinstancefloatstr_create_pax_generic_headerXHDTYPErr) rrr4rrhnamer3rSvalrYr6r6r7rs4  .  zTarInfo.create_pax_headercCs|j|tdS)zAReturn the object as a pax global header block sequence. utf8)rXGLTYPE)clsrr6r6r7create_pax_global_headerDsz TarInfo.create_pax_global_headercCsp|dtd}x |r0|ddkr0|dd}qW|t|d}|dd}| s`t|tkrhtd||fS)zUSplit a name longer than 100 chars into a prefix and a name part. Nrrzname is too longr9r9r9) LENGTH_PREFIXr0rrC)rrr r6r6r7rJs zTarInfo._posix_split_namecCsVt|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+r r!rrrs rrr r"r)r#rrr r z%dsrNilz%06or>ieii) r8getrUrr rNrPrrsr\r/)rrTr4r5partsrYrr6r6r7rYs&  &zTarInfo._create_headercCs.tt|t\}}|dkr*|t|t7}|S)zdReturn the string payload filled with zero bytes up to the next 512 byte border. r)r_r0rr1)payloadrcrdr6r6r7_create_payloaduszTarInfo._create_payloadcCsR|j||t}i}d|d<||d<t||d<t|d<|j|t|||j|S)zTReturn a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence for name. z ././@LongLinkrrrr )r/r1r0rrrr+)r%rrr4r5rr6r6r7rs zTarInfo._create_gnu_long_headerc Cs:d}x@|jD]4\}}y|jddWqtk r@d}PYqXqWd}|rV|d7}x|jD]\}}|jd}|r|j|d}n |jd}t|t|d}d } } x"|tt| } | | krP| } qW|tt| d d |d |d 7}q`Wi} d| d<|| d<t|| d<t| d<|j| td d|j |S)zReturn a POSIX.1-2008 extended or global header sequence that contains a list of keyword, value pairs. The values must be strings. Fr#r?Trs21 hdrcharset=BINARY rrrr> =rz././@PaxHeaderrrrr r) itemsr/rr0rbytesr rrr+) r%rrr4binarykeywordvaluerecordsrKrGr<rr6r6r7rs<   * z"TarInfo._create_pax_generic_headerc Cstt|dkrtdt|tkr(td|jttkr>tdt|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 jdrt|_|jtkr6d}g}xrtdD]f} y0t|||d} t||d|d} Wntk rPYnX|j| | f|d7}qWt|d} t|dd} || | f|_ |j!rN|j j"d|_ |rp|jt#krp|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headerrVrWz bad checksumr lt|ii i)iIiQiYirir,riii)$r0rrrcountr1rrIr\rDr=rrtr r!rrrrrr"r#rrAREGTYPErrGNUTYPE_SPARSErErCrrboolrisdirrstrip GNU_TYPES)r%rYr4r5robjr rstructsrHrnumbytes isextendedorigsizer6r6r7frombufsZ       zTarInfo.frombufcCs8|jjt}|j||j|j}|jjt|_|j|S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr]rrFr4r5rr _proc_member)r%rrYrAr6r6r7 fromtarfiles 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) rrr _proc_gnulongr< _proc_sparser r$SOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr6r6r7rGs    zTarInfo._proc_membercCsR|jj|_|j}|js$|jtkr4||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_inforr4r5)rrrr6r6r7rM$s zTarInfo._proc_builtinc Cs|jj|j|j}y|j|}Wntk r>tdYnX|j|_|jt krft ||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]rPrrHrrrrrr=r4r5rrr)rrrYnextr6r6r7rI5s  zTarInfo._proc_gnulongc Cs|j\}}}|`x|r|jjt}d}xvtdD]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. rrr9i)rrr]rrErIrCrrr=rrrrPrr) rrrBrDrErYrrHrrCr6r6r7rJKs(    zTarInfo._proc_sparsec Cs |jj|j|j}|jtkr&|j}n |jj}tj d|}|dk rX|j dj d|d<|j d}|dkrr|j }nd}tjd}d}x|j||}|sP|j\} } t| } ||jd d|jd| d} |j| dd|j} | tkr|j| ||j |j} n|j| dd|j} | || <|| 7}qWy|j|} Wntk rTtd YnXd |krn|j| |nHd |kr|j| ||n.|j d dkr|j ddkr|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]rPrrr$rrresearchgroupr;r(r4compilematchgroupsrBendr_decode_pax_fieldr5PAX_NAME_FIELDSrHrr_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10r rKrQrrrNrO)rrrYrr[rTr4regexrr3r1r2rRrr6r6r7rLgs`       $          zTarInfo._proc_paxcCspg}x(tjd|D]}|jt|jdqWg}x(tjd|D]}|jt|jdq@Wtt|||_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)rWfinditerrrrBrYlistzipr)rrRrrYoffsetsr[rCr6r6r7raszTarInfo._proc_gnusparse_00cCs@dd|djdD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|qSr6)rB).0rmr6r6r7 sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nrr)splitrerfr)rrRrrr6r6r7r`szTarInfo._proc_gnusparse_01cCsd}g}|jjt}|jdd\}}t|}xJt||dkrvd|krV||jjt7}|jdd\}}|jt|q.W|jj|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nrrr) rr]rrkrBr0rrrrrerfr)rrRrrfieldsrrYnumberr6r6r7rbs  zTarInfo._proc_gnusparse_10c Csx|jD]\}}|dkr(t|d|q |dkrBt|dt|q |dkr\t|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.setattrrB PAX_FIELDSPAX_NUMBER_FIELDSrCr?rr)rrr4r5r1r2r6r6r7rQs"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. r?N)r;UnicodeDecodeError)rr2r4fallback_encodingfallback_errorsr6r6r7r^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)rr:rcrdr6r6r7rP szTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr6r6r7rNsz TarInfo.isregcCs|jS)N)rN)rr6r6r7isfileszTarInfo.isfilecCs |jtkS)N)rr)rr6r6r7r>sz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr6r6r7issymsz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr6r6r7islnksz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr6r6r7ischr sz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr6r6r7isblk"sz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr6r6r7isfifo$szTarInfo.isfifocCs |jdk S)N)r)rr6r6r7issparse&szTarInfo.issparsecCs|jtttfkS)N)rrzr|r~)rr6r6r7isdev(sz TarInfo.isdevN)rrtr r!rrrrrr"r#rrrrrrrrr)rp)4ryrzr{r| __slots__rrrpropertyrrrrrrDEFAULT_FORMATENCODINGr rrr classmethodr&r staticmethodrr+rrrFrHrGrMrIrJrLrar`rbrQr^rPrNrur>rwryr{r}rrrr6r6r6r7rs`    1    3 ?  f  c @seZdZdZdZdZdZdZeZ e Z dZ e ZeZdVdd Zedddefd d ZedWd d ZedXddZedYddZddddZddZddZddZddZdZdd Zd[d"d#Zd\d$d%Zd]d&d'Z d^d)d*Z!d_d,d-Z"d.d/Z#d`d0d1Z$d2d3Z%d4d5Z&d6d7Z'd8d9Z(d:d;Z)dd?Z+d@dAZ,dBdCZ-dDdEZ.dadFdGZ/dHdIZ0dbdJdKZ1dLdMZ2dNdOZ3dPdQZ4dRdSZ5dTdUZ6dS)crz=The TarFile Class provides an interface to tar archives. rFrNrjrc Cst|dks|dkrtd||_dddd||_|sp|jdkr\tjj| r\d |_d|_t||j}d |_n0|d krt |d r|j }t |d r|j|_d|_|rtjj |nd |_ ||_ |d k r||_ |d k r||_|d k r||_|d k r||_|d k r||_| |_| d k r(|j tkr(| |_ni|_| d k r>| |_| d k rN| |_d |_g|_d |_|j j|_i|_y|jdkrd |_|j|_|jdkr$x|j j|jy|jj |}|jj!|WnTt"k r|j j|jPYn0t#k r} zt$t%| WYd d } ~ XnXqW|jdkrnd|_|jrn|jj&|jj'}|j j(||jt|7_Wn&|js|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'rbzr+bwb)rjarkrrkFNrrtTrjaw)*r0rCrt_moderrexists bltn_openrrrabspathrrTr dereference ignore_zerosr4r5rrdebug errorlevelrmembers_loadedrrinodes firstmemberrRrrHrrrrr~rr&rr^r)rrrtrrTrrrr4r5rrrerYr6r6r7rFs           $   zTarFile.__init__c Ks| r| rtd|dkrxz|jD]p}t||j|}|dk rH|j}y||d|f|Sttfk r} z|dk r|j|w$WYdd} ~ Xq$Xq$Wtdnd|kr|jdd\} }| pd} |pd}||jkrt||j|}n td |||| |f|Sd |kr|jd d\} }| p(d} |p2d}| d krFtd t|| |||} y||| | f|} Wn| j YnXd | _ | S|dkr|j |||f|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) rC OPEN_METHrrr~rrrkrrrtaropen) r%rrtrrkwargsrfunc saved_posrrxstreamrnr6r6r7rsN            z TarFile.opencKs,t|dks|dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. rrzmode must be 'r', 'a' or 'w')r0rC)r%rrtrrr6r6r7rszTarFile.taropenrc Kst|dks|dkrtdyddl}|jWn ttfk rNtdYnX|dk }y*|j||d||}|j|||f|}Wn^tk 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) r0rCgzipGzipFilerAttributeErrorrrr`rr~r) r%rrtr compresslevelrrZ extfileobjrnr6r6r7gzopens.  zTarFile.gzopencKst|dks|dkrtdy ddl}Wntk rDtdYnX|dk rZt||}n|j|||d}y|j|||f|}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 available)rznot a bzip2 fileF) r0rCrrrrBZ2Filerr`EOFErrorrr~r)r%rrtrrrrrnr6r6r7bz2open$s   zTarFile.bz2openrrr)rrrcCs|jr dS|jdkrf|jjttd|jtd7_t|jt\}}|dkrf|jjtt||j sv|jj d|_dS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NrrrT) rrtrr^r1rrr_ RECORDSIZErr)rrcrdr6r6r7rHs  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) _getmemberKeyError)rrrr6r6r7 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)rr6r6r7 getmembersgszTarFile.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|] }|jqSr6)r)rhrr6r6r7riusz$TarFile.getnames..)r)rr6r6r7getnamesqszTarFile.getnamesc Csp|jd|dk r|j}|dkr$|}tjj|\}}|jtjd}|jd}|j}||_ |dkrt tdr|j rtj |}qtj |}ntj|j}d}|j}t j|r|j|jf} |j r|jdkr| |jkr||j| krt} |j| }nt} | drx||j| <nht j|r"t} nVt j|r4t} nDt j|rPt} tj|}n(t j|rbt } nt j!|rtt"} ndS||_||_#|j$|_%|j&|_'| tkr|j(|_)nd|_)|j*|_+| |_,||_-t.ryt.j/|j%d|_0Wnt1k rYnXt2r*yt2j3|j'd|_4Wnt1k r(YnX| t t"fkrlt tdrlt td rltj5|j6|_7tj8|j6|_9|S) aOCreate a TarInfo object for either the file `name' or the file object `fileobj' (using os.fstat on its file descriptor). You can modify some of the TarInfo's attributes before you add it using addfile(). If given, `arcname' specifies an alternative name for the file in the archive. rNrlstatrprrmajorminor):rrrr splitdriverseplstriprrrrrstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrxrS_ISDIRrS_ISFIFOr~S_ISLNKrvreadlinkS_ISCHRrzS_ISBLKr|rtst_uidr st_gidr!st_sizerst_mtimerrrpwdgetpwuidr"rgrpgetgrgidr#rst_rdevrrr) rrarcnamerdrvrstatresrstmdinoderr6r6r7 gettarinfows~                 zTarFile.gettarinfoTcCs |jx|D]}|rtt|jddtd|jp6|j|jp@|jfdd|js\|j rztdd|j |j fddntd|j ddtdt j|jdd ddt|j|jrd nd dd|r|jrtd |jdd|jrtd |jddtqWdS)zPrint 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.  )r]z%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02dNrrrpz->zlink to)rprintrxrtr"r r#r!r{r}rrrr localtimerrr>rwrry)rverboserr6r6r7res& z TarFile.listc Csr|jd|dkr|}|dk rPddl}|jdtd||rP|jdd|dS|jdk rtjj||jkr|jdd|dS|jd||j ||}|dkr|jdd |dS|dk r||}|dkr|jdd|dS|j r t |d }|j |||j nd|jrd|j ||rnxHtj|D].}|jtjj||tjj|||||d q0Wn |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 %rr)filter)rwarningswarnDeprecationWarning_dbgrrrrrrNraddfilerr>listdiraddrs) rrr recursiveexcluderrrfr6r6r7rsD         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)rrr rTr4r5rr^rr0rfrr_rr1rrr)rrrrYrcrdr6r6r7r4s   zTarFile.addfile.cCsg}|dkr|}xD|D]<}|jr<|j|tj|}d|_|j|||j dqW|jddd|jx|D]~}tjj ||j }y(|j |||j |||j ||Wqttk r}z$|jdkrЂn|jdd|WYdd}~XqtXqtWdS) 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_attrscSs|jS)N)r)rr6r6r7dsz$TarFile.extractall..)keyrz tarfile: %s)r>rrrrtextractsortreverserrrsrchownutimechmodr}rr)rrr directoriesrdirpathrr6r6r7 extractallNs*       zTarFile.extractallrpcCs |jdt|tr |j|}n|}|jr>tjj||j|_ y |j |tjj||j |dWnt k r}zJ|j dkr~n6|jdkr|jdd|jn|jdd|j|jfWYdd}~XnBtk r}z$|j dkrn|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. rj)rrNrz tarfile: %sztarfile: %s %r)rrrrryrrrsrr_extract_memberrEnvironmentErrorrfilenamerstrerrorr})rmemberrrrrr6r6r7rts&      ( zTarFile.extractcCs|jdt|tr |j|}n|}|jr8|j||S|jtkrN|j||S|js^|j rt|j t rtt 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)rrrrrN fileobjectrrOryrwrrr extractfile_find_link_target)rrrr6r6r7rs        zTarFile.extractfilecCsT|jd}|jdtj}tjj|}|r@tjj| r@tj||jsP|j rj|j dd|j |j fn|j d|j |j r|j||n|jr|j||nx|jr|j||nb|js|jr|j||nD|js|j r|j||n&|jtkr|j||n |j|||rP|j|||j sP|j|||j||dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rrz%s -> %sN)r?rrrrdirnamermakedirsryrwrrrrNmakefiler>makedirrmakefifor{r}makedevmakelinkrrO makeunknownrrr)rr targetpathr upperdirsr6r6r7rs4        zTarFile._extract_membercCsFytj|dWn0tk r@}z|jtjkr0WYdd}~XnXdS)z,Make a directory called targetpath. iN)rmkdirrerrnoEEXIST)rrrrr6r6r7rs  zTarFile.makedircCs|j}|j|jt|d}|jdk rRx8|jD]\}}|j|t|||q.Wnt|||j|j|j|j|jdS)z'Make a file called targetpath. rN) rrrrrrfrtruncater)rrrsourcetargetrrr6r6r7rs     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)rrrr6r6r7r s zTarFile.makeunknowncCs"ttdrtj|ntddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rrr r})rrrr6r6r7r s  zTarFile.makefifocCsbttd sttd r td|j}|jr:|tjO}n |tjO}tj||tj |j |j dS)zt k r|jrtjj tjj |j |j}n|j}Yn6Xy|j|j ||Wntk rtdYnXdS)zMake a (symbolic) link called targetpath. If it cannot be created (platform limitation), we try to make a copy of the referenced file instead of a link. z%unable to resolve link inside archiveN)rwrsymlinkrrrrlinkrrsymlink_exceptionrsrrrr})rrrrr6r6r7r' s"      zTarFile.makelinkc Cstrttdrtjdkrytj|jd}Wntk rH|j}YnXytj |j d}Wntk rx|j }YnXy>|j rttdrtj |||ntjdkrtj|||Wn*tk r}ztdWYdd}~XnXdS)z6Set owner of targetpath according to tarinfo. geteuidrrlchownZos2emxzcould not change ownerN)rrrrrgetgrnamr#rr!getpwnamr"r rwrsysplatformrrr})rrrgurr6r6r7rD s    z TarFile.chowncCsLttdrHytj||jWn*tk rF}ztdWYdd}~XnXdS)zASet file permissions of targetpath according to tarinfo. rzcould not change modeN)rrrrtrr})rrrrr6r6r7rZ s  z TarFile.chmodcCsVttdsdSytj||j|jfWn*tk rP}ztdWYdd}~XnXdS)zBSet modification time of targetpath according to tarinfo. rNz"could not change modification time)rrrrrr})rrrrr6r6r7rc s  z TarFile.utimecCs|jd|jdk r$|j}d|_|S|jj|jd}x\y|jj|}WnBtk r}z2|jr|j dd|j|f|jt 7_w:WYdd}~Xnt k r}zJ|jr|j dd|j|f|jt 7_w:n|jdkrt t |WYdd}~Xntk r&|jdkr"t dYnjtk r`}z|jdkrPt t |WYdd}~Xn0tk r}zt t |WYdd}~XnXPq:W|dk r|jj|nd|_|S)zReturn the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available. raNrz0x%X: %srz empty fileT)rrrrrrrHrrrrrDr~rrrrrrrr)rmrrr6r6r7rRn sF       z TarFile.nextcCsn|j}|dk r"|d|j|}|r2tjj|}x6t|D]*}|rTtjj|j}n|j}||kr<|Sq  & #& 0   1  c@s,eZdZdZddZddZddZeZdS) r"zMIterator Class. for tarinfo in TarFile(...): suite... cCs||_d|_dS)z$Construct a TarIter object. rN)rr)rrr6r6r7r szTarIter.__init__cCs|S)z Return iterator object. r6)rr6r6r7r szTarIter.__iter__c Csb|jjs$|jj}|sPd|j_tn,y|jj|j}Wntk rNtYnX|jd7_|S)zReturn the next item using TarFile's next() method. When all members have been read, set TarFile as _loaded. Tr)rrrR StopIterationrr IndexError)rrr6r6r7__next__ s  zTarIter.__next__N)ryrzr{r|rrr,rRr6r6r6r7r" s r"c Cs.yt|}|jdStk r(dSXdS)zfReturn True if name points to a tar archive that we are able to handle, else return False. TFN)rrr)rrnr6r6r7r# s )rrrrr r!r"r#)rrr"r#)r-r.)N)w __future__r __version__version __author____date__ __cvsid__ __credits__rrrrrrNrrWrrrrNotImplementedErrorrZ WindowsError NameError__all__ version_info __builtin__builtinsr_openr1rrrr rr r'rr;rxrvrzr|rr~CONTTYPErrr<r r$rKrrMrrrOrtr@rosetr_rrBrpS_IFLNKS_IFREGr S_IFDIRr S_IFIFOZTSUIDZTSGIDZTSVTXZTUREADZTUWRITEZTUEXECZTGREADZTGWRITEZTGEXECZTOREADZTOWRITEZTOEXECrrgetfilesystemencodingr8r=rIrUr\rfrqrx Exceptionrr}r~rrrrrrrDrobjectrrrrrrrrr"rrr6r6r6r7s>         i?KT*