3 0f|r'@sdZdZdZdZdZdZddlmZddl 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.fZ8dlZ9d/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[ej\deZ]Gdfd d eWZ^Gdgd d eWZ_dhd Z`e_jZdidjZaebdkkreadS)mz,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)rr6rsrs([0-9]{1,20}) c@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 _setlinkpath szTarInfo._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 fromtarfileBs 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)rrr5r5r6r2Ws    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)rrrr5r5r6r8ds 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)rrrUnextr5r5r6r4us  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 Cs|jj|j|j}|jtkr&|j}n |jj}d}d}g}x0t||koV||dkrnt j ||}|srt dyt |j d}Wntk rt dYnX|dkrt d||t|krt d|jd|d} ||jdd| } | jd\} } } | s"| dks"|| dkr*t d|j|| | f| dkrd|dkrd| d kr`|j}nd }||7}q@W|dkr~d }x`|D]X\}} } |j| d d |j}|tkr|j| ||j|j}n|j| d d |j}|||<qWy|j|}Wntk r td YnXd |kr&|j||nFd |kr>|j||n.|jddkrl|jddkrl|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. rNzinvalid headerr#rrs hdrcharsetsBINARYzutf-8z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrCr')%rr[r;r'rrrrr/_header_length_prefix_rematchrHrEgrouprGrrl partitionrr3_decode_pax_fieldr4PAX_NAME_FIELDSr3r ry_proc_gnusparse_01_proc_gnusparse_00r_proc_gnusparse_10r r6r<rrr9r:)rrrUrrr3 raw_headersrBr2Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuerrr=rr5r5r6r7sx                 zTarInfo._proc_paxc Csg}g}x|D]\}}}|dkrVy|jt|jWqtk rRtdYqXq|dkry|jt|jWqtk rtdYqXqWtt|||_dS)z?Process a GNU tar extended sparse header, version 0.0. sGNU.sparse.offsetzinvalid headersGNU.sparse.numbytesN)rrEr:rGrHlistzipr)rr=rJZoffsetsr._rrr5r5r6rH!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 8sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nr$r#)rrKrLr)rr=rrr5r5r6rG5szTarInfo._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/rrrrKrLr)rr=rrZfieldsrrUZnumberr5r5r6rI;s  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<Ks"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. rBN)r:UnicodeDecodeError)rrr3Zfallback_encodingZfallback_errorsr5r5r6rEbs 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;jszTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr5r5r6r9ssz TarInfo.isregcCs|jS)N)r9)rr5r5r6isfileuszTarInfo.isfilecCs |jtkS)N)rr)rr5r5r6r)wsz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr5r5r6issymysz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr5r5r6islnk{sz TarInfo.islnkcCs |jtkS)N)rCHRTYPE)rr5r5r6ischr}sz TarInfo.ischrcCs |jtkS)N)rBLKTYPE)rr5r5r6isblksz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr5r5r6isfifoszTarInfo.isfifocCs |jdk S)N)r)rr5r5r6issparseszTarInfo.issparsecCs|jtttfkS)N)rr\r^r`)rr5r5r6isdevsz TarInfo.isdevN)rrir)r*r'r(rrrr+r,rrrrrrrrr)r)4rrrsrtru __slots__rrrpropertyr%rrr&rrrr rrrr classmethodrr staticmethodrrrr r1r3r2r8r4r5r7rHrGrIr<rEr;r9rWr)rYr[r]r_rarbrcr5r5r5r6rs`    1    3 ?  z 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|rOz!mode must be 'r', 'a', 'w' or 'x'rjr|FNrriTr{rO)rjr|rO)-rGri_moder~r%exists bltn_openrrrrr rLabspathrrPr dereference ignore_zerosr3r4rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberr=rr3rrxr rrrr\r/r)rrrirrPrrorpr3r4rrqrrrsZmodeserUr5r5r6rs             $   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_compressed<sz$TarFile.open..not_compressed)keyNz%file could not be opened successfully:r#rzunknown compression type %r|r|zmode must be 'r' or 'w'FrjrOzundiscernible mode)r{ry)r{r|)rjr|rO) rGsortedr{rmrrrrrrrrrz) rrrirr`kwargsr|rfuncZ saved_posrhstreamrr5)rr6rsP%             z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. r{rjr|rOz!mode must be 'r', 'a', 'w' or 'x')r{rjr|rO)rG)rrrirrr5r5r6rzmszTarFile.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|rOzmode must be 'r', 'w' or 'x'rNzgzip module is not availablercznot a gzip fileF)r{r|rO) rGgzipZGzipFilerAttributeErrorrrrrzrr)rrrir compresslevelrrrr5r5r6gzopenus2 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|rOzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)rznot a bzip2 fileF)r{r|rO) rGrrrZBZ2FilerzrEOFErrorrrr)rrrirrrrrr5r5r6bz2opens(  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|rOzmode must be 'r', 'w' or 'x'rNzlzma module is not available)presetznot an lzma fileF)r{r|rO) rGrrrZLZMAFilerzrrrrr)rrrirrrrrr5r5r6xzopens& zTarFile.xzopenrzrrr)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. NTrjr|rOr$r)rjr|rO) 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. )_checkru_loadrt)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)rNrr5r5r6rP sz$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% splitdriverseplstriprrrrorrgfstatfilenost_modeS_ISREGst_inost_devst_nlinkrvrZrS_ISDIRrS_ISFIFOr`S_ISLNKrXreadlinkS_ISCHRr\S_ISBLKr^rist_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr5r5r6 gettarinfo s~                 zTarFile.gettarinfoT)rtcCs|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)rYrr[rp)rverbosertrr5r5r6rKos* 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 %rrh)r)rrdrerf_dbgrr~r%rnrr9rmaddfiler)listdiraddr) rrr recursiveZexcluderrdrfr5r5r6rsB       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/rsrYr'rZrr0rtr)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)rjr5r5r6sz$TarFile.extractall..)r})rr#z tarfile: %s)r)rrriextractsortreverser~r%rrchownutimechmodr rrr)rr%rtrZ directoriesrdirpathrxr5r5r6 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_memberrrrrfilenamerstrerrorr )rmemberr%rrrrxr5r5r6r s(      ( 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[rYrrr extractfile_find_link_target)rrrr5r5r6r3s      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%dirnamerlmakedirsr[rYrrrr9makefiler)makedirramakefifor]r_makedevmakelinkrr: makeunknownrrr)rr targetpathrrZ upperdirsr5r5r6rRs4       zTarFile._extract_memberc Cs*ytj|dWntk r$YnXdS)z,Make a directory called targetpath. iN)r~mkdirFileExistsError)rrrr5r5r6rszTarFile.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. riN) rrrrsrmrrYrr'truncate)rrrsourcer`targetrr'r5r5r6rs      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)rrwrrrrr[rrr3rxrprrrHr rvrwryrtrru)rmrrxr5r5r6r= 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_argsZtestrrrrprnrrrPexitrKrrr/r~curdirZ format_helprZcreatepopr%splitextr)rrparserrCargsr]rZtfrrZtar_namerMZextZ compressionsZtar_modeZ tar_files file_namer5r5r6main s                r__main__)r%r&r'r(r)r*r+r,)cruversion __author__Z__date__Z __cvsid__ __credits__builtinsrrmrnr~rrXrgrrSrrerrrrNotImplementedErrorrr NameError__all__r0rrrrrrrrr&rZrXr\r^rr`ZCONTTYPErrr'r rr6r rrrr:rVr+rSrFr rErTrr getfilesystemencodingr7r<rKrQrVrYrhrq Exceptionrr rrr r rvrwrxrHryrzrobjectrrBufferedReaderrcompilerArrrrrrr5r5r5r6s         wh 4, Z