eaiovnaovbqoebvqoeavibavo B  fj@srdZdZdZdZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZy ddlZWnek rdZYnXy ddlZWnek rdZYnXeefZyeef7ZWnek rYnXddd d d d d ddddddddgZdZdZedZdZdZdZdZdZ 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/dZ0d+Z1d,Z2e1Z3e!e"e#e$e'e(e)e%e&e*e+e,f Z4e!e"e)e,fZ5e*e+e,fZ6d-Z7d.d/d0d1hZ8e9e9e9e:e:e:d2Z;ejZ=d5d6Z?d7d8Z@d9d:ZAd;e3fdd?ZCdedfd@dAZDdBdCZEdDdEZFGdFd d eGZHGdGddeHZIGdHd d eHZJGdId d eHZKGdJd d eHZLGdKddeHZMGdLdMdMeMZNGdNdOdOeMZOGdPdQdQeMZPGdRdSdSeMZQGdTdUdUeMZRGdVdWdWZSGdXdYdYZTGdZd[d[eUZVGd\d]d]eUZWGd^d_d_e jXZYGd`ddeUZZGdaddeUZ[dbd Z\e[jZdcddZ]e^dekrne]dS)fz,Read from and write to tar format archives. z0.9.0u"Lars Gustäbel (lars@gustaebel.de)u4Gustavo Niemeyer, Niels Gustäbel, Richard Townsend.)openNTarFileTarInfo is_tarfileTarError ReadErrorCompressionError StreamError ExtractError HeaderErrorENCODING USTAR_FORMAT GNU_FORMAT PAX_FORMATDEFAULT_FORMATrisustar sustar00d01234567LKSxgX)pathlinkpathsizemtimeuidgidunamegnamer%r&r+r,)ZatimeZctimer(r)r*r'ntzutf-8cCs(|||}|d||t|tS)z8Convert a string to a null-terminated bytes object. N)encodelenNUL)slengthencodingerrorsr5,/opt/alt/python37/lib64/python3.7/tarfile.pystns r7cCs*|d}|dkr|d|}|||S)z8Convert a null-terminated bytes object to a string. rN)finddecode)r1r3r4pr5r5r6ntss  r<cCs|ddkrfd}x0tt|dD]}|dK}|||d7}q"W|ddkrdt|d| }n@y"t|dd}t|pd d}Wntk rtd YnX|S) z/Convert a number field to a python number. r)r#r>asciistrict0zinvalid header)ranger/r<intstrip ValueErrorInvalidHeaderError)r1nir5r5r6ntis   rKr?cCst|}d|kr$d|dkrDnntd|d|fdt}n|tkrd|d |krrd|dkrnnZ|dkrtdg}ntdg}d||}x6t|dD]}|d|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   rQcCs0dttd|}dttd|}||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)sumstructZ unpack_from)bufZunsigned_chksumZ signed_chksumr5r5r6 calc_chksumss rUc Cs|pd}|dkrdS|dkr.t|||dSt||\}}x8t|D],}||}t||krh|d||qFW|dkr||}t||kr|d||dS)zjCopy length bytes from fileobj src to fileobj dst. If length is None, copy the entire content. i@rNzunexpected end of data)shutil copyfileobjdivmodrDreadr/write) srcdstr2 exceptionbufsizeblocks remainderbrTr5r5r6rWs$     rWcCs ddl}|dtdt|S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)warningswarnDeprecationWarningstatfilemode)moderbr5r5r6rfsrfcCs8ttjdd}|dk r(||d|}t|dddS)Nr3backslashreplace )end)getattrsysstdoutr.r:print)r1r3r5r5r6 _safe_print sroc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r5r5r5r6rsc@seZdZdZdS)r z%General exception for extract errors.N)rprqrrrsr5r5r5r6r sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rprqrrrsr5r5r5r6rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rprqrrrsr5r5r5r6rsc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rprqrrrsr5r5r5r6r sc@seZdZdZdS)r z!Base exception for header errors.N)rprqrrrsr5r5r5r6r sc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)rprqrrrsr5r5r5r6rt#srtc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)rprqrrrsr5r5r5r6ru&sruc@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)rprqrrrsr5r5r5r6rv)srvc@seZdZdZdS)rHzException for invalid headers.N)rprqrrrsr5r5r5r6rH,srHc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rprqrrrsr5r5r5r6rw/srwc@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}t||d|_dS)N)rwO_BINARYi) osO_RDONLYO_WRONLYO_CREATO_TRUNChasattrr{rfd)selfnamergr5r5r6__init__<s   z_LowLevelFile.__init__cCst|jdS)N)r|closer)rr5r5r6rEsz_LowLevelFile.closecCst|j|S)N)r|rYr)rr'r5r5r6rYHsz_LowLevelFile.readcCst|j|dS)N)r|rZr)rr1r5r5r6rZKsz_LowLevelFile.writeN)rprqrrrsrrrYrZr5r5r5r6rx6s  rxc@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|}|}|pt|j|jkrL|j|jd|j|j|jd|_qWdS)z]Write string s to the stream if a whole new block is ready to be written. N)rTr/r^rrZ)rr1r5r5r6Z__writesz_Stream.__writecCs|jr dSd|_z|jdkr:|jdkr:|j|j7_|jdkr|jr|j|jd|_|jdkr|jt d|j |jt d|j d@Wd|j s|j XdS) z[Close the _Stream object. No operation should be done on it afterwards. NTrzrrrzd|fg}d|_g|_d}|j}xT|D]L\}}||kr~|j d||df|j d||||f||7}||}qZW||jkr|j d||jdfdS)NrrFT) roffsetr'positionrkrr map_indexmapr)rrrr'Z blockinfoZlastposZrealposr5r5r6rhs(   z_FileInFile.__init__cCsdS)Nr5)rr5r5r6rsz_FileInFile.flushcCsdS)NTr5)rr5r5r6readablesz_FileInFile.readablecCsdS)NFr5)rr5r5r6writablesz_FileInFile.writablecCs |jS)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\}}}}||jkrd|krlnnPq:|jd7_|jt|jkr:d|_q:Wt|||j}|r|j||j||j|}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/rrrYrr0) rr'rTdatastartstoprr2rar5r5r6rYs.      z_FileInFile.readcCs&|t|}||dt|<t|S)N)rYr/)rrarTr5r5r6readintosz_FileInFile.readintocCs d|_dS)NT)r)rr5r5r6rsz_FileInFile.close)N)N)rprqrrrsrrrrrrrrrrYrrr5r5r5r6rbs  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}t|dS)N)rr offset_datar'sparsesuperr)rtarfiletarinfor) __class__r5r6rs  zExFileObject.__init__)rprqrrr __classcell__r5r5)rr6rsrc@seZdZdZdZdTddZeddZejddZed d Z e jd d Z d d Z ddZ e e dfddZddZddZddZeddZddZeddZedd Zed!d"Zed#d$Zed%d&Zed'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Z d3d4Z!d5d6Z"d7d8Z#d9d:Z$d;d<Z%d=d>Z&d?d@Z'dAdBZ(dCdDZ)dEdFZ*dGdHZ+dIdJZ,dKdLZ-dMdNZ.dOdPZ/dQdRZ0dSS)UraInformational 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. )rrgr)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)rrgr)r*r'r(rREGTYPErrr+r,rrrrrr)rrr5r5r6rs"zTarInfo.__init__cCs|jS)N)r)rr5r5r6r%sz TarInfo.pathcCs ||_dS)N)r)rrr5r5r6r%scCs|jS)N)r)rr5r5r6r&szTarInfo.linkpathcCs ||_dS)N)r)rrr5r5r6r& scCsd|jj|jt|fS)Nz<%s %r at %#x>)rrprid)rr5r5r6__repr__ szTarInfo.__repr__cCsl|j|jd@|j|j|j|j|j|j|j|j |j |j |j d }|dt krh|ddsh|dd7<|S)z9Return the TarInfo's attributes as a dictionary. i) rrgr)r*r'r(rrrr+r,rrrr/)rrgr)r*r'r(rrrr+r,rrDIRTYPEr)rinfor5r5r6get_infos  zTarInfo.get_infosurrogateescapecCsT|}|tkr||||S|tkr4||||S|tkrH|||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 drt|_|jtkr6d}g}xrtdD]f} y0t|||d} t||d|d} Wntk rPYnX|| | f|d7}qWt|d} t|dd} || | f|_ |!rN|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/rtrrucountr0rvrKrUrHr<rrgr)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErDrGrboolrisdirrstrip GNU_TYPES)rrTr3r4robjrrstructsrJrnumbytes isextendedorigsizer5r5r6frombufsZ       zTarInfo.frombufcCs8|jt}|||j|j}|jt|_||S)zOReturn the next TarInfo object from TarFile object tarfile. ) rrYrr&r3r4rr _proc_member)rrrTr!r5r5r6 fromtarfileAs zTarInfo.fromtarfilecCsT|jttfkr||S|jtkr,||S|jtttfkrF| |S| |SdS)zYChoose the right processing method depending on the type and call it. N) rrr _proc_gnulongr _proc_sparserrSOLARIS_XHDTYPE _proc_pax _proc_builtin)rrr5r5r6r'Vs    zTarInfo._proc_membercCsR|j|_|j}|s$|jtkr4|||j7}||_| |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)rrrr5r5r6r-cs zTarInfo._proc_builtincCs|j||j}y||}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)rrYr0r'r(r rwrrrr<r3r4rrr)rrrTnextr5r5r6r)ts  zTarInfo._proc_gnulongc Cs|j\}}}|`x|r|jt}d}xvtdD]j}y0t|||d}t||d|d} Wntk rzPYnX|r| r||| f|d7}q0Wt|d}qW||_ |j |_ |j | |j |_||_ |S)z8Process a GNU sparse header plus extra headers. rrri)rrrYrrDrKrGrrrrrr0r'r) rrr"r$r%rTrrJrr#r5r5r6r*s(    zTarInfo._proc_sparsecCs0|j||j}|jtkr&|j}n |j}t d|}|dk rX| d d|d<| d}|dkrr|j }nd}td}d}x|||}|sP|\} } t| } | dkrtd ||d d|d| d} || dd|j} | tkr|| ||j |j} n|| dd|j} | || <|| 7}qWy||} Wntk rdtd YnXd |kr~|| |nHd |kr|| ||n.| ddkr| ddkr|| |||jttfkr,| ||j |j|j!| _!d|kr,| j"} | #s| jt$kr&| | | 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')%rrYr0r'rrrrresearchgroupr:rr3compilematchgroupsrErHrjr_decode_pax_fieldr4PAX_NAME_FIELDSr(r rw_proc_gnusparse_01_proc_gnusparse_00_proc_gnusparse_10rr+r1rrr.r/)rrrTrr:r4r3Zregexrr2rrr2rr5r5r6r,sd       $          zTarInfo._proc_paxcCspg}x(td|D]}|t|dqWg}x(td|D]}|t|dq@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)r6finditerrrEr8listzipr)rr2rrTZoffsetsr:r#r5r5r6r?szTarInfo._proc_gnusparse_00cCs@dd|ddD}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#)rrBrCr)rr2rrr5r5r6r>szTarInfo._proc_gnusparse_01cCsd}g}|jt}|dd\}}t|}xJt||dkrvd|krV||jt7}|dd\}}|t|q.W|j|_t t |ddd|ddd|_ dS)z?Process a GNU tar extended sparse header, version 1.0. Nr r#r$) rrYrrrEr/rrrrBrCr)rr2rrZfieldsrrTZnumberr5r5r6r@s  zTarInfo._proc_gnusparse_10c Csx|D]\}}|dkr(t|d|q |dkrBt|dt|q |dkr\t|dt|q |tkr |tkryt||}Wntk rd}YnX|dkr|d}t|||q W||_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_FIELDSrGrrr)rrr3r4rrr5r5r6r1/s"  zTarInfo._apply_pax_infocCs.y ||dStk r(|||SXdS)z1Decode a single field from a pax record. rBN)r:UnicodeDecodeError)rrr3Zfallback_encodingZfallback_errorsr5r5r6r<Fs 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#)rXr)rrr_r`r5r5r6r0NszTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr5r5r6r.Wsz TarInfo.isregcCs|S)N)r.)rr5r5r6isfileYszTarInfo.isfilecCs |jtkS)N)rr)rr5r5r6r[sz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr5r5r6issym]sz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr5r5r6islnk_sz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr5r5r6ischrasz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr5r5r6isblkcsz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr5r5r6isfifoeszTarInfo.isfifocCs |jdk S)N)r)rr5r5r6issparsegszTarInfo.issparsecCs|jtttfkS)N)rrRrTrV)rr5r5r6isdevisz TarInfo.isdevN)r)1rprqrrrs __slots__rpropertyr%setterr&rrrr rrrr classmethodrr staticmethodrr rrr&r(r'r-r)r*r,r?r>r@r1r<r0r.rMrrOrQrSrUrWrXrYr5r5r5r6rsT    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#Nryrc Csddddd}||krtd||_|||_|sh|jdkrTtj|sTd|_d|_t||j}d |_n@|d krt|d rt |j t t fr|j }t|d r|j|_d |_|rtj |nd |_ ||_|d k r||_|d k r||_|d k r||_|d k r||_|d k r ||_| |_| d k r0|jtkr0| |_ni|_| d k rF| |_| d k rV| |_| |_d |_g|_d |_|j|_i|_y|jdkrd |_ |!|_ |jdkr2x|j"|jy|j#|}|j$|WnTt%k r|j"|jPYn0t&k r*}zt't |Wd d }~XYnXqW|jdkr|d |_|jr||j(|j)}|j*||jt+|7_Wn&|js|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. rbzr+bwbZxb)ryarzrEz!mode must be 'r', 'a', 'w' or 'x'rarzFNrrgTry)rarzrE)-rGrg_moder|r%exists bltn_openrrrrrrLabspathrrPr dereference ignore_zerosr3r4rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberr2rr(rrvr rrrrZr/r)rrrgrrPrrfrgr3r4rrhrirjZmodeserTr5r5r6rs            $   zTarFile.__init__c s|s|std|dkrfdd}xttj|dD]b}tj|}|dk rX|} y||d|f|Sttfk r|dk r|| w4Yq4Xq4Wtdnd |kr|d d \} }| pd} |pd }|jkrtj|}n td |||| |f|Sd |kr|d d \} }| p*d} |p4d }| dkrHtdt || |||} y|| | f|} Wn| 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 open)ryzr:*csj|dkS)Ntaropen) OPEN_METH)r)rr5r6not_compressed sz$TarFile.open..not_compressed)keyNryz%file could not be opened successfully:r#rzunknown compression type %r|)ryrzzmode must be 'r' or 'w'F)rarzrEzundiscernible mode) rGsortedrqrkrrrrrrrrrp) rrrgrr^kwargsrrrfuncZ saved_posrfstreamrr5)rr6rsP%            z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. )ryrarzrEz!mode must be 'r', 'a', 'w' or 'x')rG)rrrgrrwr5r5r6rpQszTarFile.taropenrcKs|dkrtdyddlm}Wntk r<tdYnXy|||d||}Wn.tk r|dk r||dkr|td YnXy|j|||f|}WnBtk r||dkrtd Yn|YnXd |_ |S) zkOpen gzip compressed tar archive name for reading or writing. Appending is not allowed. )ryrzrEzmode must be 'r', 'w' or 'x'r)GzipFilezgzip module is not availableraNryznot a gzip fileF) rGZgziprzrrrrrprr)rrrgr compresslevelrwrzrr5r5r6gzopenYs0zTarFile.gzopenc Ks|dkrtdyddlm}Wntk r<tdYnX||pF|||d}y|j|||f|}WnFttfk r||dkrt dYn|YnXd |_ |S) zlOpen bzip2 compressed tar archive name for reading or writing. Appending is not allowed. )ryrzrEzmode must be 'r', 'w' or 'x'r)BZ2Filezbz2 module is not available)r{ryznot a bzip2 fileF) rGrr}rrrprEOFErrorrrr)rrrgrr{rwr}rr5r5r6bz2openzs&zTarFile.bz2openc Ks|dkrtdyddlm}m}Wntk r@tdYnX||pJ|||d}y|j|||f|}WnF|tfk r||dkrt dYn|YnXd |_ |S) zkOpen lzma compressed tar archive name for reading or writing. Appending is not allowed. )ryrzrEzmode must be 'r', 'w' or 'x'r)LZMAFilerzlzma module is not available)presetryznot an lzma fileF) rGrrrrrrpr~rrr) rrrgrrrwrrrr5r5r6xzopens&zTarFile.xzopenrpr|rr)rrrrcCs|jr dSd|_z`|jdkrn|jttd|jtd7_t|jt\}}|dkrn|jtt|Wd|j s|j XdS)zlClose the TarFile. In write-mode, two finishing zero blocks are appended to the archive. NT)rarzrEr$r) rrgrrZr0rrrX RECORDSIZErr)rr_r`r5r5r6rs z TarFile.closecCs"||}|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||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. )_checkrl_loadrk)rr5r5r6 getmembersszTarFile.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|] }|jqSr5)r)rDrr5r5r6rFsz$TarFile.getnames..)r)rr5r5r6getnamesszTarFile.getnamesc Csl|d|dk r|j}|dkr$|}tj|\}}|tjd}|d}|}||_ |dkrt tdr~|j s~t |}qt |}nt|}d}|j}t |r |j|jf} |j s|jdkr| |jkr||j| krt} |j| }nt} | drt||j| <nht |rt} nVt |r0t} nDt |rLt} t|}n(t |r^t } nt !|rpt"} ndS||_||_#|j$|_%|j&|_'| tkr|j(|_)nd|_)|j*|_+| |_,||_-t.ryt./|j%d|_0Wnt1k rYnXt2r&yt23|j'd|_4Wnt1k r$YnX| t t"fkrht tdrht td rht5|j6|_7t8|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% splitdriverseplstriprrrrfrrefstatfilenost_modeS_ISREGst_inost_devst_nlinkrmrPrS_ISDIRrS_ISFIFOrVS_ISLNKrNreadlinkS_ISCHRrRS_ISBLKrTrgst_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr5r5r6 gettarinfos~                 zTarFile.gettarinfoT)rkcCs||dkr|}x|D]}|rtt|jtd|jp@|j|jpJ|jf| sb| r|tdd|j |j fntd|j tdt|jddt|j|rdnd |r|rtd |j|rtd |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 )rrorerfrgr+r)r,r*rSrUrrr'rZ localtimer(rrrOrrQrn)rverboserkrr5r5r6rBQs* z TarFile.list)filterc Cs:|d|dkr|}|jdk rFtj||jkrF|dd|dS|d||||}|dkrz|dd|dS|dk r||}|dkr|dd|dS|rt|d}| ||WdQRXnd| r,| ||r6xHt t |D]*}|j tj||tj||||d qWn | |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. `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. rNr$ztarfile: Skipped %rr#ztarfile: Unsupported type %rztarfile: Excluded %rr_)r)rrr|r%re_dbgrr.rdaddfilerrvlistdiraddr)rrr recursiverrfr5r5r6rqs4      z TarFile.addcCs|dt|}||j|j|j}|j||jt |7_|j }|dk rt ||j|j |dt |j t\}}|dkr|jtt||d7}|j|t7_|j|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#)rrrrPr3r4rrZrr/rjrWr'rXrr0rkr)rrrrTr^r_r`r5r5r6rs   zTarFile.addfile.) numeric_ownerc Csg}|dkr|}xF|D]>}|r<||t|}d|_|j||| |dqW|jddd|x|D]}tj ||j }y,|j |||d| ||| ||Wqvtk r}z"|jdkrւn|dd |Wdd}~XYqvXqvWdS) 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)rar5r5r6rz$TarFile.extractall..)rs)rr#z tarfile: %s)rrrrgextractsortreverser|r%rrchownutimechmodr rir)rr%rkrZ directoriesrdirpathror5r5r6 extractalls,       zTarFile.extractallrc Cs |dt|tr ||}n|}|r>tj||j|_ y"|j |tj||j ||dWnt k r}zH|j dkrn6|jdkr|dd|jn|dd|j|jfWdd}~XYnBtk r}z"|j dkrn|dd|Wdd}~XYnXdS)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. ry)rrrNr#z tarfile: %sztarfile: %s %r)rrrrrQr|r%rrr_extract_memberrrrifilenamerstrerrorr )rmemberr%rrrror5r5r6rs(      ( zTarFile.extractcCs|dt|tr ||}n|}|s6|jtkrB|||S|sR| rzt|j t rht dq~| ||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. ryz'cannot extract (sym)link as file objectN)rrrrr.rr/ fileobjectrQrOrrr extractfile_find_link_target)rrrr5r5r6r s      zTarFile.extractfilecCsT|d}|dtj}tj|}|r>tj|s>t||sN| rh| dd|j |j fn| d|j | r|||n|r|||nx|r|||nb|s|r|||nD|s| r|||n&|jtkr|||n ||||rP||||| sP||||||dS)z\Extract the TarInfo object tarinfo to a physical file called targetpath. rr#z%s -> %sN)rrr|rr%dirnamercmakedirsrQrOrrrr.makefilermakedirrWmakefiforSrUmakedevmakelinkrr/ makeunknownrrr)rr targetpathrrZ upperdirsr5r5r6r*s4       zTarFile._extract_membercCs*yt|dWntk r$YnXdS)z,Make a directory called targetpath. iN)r|mkdirFileExistsError)rrrr5r5r6r[szTarFile.makedirc Cs|j}||j|j}t|df}|jdk rrx,|jD]"\}}||t|||t|q6W||j| nt|||jt|WdQRXdS)z'Make a file called targetpath. r`N) rrrrjrdrrWrr'truncate)rrrsourcer^targetrr'r5r5r6res      zTarFile.makefilecCs"||||dd|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)rrrr5r5r6rus zTarFile.makeunknowncCs"ttdrt|ntddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr|rr )rrrr5r5r6r}s  zTarFile.makefifocCs^ttdrttdstd|j}|r6|tjO}n |tjO}t||t |j |j dS)zttdr:yt||jWntk r8tdYnXdS)zASet file permissions of targetpath according to tarinfo. rzcould not change modeN)rr|rrgrr )rrrr5r5r6rs  z TarFile.chmodcCsHttdsdSyt||j|jfWntk rBtdYnXdS)zBSet modification time of targetpath according to tarinfo. rNz"could not change modification time)rr|rr(rr )rrrr5r5r6rs  z TarFile.utimec Cs|d|jdk r$|j}d|_|S|j|jkrZ|j|jd|jdsZtdd}xby|j |}WnHt k r}z0|j r| dd|j|f|jt 7_wbWdd}~XYntk r,}zL|j r| dd|j|f|jt 7_wbn|jdkrtt|Wdd}~XYntk rT|jdkrPtdYnjtk r}z|jdkr~tt|Wdd}~XYn0tk r}ztt|Wdd}~XYnXPqbW|dk r|j|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)rrnrrrrrYrrr(rvrgrrrHrrtrurwrkrrl)rmrror5r5r6r2sL        z TarFile.nextcCsn|}|dk r"|d||}|r2tj|}x6t|D]*}|rTtj|j}n|j}||kr<|SqzShow listing of a tarfile)metavarrz-ez --extract+)z z zExtract tarfile into target dir)nargsrrz-cz--create)zzzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validry)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:rzz{!r} file created.)argparseArgumentParser add_argumentZadd_mutually_exclusive_group parse_argsZtestrrrrnrlrrrPexitrBrrr/r|curdirZ format_helprZcreatepopr%splitextr)rrparserr8argsr[rZtfrrZtar_name_ZextZ compressionsZtar_modeZ tar_files file_namer5r5r6main s                     r__main__)_rsversion __author__ __credits__builtinsrrdrlr|rrVrerrSrr6rrrAttributeErrorNotImplementedErrorrr NameError__all__r0rrrrrrrrrrPrNrRrTrrVZCONTTYPErrrrrr+r rrrr/rLr rIr=rrErJrr getfilesystemencodingr7r<rKrQrUrWrfro Exceptionrr rrr r rtrurvrHrwrxrobjectrrBufferedReaderrrrrrrpr5r5r5r6s         wh   W