3 $fwl'@szdZdZdZdZdZdZddlmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZy ddlZWnek rdZYnXy ddlZWnek rdZYnXeefZyeef7ZWnek rYnXd 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;ed8krd9Z?ne j@Z?d:d;ZAdd?ZCd@e5fdAdBZDdCdDZEdedfdEdFZFdGdHZGdIdJZHGdKd d eIZJGdLddeJZKGdMd d eJZLGdNddeJZMGdOddeJZNGdPddeJZOGdQdRdReOZPGdSdTdTeOZQGdUdVdVeOZRGdWdXdXeOZSGdYdZdZeOZTGd[d\d\ZUGd]d^d^ZVGd_d`d`eWZXGdadbdbeWZYGdcdddde jZZ[Gded d eWZ\Gdfd d eWZ]dgd Z^e]jZdhdiZ_e`djkrve_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.)openNTarFileTarInfo is_tarfileTarError ReadErrorCompressionError StreamError ExtractError HeaderErrorENCODING USTAR_FORMAT GNU_FORMAT PAX_FORMATDEFAULT_FORMATrisustar sustar00d01234567LKSxgXpathlinkpathsizemtimeuidgidunamegname)ZatimeZctimer(r)r*r'ntzutf-8cCs(|j||}|d||t|tS)z8Convert a string to a null-terminated bytes object. N)encodelenNUL)slengthencodingerrorsr5/usr/lib64/python3.6/tarfile.pystns r7cCs*|jd}|dkr|d|}|j||S)z8Convert a null-terminated bytes object to a string. rr#N)finddecode)r1r3r4pr5r5r6ntss  r<c Cs|dd krfd}x0tt|dD]}|dK}|||d7}q"W|ddkrdt|d| }n@y"t|dd}t|jpd d}Wntk rtd YnX|S) z/Convert a number field to a python number. rr#asciistrict0zinvalid header)r=r>)ranger/r<intstrip ValueErrorInvalidHeaderError)r1nir5r5r6ntis   rKr?cCst|}d|ko"d|dknrDtd|d|fdt}n|tkrd|d |kopd|dknr|dkrtdg}ntdg}d||}x6t|dD]}|jd|d@|dL}qWntd |S) z/Convert a python number to a number field. rr?r#z%0*orAr@r=r>zoverflow in number field)rEbytesr0r bytearrayrDinsertrG)rIdigitsformatr1rJr5r5r6itns  2   rQcCs0dttjd|}dttjd|}||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@Z 148B8x356BZ 148b8x356b)sumstruct unpack_from)bufZunsigned_chksumZ signed_chksumr5r5r6 calc_chksumss rVc 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 copyfileobjdivmodrDreadr/write) srcdstr2 exceptionbufsizeblocks remainderbrUr5r5r6rYs$     rYcCs ddl}|jdtdtj|S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)warningswarnDeprecationWarningstatfilemode)moderdr5r5r6rhsrhcCs8ttjdd}|dk r(|j|dj|}t|dddS)Nr3backslashreplace )end)getattrsysstdoutr.r:print)r1r3r5r5r6 _safe_print srqc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r5r5r5r6rsc@seZdZdZdS)r z%General exception for extract errors.N)rrrsrtrur5r5r5r6r sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rrrsrtrur5r5r5r6rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rrrsrtrur5r5r5r6rsc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rrrsrtrur5r5r5r6r sc@seZdZdZdS)r z!Base exception for header errors.N)rrrsrtrur5r5r5r6r "sc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)rrrsrtrur5r5r5r6rv%srvc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)rrrsrtrur5r5r5r6rw(srwc@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)rrrsrtrur5r5r5r6rx+srxc@seZdZdZdS)rHzException for invalid headers.N)rrrsrtrur5r5r5r6rH.srHc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rrrsrtrur5r5r5r6ry1sryc@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)rwO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr}rfd)selfnamerir5r5r6__init__>s   z_LowLevelFile.__init__cCstj|jdS)N)r~closer)rr5r5r6rGsz_LowLevelFile.closecCstj|j|S)N)r~r[r)rr'r5r5r6r[Jsz_LowLevelFile.readcCstj|j|dS)N)r~r\r)rr1r5r5r6r\Msz_LowLevelFile.writeN)rrrsrtrurrr[r\r5r5r5r6rz8s  rzc@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. c Csd|_|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)rUr/r`rr\)rr1r5r5r6Z__writesz_Stream.__writec Cs|jr dSd|_z|jdkr:|jdkr:|j|jj7_|jdkr|jr|jj|jd|_|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|rrrzd|fg}d|_g|_d}|j}xT|D]L\}}||kr~|jj d||df|jj d||||f||7}||}qZW||jkr|jj d||jdfdS)NrrFT) roffsetr'positionrmrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr5r5r6rjs(   z_FileInFile.__init__cCsdS)Nr5)rr5r5r6rsz_FileInFile.flushcCsdS)NTr5)rr5r5r6readablesz_FileInFile.readablecCsdS)NFr5)rr5r5r6writablesz_FileInFile.writablecCs |jjS)N)rseekable)rr5r5r6rsz_FileInFile.seekablecCs|jS)z*Return the current file position. )r)rr5r5r6rsz_FileInFile.tellcCs|tjkr tt|d|j|_nj|tjkr\|dkrFt|j|d|_qt|j||j|_n.|tjkrtt|j||jd|_ntd|jS)z(Seek to a position in the file. rzInvalid argument) ioSEEK_SETminmaxr'rSEEK_CURSEEK_ENDrG)rrwhencer5r5r6rs   z_FileInFile.seekc Cs|dkr|j|j}nt||j|j}d}x|dkr xZ|j|j\}}}}||jkob|knrlPq:|jd7_|jt|jkr:d|_q:Wt|||j}|r|jj||j||jj|}t||krt d||7}n |t |7}||8}|j|7_q.W|S)z!Read data from the file. Nrrr#zunexpected end of data) r'rrrrr/rrr[rr0) rr'rUdatastartstoprr2rcr5r5r6r[s.      z_FileInFile.readcCs&|jt|}||dt|<t|S)N)r[r/)rrcrUr5r5r6readintosz_FileInFile.readintocCs d|_dS)NT)r)rr5r5r6rsz_FileInFile.close)N)N)rrrsrtrurrrrrrrrrr[rrr5r5r5r6rds  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}tj|dS)N)rr offset_datar'sparsesuperr)rtarfiletarinfor) __class__r5r6rs  zExFileObject.__init__)rrrsrtr __classcell__r5r5)rr6rsrc@seZdZdZdiZdjddZddZddZeeeZ ddZ dd Z ee e Z d!d"Z d#d$Zeed%fd&d'Zd(d)Zd*d+Zd,d-Zed.d/Zd0d1Zed2d3Zed4d5Zed6d7Zed8d9Zed:d;Zedd?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. rrir)r*r'r(chksumtypelinknamer+r,devmajordevminorrr pax_headersrr_sparse_structs _link_targetrcCsj||_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. irrN)rrir)r*r'r(rREGTYPErrr+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._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>)rrrrid)rr5r5r6__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) rrir)r*r'r(rrrr+r,rrrr/)rrir)r*r'r(rrrr+r,rrDIRTYPEr)rinfor5r5r6get_infos  zTarInfo.get_infosurrogateescapecCsT|j}|tkr|j|||S|tkr4|j|||S|tkrH|j||StddS)ztdt|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 headerz bad checksumrlt|ii i)iIiQiYirirriii)$r/rvrrwcountr0rxrKrVrHr<rrir)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErDrGrboolrisdirrstrip GNU_TYPES)rrUr3r4robjrrstructsrJrnumbytes isextendedorigsizer5r5r6frombufsZ       zTarInfo.frombufcCs8|jjt}|j||j|j}|jjt|_|j|S)zOReturn the next TarInfo object from TarFile object tarfile. ) rr[rr1r3r4rr _proc_member)rrrUr,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) rrr _proc_gnulongr' _proc_sparser rSOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr5r5r6r2Ts    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_blockr'r_apply_pax_inforr3r4)rrrr5r5r6r8as 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[r;r'r3r ryrrrr<r3r4rrr)rrrUnextr5r5r6r4rs  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. rrr$i)rrr[rrDrKrGrr(rrrr;r'r) rrr-r/r0rUrrJrr.r5r5r6r5s(    zTarInfo._proc_sparsec Cs0|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| } | dkrtd ||jd d|jd| d} |j| dd|j} | tkr|j| ||j |j} n|j| dd|j} | || <|| 7}qWy|j|} Wntk rdtd YnXd |kr~|j| |nHd |kr|j| ||n.|j ddkr|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]+)\nNr#zutf-8 hdrcharsetZBINARYs(\d+) ([^=]+)=rzinvalid headerr$z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrCr')%rr[r;r'rrrrresearchgroupr:rr3compilematchgroupsrErHrlr_decode_pax_fieldr4PAX_NAME_FIELDSr3r ry_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10r r6r<rrr9r:)rrrUrrEr?r3Zregexrr2rrr=rr5r5r6r7sd       $          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+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\nN)rAfinditerrrErClistzipr)rr=rrUZoffsetsrEr.r5r5r6rJ szTarInfo._proc_gnusparse_00cCs@dd|djdD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|qSr5)rE).0xr5r5r6 sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nr$r#)rrMrNr)rr=rrr5r5r6rIszTarInfo._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. Nrr#r$) rr[rrrEr/rrrrMrNr)rr=rrZfieldsrrUZnumberr5r5r6rKs  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.namer%zGNU.sparse.sizer'zGNU.sparse.realsizerrN) rsetattrrE PAX_FIELDSPAX_NUMBER_FIELDSrGr*rr)rrr3r4rrr5r5r6r<-s"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. rBN)r:UnicodeDecodeError)rrr3Zfallback_encodingZfallback_errorsr5r5r6rGDs zTarInfo._decode_pax_fieldcCs"t|t\}}|r|d7}|tS)z_Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024. r#)rZr)rr%rarbr5r5r6r;LszTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr5r5r6r9Usz TarInfo.isregcCs|jS)N)r9)rr5r5r6isfileWszTarInfo.isfilecCs |jtkS)N)rr)rr5r5r6r)Ysz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr5r5r6issym[sz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr5r5r6islnk]sz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr5r5r6ischr_sz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr5r5r6isblkasz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr5r5r6isfifocszTarInfo.isfifocCs |jdk S)N)r)rr5r5r6issparseeszTarInfo.issparsecCs|jtttfkS)N)rr]r_ra)rr5r5r6isdevgsz TarInfo.isdevN)rrir)r*r'r(rrrr+r,rrrrrrrrr)r)4rrrsrtru __slots__rrrpropertyr%rrr&rrrr rrrr classmethodrr staticmethodrrrr r1r3r2r8r4r5r7rJrIrKr<rGr;r9rXr)rZr\r^r`rbrcrdr5r5r5r6rs`    1    3 ?  h  c @seZdZdZdZdZdZdZeZ e Z dZ e ZeZd\dd Zedddefd d Zed]d d Zed^ddZed_ddZed`ddZdddddZddZddZddZd d!Zdad"d#Zdbdd%d&d'Zdcdd(d)d*Z ddd+d,Z!dedd.d/d0Z"dfdd.d2d3Z#d4d5Z$dgd6d7Z%d8d9Z&d:d;Z'dd?Z)d@dAZ*dBdCZ+dDdEZ,dFdGZ-dHdIZ.dJdKZ/dhdLdMZ0dNdOZ1didPdQZ2dRdSZ3dTdUZ4dVdWZ5dXdYZ6dZd[Z7dS)jrz=The TarFile Class provides an interface to tar archives. rFr#Nr{rcCsddddd}||krtd||_|||_|sj|jdkrVtjj| rVd|_d|_t||j}d |_n@|d krt|d rt |j t t fr|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 r4|jtkr4| |_ni|_| d k rJ| |_| d k rZ| |_| |_d |_g|_d |_|jj|_i|_y|jdkrd |_ |j!|_ |jdkr8x|jj"|jy|jj#|}|jj$|WnTt%k r|jj"|jPYn0t&k r0}zt't |WYd d }~XnXqW|jdkrd |_|jr|jj(|jj)}|jj*||jt+|7_Wn&|js|jj,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. rbzr+bwbZxb)r{ar|rPz!mode must be 'r', 'a', 'w' or 'x'rkr|FNrriTr{rP)rkr|rP)-rGri_moder~r%exists bltn_openrrrrr rLabspathrrPr dereference ignore_zerosr3r4rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberr=rr3rrxr rrrr\r/r)rrrirrPrrprqr3r4rrrrsrtZmodeserUr5r5r6rs             $   zTarFile.__init__c s| r| rtd|dkrfdd}xttj|dD]b}tj|}|dk r\|j} y||d|f|Sttfk r|dk r|j| w8Yq8Xq8Wtdnd |kr |jd d \} }| pd} |pd }|jkrtj|}n td |||| |f|Sd |kr|jd d \} }| p.d} |p8d }| dkrLtdt || |||} y|| | f|} Wn| j YnXd| _ | S|dkrj |||f|StddS)aOpen 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 openr{r:*csj|dkS)Ntaropen) OPEN_METH)r)rr5r6not_compressedsz$TarFile.open..not_compressed)keyNz%file could not be opened successfully:r#rzunknown compression type %r|r|zmode must be 'r' or 'w'FrkrPzundiscernible mode)r{rz)r{r|)rkr|rP) rGsortedr|rmrrrrrrrrr{) rrrirr`kwargsr}rfuncZ saved_posrhstreamrr5)rr6rsP%             z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. r{rkr|rPz!mode must be 'r', 'a', 'w' or 'x')r{rkr|rP)rG)rrrirrr5r5r6r{OszTarFile.taropenrcKs|d krtdyddl}|jWn ttfk rBtdYnXy|j||d||}Wn.tk r|dk r|dkrtd YnXy|j|||f|}WnBtk r|j |dkrtd Yn|j YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. r{r|rPzmode must be 'r', 'w' or 'x'rNzgzip module is not availablercznot a gzip fileF)r{r|rP) rGgzipZGzipFilerAttributeErrorrrrr{rr)rrrir compresslevelrrrr5r5r6gzopenWs2 zTarFile.gzopencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnFttfk r|j|dkrt d Yn|jYnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. r{r|rPzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)rznot a bzip2 fileF)r{r|rP) rGrrrZBZ2Filer{rEOFErrorrrr)rrrirrrrrr5r5r6bz2openys(  zTarFile.bz2opencKs|d krtdy ddl}Wntk r8tdYnX|j|pD|||d}y|j|||f|}WnH|jtfk r|j|dkrt d Yn|jYnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. r{r|rPzmode must be 'r', 'w' or 'x'rNzlzma module is not available)presetznot an lzma fileF)r{r|rP) rGrrrZLZMAFiler{rrrrr)rrrirrrrrr5r5r6xzopens& zTarFile.xzopenr{rrr)rrrrc 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. NTrkr|rPr$r)rkr|rP) rrirr\r0rrrZ RECORDSIZErr)rrarbr5r5r6rs 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)rrrr5r5r6 getmembers  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. )_checkrv_loadru)rr5r5r6 getmembersszTarFile.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)rOrr5r5r6rQsz$TarFile.getnames..)r)rr5r5r6getnamesszTarFile.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) aCreate 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. awxNrlstatrr#rmajorminor):rrr~r% splitdriverseplstriprrrrprrgfstatfilenost_modeS_ISREGst_inost_devst_nlinkrwr[rS_ISDIRrS_ISFIFOraS_ISLNKrYreadlinkS_ISCHRr]S_ISBLKr_rist_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr5r5r6 gettarinfos~                 zTarFile.gettarinfoT)rucCs|j|dkr|}x|D]}|rttj|jtd|jp@|j|jpJ|jf|j sb|j r|tdd|j |j fntd|j tdtj|jddt|j|jrdnd |r|jrtd |j|jrtd |jtqWdS) 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:%02drrrz-> zlink to )rrqrgrhrir+r)r,r*r^r`rrr'rZ localtimer(rr)rZrr\rp)rverboserurr5r5r6rMQs* z TarFile.list)filterc Csv|jd|dkr|}|dk rPddl}|jdtd||rP|jdd|dS|jdk rtjj||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. rNrzuse the filter argument insteadr$ztarfile: Excluded %rztarfile: Skipped %rr#ztarfile: Unsupported type %rri)r)rrdrerf_dbgrr~r%rorr9rnaddfiler)listdiraddr) rrr recursiveZexcluderrdrfr5r5r6rqsB       z TarFile.addcCs|jdtj|}|j|j|j|j}|jj||jt |7_|j }|dk rt ||j|j |dt |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(). rN)r`rr#)rrrrPr3r4rr\rr/rtrYr'rZrr0rur)rrrrUr`rarbr5r5r6rs   zTarFile.addfile.) numeric_ownercCsg}|dkr|}xF|D]>}|jr<|j|tj|}d|_|j|||j |dqW|jddd|jx|D]}tjj ||j }y,|j |||d|j |||j ||Wqvtk r}z$|jdkrւn|jdd |WYdd}~XqvXqvWdS) aExtract 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_attrsrcSs|jS)N)r)rkr5r5r6sz$TarFile.extractall..)r~)rr#z tarfile: %s)r)rrriextractsortreverser~r%rrchownutimechmodr rsr)rr%rurZ directoriesrdirpathryr5r5r6 extractalls,       zTarFile.extractallrcCs |jdt|tr |j|}n|}|jr>tjj||j|_ y"|j |tjj||j ||dWnt k r}zJ|j dkrn6|jdkr|jdd|jn|jdd|j|jfWYdd}~XnBtk r}z$|j dkrn|jdd|WYdd}~XnXdS)aExtract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a TarInfo object. You can specify a different directory using `path'. File attributes (owner, mtime, mode) are set unless `set_attrs' is False. If `numeric_owner` is True, only the numbers for user/group names are used and not the names. r{)rrrNr#z tarfile: %sztarfile: %s %r)rrr rr\r~r%rrr_extract_memberrrrsfilenamerstrerrorr )rmemberr%rrrryr5r5r6rs(      ( zTarFile.extractcCs|jdt|tr |j|}n|}|js6|jtkrB|j||S|jsR|j rzt|j t rht dq~|j |j|SndSdS)zExtract a member from the archive as a file object. `member' may be a filename or a TarInfo object. If `member' is a regular file or a link, an io.BufferedReader object is returned. Otherwise, None is returned. r{z'cannot extract (sym)link as file objectN)rrr rr9rr: fileobjectr\rZrrr extractfile_find_link_target)rrrr5r5r6rs      zTarFile.extractfilecCsV|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|||rR|j||||j sR|j|||j||dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rr#z%s -> %sN)r*rr~rr%dirnamermmakedirsr\rZrrrr9makefiler)makedirrbmakefifor^r`makedevmakelinkrr: makeunknownrrr)rr targetpathrrZ upperdirsr5r5r6r4s4       zTarFile._extract_memberc Cs*ytj|dWntk r$YnXdS)z,Make a directory called targetpath. iN)r~mkdirFileExistsError)rrrr5r5r6reszTarFile.makedircCs|j}|j|j|j}t|df}|jdk rrx,|jD]"\}}|j|t|||t|q6W|j|j|j nt|||jt|WdQRXdS)z'Make a file called targetpath. rjN) rrrrtrnrrYrr'truncate)rrrsourcer`targetrr'r5r5r6ros      zTarFile.makefilecCs"|j|||jdd|jdS)zYMake a file from a TarInfo object with an unknown type at targetpath. r#z9tarfile: Unknown file type %r, extracted as regular file.N)rrr)rrrr5r5r6rs zTarFile.makeunknowncCs"ttdrtj|ntddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr~rr )rrrr5r5r6rs  zTarFile.makefifocCsbttd sttd r td|j}|jr:|tjO}n |tjO}tj||tj |j |j dS)zttdr:ytj||jWntk r8tdYnXdS)zASet file permissions of targetpath according to tarinfo. rzcould not change modeN)rr~rrirr )rrrr5r5r6rs  z TarFile.chmodc CsHttdsdSytj||j|jfWntk rBtdYnXdS)zBSet modification time of targetpath according to tarinfo. rNz"could not change modification time)rr~rr(rr )rrrr5r5r6rs  z TarFile.utimecCs|jd|jdk r$|j}d|_|S|j|jjkrZ|jj|jd|jjdsZtdd}xby|jj |}WnHt k r}z2|j r|j dd|j|f|jt 7_wbWYdd}~Xntk r,}zN|j r|j dd|j|f|jt 7_wbn|jdkrtt|WYdd}~Xntk rT|jdkrPtdYnjtk r}z|jdkr~tt|WYdd}~Xn0tk r}ztt|WYdd}~XnXPqbW|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. ZraNr#zunexpected end of datar$z0x%X: %srz empty fileT)rrxrrrrr[rrr3rxrqrrrHr rvrwryrurrv)rmrryr5r5r6r=sL        z TarFile.nextcCsn|j}|dk r"|d|j|}|r2tjj|}x6t|D]*}|rTtjj|j}n|j}||kr<|SqzShow listing of a tarfile)metavarrz-ez --extract+ zExtract tarfile into target dir)nargsrrz-cz--createzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr{)rz{!r} is a tar archive.r#z{!r} is not a tar archive. zr:*)rr$)r%rz{!r} file is extracted.z+{!r} file is extracted into {!r} directory.rrr)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r|z{!r} file created.)rr)rr)argparseArgumentParser add_argumentZadd_mutually_exclusive_group parse_argsZtestrrrrprnrrrPexitrMrrr/r~curdirZ format_helprZcreatepopr%splitextr)rrparserrCargsr]rZtfr rZtar_name_ZextZ compressionsZtar_modeZ tar_files file_namer5r5r6main s                r__main__)r%r&r'r(r)r*r+r,)aruversion __author__Z__date__Z __cvsid__ __credits__builtinsrrnrnr~rrXrgrrSrrArrrrNotImplementedErrorrr NameError__all__r0rrrrrrrrr&r[rYr]r_rraZCONTTYPErrr'r rr6r rrrr:rWr+rTrHr rErUrr getfilesystemencodingr7r<rKrQrVrYrhrq Exceptionrr rrr r rvrwrxrHryrzrobjectrrBufferedReaderrrrrrrrr5r5r5r6s         wh , Z