3 sj'@s:dZdZdZdZdZdZddlmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZy ddlZWnek rdZYnXy ddlZWnek rdZYnXeefZyeef7ZWnek rYnXd d d d d ddddddddddgZdZdZedZdZdZ dZ!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*Z0d+Z1d,Z2dZ3d-Z4d.Z5e4Z6e$e%e&e'e*e+e,e(e)e-e.e/f Z7e$e%e,e/fZ8e-e.e/fZ9dZ:d/d0d5d6hZ;ee j?d8krd9Z@ne jAZ@d:d;ZBdd?ZDd@e6fdAdBZEdCdDZFdedfdEdFZGdGdHZHdIdJZIGdKd d eJZKGdLddeKZLGdMd d eKZMGdNddeKZNGdOddeKZOGdPddeKZPGdQdRdRePZQGdSdTdTePZRGdUdVdVePZSGdWdXdXePZTGdYdZdZePZUGd[d\d\ZVGd]d^d^ZWGd_d`d`eXZYGdadbdbeXZZGdcdddde j[Z\ej]deZ^GdfdgdgeKZ_Gdhdidie_Z`Gdjdkdke_ZaGdldmdme_ZbGdndodoe_ZcGdpdqdqe_ZdGdrdsdse_Zee_eeLfZfddudvZgdwdxZhdydzZid{d|Zjeheiejd}ZkeXZlGd~d d eXZmGdd d eXZndd ZoenjZddZpeqdkr6epdS)z,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-8cCs8|dkrtd|j||}|d||t|tS)z8Convert a string to a null-terminated bytes object. Nzmetadata cannot contain None) ValueErrorencodelenNUL)slengthencodingerrorsr6/usr/lib64/python3.6/tarfile.pystns r8cCs*|jd}|dkr|d|}|j||S)z8Convert a null-terminated bytes object to a string. rr#N)finddecode)r2r4r5pr6r6r7ntss  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?)ranger0r=intstripr.InvalidHeaderError)r2nir6r6r7ntis   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*orBrAr>r?zoverflow in number field)rFbytesr1r bytearrayrEinsertr.)rIdigitsformatr2rJr6r6r7itns  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. rAZ 148B8x356BZ 148b8x356b)sumstruct unpack_from)bufZunsigned_chksumZ signed_chksumr6r6r7 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 copyfileobjdivmodrEreadr0write) srcdstr3 exceptionbufsizeblocks remainderbrUr6r6r7rYs$     rYcCs ddl}|jdtdtj|S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)warningswarnDeprecationWarningstatfilemode)moderdr6r6r7rh srhcCs8ttjdd}|dk r(|j|dj|}t|dddS)Nr4backslashreplace )end)getattrsysstdoutr/r;print)r2r4r6r6r7 _safe_printsrqc@seZdZdZdS)rzBase exception.N)__name__ __module__ __qualname____doc__r6r6r6r7rsc@seZdZdZdS)r z%General exception for extract errors.N)rrrsrtrur6r6r6r7r sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rrrsrtrur6r6r6r7rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rrrsrtrur6r6r6r7r sc@seZdZdZdS)r z=Exception for unsupported operations on stream-like TarFiles.N)rrrsrtrur6r6r6r7r #sc@seZdZdZdS)r z!Base exception for header errors.N)rrrsrtrur6r6r6r7r &sc@seZdZdZdS)EmptyHeaderErrorzException for empty headers.N)rrrsrtrur6r6r6r7rv)srvc@seZdZdZdS)TruncatedHeaderErrorz Exception for truncated headers.N)rrrsrtrur6r6r6r7rw,srwc@seZdZdZdS)EOFHeaderErrorz"Exception for end of file headers.N)rrrsrtrur6r6r6r7rx/srxc@seZdZdZdS)rHzException for invalid headers.N)rrrsrtrur6r6r6r7rH2srHc@seZdZdZdS)SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rrrsrtrur6r6r6r7ry5sryc@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)selfnamerir6r6r7__init__Bs   z_LowLevelFile.__init__cCstj|jdS)N)r~closer)rr6r6r7rKsz_LowLevelFile.closecCstj|j|S)N)r~r[r)rr'r6r6r7r[Nsz_LowLevelFile.readcCstj|j|dS)N)r~r\r)rr2r6r6r7r\Qsz_LowLevelFile.writeN)rrrsrtrurrr[r\r6r6r6r7rz<s  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)rUr0r`rr\)rr2r6r6r7Z__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 blockinfoZlastposZrealposr6r6r7rps(   z_FileInFile.__init__cCsdS)Nr6)rr6r6r7rsz_FileInFile.flushcCsdS)NTr6)rr6r6r7readablesz_FileInFile.readablecCsdS)NFr6)rr6r6r7writablesz_FileInFile.writablecCs |jjS)N)rseekable)rr6r6r7rsz_FileInFile.seekablecCs|jS)z*Return the current file position. )r)rr6r6r7rsz_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_ENDr.)rrwhencer6r6r7rs   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'rrrrr0rrr[rr1) rr'rUrstartstoprr3rcr6r6r7r[s.      z_FileInFile.readcCs&|jt|}||dt|<t|S)N)r[r0)rrcrUr6r6r7readintosz_FileInFile.readintocCs d|_dS)NT)r)rr6r6r7rsz_FileInFile.close)N)N)rrrsrtrurrrrrrrrrr[rrr6r6r6r7rjs  rcseZdZfddZZS) ExFileObjectcs&t|j|j|j|j}tj|dS)N)rr offset_datar'sparsesuperr)rtarfiletarinfor) __class__r6r7rs  zExFileObject.__init__)rrrsrtr __classcell__r6r6)rr7rsrs([0-9]{1,20}) c@s eZdZdS) FilterErrorN)rrrsrtr6r6r6r7rsrcseZdZfddZZS)AbsolutePathErrorcs ||_tjd|jddS)Nzmember z has an absolute path)rrrr)rr)rr6r7rszAbsolutePathError.__init__)rrrsrtrrr6r6)rr7rsrcseZdZfddZZS)OutsideDestinationErrorcs.||_||_tj|jd|dddS)Nz would be extracted to z, z which is outside the destination)r_pathrrr)rrr%)rr6r7rsz OutsideDestinationError.__init__)rrrsrtrrr6r6)rr7rsrcseZdZfddZZS)SpecialFileErrorcs||_tj|jddS)Nz is a special file)rrrr)rr)rr6r7rszSpecialFileError.__init__)rrrsrtrrr6r6)rr7rsrcseZdZfddZZS)AbsoluteLinkErrorcs||_tj|jddS)Nz is a link to an absolute path)rrrr)rr)rr6r7rszAbsoluteLinkError.__init__)rrrsrtrrr6r6)rr7rsrcseZdZfddZZS)LinkOutsideDestinationErrorcs.||_||_tj|jd|dddS)Nz would link to z, z which is outside the destination)rrrrr)rrr%)rr6r7rsz$LinkOutsideDestinationError.__init__)rrrsrtrrr6r6)rr7rsrcseZdZfddZZS)LinkFallbackErrorcs2||_||_tjd|jdd|ddS)Nzlink z would be extracted as a zcopy of z, which was rejected)rrrrr)rrr%)rr6r7rszLinkFallbackError.__init__)rrrsrtrrr6r6)rr7rsrTcCs$i}|j}tjj|tjjd}|jdtjfrH|jjdtj}|d<tjj|r\t |tjjtjj ||tjjd}tjj ||g|krt |||j }|dk r|d@}|r|js|jr|d@s|dM}|dO}n|js|jrd}nt|||j kr||d<|r |jdk r,d|d <|jdk r@d|d <|jdk rTd|d <|jdk rhd|d <|js||jr tjj|jrt|tjj|j}||jkr||d <|jrtjj |tjj||j}ntjj ||j}tjj|tjjd}tjj ||g|kr t|||S)N)rC/ri@Iirir)r*r+r,linknamei)rr~r%realpath ALLOW_MISSINGrseplstripisabsrr commonpathrriisregislnkisdirissymrr)r*r+r,rrnormpathdirnamer)member dest_pathZfor_data new_attrsrZ target_pathriZ normalizedr6r6r7_get_filtered_attrssd                rcCs|S)Nr6)rrr6r6r7fully_trusted_filterRsrcCs(t||d}|r$|jf|ddiS|S)NFdeep)rr)rrrr6r6r7 tar_filterUs rcCs(t||d}|r$|jf|ddiS|S)NTrF)rr)rrrr6r6r7 data_filter[s r)Z fully_trustedrrc@seZdZdZdrZdsddZddZddZeeeZ ddZ dd Z ee e Z d!d"Z eeeeeeeed#ed$ d%d&Zd'd(Zeed)fd*d+Zd,d-Zd.d/Zd0d1Zed2d3Zd4d5Zed6d7Zed8d9Zed:d;Zedd?Zed#d@dAdBZ edCdDZ!ed#d@dEdFZ"dGdHZ#dIdJZ$dKdLZ%dMdNZ&dOdPZ'dQdRZ(dSdTZ)dUdVZ*dWdXZ+dYdZZ,d[d\Z-d]d^Z.d_d`Z/dadbZ0dcddZ1dedfZ2dgdhZ3didjZ4dkdlZ5dmdnZ6dodpZ7dqS)traInformational 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(chksumtyperr+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)rrr6r6r7r|s"zTarInfo.__init__cCs|jS)N)r)rr6r6r7_getpathszTarInfo._getpathcCs ||_dS)N)r)rrr6r6r7_setpathszTarInfo._setpathcCs|jS)N)r)rr6r6r7 _getlinkpathszTarInfo._getlinkpathcCs ||_dS)N)r)rrr6r6r7 _setlinkpathszTarInfo._setlinkpathcCsd|jj|jt|fS)Nz<%s %r at %#x>)rrrrid)rr6r6r7__repr__szTarInfo.__repr__T) rr(rirr)r*r+r,r_KEEPc Cs| rtj|} n tj|} || k r(|| _|| k r6|| _|| k rD|| _|| k rR|| _|| k r`|| _|| k rn|| _|| k r||| _|| k r|| _ | S)zGReturn a deep copy of self with the given attributes replaced. ) copydeepcopyrr(rirr)r*r+r,) rrr(rirr)r*r+r,rr resultr6r6r7rs(  zTarInfo.replacecCs|jdkrd}n |jd@}|j||j|j|j|j|j|j|j|j |j |j |j d }|dt kr~|djd r~|dd7<|S)z9Return the TarInfo's attributes as a dictionary. Ni) rrir)r*r'r(rrrr+r,rrrrr)rirr)r*r'r(rrrr+r,rrDIRTYPEr)rriinfor6r6r7get_infos&   zTarInfo.get_infosurrogateescapecCs~|j}x(|jD]\}}|dkrtd|qW|tkrH|j|||S|tkr^|j|||S|tkrr|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||}|r|jtkr|j jdrt|_|jtkrs zTarInfo._proc_builtinc Cs|jj|j|j}y|j|dd}Wntk rBtdYnX|j|_|jt krjt ||j |j |_ n|jtkrt ||j |j |_|S)zSProcess the blocks that hold a GNU longname or longlink member. F)rBz missing or bad subsequent header)rr[r_r'rVr ryrrr#r=r4r5rr"r)rrrUnextr6r6r7rYOs  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. rr%rJi)rrr[rrErKr.rrNrrrr_r'r) rrrRrTrUrUrrJrrSr6r6r7rZes(    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|d d }Wntk rtd YnXd|kr*|j||nFd|krB|j||n.|jddkrp|jddkrp|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#r<rs hdrcharsetsBINARYzutf-8F)rBz missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major1zGNU.sparse.minorrDr')%rr[r_r'rr1rr r0_header_length_prefix_rematchrHrFgroupr.rrl partitionrr4_decode_pax_fieldr5PAX_NAME_FIELDSrVr ry_proc_gnusparse_01_proc_gnusparse_00r6_proc_gnusparse_10r/r[r`rrrr^)rrrUrrr4 raw_headersrfr3Zheader_value_end_offsetZkeyword_and_valueZ raw_keywordZequalsZ raw_valuer>rrarr6r6r7r\sx                 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)rrFr;r.rHlistzipr)rrarnZoffsetsrS_r>rr6r6r7rlszTarInfo._proc_gnusparse_00cCs@dd|djdD}tt|ddd|ddd|_dS)z?Process a GNU tar extended sparse header, version 0.1. cSsg|] }t|qSr6)rF).0xr6r6r7 sz.TarInfo._proc_gnusparse_01..zGNU.sparse.map,Nr$r#)r4rorpr)rrarrr6r6r7rkszTarInfo._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. Nr=r#r$) rr[rr4rFr0rrrrorpr)rrarrZfieldsrrUZnumberr6r6r7rms  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) rsetattrrF PAX_FIELDSPAX_NUMBER_FIELDSr.rOr r)rrr4r5r>rr6r6r7r`%s"  zTarInfo._apply_pax_infoc Cs.y |j|dStk r(|j||SXdS)z1Decode a single field from a pax record. rCN)r;UnicodeDecodeError)rrr4Zfallback_encodingZfallback_errorsr6r6r7ri<s zTarInfo._decode_pax_fieldcCs2|dkrtdt|t\}}|r*|d7}|tS)z_Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024. rzinvalid offsetr#)rHrZr)rrKrarbr6r6r7r_Ds zTarInfo._blockcCs |jtkS)N)r REGULAR_TYPES)rr6r6r7rPsz TarInfo.isregcCs|jS)N)r)rr6r6r7isfileRszTarInfo.isfilecCs |jtkS)N)rr)rr6r6r7rTsz TarInfo.isdircCs |jtkS)N)rSYMTYPE)rr6r6r7rVsz TarInfo.issymcCs |jtkS)N)rLNKTYPE)rr6r6r7rXsz TarInfo.islnkcCs |jtkS)N)rr7)rr6r6r7ischrZsz TarInfo.ischrcCs |jtkS)N)rr8)rr6r6r7isblk\sz TarInfo.isblkcCs |jtkS)N)rFIFOTYPE)rr6r6r7isfifo^szTarInfo.isfifocCs |jdk S)N)r)rr6r6r7issparse`szTarInfo.issparsecCs|jtttfkS)N)rr7r8r)rr6r6r7isdevbsz TarInfo.isdevN)rrir)r*r'r(rrrr+r,rrrrrrrrr)r)8rrrsrtru __slots__rrrpropertyr%rrr&r r rrrr rrrr classmethodr3r staticmethodrr:r!r.rAr@rWrVrXr]rYrZr\rlrkrmr`rir_rr{rrrr~rrrrr6r6r6r7rnsn    1  *  3 C  z c @seZdZdZdZdZdZdZeZ e Z dZ e ZeZdZdkdd Zedddefd d Zedld d ZedmddZednddZedoddZdddddZddZddZddZd d!Zdpd"d#Zdqdd%d&d'Z drdd(d)d*Z!dsd+d,Z"d-d.Z#dtddd0d1d2Z$d3d4Z%duddd0d6d7Z&d8d9Z'dvd:d;Z(dd?Z*d@dAZ+dwdddBdCdDZ,dEdFZ-dGdHZ.dIdJZ/dKdLZ0dMdNZ1dOdPZ2dQdRZ3dSdTZ4dUdVZ5dWdXZ6dYdZZ7dxd[d\Z8d]d^Z9dyd_d`Z:dadbZ;dcddZdidjZ?dS)zrz=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|rsz!mode must be 'r', 'a', 'w' or 'x'rr|FNrriTr{rs)rr|rs)-r.ri_moder~r%exists bltn_openrrr+rr-rLabspathrrPr dereference ignore_zerosr4r5rrdebug errorlevel copybufsizermembers_loadedrrinodes firstmemberrarrWrrxr rr3r r\r0r)rrrirrPrrrr4r5rrrrZmodeserUr6r6r7rs             $   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)r2r6r7not_compressedsz$TarFile.open..not_compressed)keyNz%file could not be opened successfully:r#rzunknown compression type %r|r|zmode must be 'r' or 'w'Frrszundiscernible mode)r{r)r{r|)rr|rs) r.sortedrrmrrrrr4rrrr) r2rrirr`kwargsrrfuncZ saved_posrhstreamrr6)r2r7rsP%             z TarFile.opencKs |dkrtd||||f|S)zCOpen uncompressed tar archive name for reading or writing. r{rr|rsz!mode must be 'r', 'a', 'w' or 'x')r{rr|rs)r.)r2rrirrr6r6r7rLszTarFile.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|rszmode must be 'r', 'w' or 'x'rNzgzip module is not availablercznot a gzip fileF)r{r|rs) r.gzipZGzipFilerAttributeErrorrrrrrr)r2rrir compresslevelrrrr6r6r7gzopenTs2 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|rszmode must be 'r', 'w' or 'x'rNzbz2 module is not available)rznot a bzip2 fileF)r{r|rs) r.rrrZBZ2FilerrEOFErrorrrr)r2rrirrrrrr6r6r7bz2openvs(  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|rszmode must be 'r', 'w' or 'x'rNzlzma module is not available)presetznot an lzma fileF)r{r|rs) r.rrrZLZMAFilerrrrrr)r2rrirrrrrr6r6r7xzopens& zTarFile.xzopenrrrr)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. NTrr|rsr$r)rr|rs) rrirr\r1rrrZ RECORDSIZErr)rrarbr6r6r7rs z TarFile.closecCs"|j|}|dkrtd||S)aReturn a TarInfo object for member `name'. If `name' can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version. Nzfilename %r not found) _getmemberKeyError)rrrr6r6r7 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. )_checkr_loadr)rr6r6r7 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|] }|jqSr6)r)rrrr6r6r7rtsz$TarFile.getnames..)r)rr6r6r7getnamesszTarFile.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% splitdriverrrrrrrrrgfstatfilenost_modeS_ISREGst_inost_devst_nlinkrr}rS_ISDIRrS_ISFIFOrS_ISLNKr|readlinkS_ISCHRr7S_ISBLKr8rist_uidr)st_gidr*st_sizer'st_mtimer(rrpwdgetpwuidr+rgrpZgetgrgidr,rst_rdevrrr) rrarcnamerZdrvrZstatresrZstmdinoderr6r6r7 gettarinfos~                 zTarFile.gettarinfoT)rcCs0|j|dkr|}x|D] }|r|jdkr:tdnttj|jtd|jpX|j|jpb|jf|j sz|j rtdd|j |j fntd|j |jdkrtdntdtj|jdd t|j|jrd nd |r"|jr td |j|jr"td |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 ??????????z%s/%sz%10sz%d,%dz%10dz????-??-?? ??:??:??z%d-%02d-%02d %02d:%02d:%02drrrz-> zlink to )rrirqrgrhr+r)r,r*r~rrrr'r(rZ localtimerrrrrrp)rverboserrr6r6r7roNs2      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 %rr)r)rrdrerf_dbgrr~r%rrrraddfilerlistdiraddr) rrr recursiveZexcluderrdrfr6r6r7rtsB       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#)rr rrPr4r5rr\rr0rrYr'rZrr1rr)rrrrUr`rarbr6r6r7rs   zTarFile.addfilec Csp|dkr0|j}|dkrtSt|tr,td|St|r<|Syt|Stk rjtd|ddYnXdS)Nz#String names are not supported for z2TarFile.extraction_filter. Use a function such as ztarfile.data_filter directly.zfilter z not foundzUString names are not supported for TarFile.extraction_filter. Use a function such as zrString names are not supported for TarFile.extraction_filter. Use a function such as tarfile.data_filter directly.) extraction_filterrr+r- TypeErrorcallable_NAMED_FILTERSrr.)rrr6r6r7_get_filter_functions zTarFile._get_filter_function.) numeric_ownerrc"Csg}|j|}|dkr|}xT|D]L}|j|||\}} |dkr@q |jrR|j| |j|||j ||dq W|jddddx$|D]} yy|| |}Wn4tk r} z|j| t| wWYdd} ~ XnX|dkr|j| dwt j j ||j } yt j | } Wn$tk r.|j|dwYnXtj| jsL|j|d w|j|| |d |j|| |j|| Wqtk r} z|j| WYdd} ~ XqXqWdS) 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. The `filter` function will be called on each member just before extraction. It can return a changed TarInfo or None to skip the member. String names of common filters are accepted. N) set_attrsrfilter_functioncSs|jS)N)r)rr6r6r7sz$TarFile.extractall..T)rreversezexcluded by filterZmissingznot a directory)r)r_get_extract_tarinforr _extract_onesort_FILTER_ERRORS_log_no_directory_fixupreprr~r%rrrFileNotFoundErrorrgrrchownutimechmodr _handle_nonfatal_error)rr%rrrZ directoriesrrr unfilteredexcdirpathrrr6r6r7 extractallsP       zTarFile.extractallcCs|jdd|j|fdS)Nr$z(tarfile: Not fixing up directory %r (%s))rr)rrreasonr6r6r7r! szTarFile._log_no_directory_fixuprc Cs8|j|}|j|||\}}|dk r4|j||||dS)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. The `filter` function will be called before extraction. It can return a changed TarInfo or None to skip the member. String names of common filters are accepted. N)rrr) rrr%rrrrrrr6r6r7extract% s  zTarFile.extractcCst|tr|j|}n|}d}y|||}WnZttfk r\}z|j|WYdd}~Xn,tk r}z|j|WYdd}~XnX|dkr|jdd|j dS|j rt j |}t j j||j|_||fS)zGet (filtered, unfiltered) TarInfos from *member* *member* might be a string. Return (None, None) if not found. Nr$ztarfile: Excluded %r)NN)r+r-rrr_handle_fatal_errorr rrrrr r~r%rrr)rrrr%rfilteredrr6r6r7r9 s"   zTarFile._get_extract_tarinfocCs|jdy&|j|tjj||j||||dWnVtk r\}z|j|WYdd}~Xn,tk r}z|j |WYdd}~XnXdS)zExtract from filtered tarinfo to disk. filter_function is only used when extracting a *different* member (e.g. as fallback to creating a symlink) r{)rrrextraction_rootN) r_extract_memberr~r%rrrrr r)rrr%rrrrr6r6r7rW s  zTarFile._extract_onecCs"|jdkrn|jdd|dS)z=Handle non-fatal error (ExtractError) according to errorlevelr#z tarfile: %sN)rr)rrr6r6r7rk s zTarFile._handle_nonfatal_errorcCsn|jdkrn\t|trP|jdkr6|jdd|jqj|jdd|j|jfn|jddt|j|fdS)z1Handle "fatal" error according to self.errorlevelrNr#z tarfile: %sztarfile: %s %rztarfile: %s %s)rr+rfilenamerstrerrorrrr)rrr6r6r7rr s   zTarFile._handle_fatal_errorcCs|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)rr+r-rrrr^ fileobjectrrrrr extractfile_find_link_target)rrrr6r6r7r~ s      zTarFile.extractfile)rrcCs\|jd}|jdtj}tjj|}|r@tjj| r@tj||jsP|j rj|j dd|j |j fn|j d|j |j r|j||n|jr|j||n~|jr|j||nh|js|jr|j||nJ|js|j r|j||||dn&|jtkr|j||n |j|||rX|j||||j sX|j|||j||dS)zExtract the filtered TarInfo object tarinfo to a physical file called targetpath. filter_function is only used when extracting a *different* member (e.g. as fallback to creating a symlink) rr#z%s -> %s)rrN)rOrr~rr%rrmakedirsrrrrrrmakefilermakedirrmakefifor~rmakedevmakelink_with_filterrr^ makeunknownrrr)rr targetpathrrrrZ upperdirsr6r6r7r s:        zTarFile._extract_memberc Cs@y&|jdkrtj|n tj|dWntk r:YnXdS)z,Make a directory called targetpath. Ni)rir~mkdirFileExistsError)rrrr6r6r7r s   zTarFile.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. rN) rrrrrrrYrr'truncate)rrrsourcer`targetrr'r6r6r7r s      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)rrrr6r6r7r s zTarFile.makeunknowncCs"ttdrtj|ntddS)z'Make a fifo called targetpath. mkfifozfifo not supported by systemN)rr~r r )rrrr6r6r7r s  zTarFile.makefifocCsnttd sttd r td|j}|dkr2d}|jrF|tjO}n |tjO}tj||tj |j |j dS)zzFilter for extraction)metavarchoicesr-z-lz--list zShow listing of a tarfile)r.r-z-ez --extract+ zExtract tarfile into target dir)nargsr.r-z-cz--createzCreate tarfile from sourcesz-tz--testzTest if a tarfile is validr#z&--filter is only valid for extraction r{)r"z{!r} is a tar archive.z{!r} is not a tar archive. zr:*)rr$)r%rrz{!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.)r0r2)r4r5)argparseArgumentParser add_argumentrZadd_mutually_exclusive_group parse_argsrrexitZtestrrrrprnr#rrProrr0r~curdirZ format_helprZcreatepopr%splitextr)r6r)parserrgargsr]rZtfr;r%Ztar_namerqZextZ compressionsZtar_modeZ tar_files file_namer6r6r7mainM s                  rA__main__)r%r&r'r(r)r*r+r,)T)rruversion __author__Z__date__Z __cvsid__ __credits__builtinsrrrnr~rrXrgrrSr rerdrrrrNotImplementedErrorrr NameError__all__r1rrr rrrr5rrLr}r|r7r8rrZCONTTYPEr#r"rMr/r1r[r rrrr^rzrPrwrjr,rFrxrr getfilesystemencodingr8r=rKrQrVrYrhrq Exceptionrr rrr r rvrwrxrHryrzrobjectrrBufferedReaderrcompilererrrrrrrrrrrrrr rrrrArrr6r6r6r7s          yh   D}b b