o ƚRei @sHddlmZ dZdZdZdZdZdZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZz ddlZddlZWn eyJdZZYnweefZzeef7ZWn ey_Ynwgd Zejdd krpddlZnddlZejZd Zd Zed Z dZ!dZ"dZ#dZ$dZ%dZ&d Z'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5d Z6d!Z7e6Z8e&e'e(e)e,e-e.e*e+e/e0e1f Z9e&e'e.e1fZ:e/e0e1fZ;d"Ze?e?e?e@e@e@d$ZAd%ZBd&ZCd'ZDd(ZEd)ZFd*ZGd+ZHd,ZId ZJd-ZKd.ZLd/ZMd0ZNd1ZOd2ZPd3ZQd!ZRd ZSe jTd4vrd5ZUneVZUd6d7ZWd8d9ZXd:d;ZYd2e8fdd?Z[d{d@dAZ\eBdBfeCdCfeDdDfeEdEfeFdFfeGdGffeKdHffeLdIffeMeHBdJfeHdKfeMdLffeNdHffeOdIffePeIBdJfeIdKfePdLffeQdHffeRdIffeSeJBdMfeJdNfeSdLfff Z]dOdPZ^GdQdRdRe_Z`GdSdTdTe`ZaGdUdVdVe`ZbGdWdXdXe`ZcGdYdZdZe`ZdGd[d\d\e`ZeGd]d^d^eeZfGd_d`d`eeZgGdadbdbeeZhGdcddddeeZiGdedfdfeeZjGdgdhdhekZlGdidjdjekZmGdkdldlekZnGdmdndnekZoGdodpdpekZpGdqdrdrekZqGdsdtdtekZrGdudvdvekZsGdwdxdxekZtdydzZueZvesjZdS)|)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.N)TarFileTarInfo is_tarfileTarErrorsustar sustar00d01234567LKSxgX)pathlinkpathsizemtimeuidgidunamegname)rrr#r$)atimectimer r!r"riii`@i ii@ )ntZcezutf-8cCs(|||}|d||t|tS)z8Convert a string to a null-terminated bytes object. N)encodelenNUL)slengthencodingerrorsr8/builddir/build/BUILDROOT/alt-python310-pip-21.3.1-3.el8.x86_64/opt/alt/python310/lib/python3.10/site-packages/pip/_vendor/distlib/_backport/tarfile.pystns r:cCs*|d}|dkr|d|}|||S)z8Convert a null-terminated bytes object to a string. rN)finddecode)r4r6r7pr8r8r9ntss   r?cCs~|dtdkr"ztt|ddpdd}W|Sty!tdwd}tt|dD]}|dK}|t||d7}q,|S) z/Convert a number field to a python number. rr*asciistrict0r.zinvalid headerr)chrintr? ValueErrorInvalidHeaderErrorranger2ord)r4nir8r8r9ntis rKcCsd|krd|dkrnnd|d|fdt}|S|tks+|d|dkr/td|dkr?tdtd |d}t}t|dD]}| d|d @|dL}qH| dd |S) z/Convert a python number to a number field. rr.rz%0*or@r)zoverflow in number fieldLlr*) r1r3 GNU_FORMATrEstructunpackpack bytearrayrGinsert)rIdigitsformatr4rJr8r8r9itns   rWc Cshdttd|ddtd|dd}dttd|ddtd |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)Z148BNZ356Br Z148bZ356b)sumrPrQ)bufunsigned_chksum signed_chksumr8r8r9 calc_chksumss0 0r^cCs|dkrdS|dur |d}|s dS||q d}t||\}}t|D]}||}t||kr9td||q(|dkrW||}t||krRtd||dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. rNTr'zend of file reached)readwritedivmodrGr2IOError)srcdstr5r[BUFSIZEblocks remainderbr8r8r9 copyfileobjs.         rirM-rhdcr>rwr4SxtTcCsJg}tD]}|D]\}}||@|kr||nq|dqd|S)zcConvert a file's mode to a string of the form -rwxrwxrwx. Used by TarFile.list() rj)filemode_tableappendjoin)modepermtablebitcharr8r8r9filemode8s     r|c@eZdZdZdS)rzBase exception.N__name__ __module__ __qualname____doc__r8r8r8r9rGrc@r}) ExtractErrorz%General exception for extract errors.Nr~r8r8r8r9rJrrc@r}) ReadErrorz&Exception for unreadable tar archives.Nr~r8r8r8r9rMrrc@r})CompressionErrorz.Exception for unavailable compression methods.Nr~r8r8r8r9rPrrc@r}) StreamErrorz=Exception for unsupported operations on stream-like TarFiles.Nr~r8r8r8r9rSrrc@r}) HeaderErrorz!Base exception for header errors.Nr~r8r8r8r9rVrrc@r})EmptyHeaderErrorzException for empty headers.Nr~r8r8r8r9rYrrc@r})TruncatedHeaderErrorz Exception for truncated headers.Nr~r8r8r8r9r\rrc@r})EOFHeaderErrorz"Exception for end of file headers.Nr~r8r8r8r9r_rrc@r})rFzException for invalid headers.Nr~r8r8r8r9rFbrrFc@r})SubsequentHeaderErrorz3Exception for missing and invalid extended headers.Nr~r8r8r8r9rerrc@0eZdZdZddZddZddZdd Zd S) _LowLevelFilezLow-level file object. Supports reading and writing. It is used instead of a regular file object for streaming access. cCsFtjtjtjBtjBd|}ttdr|tjO}t||d|_dS)N)rmrnO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrropenfd)selfnamerwr8r8r9__init__rs  z_LowLevelFile.__init__cCst|jdSN)rcloserrr8r8r9r{z_LowLevelFile.closecCst|j|Sr)rr_rrrr8r8r9r_~z_LowLevelFile.readcCst|j|dSr)rr`rrr4r8r8r9r`sz_LowLevelFile.writeN)rrrrrrr_r`r8r8r8r9rls   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. cCsBd|_|durt||}d|_|dkrt|}|}|pd|_||_||_||_||_d|_ d|_ d|_ z\|dkrbzddl }Wn t yKtd w||_ |d|_|d kr^|n||d krzddl}Wn t ywtd w|d krd|_||_WdS||_WdSWdS|js|jd|_ ) z$Construct a _Stream object. TNF*rsrgzzzlib module is not availablermbz2bz2 module is not available) _extfileobjr _StreamProxy getcomptyperrwcomptypefileobjbufsizer[posclosedzlib ImportErrorrcrc32crc _init_read_gz_init_write_gzrdbufBZ2Decompressorcmp BZ2Compressorr)rrrwrrrrrr8r8r9rsV          z_Stream.__init__cCs$t|dr|js|dSdSdS)Nr)rrrrr8r8r9__del__s z_Stream.__del__cCs|jd|jj|jj |jjd|_tdtt }| d|d|j dr3|j dd|_ | |j d d tdS) z6Initialize for writing with gzip compression. rZ(d?d@Z)dAdBZ*dCdDZ+dEdFZ,dGdHZ-dIdJZ.dKdLZ/dMdNZ0dOdPZ1dQdRZ2dSdTZ3dUS)WraInformational 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. )rrwr!r"rr chksumtypelinknamer#r$devmajordevminorrr pax_headersrr_sparse_structs _link_targetrscCsj||_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. irrsN)rrwr!r"rr rREGTYPErrr#r$rrrrrrrrr8r8r9rs" zTarInfo.__init__cCrrrrr8r8r9_getpathrzTarInfo._getpathcC ||_dSrrrr8r8r9_setpathrzTarInfo._setpathcCrrrrr8r8r9 _getlinkpathrzTarInfo._getlinkpathcCrrr)rrr8r8r9 _setlinkpathrzTarInfo._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>) __class__rridrr8r8r9__repr__szTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt kr4|dds4|dd7<|S)z9Return the TarInfo's attributes as a dictionary. ) rrwr!r"rr rrrr#r$rrrr/)rrwr!r"rr rrrr#r$rrDIRTYPEr)rinfor8r8r9get_infos"zTarInfo.get_infosurrogateescapecCsP|}|tkr||||S|tkr||||S|tkr$|||Std)zr&r8r8r9r@sB   ( z"TarInfo._create_pax_generic_headerc Csft|dkr tdt|tkrtd|ttkrtdt|dd}|t|vr1t 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 drt|_|jtkrd}g}tdD]0} zt|||d} t||d|d} Wn tyYn w|| | f|d7}qt|d} t|dd} || | f|_ |!r |j "d|_ |r1|jt#vr1|d|j |_ |S)zAConstruct a TarInfo object from a 512 byte bytes object. rz empty headerztruncated headerzend of file headerrXrYz bad checksumr lt|ii i)iIiQiYir$ir/r:iii)$r2rrrcountr3rrKr^rFr?rrwr!r"rr rrrr#r$rrAREGTYPErr%GNUTYPE_SPARSErGrEruboolrisdirrstrip GNU_TYPES)rFr[r6r7robjr0rstructsrJrnumbytes isextendedorigsizer8r8r9frombufs\          zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr_rrgr6r7rr _proc_member)rFrr[rbr8r8r9 fromtarfiles  zTarInfo.fromtarfilecCsP|jttfvr ||S|jtkr||S|jtttfvr#| |S| |S)zYChoose the right processing method depending on the type and call it. ) rr9r8 _proc_gnulongr] _proc_sparserArESOLARIS_XHDTYPE _proc_pax _proc_builtinrrr8r8r9rhs     zTarInfo._proc_membercCsR|j|_|j}|s|jtvr|||j7}||_| |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_inforr6r7)rrrr8r8r9rn$s zTarInfo._proc_builtincCs|j||j}z||}Wn tytdw|j|_|jt kr1t ||j |j |_ |S|jtkr?t ||j |j |_|S)zSProcess the blocks that hold a GNU longname or longlink member. missing or bad subsequent header)rr_rrrrirrrrr9r?r6r7rr8r)rrr[nextr8r8r9rj5s   zTarInfo._proc_gnulongc Cs|j\}}}|`|rS|jt}d}tdD]4}zt|||d}t||d|d} Wn ty:Ynw|rF| rF||| f|d7}qt|d}|s ||_ |j |_ |j | |j |_||_ |S)z8Process a GNU sparse header plus extra headers. rr:rZi)rrr_rrGrKrErur^rrrrrrr) rrrcrerfr[rrJrrdr8r8r9rkKs,       zTarInfo._proc_sparsecCs|j||j}|jtkr|j}n|j}t d|}|dur,| d d|d<| d}|dkr9|j }nd}td}d} |||}|sLnF|\} } t| } ||d d|d| d} || dd|j} | tvr|| ||j |j} n || dd|j} | || <|| 7}qCz||} Wn tytd wd |vr|| |n!d |vr|| ||n| ddkr| ddkr|| |||jttfvr| ||j |j|j | _ d|vr| j!} | "s| jt#vr| | | j7} | |_ | S)zVProcess an extended or global header as described in POSIX.1-2008. s\d+ hdrcharset=([^\n]+)\nNrrD hdrcharsetBINARYs(\d+) ([^=]+)=rTrrtGNU.sparse.mapGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrBr)$rr_rrrrrErr;researchgroupr=rIr6compilematchgroupsrDendr_decode_pax_fieldr7PAX_NAME_FIELDSrirr_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rArlrsrrrprq)rrr[rrrwr6regexrr5rRrSrurr8r8r9rmgsj       $     zTarInfo._proc_paxcCshg}td|D] }|t|dqg}td|D] }|t|dqtt|||_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)r|finditerrurDr~listzipr)rrurr[offsetsrrdr8r8r9rszTarInfo._proc_gnusparse_00cCs@dd|ddD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|]}t|qSr8)rD).0rpr8r8r9 sz.TarInfo._proc_gnusparse_01..ry,Nrr)splitrrr)rrurrr8r8r9rs(zTarInfo._proc_gnusparse_01cCsd}g}|jt}|dd\}}t|}t||dkrAd|vr*||jt7}|dd\}}|t|t||dks|j|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nrrr) rr_rrrDr2rurrrrr)rrurrfieldsrr[numberr8r8r9rs  (zTarInfo._proc_gnusparse_10c Cs|D]T\}}|dkrt|d|q|dkr t|dt|q|dkr-t|dt|q|tvrX|tvrIzt||}Wn tyHd}Ynw|dkrR|d}t|||q||_dS) zoReplace fields with supplemental information from a previous pax extended or global header. zGNU.sparse.namerrzrzGNU.sparse.realsizerr$N) rOsetattrrD PAX_FIELDSPAX_NUMBER_FIELDSrEr`r;r)rrr6r7rRrSr8r8r9rss&   zTarInfo._apply_pax_infocCs.z||dWSty|||YSw)z1Decode a single field from a pax record. rA)r=UnicodeDecodeError)rrSr6fallback_encodingfallback_errorsr8r8r9rs  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)rar)rr[rfrgr8r8r9rr szTarInfo._blockcCs |jtvSr)r REGULAR_TYPESrr8r8r9rprz TarInfo.isregcCs|Sr)rprr8r8r9isfileszTarInfo.isfilecC |jtkSr)rr%rr8r8r9r_rz TarInfo.isdircCrr)rSYMTYPErr8r8r9issymrz TarInfo.issymcCrr)rLNKTYPErr8r8r9islnkrz TarInfo.islnkcCrr)rCHRTYPErr8r8r9ischr rz TarInfo.ischrcCrr)rBLKTYPErr8r8r9isblk"rz TarInfo.isblkcCrr)rFIFOTYPErr8r8r9isfifo$rzTarInfo.isfifocCs |jduSr)rrr8r8r9issparse&rzTarInfo.issparsecCs|jtttfvSr)rrrrrr8r8r9isdev(rz TarInfo.isdevN)rs)4rrrr __slots__rrrpropertyrrrrr"r'DEFAULT_FORMATENCODINGr.r*r+r- classmethodrGr4 staticmethodr5rLr7r@rgrirhrnrjrkrmrrrrsrrrrprr_rrrrrrrr8r8r8r9rsh    1     2 >  f   rc@seZdZdZdZdZdZdZeZ e Z dZ e ZeZ   dVdd Zedddefd d ZedWd d ZedXddZedXddZddddZddZddZddZddZdYdd ZdZd"d#Zd[d$d%Zd\d&d'Z d]d)d*Z!d^d,d-Z"d.d/Z#dZd0d1Z$d2d3Z%d4d5Z&d6d7Z'd8d9Z(d:d;Z)dd?Z+d@dAZ,dBdCZ-dDdEZ.d_dFdGZ/dHdIZ0d\dJdKZ1dLdMZ2dNdOZ3dPdQZ4dRdSZ5dTdUZ6dS)`rz=The TarFile Class provides an interface to tar archives. rFrNrmr(c  Cszt|dks |dvrtd||_dddd||_|s7|jdkr-tj|s-d |_d|_t||j}d |_n|d urCt |d rC|j }t |d rL|j|_d|_|rWtj |nd |_ ||_ |d urd||_ |d urk||_|d urr||_|d ury||_|d ur||_| |_| d ur|j tkr| |_ni|_| d ur| |_| d ur| |_d |_g|_d |_|j |_i|_zy|jdkrd |_||_|jdkr |j |jz|j |}|j!|Wn!t"y|j |jYnt#y} zt$t%| d } ~ wwq|jdvr'd|_|jr*|j&|j'}|j (||jt|7_Wd SWd SWd S|js9|j )d|_)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. rrmode must be 'r', 'a' or 'w'rbzr+bwb)rmarnrrnFNrrwTrmaw)*r2rErw_moderrexists bltn_openrrrabspathrrVr dereference ignore_zerosr6r7r,rdebug errorlevelrmembers_loadedrrinodes firstmemberrurrirurrrr?rGr;r`r)rrrwrrVrrrr6r7rrrer[r8r8r9rFs             zTarFile.__init__c Ks|s|std|dvrP|jD]<}t||j|}|dur!|}z ||d|fi|WSttfyK} z|durA||WYd} ~ qd} ~ wwtdd|vr|dd\} }| p_d} |pcd}||jvrrt||j|}ntd |||| |fi|Sd |vr|d d\} }| pd} |pd}| d vrtd t|| |||} z ||| | fi|} Wn| d | _ | S|dvr|j |||fi|Std)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 open)rmzr:*Nrmz%file could not be opened successfully:rrzunknown compression type %r|rwmode must be 'r' or 'w'Frzundiscernible mode) rE OPEN_METHrrrrrrrrrtaropen) rFrrwrrkwargsrfunc saved_posrr|streamrqr8r8r9rsR     z TarFile.opencKs0t|dks |dvrtd||||fi|S)zCOpen uncompressed tar archive name for reading or writing. rrr)r2rE)rFrrwrrr8r8r9rszTarFile.taropenrc Kst|dks |dvrtdz ddl}|jWn ttfy$tdw|du}z|||d||}|j|||fi|}Wn'tyZ|sQ|durQ| |durVt d|sg|durg| ||_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. rrrrNzgzip module is not availablerhr) r2rEgzipGzipFilerAttributeErrorrrrbrrr) rFrrwr compresslevelrrZ extfileobjrqr8r8r9gzopens0    zTarFile.gzopenc Kst|dks |dvrtdzddl}Wn tytdw|dur*t||}n|j|||d}z |j|||fi|}Wntt fyP| t dwd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. rrzmode must be 'r' or 'w'.rNr)rznot a bzip2 fileF) r2rErrrrBZ2FilerrbEOFErrorrrr)rFrrwrrrrrqr8r8r9bz2open$s$   zTarFile.bz2openrrr)rrrcCs|jrdS|jdvr3|jttd|jtd7_t|jt\}}|dkr3|jtt||j s;|j d|_dS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NrrrT) rrwrr`r3rrra RECORDSIZErr)rrfrgr8r8r9rHs   z TarFile.closecCs"||}|durtd||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)rrrr8r8r9 getmember\s  zTarFile.getmembercCs||js ||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_loadrrr8r8r9 getmembersgszTarFile.getmemberscCsdd|DS)zReturn the members of the archive as a list of their names. It has the same order as the list returned by getmembers(). cSsg|]}|jqSr8r)rrr8r8r9rusz$TarFile.getnames..)rrr8r8r9getnamesqszTarFile.getnamesc CsT|d|dur |j}|dur|}tj|\}}|tjd}|d}|}||_ |durEt tdr?|j s?t |}n t |}nt|}d}|j}t |r|j|jf} |j sx|jdkrx| |jvrx||j| krxt} |j| }n;t} | dr||j| <n/t |rt} n't |rt} nt |rt} t|}nt |rt } n t !|rt"} ndS||_||_#|j$|_%|j&|_'| tkr|j(|_)nd|_)|j*|_+| |_,||_-t.rz t./|j%d|_0Wn t1yYnwt2rz t23|j'd|_4Wn t1yYnw| t t"fvr(t tdr(t td r(t5|j6|_7t8|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. rNr$lstatrsrrmajorminor):rrrr splitdriverseplstriprrrrrstatfstatfilenost_modeS_ISREGst_inost_devst_nlinkrrrS_ISDIRr%S_ISFIFOrS_ISLNKrreadlinkS_ISCHRrS_ISBLKrrwst_uidr!st_gidr"st_sizerst_mtimer rrpwdgetpwuidr#rgrpgetgrgidr$rst_rdevrrr) rrarcnamerdrvrstatresrstmdinoderr8r8r9 gettarinfows                zTarFile.gettarinfoTcCs||D]{}|rUtt|jddtd|jp|j|jp|jfdd|s-| rzlink to)rprintr|rwr#r!r$r"rrrrrr localtimer rr_rrr)rverboserr8r8r9rs:  z TarFile.listc Csn|d|dur |}|dur(ddl}|dtd||r(|dd|dS|jdur@tj||jkr@|dd|dS|d|| ||}|durZ|dd |dS|durp||}|durp|dd|dS| rt |d }| ||| dS|r| ||rt|D]}|jtj||tj|||||d qdSdS| |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_dbgrrrrrrpraddfilerr_listdiraddrv) rrr recursiveexcluderrrfr8r8r9rsL       z TarFile.addcCs|dt|}||j|j|j}|j||jt |7_|durRt ||j|j t |j t \}}|dkrI|jtt ||d7}|j|t 7_|j|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)rr;r.rVr6r7rr`rr2rirrarr3rru)rrrr[rfrgr8r8r9r4s   zTarFile.addfile.c Csg}|dur|}|D]}|r||t|}d|_|j||| dq |jddd||D]>}tj ||j }z| ||| ||| ||Wq7tyu}z|jdkrc|dd|WYd}~q7d}~wwdS) 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(). N set_attrscSrrr)rr8r8r9dsz$TarFile.extractall..)keyr tarfile: %s)r_rur;rwextractsortreverserrrvrchownutimechmodrrr)rrr directoriesrdirpathrr8r8r9 extractallNs0     zTarFile.extractallrsc Cs|dt|tr||}n|}|rtj||j|_ z|j |tj||j |dWdSt yk}z/|j dkr>|jdurM|dd|jn|dd|j|jfWYd}~dSWYd}~dSd}~wty}z|j dkrx|dd|WYd}~dSd}~ww)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. rmrrNrrztarfile: %s %r)rr=r?rrrrrvrr_extract_memberrEnvironmentErrorrfilenamerstrerrorr)rmemberrrrrr8r8r9rts0      & zTarFile.extractcCs|dt|tr||}n|}|r|||S|jtvr'|||S|s/| rAt|j t r9t d| ||SdS)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() rmz'cannot extract (sym)link as file objectN)rr=r?rrp fileobjectrrqrrrrr extractfile_find_link_target)rr rr8r8r9r"s      zTarFile.extractfilecCsT|d}|dtj}tj|}|rtj|st||s'| r4| dd|j |j fn| d|j | rF|||nF|rQ|||n;|r\|||n0|sd|rk|||n!|ss| rz|||n|jtvr|||n||||r|||| s||||||dSdSdS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. r$rz%s -> %sN)r`rrrrdirnamermakedirsrrrrrrpmakefiler_makedirrmakefiforrmakedevmakelinkrrq makeunknownrrr)rr targetpathr upperdirsr8r8r9rs8       zTarFile._extract_memberc CsHz t|dWdSty#}z|jtjkrWYd}~dSd}~ww)z,Make a directory called targetpath. r N)rmkdirrerrnoEEXISTrrr,rr8r8r9r's zTarFile.makedircCs||j}||jt|d}|jdur'|jD]\}}||t|||qnt|||j||j||dS)z'Make a file called targetpath. rN) rrrrrrirtruncater)rrr,sourcetargetrrr8r8r9r&s      zTarFile.makefilecCs"||||dd|jdS)zYMake a file from a TarInfo object with an unknown type at targetpath. rz9tarfile: Unknown file type %r, extracted as regular file.N)r&rrrrr,r8r8r9r+ s  zTarFile.makeunknowncCs ttdr t|dStd)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rrr6rr5r8r8r9r( s zTarFile.makefifocCs^ttdr ttdstd|j}|r|tjO}n|tjO}t||t |j |j dS)zsysplatformrrr)rrr,gurr8r8r9rD s,      z TarFile.chownc CsDttdr z t||jWdSty}ztdd}~wwdS)zASet file permissions of targetpath according to tarinfo. rzcould not change modeN)rrrrwrrr1r8r8r9rZ s z TarFile.chmodc CsJttdsdSz t||j|jfWdSty$}ztdd}~ww)zBSet modification time of targetpath according to tarinfo. rNz"could not change modification time)rrrr rrr1r8r8r9rc s z TarFile.utimec Cs|d|jdur|j}d|_|S|j|jd} z|j|}WntyQ}z!|jrG| dd|j|f|jt 7_WYd}~qWYd}~nzd}~wt y}z,|jrs| dd|j|f|jt 7_WYd}~q|jdkr~t t |WYd}~nCd}~wty|jdkrt dYn.ty}z|jdkrt t |WYd}~nd}~wty}zt t |d}~ww |dur|j||Sd|_|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. raNTrz0x%X: %srz empty file)rrrrrrrirrrrrFrr?rrrrrur)rmrrr8r8r9run sZ               z TarFile.nextcCsn|}|dur|d||}|rtj|}t|D]}|r)tj|j}n|j}||kr4|SqdS)z}Find an archive member by name from bottom to top. If tarinfo is given, it is used as the starting point. N)rindexrrnormpathreversedr)rrr normalizerr  member_namer8r8r9r s  zTarFile._getmembercCs |}|dur nqd|_dS)zWRead through the entire archive file and look for readable members. TN)rurrrr8r8r9r s  z TarFile._loadcCs>|jr td|jj|dur|j|vrtd|jdSdS)znCheck if TarFile is still open, and if the operation's mode corresponds to TarFile's mode. z %s is closedNzbad operation for mode %r)rrbr rrw)rrwr8r8r9r s zTarFile._checkcCsX|rtj|jd|j}d}n|j}|}|j||dd}|dur*td||S)zZFind the target member of a symlink or hardlink member in the archive. r$NT)rrJzlinkname %r not found)rrrr$rrrr)rrrlimitr r8r8r9r# s zTarFile._find_link_targetcCs|jrt|jSt|S)z$Provide an iterator object. )riterrTarIterrr8r8r9r  s zTarFile.__iter__cCs ||jkrt|tjddSdS)z.Write debugging output to sys.stderr. )fileN)rrrAstderr)rlevelmsgr8r8r9r s z TarFile._dbgcCs ||Sr)rrr8r8r9 __enter__ szTarFile.__enter__cCs.|dur |dS|js|jd|_dSr)rrrr)rrrS tracebackr8r8r9__exit__ s    zTarFile.__exit__) NrmNNNNNNr(NNN)rmN)rmNr)NNN)T)NTNNr)r N)rsTr)7rrrrrrrrrrVrr6r7rrrr!rrrrrrrrrrrrrrrrrrr"rr'r&r+r(r)r*rrrrurrrr#r rrTrVr8r8r8r9r,sr kK       b  >  &# &0   1   rc@s,eZdZdZddZddZddZeZdS) rOzMIterator Class. for tarinfo in TarFile(...): suite... cCs||_d|_dS)z$Construct a TarIter object. rN)rrGror8r8r9r s zTarIter.__init__cCs|S)z Return iterator object. r8rr8r8r9r  szTarIter.__iter__cCs\|jjs|j}|sd|j_tnz |jj|j}Wn ty$tw|jd7_|S)zReturn the next item using TarFile's next() method. When all members have been read, set TarFile as _loaded. Tr)rrru StopIterationrrG IndexErrorrLr8r8r9__next__ s  zTarIter.__next__N)rrrrrr rYrur8r8r8r9rO s rOcCs,z t|}|WdStyYdSw)zfReturn True if name points to a tar archive that we are able to handle, else return False. TF)rrr)rrqr8r8r9r# s rr)w __future__r __version__version __author____date__Z __cvsid__ __credits__rArrr/rrPr;r|rrrrNotImplementedErrorr<Z WindowsError NameError__all__ version_info __builtin__builtinsr_openr3rrr6r1r3r2rHrr\rrrrr%rCONTTYPEr9r8r]rArErlr)rOr,rrqrrarsetrr>rDrS_IFLNKS_IFREGr8S_IFDIRr9S_IFIFOZTSUIDZTSGIDZTSVTXZTUREADZTUWRITEZTUEXECZTGREADZTGWRITEZTGEXECZTOREADZTOWRITEZTOEXECrrgetfilesystemencodingr:r?rKrWr^rirtr| ExceptionrrrrrrrrrrFrobjectrrrrrrrrrOrrr8r8r8r9sV           i?KT*