Viewing File: /usr/lib64/python3.6/__pycache__/tarfile.cpython-36.pyc

3

sj'@s:dZdZdZdZdZdZddlmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZyddlZWnek
rdZYnXyddlZWnek
rdZYnXeefZyeef7ZWnek
rYnXd	d
dd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;e<e<e<e=e=e=d7Z>e
j?d8krd9Z@ne	jAZ@d:d;ZBd<d=ZCd>d?ZDd@e6fdAdBZEdCdDZFdedfdEdFZGdGdHZHdIdJZIGdKddeJZKGdLddeKZLGdMd
d
eKZMGdNddeKZNGdOddeKZOGdPddeKZPGdQdRdRePZQGdSdTdTePZRGdUdVdVePZSGdWdXdXePZTGdYdZdZePZUGd[d\d\ZVGd]d^d^ZWGd_d`d`eXZYGdadbdbeXZZGdcddddej[Z\ej]deZ^GdfdgdgeKZ_Gdhdidie_Z`Gdjdkdke_ZaGdldmdme_ZbGdndodoe_ZcGdpdqdqe_ZdGdrdsdse_Zee_eeLfZfddudvZgdwdxZhdydzZid{d|Zjeheiejd}ZkeXZlGd~d
d
eXZmGdd	d	eXZnddZoenjZddZpeqdkr6epdS)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	ReadErrorCompressionErrorStreamErrorExtractErrorHeaderErrorENCODING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=cCs|ddkrfd}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)sumstructunpack_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@)shutilcopyfileobjdivmodrEreadr0write)	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)rz!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
dZdd
Z	ddZ
ddZdddZ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}|p<d|_||_||_||_||_d|_	d|_
d|_yL|dkryddl}Wnt
k
rtd	YnX||_|jd|_|d
kr|j|j|_n|jn|dkr:yddl}Wnt
k
r
tdYnX|d
kr.d|_|j|_t|_n
|j|_n||d
kryddl}Wnt
k
rntdYnX|d
krd|_|j|_|j|_n
|j|_n|dkrtd|Wn&|js|jjd|_YnXdS)z$Construct a _Stream object.
        TNF*rgzzzlib module is not availabler{bz2zbz2 module is not availablexzzlzma module is not availabletarzunknown compression type %r) _extfileobjrz_StreamProxygetcomptyperricomptypefileobjr`rUposclosedzlibImportErrorrcrc32crc
_init_read_gzerrorr__init_write_gzrdbufZBZ2DecompressorcmpOSErrorZ
BZ2CompressorlzmaZLZMADecompressor	LZMAErrorZLZMACompressorr)	rrrirrr`rrrr6r6r7r_sl












z_Stream.__init__cCst|dr|jr|jdS)Nr)rrr)rr6r6r7__del__sz_Stream.__del__cCs|jjd|jj|jj|jjd|_tjdtt	j	}|j
d|d|jjdrf|jdd|_|j
|jj
d	d
tdS)z6Initialize for writing with gzip compression.
        	rz<Lssz.gzNz
iso-8859-1replace)rZcompressobjZDEFLATED	MAX_WBITSZ
DEF_MEM_LEVELrrSpackrFtime_Stream__writerendswithr/r1)rZ	timestampr6r6r7rsz_Stream._init_write_gzcCsR|jdkr|jj||j|_|jt|7_|jdkrD|jj|}|j|dS)z&Write string s to the stream.
        rrN)	rrrrrr0rcompressr)rr2r6r6r7r\s

z
_Stream.writecCsR|j|7_x>t|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.__writecCs|jr
dSd|_z|jdkr:|jdkr:|j|jj7_|jdkr|jr|jj|jd|_|jdkr|jjtj	d|j
|jjtj	d|jd@Wd|js|jj
XdS)	z[Close the _Stream object. No operation should be
           done on it afterwards.
        NTr|rrrz<Ll)rrirrUrflushrr\rSrrrrr)rr6r6r7rs
z
_Stream.closecCs|jj|jj|_d|_|jddkr0td|jddkrFtdt|jd}|jd|d	@rt|jdd
t|jd}|j	||d@rx|jd}|s|t
krPqW|d@rx|jd}|s|t
krPqW|d@r|jdd
S)z:Initialize for reading a gzip compressed fileobj.
        rr$sznot a gzip filer#zunsupported compression methodrAr@rWN)rZ
decompressobjrrr
_Stream__readrrordr[r1)rflagZxlenr2r6r6r7rs.
 


z_Stream._init_read_gzcCs|jS)z3Return the stream's file pointer position.
        )r)rr6r6r7tellsz_Stream.tellrcCsb||jdkrTt||j|j\}}x"t|D]}|j|j}|s.Pq.W|j|ntd|jS)zXSet the stream's file pointer to pos. Negative seeking
           is forbidden.
        rz seeking backwards is not allowed)rrZr`rEr[r	)rrrarbrJdatar6r6r7seeksz_Stream.seekNcCsZ|dkr:g}x |j|j}|s P|j|qWdj|}n
|j|}|jt|7_|S)zReturn the next size number of bytes from the stream.
           If size is not defined, return all bytes of the stream
           up to EOF.
        Nr)_readr`appendjoinrr0)rr'trUr6r6r7r[s
z_Stream.readcCs|jdkr|j|St|j}|jg}xd||kr|j|j}|sBPy|jj|}Wn|jk
rptdYnX|j	||t|7}q(Wdj
|}||d|_|d|S)z+Return size bytes from the stream.
        rzinvalid compressed datarN)rrr0rr`rZ
decompressr_rrr)rr'crrUr6r6r7r%s"





z
_Stream._readcCsnt|j}|jg}x6||krH|jj|j}|s0P|j||t|7}qWdj|}||d|_|d|S)zsReturn size bytes from stream. If internal buffer is empty,
           read another block from the stream.
        rN)r0rUrr[r`rr)rr'rrrUr6r6r7Z__read;s



z_Stream.__read)r)N)rrrsrtrurrrr\rrrrrr[rrr6r6r6r7rTs	F
	

rc@s0eZdZdZddZddZddZdd	Zd
S)rzsSmall proxy class that enables transparent compression
       detection for the Stream interface (mode 'r|*').
    cCs||_|jjt|_dS)N)rr[	BLOCKSIZErU)rrr6r6r7rQsz_StreamProxy.__init__cCs|jj|_|jS)N)rr[rU)rr'r6r6r7r[Us
z_StreamProxy.readcCsP|jjdrdS|jdddkr8|jdddkr8d	S|jjdrHdSd
SdS)NsrrrsBZhr
s1AY&SYr]7zXZrr)rr)rU
startswith)rr6r6r7rYs$z_StreamProxy.getcomptypecCs|jjdS)N)rr)rr6r6r7rcsz_StreamProxy.closeN)rrrsrtrurr[rrr6r6r6r7rLs

rc@sjeZdZdZdddZddZddZd	d
ZddZd
dZ	e
jfddZdddZ
ddZddZdS)_FileInFilezA thin wrapper around an existing file object that
       provides a part of its data as an individual file
       object.
    NcCs||_||_||_d|_t|dd|_d|_|dkr>d|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)rroffset_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_attrsrZtarget_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)rrrr6r6r7data_filter[sr)Z
fully_trustedrrc@seZdZdZdrZdsddZddZddZeeeZ	ddZ
dd Zee
eZd!d"Z
eeeeeeeed#ed$
d%d&Zd'd(Zeed)fd*d+Zd,d-Zd.d/Zd0d1Zed2d3Zd4d5Zed6d7Zed8d9Zed:d;Zed<d=Zed>d?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,devmajordevminorrrpax_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,rrresultr6r6r7rs(
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)z<Return a tar header as a string of 512 byte blocks.
        Nz%s may not be Nonezinvalid format)	ritemsr.r
create_ustar_headerrcreate_gnu_headerrcreate_pax_header)rrPr4r5rrvaluer6r6r7tobufsz
TarInfo.tobufcCsnt|d<t|dj||tkr(tdt|dj||tkr^|j|d||\|d<|d<|j|t||S)z3Return the object as a ustar header block.
        magicrzlinkname is too longrprefix)	POSIX_MAGICr0r/LENGTH_LINKr.LENGTH_NAME_posix_split_name_create_headerr
)rrr4r5r6r6r7rszTarInfo.create_ustar_headercCst|d<d}t|dj||tkr<||j|dt||7}t|dj||tkrl||j|dt||7}||j|t	||S)z:Return the object as a GNU header block sequence.
        rrrr)
	GNU_MAGICr0r/r_create_gnu_long_headerGNUTYPE_LONGLINKrGNUTYPE_LONGNAMErr)rrr4r5rUr6r6r7rszTarInfo.create_gnu_headerc
Cs4t|d<|jj}xddtfddtfddfD]h\}}}||kr@q,y||jd	d
Wn"tk
rv||||<w,YnXt|||kr,||||<q,WxldD]d\}}||krd||<q||}d|kod|dknst|t	rt
|||<d||<qW|r|j|t|}	nd}	|	|j
|td	dS)zReturn the object as a ustar header block. If it cannot be
           represented this way, prepend a pax extended header sequence
           with supplement information.
        rrr%rr&r+ r,rBrCr)r@r*r'r(rr#rr)r+r+r$)r,r,r$r)r@r*r@r'r%r(r%)r&r'r(r))rrrrrr/UnicodeEncodeErrorr0
isinstancefloatstr_create_pax_generic_headerXHDTYPErr
)
rrr4rrZhnamer3rOvalrUr6r6r7r	s4
.zTarInfo.create_pax_headercCs|j|tdS)zAReturn the object as a pax global header block sequence.
        zutf-8)r.XGLTYPE)clsrr6r6r7create_pax_global_header:sz TarInfo.create_pax_global_headercCs|jd}xltdt|D]R}dj|d|}dj||d}t|j||tkrt|j||tkrPqWtd||fS)zUSplit a name longer than 100 chars into a prefix
           and a name part.
        rr#Nzname is too long)splitrEr0rr/
LENGTH_PREFIXrr.)rrr4r5Z
componentsrJrr6r6r7r@s
zTarInfo._posix_split_namecCs|jdttfk}|r@t|jddd|}t|jddd|}ntdd||}tdd||}|jdt}|dkrxtdt|jd	dd
||t|jddd@d|t|jd
dd|t|jddd|t|jddd|t|jddd|d|t|jddd
|||jdtt|jddd||t|jddd||t|jddd|t|jddd|t|jddd||g}tj	dt
dj|}	t|	t
dd}
|	dd t
d|
d|	d!d}	|	S)"zReturn a header block. info is a dictionary with file
           information, format must be one of the *_FORMAT constants.
        rrrr@rrNzTarInfo.type must not be Nonerrriir)r*r'r%r(s        rrr+r$r,rrz%dsrilz%06orBieii)getCHRTYPEBLKTYPErQr8rr.rrSrrrrVrL)rrPr4r5Zhas_device_fieldsrrZfiletypepartsrUrr6r6r7rPs8
&zTarInfo._create_headercCs.tt|t\}}|dkr*|t|t7}|S)zdReturn the string payload filled with zero bytes
           up to the next 512 byte border.
        r)rZr0rr1)Zpayloadrarbr6r6r7_create_payloadzszTarInfo._create_payloadcCsR|j||t}i}d|d<||d<t||d<t|d<|j|t|||j|S)zTReturn a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
           for name.
        z
././@LongLinkrrr'r)r/r1r0r rr
r:)r2rrr4r5rr6r6r7r!szTarInfo._create_gnu_long_headercCs:d}x@|jD]4\}}y|jddWqtk
r@d}PYqXqWd}|rV|d7}x|jD]\}}|jd}|r|j|d}n
|jd}t|t|d}d	}	}
x"|tt|
}	|	|
krP|	}
qW|tt|
d
d|d|d
7}q`Wi}d|d<||d<t||d<t|d<|j|td
d|j	|S)zReturn a POSIX.1-2008 extended or global header sequence
           that contains a list of keyword, value pairs. The values
           must be strings.
        Fzutf-8rCTrs21 hdrcharset=BINARY
rrrrB =
z././@PaxHeaderrrr'rr)
rr/r*r0r-rLrrr
r:)r2rrr4ZbinarykeywordrZrecordslrIr<rr6r6r7r.s<

*z"TarInfo._create_pax_generic_headercCs|j|||S)zConstruct a TarInfo object from a 512 byte bytes object.

        To support the old v7 tar format AREGTYPE headers are
        transformed to DIRTYPE headers if their name ends in '/'.
        )_frombuf)r2rUr4r5r6r6r7frombufszTarInfo.frombuf)dircheckcCszt|dkrtdt|tkr(td|jttkr>tdt|dd}|t|krbt	d|}t
|dd|||_t|dd	|_t|d	d
|_
t|d
d|_t|dd|_t|dd|_||_|dd
|_t
|d
d|||_t
|dd|||_t
|dd|||_t|dd|_t|dd|_t
|dd||}|r|jtkr|jjdrt|_|jtkr<d}g}	xrtdD]f}
y0t|||d}t||d|d}Wntk
rPYnX|	j||f|d7}qWt|d}
t|dd}|	|
|f|_ |j!rT|jj"d|_|rv|jt#krv|d|j|_|S)a\Construct a TarInfo object from a 512 byte bytes object.

        If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will
        be normalized to ``DIRTYPE`` if the name ends in a trailing slash.
        ``dircheck`` must be set to ``False`` if this function is called
        on a follow-up header such as ``GNUTYPE_LONGNAME``.
        rzempty headerztruncated headerzend of file headerzbad checksumrlt|ii	i)iIiQiYirirr%iii)$r0rvrrwcountr1rxrKrVrHr=rrir)r*r'r(rrrr+r,rrAREGTYPErrGNUTYPE_SPARSErEr.rboolrrrstrip	GNU_TYPES)r2rUr4r5rBrobjrrstructsrJrnumbytes
isextendedorigsizer6r6r7r@sZ	 
zTarInfo._frombufcCs
|j|S)zOReturn the next TarInfo object from TarFile object
           tarfile.
        )_fromtarfile)r2rr6r6r7fromtarfileszTarInfo.fromtarfilecCs<|jjt}|j||j|j|d}|jjt|_|j|S)z;
        See dircheck documentation in _frombuf().
        )rB)	rr[rr@r4r5rr_proc_member)r2rrBrUrQr6r6r7rVs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)rr#r"
_proc_gnulongrM_proc_sparser/r1SOLARIS_XHDTYPE	_proc_pax
_proc_builtin)rrr6r6r7rX1s



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.
        )
rrrrrSUPPORTED_TYPES_blockr'r_apply_pax_inforr4r5)rrrr6r6r7r]>s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'rVrryrrr#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
||}|srtdyt|j
d}Wntk
rtdYnX|dkrtd||t|krtd|jd|d}	||jdd|	}
|
jd\}}}
|s"|dks"||	dkr*td|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|dd}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_FIELDSrVrry_proc_gnusparse_01_proc_gnusparse_00r6_proc_gnusparse_10r/r[r`rrrr^)rrrUrrr4raw_headersrfr3Zheader_value_end_offsetZkeyword_and_valueZraw_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
<listcomp>sz.TarInfo._proc_gnusparse_01.<locals>.<listcomp>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_infocCs.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
rrrrrrrrrclassmethodr3rstaticmethodrr:r!r.rAr@rWrVrXr]rYrZr\rlrkrmr`rir_rr{rrrr~rrrrr6r6r6r7rnsn



1*
3	C
zc
@seZdZdZdZdZdZdZeZ	e
ZdZe
ZeZdZdkdd	Zedddefd
dZedld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(d<d=Z)d>d?Z*d@dAZ+dwdddBdCdDZ,dEdFZ-dGdHZ.dIdJZ/dKdLZ0dMdNZ1dOdPZ2dQdRZ3dSdTZ4dUdVZ5dWdXZ6dYdZZ7dxd[d\Z8d]d^Z9dyd_d`Z:dadbZ;dcddZ<dedfZ=dgdhZ>didjZ?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|drt	|j
tt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-rLabspathrrPrdereferenceignore_zerosr4r5rrdebug
errorlevelcopybufsizermembers_loadedrrinodesfirstmemberrarrWrrxrrr3r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.<locals>.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|dkrtdyddl}|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|dkrtdy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|dkrtdy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.<locals>.<listcomp>)r)rr6r6r7getnamesszTarFile.getnamescCsp|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|||||dq4Wn
|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



zTarFile.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.addfilecCsp|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)rr6r6r7<lambda>sz$TarFile.extractall.<locals>.<lambda>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%rrrZdirectoriesrrr
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
rtj|}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#ztarfile: %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#ztarfile: %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
trht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_membercCs@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~rr
)rrrr6r6r7r	s
zTarFile.makefifocCsnttdsttdr td|j}|dkr2d}|jrF|tjO}n
|tjO}tj||tj	|j
|jdS)z<Make a character or block device called targetpath.
        mknodrz'special devices not supported by systemNi)rr~r
rirrgS_IFBLKS_IFCHRrrrr)rrrrir6r6r7r
s
zTarFile.makedevcCs|j||ddS)N)r)rrrr6r6r7makelink
szTarFile.makelinkc	$Cs&d}yT|jr6tjj|r$tj|tj|j|dStjj|jrVtj	|j|dSWnt
k
rpd}YnXy|j|}Wn$tk
r|rt
dnYnX|dkr|}nR|dkrt
dy|||}Wn2tk
r}zt||j|WYdd}~XnX|dk	r"|j||||ddS)	a3Make a (symbolic) link called targetpath. If it cannot be created
          (platform limitation), we try to make a copy of the referenced file
          instead of a link.

          filter_function is only used when extracting a *different*
          member (e.g. as fallback to creating a link).
        FNTz%unable to resolve link inside archivez6makelink_with_filter: if filter_function is not None, z%extraction_root must also not be None)rrz[makelink_with_filter: if filter_function is not None, extraction_root must also not be None)rr~r%lexistsunlinksymlinkrrrlinksymlink_exceptionrrr
rrrr)	rrrrrZkeyerror_to_extracterrorrrcauser6r6r7r
sB	

 
zTarFile.makelink_with_filtercCsttdrtjdkr|j}|j}|sytrB|jrBtj|jd}Wntk
rXYnXyt	rv|j
rvt	j|j
d}Wntk
rYnX|dkrd}|dkrd	}y4|jrttdrtj
|||ntj|||Wntk
rtdYnXdS)
zSet owner of targetpath according to tarinfo. If numeric_owner
           is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
           is False, fall back to .gid/.uid when the search based on name
           fails.
        geteuidrr$Nr#lchownzcould not change ownerr9r9)rr~rr*r)rr,Zgetgrnamrrr+getpwnamrrrrr
)rrrrgur6r6r7rJ
s0

z
TarFile.chowncCsL|jdkrdSttdrHytj||jWntk
rFtdYnXdS)zASet file permissions of targetpath according to tarinfo.
        Nrzcould not change mode)rirr~rrr
)rrrr6r6r7rk
s

z
TarFile.chmodcCsV|j}|dkrdSttds dSytj|||fWntk
rPtdYnXdS)zBSet modification time of targetpath according to tarinfo.
        Nrz"could not change modification time)r(rr~rrr
)rrrr(r6r6r7rv
s
z
TarFile.utimecCs|jd|jdk	r$|j}d|_|S|j|jjkrZ|jj|jd|jjdsZtdd}xby|jj	|}WnHt
k
r}z2|jr|jdd|j|f|jt
7_wbWYdd}~Xntk
r,}zN|jr|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)rrrrrrr[rrrWrxrrrrHr-rvrwryrrr)rmrrr6r6r7ra
sL


zTarFile.nextc	Cs|j}d}|dk	rHy|j|}Wntk
r:d}YnX|d|}|rXtjj|}xLt|D]@}|r||j|jkrbd}qb|rtjj|j}n|j}||krb|SqbW|rt|dS)z}Find an archive member by name from bottom to top.
           If tarinfo is given, it is used as the starting point.
        FNT)	rindexr.r~r%rreversedrr)	rrr	normalizerZskippingrrmember_namer6r6r7r
s,
zTarFile._getmembercCs"x|j}|dkrPqWd|_dS)zWRead through the entire archive file and look for readable
           members.
        NT)rar)rrr6r6r7r
s
z
TarFile._loadcCs:|jrtd|jj|dk	r6|j|kr6td|jdS)znCheck if TarFile is still open, and if the operation's mode
           corresponds to TarFile's mode.
        z%s is closedNzbad operation for mode %r)rrrrrri)rrir6r6r7r
szTarFile._checkcCs`|jr.djtdtjj|j|jf}d}n
|j}|}|j||dd}|dkr\t	d||S)zZFind the target member of a symlink or hardlink member in the
           archive.
        rNT)rrzlinkname %r not found)
rrrr~r%rrrrr)rrrlimitrr6r6r7r
s zTarFile._find_link_targetccs|jr|jEdHdSd}|jdk	r:|j}|d7}|VxN|t|jkrV|j|}n"|jst|j}|sxd|_dSndS|d7}|Vq<WdS)z$Provide an iterator object.
        Nrr#T)rrrrar0)rrrr6r6r7__iter__s&
zTarFile.__iter__cCs||jkrt|tjddS)z.Write debugging output to sys.stderr.
        )fileN)rrprnstderr)rlevelmsgr6r6r7r's
zTarFile._dbgcCs|j|S)N)r)rr6r6r7	__enter__-szTarFile.__enter__cCs,|dkr|jn|js"|jjd|_dS)NT)rrrr)rrr	tracebackr6r6r7__exit__1s


zTarFile.__exit__)
Nr{NNNNNNrNNNN)r{N)r{Nr)r{Nr)r{NN)NNN)T)NTN)N)rN)rT)N)TF)NF)N)@rrrsrtrurrrrrrPrr4r5rrrrrrrrrrrrrrrrrrrrorrrrrrrrrrrrrrrrrrrrrrrarrrrr!rr&r(r6r6r6r7rfs
lZ!

d&=
D
62!6
(

	cCs.yt|}|jdStk
r(dSXdS)zfReturn True if name points to a tar archive that we
       are able to handle, else return False.
    TFN)rrr)rrr6r6r7r?scCsddl}d}|j|d}|jdddddd	|jd
dtdd
|j}|jddddd|jdddd5dd|jdddd6dd|jdddd d|j}|jr|jr|jd!d"|j	r*|j	}t
|rt|d# }|jt
|jtjd$WdQRX|jr&t
d%j|n|jd!d&j|n|jr|j}t
|rjtj|d'}|j|jd(WdQRXn|jd!d&j|n|jrPt|jd!kr|jd}tj}n,t|jd)kr|j\}}n|jd!|jt
|r<tj|d'}|j||jd*WdQRX|jrN|d+kr&d,j|}	nd-j||}	t
|	n|jd!d&j|n|jr|jjd}
tjj|
\}}d.d.d/d/d0d0d0d0d1}
||
krd2|
|nd3}|j}tj|
| }x|D]}|j|qWWdQRX|jrt
d4j|
n|jd!|jdS)7Nrz3A simple command line interface for tarfile module.)descriptionz-vz	--verbose
store_trueFzVerbose output)actiondefaulthelpz--filterz<filtername>zFilter for extraction)metavarchoicesr-z-lz--list	<tarfile>zShow listing of a tarfile)r.r-z-ez	--extract+<output_dir>zExtract tarfile into target dir)nargsr.r-z-cz--create<name><file>zCreate 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~curdirZformat_helprZcreatepopr%splitextr)r6r)parserrgargsr]rZtfr;r%Ztar_namerqZextZcompressionsZtar_modeZ	tar_files	file_namer6r6r7mainMs










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	rrvrwrxrHryrzrobjectrrBufferedReaderrcompilererrrrrrrrrrrrrrrrrrArrr6r6r6r7<module>s 



yh	



D}bb

Back to Directory File Manager