ok

Mini Shell

Direktori : /proc/self/root/opt/alt/python35/lib64/python3.5/__pycache__/
Upload File :
Current File : //proc/self/root/opt/alt/python35/lib64/python3.5/__pycache__/tarfile.cpython-35.pyc



>�Ma�k�@s�dZdZdZdZdZdZddlmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZyddlZddlZWnek
r�dZZYnXeefZyeef7ZWnek
rYnXd	d
ddgZd
ZdZedZdZdZdZ dZ!dZ"dZ#d
Z$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/d Z0d!Z1dZ2d"Z3d#Z4e3Z5e#e$e%e&e)e*e+e'e(e,e-e.fZ6e#e$e+e.fZ7e,e-e.fZ8d$d%d&d'd(d)d*d+fZ9d$d%d*d+hZ:d,e;d-e;d'e;d(e<d)e<d&e<iZ=e
j>d.d/fkr�d0Z?ne	j@�Z?d1d2�ZAd3d4�ZBd5d6�ZCd7e5d8d9�ZDd:d;�ZEded<d=�ZFd>d?�ZGd@dA�ZHGdBd�deI�ZJGdCdD�dDeJ�ZKGdEdF�dFeJ�ZLGdGdH�dHeJ�ZMGdIdJ�dJeJ�ZNGdKdL�dLeJ�ZOGdMdN�dNeO�ZPGdOdP�dPeO�ZQGdQdR�dReO�ZRGdSdT�dTeO�ZSGdUdV�dVeO�ZTGdWdX�dX�ZUGdYdZ�dZ�ZVGd[d\�d\eW�ZXGd]d^�d^eW�ZYGd_d`�d`ejZ�Z[Gdad
�d
eW�Z\Gdbd	�d	eW�Z]Gdcdd�dd�Z^ded�Z_e]jZdfdg�Z`eadhkr�e`�dS)iz,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.�)�openN�TarFile�TarInfo�
is_tarfile�TarErrorsi�sustar  sustar00�d��0�1�2�3�4�5�6�7�L�K�S�x�g�X���path�linkpath�size�mtime�uid�gid�uname�gnameZatimeZctime�ntZcezutf-8cCs2|j||�}|d|�|t|�tS)z8Convert a string to a null-terminated bytes object.
    N)�encode�len�NUL)�s�length�encoding�errors�r*�,/opt/alt/python35/lib64/python3.5/tarfile.py�stn�sr,cCs;|jd�}|dkr+|d|�}|j||�S)z8Convert a null-terminated bytes object to a string.
    srN���)�find�decode)r&r(r)�pr*r*r+�nts�sr1cCs�|ddkrd}x:tt|�d�D]"}|dK}|||d7}q-W|ddkr�dt|�d|}nRy1t|dd�}t|j�p�d	d�}Wntk
r�td
��YnX|S)z/Convert a number field to a python number.
    r��r���ascii�strict�0zinvalid header)r2r3)�ranger$r1�int�strip�
ValueError�InvalidHeaderError)r&�n�ir*r*r+�nti�s

r@r4cCsd|kod|dknrNtd|dt|�fd�t}n�|tkr�d|d|ko�d|dknr�|dkr�tdg�}ntdg�}d||}xEt|d�D]$}|jd|d@�|dL}q�Wntd	��|S)
z/Convert a python number to a number field.
    rr4rz%0*or6r5r2r3zoverflow in number field)�bytesr:r%�
GNU_FORMAT�	bytearrayr9�insertr<)r>�digits�formatr&r?r*r*r+�itn�s$*9rGcCsBdttjd|��}dttjd|��}||fS)a�Calculate 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.
    r5Z
148B8x356BZ
148b8x356b)�sum�structZunpack_from)�bufZunsigned_chksumZ
signed_chksumr*r*r+�calc_chksums�s	rKc	Cs�|dkrdS|dkr0tj||�dSd}t||�\}}xNt|�D]@}|j|�}t|�|kr�|d��|j|�qXW|dkr�|j|�}t|�|kr�|d��|j|�dS)zjCopy length bytes from fileobj src to fileobj dst.
       If length is None, copy the entire content.
    rN�izunexpected end of datai@)�shutil�copyfileobj�divmodr9�readr$�write)	�srcZdstr'�	exceptionZBUFSIZE�blocks�	remainder�brJr*r*r+rN�s$
rNcCs,ddl}|jdtd�tj|�S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder)�warnings�warn�DeprecationWarning�stat�filemode)�moderWr*r*r+r[�s	
r[cCsPttjdd�}|dk	r<|j|d�j|�}t|dd�dS)Nr(�backslashreplace�end� )�getattr�sys�stdoutr#r/�print)r&r(r*r*r+�_safe_printsrdc@seZdZdZdS)rzBase exception.N)�__name__�
__module__�__qualname__�__doc__r*r*r*r+rsc@seZdZdZdS)�ExtractErrorz%General exception for extract errors.N)rerfrgrhr*r*r*r+risric@seZdZdZdS)�	ReadErrorz&Exception for unreadable tar archives.N)rerfrgrhr*r*r*r+rjsrjc@seZdZdZdS)�CompressionErrorz.Exception for unavailable compression methods.N)rerfrgrhr*r*r*r+rksrkc@seZdZdZdS)�StreamErrorz=Exception for unsupported operations on stream-like TarFiles.N)rerfrgrhr*r*r*r+rlsrlc@seZdZdZdS)�HeaderErrorz!Base exception for header errors.N)rerfrgrhr*r*r*r+rmsrmc@seZdZdZdS)�EmptyHeaderErrorzException for empty headers.N)rerfrgrhr*r*r*r+rnsrnc@seZdZdZdS)�TruncatedHeaderErrorz Exception for truncated headers.N)rerfrgrhr*r*r*r+ro sroc@seZdZdZdS)�EOFHeaderErrorz"Exception for end of file headers.N)rerfrgrhr*r*r*r+rp#srpc@seZdZdZdS)r=zException for invalid headers.N)rerfrgrhr*r*r*r+r=&sr=c@seZdZdZdS)�SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rerfrgrhr*r*r*r+rq)srqc@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�
_LowLevelFilez�Low-level file object. Supports reading and writing.
       It is used instead of a regular file object for streaming
       access.
    cCsbdtjdtjtjBtjBi|}ttd�rF|tjO}tj||d�|_dS)N�r�w�O_BINARYi�)	�os�O_RDONLY�O_WRONLY�O_CREAT�O_TRUNC�hasattrrur�fd)�self�namer\r*r*r+�__init__6s	
z_LowLevelFile.__init__cCstj|j�dS)N)rv�closer|)r}r*r*r+r�?sz_LowLevelFile.closecCstj|j|�S)N)rvrPr|)r}rr*r*r+rPBsz_LowLevelFile.readcCstj|j|�dS)N)rvrQr|)r}r&r*r*r+rQEsz_LowLevelFile.writeN)rerfrgrhrr�rPrQr*r*r*r+rr0s
	rrc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zddd�Zddd�Z
dd�Zdd�ZdS)�_Streama�Class 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	Cs{d|_|dkr-t||�}d|_|dkrQt|�}|j�}|pZd|_||_||_||_||_d|_	d|_
d|_y�|dkr,yddl}Wnt
k
r�td	��YnX||_|jd�|_|d
kr|j�|j|_qI|j�n|dkr�yddl}Wnt
k
rhtd��YnX|d
kr�d|_|j�|_t|_qI|j�|_n�|d
kr-yddl}Wnt
k
r�td��YnX|d
krd|_|j�|_|j|_qI|j�|_n|dkrItd|��Wn*|jsf|jj�d|_�YnXdS)z$Construct a _Stream object.
        TNF�*��r�gzzzlib module is not availablers�bz2zbz2 module is not available�xzzlzma module is not available�tarzunknown compression type %r) �_extfileobjrr�_StreamProxy�getcomptyper~r\�comptype�fileobj�bufsizerJ�pos�closed�zlib�ImportErrorrk�crc32�crc�
_init_read_gz�errorrS�_init_write_gzr��dbufZBZ2Decompressor�cmp�OSErrorZ
BZ2Compressor�lzmaZLZMADecompressor�	LZMAErrorZLZMACompressorr�)	r}r~r\r�r�r�r�r�r�r*r*r+rSsl									
	


	
		
	z_Stream.__init__cCs't|d�r#|jr#|j�dS)Nr�)r{r�r�)r}r*r*r+�__del__�sz_Stream.__del__cCs�|jjd|jj|jj|jjd�|_tjdtt	j	���}|j
d|d�|jjd�r�|jdd�|_|j
|jj
d	d
�t�dS)z6Initialize for writing with gzip compression.
        �	rz<Ls�s�z.gzN�z
iso-8859-1�replace���)r�ZcompressobjZDEFLATED�	MAX_WBITSZ
DEF_MEM_LEVELr�rI�packr:�time�_Stream__writer~�endswithr#r%)r}Z	timestampr*r*r+r��s
	z_Stream._init_write_gzcCsq|jdkr*|jj||j�|_|jt|�7_|jdkr`|jj|�}|j|�dS)z&Write string s to the stream.
        r�r�N)	r�r�r�r�r�r$r��compressr�)r}r&r*r*r+rQ�sz
_Stream.writecCsk|j|7_xUt|j�|jkrf|jj|jd|j��|j|jd�|_qWdS)z]Write string s to the stream if a whole new block
           is ready to be written.
        N)rJr$r�r�rQ)r}r&r*r*r+Z__write�s z_Stream.__writecCs�|jr
dSd|_z�|jdkrO|jdkrO|j|jj�7_|jdkr�|jr�|jj|j�d|_|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.
        NTrtr�r�r�z<Ll��)r�r\r�rJr��flushr�rQrIr�r�r�r�r�)r}r*r*r+r��s			'	z
_Stream.closecCsO|jj|jj�|_d|_|jd�dkrFtd��|jd�dkrgtd��t|jd��}|jd�|d	@r�t|jd��d
t|jd��}|j	|�|d@rx'|jd�}|s�|t
kr�Pq�W|d@r4x'|jd�}|s/|t
kr
Pq
W|d@rK|jd�d
S)z:Initialize for reading a gzip compressed fileobj.
        r�rs�znot a gzip filerszunsupported compression method��r5r4rLN)r�Z
decompressobjr�r�r��
_Stream__readrjrk�ordrPr%)r}ZflagZxlenr&r*r*r+r��s.	

,



z_Stream._init_read_gzcCs|jS)z3Return the stream's file pointer position.
        )r�)r}r*r*r+�tell�sz_Stream.tellrcCs|||jdkrit||j|j�\}}x$t|�D]}|j|j�q?W|j|�ntd��|jS)zXSet the stream's file pointer to pos. Negative seeking
           is forbidden.
        rz seeking backwards is not allowed)r�rOr�r9rPrl)r}r�rTrUr?r*r*r+�seek�sz_Stream.seekNcCsy|dkrQg}x*|j|j�}|s.P|j|�qWdj|�}n|j|�}|jt|�7_|S)z�Return 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��append�joinr�r$)r}r�trJr*r*r+rPsz_Stream.readcCs�|jdkr|j|�St|j�}x�||kr�|j|j�}|sSPy|jj|�}Wn!|jk
r�td��YnX|j|7_|t|�7}q.W|jd|�}|j|d�|_|S)z+Return size bytes from the stream.
        r�zinvalid compressed dataN)	r�r�r$r�r�r�Z
decompressrSrj)r}r�crJr*r*r+r�s 
z
_Stream._readcCs�t|j�}xK||kr\|jj|j�}|s:P|j|7_|t|�7}qW|jd|�}|j|d�|_|S)zsReturn size bytes from stream. If internal buffer is empty,
           read another block from the stream.
        N)r$rJr�rPr�)r}rr�rJr*r*r+Z__read,sz_Stream.__read)rerfrgrhrr�r�rQr�r�r�r�r�rPr�r�r*r*r*r+r�Hs	F
	
r�c@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r�zsSmall proxy class that enables transparent compression
       detection for the Stream interface (mode 'r|*').
    cCs"||_|jjt�|_dS)N)r�rP�	BLOCKSIZErJ)r}r�r*r*r+rAs	z_StreamProxy.__init__cCs|jj|_|jS)N)r�rPrJ)r}rr*r*r+rPEsz_StreamProxy.readcCsj|jjd�rdS|jdd�dkrL|jdd�dkrLd	S|jjd�rbdSd
SdS)Ns�r�rr�sBZhr��
s1AY&SYr��]���7zXZr�r�)r�r�)rJ�
startswith)r}r*r*r+r�Is2z_StreamProxy.getcomptypecCs|jj�dS)N)r�r�)r}r*r*r+r�Ssz_StreamProxy.closeN)rerfrgrhrrPr�r�r*r*r*r+r�<s

r�c@s�eZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
jdd�Zddd�Z
dd�Zdd�ZdS)�_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}xp|D]h\}}||kr�|jj	d||df�|jj	d||||f�||7}||}q�W||jkr|jj	d||jdf�dS)Nrr~FT)
r��offsetr�positionr`r~r��	map_index�mapr�)r}r�r�rZ	blockinfoZlastposZrealposr*r*r+r`s(								 
z_FileInFile.__init__cCsdS)Nr*)r}r*r*r+r�ysz_FileInFile.flushcCsdS)NTr*)r}r*r*r+�readable|sz_FileInFile.readablecCsdS)NFr*)r}r*r*r+�writablesz_FileInFile.writablecCs
|jj�S)N)r��seekable)r}r*r*r+r��sz_FileInFile.seekablecCs|jS)z*Return the current file position.
        )r�)r}r*r*r+r��sz_FileInFile.tellcCs�|tjkr0tt|d�|j�|_n�|tjkr�|dkrgt|j|d�|_q�t|j||j�|_nC|tjkr�tt|j||j�d�|_ntd��|jS)z(Seek to a position in the file.
        rzInvalid argument)	�io�SEEK_SET�min�maxrr��SEEK_CUR�SEEK_ENDr<)r}r��whencer*r*r+r��s!(z_FileInFile.seekc	Csf|dkr|j|j}nt||j|j�}d}x!|dkraxs|j|j\}}}}||jko�|knr�PqP|jd7_|jt|j�krPd|_qPWt|||j�}|r7|jj||j|�|jj|�}t|�|kr*t	d��||7}n|t
|7}||8}|j|7_qAW|S)z!Read data from the file.
        Nr�rrzunexpected end of data)rr�r�r�r�r$r�r�rPrjr%)	r}rrJ�data�start�stopr�r'rVr*r*r+rP�s.


z_FileInFile.readcCs5|jt|��}||dt|��<t|�S)N)rPr$)r}rVrJr*r*r+�readinto�sz_FileInFile.readintocCs
d|_dS)NT)r�)r}r*r*r+r��sz_FileInFile.close)rerfrgrhrr�r�r�r�r�r�r�r�rPr�r�r*r*r*r+r�Zsr�cs"eZdZ�fdd�Z�S)�ExFileObjectcs5t|j|j|j|j�}t�j|�dS)N)r�r��offset_datar�sparse�superr)r}�tarfile�tarinfor�)�	__class__r*r+r�szExFileObject.__init__)rerfrgrr*r*)r�r+r��sr�c@sPeZdZdZdiZddd�Zdd�Zdd�Zeee�Z	dd�Z
dd �Zee
e�Zd!d"�Z
d#d$�Zeed%d&d'�Zd(d)�Zd*d+�Zd,d-�Zed.d/��Zd0d1�Zed2d3��Zed4d5��Zed6d7��Zed8d9��Zed:d;��Zed<d=��Zd>d?�Zd@dA�Z dBdC�Z!dDdE�Z"dFdG�Z#dHdI�Z$dJdK�Z%dLdM�Z&dNdO�Z'dPdQ�Z(dRdS�Z)dTdU�Z*dVdW�Z+dXdY�Z,dZd[�Z-d\d]�Z.d^d_�Z/d`da�Z0dbdc�Z1ddde�Z2dfdg�Z3dhS)jraInformational 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.
    r~r\rrrr�chksum�type�linknamer r!�devmajor�devminorr�r��pax_headersr�r��_sparse_structs�_link_targetr�cCs�||_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.
        i�rr�N)r~r\rrrrr��REGTYPEr�r�r r!r�r�r�r�r�r�)r}r~r*r*r+r�s"																zTarInfo.__init__cCs|jS)N)r~)r}r*r*r+�_getpath�szTarInfo._getpathcCs
||_dS)N)r~)r}r~r*r*r+�_setpath�szTarInfo._setpathcCs|jS)N)r�)r}r*r*r+�_getlinkpath�szTarInfo._getlinkpathcCs
||_dS)N)r�)r}r�r*r*r+�_setlinkpath�szTarInfo._setlinkpathcCs d|jj|jt|�fS)Nz<%s %r at %#x>)r�rer~�id)r}r*r*r+�__repr__szTarInfo.__repr__cCs�d|jd|jd@d|jd|jd|jd|jd|jd	|jd
|jd|j	d|j
d
|jd|ji
}|d	t
kr�|djd�r�|dd7<|S)z9Return the TarInfo's attributes as a dictionary.
        r~r\i�rrrrr�r�r�r r!r�r��/)r~r\rrrrr�r�r�r r!r�r��DIRTYPEr�)r}�infor*r*r+�get_infos 	
										$zTarInfo.get_info�surrogateescapecCsv|j�}|tkr+|j|||�S|tkrJ|j|||�S|tkrf|j||�Std��dS)z<Return a tar header as a string of 512 byte blocks.
        zinvalid formatN)r��USTAR_FORMAT�create_ustar_headerrB�create_gnu_header�
PAX_FORMAT�create_pax_headerr<)r}rFr(r)r�r*r*r+�tobufsz
TarInfo.tobufcCs�t|d<t|dj||��tkr8td��t|dj||��tkr�|j|d||�\|d<|d<|j|t||�S)z3Return the object as a ustar header block.
        �magicr�zlinkname is too longr~�prefix)	�POSIX_MAGICr$r#�LENGTH_LINKr<�LENGTH_NAME�_posix_split_name�_create_headerr�)r}r�r(r)r*r*r+r�*s
""'zTarInfo.create_ustar_headercCs�t|d<d}t|dj||��tkrR||j|dt||�7}t|dj||��tkr�||j|dt||�7}||j|t	||�S)z:Return the object as a GNU header block sequence.
        r�r�r�r~)
�	GNU_MAGICr$r#r��_create_gnu_long_header�GNUTYPE_LONGLINKr��GNUTYPE_LONGNAMEr�rB)r}r�r(r)rJr*r*r+r�7s
" " zTarInfo.create_gnu_headerc
Cs�t|d<|jj�}x�ddtfddtfddfD]�\}}}||kr\q>y||jd	d
�Wn#tk
r�||||<w>YnXt||�|kr>||||<q>Wx�dD]}\}}||kr�d||<q�||}d|kod|dkns,t|t	�r�t
|�||<d||<q�W|rh|j|t|�}	nd}	|	|j
|td	d�S)z�Return the object as a ustar header block. If it cannot be
           represented this way, prepend a pax extended header sequence
           with supplement information.
        r�r~rr�rr � r!r6r7rr4rr�rrrr�r�)r r r�)r!r!r��rr4�rr4�rr��rr�)r�r�r�r�)r�r��copyr�r�r#�UnicodeEncodeErrorr$�
isinstance�float�str�_create_pax_generic_header�XHDTYPEr�r�)
r}r�r(r�r~Zhnamer'rE�valrJr*r*r+r�Es4



4zTarInfo.create_pax_headercCs|j|td�S)zAReturn the object as a pax global header block sequence.
        zutf-8)r�XGLTYPE)�clsr�r*r*r+�create_pax_global_headervsz TarInfo.create_pax_global_headercCs�|jd�}x�tdt|��D]u}dj|d|��}dj||d��}t|j||��tkr%t|j||��tkr%Pq%Wtd��||fS)zUSplit a name longer than 100 chars into a prefix
           and a name part.
        r�rNzname is too long)�splitr9r$r�r#�
LENGTH_PREFIXr�r<)r}r~r(r)Z
componentsr?r�r*r*r+r�|szTarInfo._posix_split_namecCs�t|jdd�d||�t|jdd�d@d|�t|jdd�d|�t|jd	d�d|�t|jd
d�d|�t|jdd�d|�d
|jdt�t|jdd�d||�|jdt�t|jdd�d||�t|jdd�d||�t|jdd�d|�t|jdd�d|�t|jdd�d||�g}tjdtdj|��}t	|td��d}|dd�t
d|d�|d d�}|S)!z�Return a header block. info is a dictionary with file
           information, format must be one of the *_FORMAT constants.
        r~r�rr\ri�r4rrrr�rs        r�r�r�r r�r!r�r�r�r	z%dsr�Nilz%06or6iei����i����)r,�getrGr�r�rIr�r�r�rKrA)r�rFr(r)�partsrJr�r*r*r+r��s&$/zTarInfo._create_headercCs=tt|�t�\}}|dkr9|t|t7}|S)zdReturn the string payload filled with zero bytes
           up to the next 512 byte border.
        r)rOr$r�r%)ZpayloadrTrUr*r*r+�_create_payload�szTarInfo._create_payloadcCsm|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
././@LongLinkr~r�rr�)r#r%r$r�r�r�r
)rr~r�r(r)r�r*r*r+r��s


zTarInfo._create_gnu_long_headercCs�d}xJ|j�D]<\}}y|jdd�Wqtk
rNd}PYqXqWd}|ri|d7}x�|j�D]�\}}|jd�}|r�|j|d�}n|jd�}t|�t|�d}d	}	}
x-|tt|
��}	|	|
krP|	}
q�W|tt|
�d
�d|d|d
7}qvWi}d|d<||d<t|�|d<t|d<|j|td
d�|j	|�S)z�Return a POSIX.1-2008 extended or global header sequence
           that contains a list of keyword, value pairs. The values
           must be strings.
        Fzutf-8r7Tr�s21 hdrcharset=BINARY
r�r�rr6� �=s
z././@PaxHeaderr~r�rr�r�)
�itemsr#r�r$rrAr�r�r�r
)rr�r�r(Zbinary�keyword�valueZrecords�lr>r0r�r*r*r+r�s<




1


z"TarInfo._create_pax_generic_headercCsNt|�dkrtd��t|�tkr<td��|jt�tkr]td��t|dd��}|t|�kr�t	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�||�}|jtkr'|jjd�r't|_|jtkrd}g}x�td�D]u}	y<t|||d��}
t||d|d��}Wntk
r�PYnX|j|
|f�|d7}qOWt|d�}t|dd��}
|||
f|_ |j!�r!|jj"d�|_|rJ|jt#krJ|d|j|_|S)zAConstruct a TarInfo object from a 512 byte bytes object.
        rzempty headerztruncated headerzend of file header��zbad checksumr�l�t�|��ii	i)iIiQiYi�r�i�r�r��i�i�i�)$r$rnr�ro�countr%rpr@rKr=r1r~r\rrrrr�r�r�r r!r�r��AREGTYPEr�r��GNUTYPE_SPARSEr9r<r��boolr��isdir�rstrip�	GNU_TYPES)rrJr(r)r��objr�r��structsr?r��numbytes�
isextended�origsizer*r*r+�frombuf�sZ		!	"
zTarInfo.frombufcCsP|jjt�}|j||j|j�}|jj�t|_|j|�S)zOReturn the next TarInfo object from TarFile object
           tarfile.
        )	r�rPr�r(r(r)r�r��_proc_member)rr�rJr#r*r*r+�fromtarfile5szTarInfo.fromtarfilecCst|jttfkr"|j|�S|jtkr>|j|�S|jtttfkrc|j	|�S|j
|�SdS)zYChoose the right processing method depending on
           the type and call it.
        N)r�r�r��
_proc_gnulongr�_proc_sparserr�SOLARIS_XHDTYPE�	_proc_pax�
_proc_builtin)r}r�r*r*r+r)Js


zTarInfo._proc_membercCsu|jj�|_|j}|j�s6|jtkrL||j|j�7}||_|j	|j
|j|j�|S)zfProcess a builtin type or an unknown type which
           will be treated as a regular file.
        )
r�r�r��isregr��SUPPORTED_TYPES�_blockrr��_apply_pax_infor�r(r))r}r�r�r*r*r+r/Ws		zTarInfo._proc_builtincCs�|jj|j|j��}y|j|�}Wntk
rQtd��YnX|j|_|jt	kr�t
||j|j�|_
n*|jtkr�t
||j|j�|_|S)zSProcess the blocks that hold a GNU longname
           or longlink member.
        z missing or bad subsequent header)r�rPr2rr*rmrqr�r�r�r1r(r)r~r�r�)r}r�rJ�nextr*r*r+r+hs
zTarInfo._proc_gnulongc
Cs#|j\}}}|`x�|r�|jjt�}d}x�td�D]�}y<t|||d��}t||d|d��}	Wntk
r�PYnX|r�|	r�|j||	f�|d7}qFWt|d�}qW||_	|jj
�|_|j|j|j
�|_||_
|S)z8Process a GNU sparse header plus extra headers.
        r�r�ri�)r�r�rPr�r9r@r<r�rr�r�r�r2rr�)
r}r�r$r&r'rJr�r?r�r%r*r*r+r,~s(	"
		zTarInfo._proc_sparsecCs�|jj|j|j��}|jtkr9|j}n|jj�}tj	d|�}|dk	r�|j
d�jd�|d<|jd�}|dkr�|j
}nd}tjd�}d}x�|j||�}|s�P|j�\}	}
t|	�}	||jd	�d|jd�|	d�}|j|
dd|j�}
|
tkrt|j|||j
|j�}n|j|dd|j�}|||
<||	7}q�Wy|j|�}Wntk
r�td
��YnXd|kr�|j||�n_d|kr|j|||�n=|jd
�dkrY|jd�dkrY|j|||�|jttfkr�|j||j
|j�|j |_ d|kr�|j!}
|j"�s�|jt#kr�|
|j|j�7}
|
|_ |S)zVProcess an extended or global header as described in
           POSIX.1-2008.
        s\d+ hdrcharset=([^\n]+)\nNrzutf-8�
hdrcharsetZBINARYs(\d+) ([^=]+)=rrz missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major�1zGNU.sparse.minorr8r)$r�rPr2rr�rr�r��re�search�groupr/rr(�compile�match�groupsr:r^r��_decode_pax_fieldr)�PAX_NAME_FIELDSr*rmrq�_proc_gnusparse_01�_proc_gnusparse_00�_proc_gnusparse_10rr-r3r�r�r0r1)r}r�rJr�r<r6r(Zregexr�r'rrr4r�r*r*r+r.�s`.	

*		zTarInfo._proc_paxcCs�g}x6tjd|�D]"}|jt|jd���qWg}x6tjd|�D]"}|jt|jd���qXWtt||��|_dS)z?Process a GNU tar extended sparse header, version 0.0.
        s\d+ GNU.sparse.offset=(\d+)\nrs\d+ GNU.sparse.numbytes=(\d+)\nN)r8�finditerr�r:r:�list�zipr�)r}r4r�rJZoffsetsr<r%r*r*r+rAs  zTarInfo._proc_gnusparse_00cCsVdd�|djd�D�}tt|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 0.1.
        cSsg|]}t|��qSr*)r:)�.0�xr*r*r+�
<listcomp>s	z.TarInfo._proc_gnusparse_01.<locals>.<listcomp>zGNU.sparse.map�,Nrr)r	rDrEr�)r}r4r�r�r*r*r+r@s zTarInfo._proc_gnusparse_01cCs�d}g}|jjt�}|jdd�\}}t|�}xgt|�|dkr�d|kr}||jjt�7}|jdd�\}}|jt|��qEW|jj�|_t	t
|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 1.0.
        Ns
rr)r�rPr�r	r:r$r�r�r�rDrEr�)r}r4r�r�Zfieldsr�rJZnumberr*r*r+rBszTarInfo._proc_gnusparse_10c
Csx�|j�D]�\}}|dkr8t|d|�q
|dkr]t|dt|��q
|dkr�t|dt|��q
|tkr
|tkr�yt||�}Wntk
r�d}YnX|dkr�|jd�}t|||�q
W|j�|_dS)	zoReplace fields with supplemental information from a previous
           pax extended or global header.
        zGNU.sparse.namerzGNU.sparse.sizerzGNU.sparse.realsizerr�N)	r�setattrr:�
PAX_FIELDS�PAX_NUMBER_FIELDSr<r!r�r�)r}r�r(r)rrr*r*r+r3!s"
zTarInfo._apply_pax_infocCs=y|j|d�SWn"tk
r8|j||�SYnXdS)z1Decode a single field from a pax record.
        r7N)r/�UnicodeDecodeError)r}rr(Zfallback_encodingZfallback_errorsr*r*r+r>8s
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)rOr�)r}rrTrUr*r*r+r2@s
zTarInfo._blockcCs
|jtkS)N)r��
REGULAR_TYPES)r}r*r*r+r0Isz
TarInfo.isregcCs
|j�S)N)r0)r}r*r*r+�isfileKszTarInfo.isfilecCs
|jtkS)N)r�r�)r}r*r*r+r Msz
TarInfo.isdircCs
|jtkS)N)r��SYMTYPE)r}r*r*r+�issymOsz
TarInfo.issymcCs
|jtkS)N)r��LNKTYPE)r}r*r*r+�islnkQsz
TarInfo.islnkcCs
|jtkS)N)r��CHRTYPE)r}r*r*r+�ischrSsz
TarInfo.ischrcCs
|jtkS)N)r��BLKTYPE)r}r*r*r+�isblkUsz
TarInfo.isblkcCs
|jtkS)N)r��FIFOTYPE)r}r*r*r+�isfifoWszTarInfo.isfifocCs
|jdk	S)N)r�)r}r*r*r+�issparseYszTarInfo.issparsecCs|jtttfkS)N)r�rTrVrX)r}r*r*r+�isdev[sz
TarInfo.isdevN)r~r\rrrrr�r�r�r r!r�r�r�r�r�r�r�r�r�)4rerfrgrh�	__slots__rr�r��propertyrr�r�rr�r��DEFAULT_FORMAT�ENCODINGr�r�r�r��classmethodrr��staticmethodr�r
r�rr(r*r)r/r+r,r.rAr@rBr3r>r2r0rOr rQrSrUrWrYrZr[r*r*r*r+r�s`
1
3?
f	c@s�eZdZdZdZdZdZdZeZ	e
ZdZe
ZeZdddddddddddddd	�Zeddded
d��Zedddd
��Zeddddd��Zeddddd��Zeddddd��ZddddddddiZdd�Zdd �Zd!d"�Zd#d$�Zdddd%d&�Zd'd(dd)d*�Zdd'dd+dd,d-�Z dd.d/�Z!d0dd1dd2d3�Z"d4d'd1dd5d6�Z#d7d8�Z$d'dd9d:�Z%d;d<�Z&d=d>�Z'd?d@�Z(dAdB�Z)dCdD�Z*dEdF�Z+dGdH�Z,dIdJ�Z-dKdL�Z.dMdN�Z/dddOdP�Z0dQdR�Z1ddSdT�Z2dUdV�Z3dWdX�Z4dYdZ�Z5d[d\�Z6d]d^�Z7dS)_rz=The TarFile Class provides an interface to tar archives.
    rFrNrsr�c
Cs�ddddddddi}
||
kr6td	��||_|
||_|s�|jdkr�tjj|�r�d|_d|_t||j�}d
|_n`|dkr�t|d�r�t	|j
ttf�r�|j
}t|d
�r�|j|_d|_|rtjj
|�nd|_
||_|dk	rC||_|dk	rX||_|dk	rm||_|dk	r�||_|dk	r�||_|	|_|
dk	r�|jtkr�|
|_n	i|_|dk	r�||_|dk	r�||_d
|_g|_d
|_|jj�|_i|_y:|jdkrZd|_|j �|_|jdkrx�|jj!|j�y&|jj"|�}|jj#|�Wqlt$k
r�|jj!|j�PYqlt%k
r}zt&t|���WYdd}~XqlXqlW|jdkrid|_|jri|jj'|jj(��}|jj)|�|jt*|�7_Wn*|js�|jj+�d|_�YnXdS)a�Open 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.
        rs�rb�azr+brt�wbrGZxbz!mode must be 'r', 'a', 'w' or 'x'FNr~r\T)rcrtrG),r<r\�_modervr�exists�	bltn_openr�r{rr~rrA�abspathr�rFr��dereference�ignore_zerosr(r)r�r��debug�
errorlevelr��members�_loadedr�r��inodes�firstmemberr4r�r*r�rprmrjrr�rQr$r�)r}r~r\r�rFr�rirjr(r)r�rkrlZmodes�erJr*r*r+rys�	
"				!															
)			
	zTarFile.__init__csY|r|rtd��|dkr��fdd�}x�t�jd|�D]�}t��j|�}|dk	r�|j�}	y||d||�SWqNttfk
r�}
z!|dk	r�|j|	�wNWYdd}
~
XqNXqNWtd��nSd	|kr}|jd	d
�\}}|p#d}|p/d}|�jkrZt��j|�}ntd|��|||||�Sd
|kr'|jd
d
�\}}|p�d}|p�d}|dkr�td��t	|||||�}y�||||�}
Wn|j
��YnXd|
_|
S|dkrI�j||||�Std��dS)a�Open 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 openrs�r:*cs�j|dkS)N�taropen)�	OPEN_METH)r�)rr*r+�not_compressedsz$TarFile.open.<locals>.not_compressed�keyNz%file could not be opened successfully�:rr�zunknown compression type %r�|rtzmode must be 'r' or 'w'FrcrGzundiscernible mode)rsrr)rsrt)rcrtrG)
r<�sortedrtr`r�rjrkr�r	r�r�r�rs)rr~r\r�r��kwargsrur��funcZ	saved_posrqr[�streamr�r*)rr+r�sP%

	zTarFile.opencKs+|dkrtd��|||||�S)zCOpen uncompressed tar archive name for reading or writing.
        rsrcrtrGz!mode must be 'r', 'a', 'w' or 'x')rsrcrtrG)r<)rr~r\r�rzr*r*r+rsAszTarFile.taropenr�cKs)|dkrtd��yddl}|jWn$ttfk
rUtd��YnXy |j||d||�}Wn9tk
r�|dk	r�|dkr�td	���YnXy|j||||�}WnKtk
r|j	�|dkrtd	���Yn|j	��YnXd
|_
|S)zkOpen gzip compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtrGzmode must be 'r', 'w' or 'x'rNzgzip module is not availablerVznot a gzip fileF)rsrtrG)r<�gzipZGzipFiler��AttributeErrorrkr�rjrsr�r�)rr~r\r��
compresslevelrzr}r�r*r*r+�gzopenIs2 



	zTarFile.gzopencKs�|dkrtd��yddl}Wntk
rHtd��YnX|j|pX||d|�}y|j||||�}WnQttfk
r�|j�|dkr�t	d	���Yn|j��YnXd
|_
|S)zlOpen bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtrGzmode must be 'r', 'w' or 'x'rNzbz2 module is not availablerznot a bzip2 fileF)rsrtrG)r<r�r�rkZBZ2Filersr��EOFErrorr�rjr�)rr~r\r�rrzr�r�r*r*r+�bz2openks(
	

	zTarFile.bz2opencKs�|dkrtd��yddl}Wntk
rHtd��YnX|j|pX||d|�}y|j||||�}WnT|jtfk
r�|j�|dkr�t	d	���Yn|j��YnXd
|_
|S)zkOpen lzma compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtrGzmode must be 'r', 'w' or 'x'rNzlzma module is not available�presetznot an lzma fileF)rsrtrG)r<r�r�rkZLZMAFilersr�r�r�rjr�)rr~r\r�r�rzr�r�r*r*r+�xzopen�s&


	zTarFile.xzopenr�rsr�r�r�r�r�r�c
Cs�|jr
dSd|_zz|jdkr�|jjttd�|jtd7_t|jt�\}}|dkr�|jjtt|�Wd|j	s�|jj
�XdS)	zlClose the TarFile. In write-mode, two finishing zero blocks are
           appended to the archive.
        NTrcrtrGrr)rcrtrG)r�r\r�rQr%r�r�rO�
RECORDSIZEr�r�)r}rTrUr*r*r+r��s			z
TarFile.closecCs/|j|�}|dkr+td|��|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)�
_getmember�KeyError)r}r~r�r*r*r+�	getmember�szTarFile.getmembercCs$|j�|js|j�|jS)z�Return the members of the archive as a list of TarInfo objects. The
           list has the same order as the members in the archive.
        )�_checkrn�_loadrm)r}r*r*r+�
getmembers�s
	
zTarFile.getmemberscCsdd�|j�D�S)z�Return the members of the archive as a list of their names. It has
           the same order as the list returned by getmembers().
        cSsg|]}|j�qSr*)r~)rFr�r*r*r+rH�s	z$TarFile.getnames.<locals>.<listcomp>)r�)r}r*r*r+�getnames�szTarFile.getnamescCsI|jd�|dk	r"|j}|dkr4|}tjj|�\}}|jtjd�}|jd�}|j�}||_	|dkr�t
td�r�|jr�tj|�}q�tj
|�}ntj|j��}d}|j}t
j|�r�|j|jf}	|jrd|jdkrd|	|jkrd||j|	krdt}
|j|	}qt}
|	dr||j|	<n�t
j|�r�t}
nst
j|�r�t}
n[t
j|�r�t}
tj|�}n4t
j|�r�t }
nt
j!|�rt"}
ndS||_||_#|j$|_%|j&|_'|
tkrT|j(|_)n	d|_)|j*|_+|
|_,||_-t.r�yt.j/|j%�d|_0Wnt1k
r�YnXt2r�yt2j3|j'�d|_4Wnt1k
r�YnX|
t t"fkrEt
td�rEt
td	�rEtj5|j6�|_7tj8|j6�|_9|S)
a�Create 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.
        �awxNr��lstatr�rr�major�minor):r�r~rvr�
splitdriver��sep�lstripr�r�r{rir�rZ�fstat�fileno�st_mode�S_ISREG�st_ino�st_dev�st_nlinkrorRr��S_ISDIRr��S_ISFIFOrX�S_ISLNKrP�readlink�S_ISCHRrT�S_ISBLKrVr\�st_uidr�st_gidr�st_sizer�st_mtimerr�r��pwd�getpwuidr r��grpZgetgrgidr!r��st_rdevr�r�r�)r}r~�arcnamer�Zdrvr�Zstatresr�Zstmd�inoder�r*r*r+�
gettarinfo�s~	
			"
									

zTarFile.gettarinfoTrmcCsQ|j�|dkr|}x.|D]&}|r�ttj|j��td|jpZ|j|jpi|jf�|j	�s�|j
�r�tdd|j|jf�ntd|j
�tdtj|j�dd��t|j|j�r�dnd	�|rB|j�r%td
|j�|j�rBtd|j�t�q#WdS)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:%02dr�r�r�z-> zlink to )r�rdrZr[r\r rr!rrUrWr�r�rr�Z	localtimerr~r rQr�rSrc)r}�verbosermr�r*r*r+rDCs*

#zTarFile.list�filterc	
Cs�|jd�|dkr|}|dk	rnddl}|jdtd�||�rn|jdd|�dS|jdk	r�tjj|�|jkr�|jdd|�dS|jd|�|j	||�}|dkr�|jdd	|�dS|dk	r2||�}|dkr2|jdd|�dS|j
�rjt|d
��}|j||�WdQRXn�|j
�r�|j|�|r�xatj|�D]@}|jtjj||�tjj||�||d|�q�Wn
|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.
        r�Nrzuse the filter argument insteadrztarfile: Excluded %rztarfile: Skipped %rrztarfile: Unsupported type %rrbr�)r�rWrXrY�_dbgr~rvrrhr�r0rg�addfiler �listdir�addr�)	r}r~r�Z	recursiveZexcluder�rWr��fr*r*r+r�csB
	
*
*zTarFile.addcCs�|jd�tj|�}|j|j|j|j�}|jj|�|jt	|�7_|dk	r�t
||j|j�t|jt
�\}}|dkr�|jjtt
|�|d7}|j|t
7_|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().
        r�Nrr)r�r�r�rFr(r)r�rQr�r$rNrrOr�r%rmr�)r}r�r�rJrTrUr*r*r+r��s

zTarFile.addfile�.�
numeric_ownercCsUg}|dkr|}xb|D]Z}|j�rV|j|�tj|�}d|_|j||d|j�d|�qW|jddd��|j�x�|D]�}tjj	||j
�}y:|j||d|�|j||�|j
||�Wq�tk
rL}z.|jdkr&�n|jdd	|�WYdd}~Xq�Xq�WdS)
a�Extract 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_attrsr�rvcSs|jS)N)r~)rcr*r*r+�<lambda>�sz$TarFile.extractall.<locals>.<lambda>rztarfile: %s)r r�r�r\�extract�sort�reverservrr�r~�chown�utime�chmodrirlr�)r}rrmr�Zdirectoriesr��dirpathrqr*r*r+�
extractall�s,

	

zTarFile.extractallr�cCsd|jd�t|t�r.|j|�}n|}|j�r[tjj||j�|_	y2|j
|tjj||j�d|d|�Wn�tk
r}zc|j
dkr��nI|jdkr�|jdd|j�n |jdd|j|jf�WYdd}~XnNtk
r_}z.|j
dkr9�n|jdd|�WYdd}~XnXdS)	a�Extract 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.
        rsr�r�rNrztarfile: %sztarfile: %s %r)r�rrr�rSrvrr�r�r��_extract_memberr~r�rl�filenamer��strerrorri)r}�memberrr�r�r�rqr*r*r+r��s(	
!2zTarFile.extractcCs�|jd�t|t�r.|j|�}n|}|j�sO|jtkr_|j||�S|j�sw|j	�r�t|j
t�r�td��q�|j
|j|��SndSdS)z�Extract 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.
        rsz'cannot extract (sym)link as file objectN)r�rrr�r0r�r1�
fileobjectrSrQr�r�rl�extractfile�_find_link_target)r}r�r�r*r*r+r�s
zTarFile.extractfilecCs�|jd�}|jdtj�}tjj|�}|r\tjj|�r\tj|�|j�st|j	�r�|j
dd|j|jf�n|j
d|j�|j
�r�|j||�n�|j�r�|j||�n�|j�r|j||�n�|j�s|j�r2|j||�n]|j�sJ|j	�r]|j||�n2|jtkr|j||�n|j||�|r�|j|||�|j	�s�|j||�|j||�dS)z\Extract the TarInfo object tarinfo to a physical
           file called targetpath.
        r�rz%s -> %sN)r!r�rvr�r�dirnamerf�makedirsrSrQr�r~r�r0�makefiler �makedirrY�makefiforUrW�makedev�makelinkr�r1�makeunknownr�r�r�)r}r��
targetpathr�r�Z	upperdirsr*r*r+r�&s4
#zTarFile._extract_membercCs-ytj|d�Wntk
r(YnXdS)z,Make a directory called targetpath.
        i�N)rv�mkdir�FileExistsError)r}r�r�r*r*r+r�Ws
zTarFile.makedirc
Cs�|j}|j|j�t|d���}|jdk	r�x7|jD],\}}|j|�t|||t�qDW|j|j�|j�nt|||jt�WdQRXdS)z'Make a file called targetpath.
        rdN)	r�r�r�rgr�rNrjr�truncate)r}r�r��source�targetr�rr*r*r+r�as	

zTarFile.makefilecCs+|j||�|jdd|j�dS)zYMake a file from a TarInfo object with an unknown type
           at targetpath.
        rz9tarfile: Unknown file type %r, extracted as regular file.N)r�r�r�)r}r�r�r*r*r+r�pszTarFile.makeunknowncCs/ttd�rtj|�ntd��dS)z'Make a fifo called targetpath.
        �mkfifozfifo not supported by systemN)r{rvr�ri)r}r�r�r*r*r+r�xszTarFile.makefifocCs�ttd�s ttd�r,td��|j}|j�rQ|tjO}n
|tjO}tj||tj	|j
|j��dS)z<Make a character or block device called targetpath.
        �mknodr�z'special devices not supported by systemN)r{rvrir\rWrZ�S_IFBLK�S_IFCHRr�r�r�r�)r}r�r�r\r*r*r+r��s 	
zTarFile.makedevcCs�yj|j�r%tj|j|�nDtjj|j�rPtj|j|�n|j|j	|�|�WnPt
k
r�y|j|j	|�|�Wntk
r�td��YnXYnXdS)z�Make 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.
        z%unable to resolve link inside archiveN)
rQrv�symlinkr�rrfr��linkr�r��symlink_exceptionr�ri)r}r�r�r*r*r+r��s

zTarFile.makelinkc Cs2tr.ttd�r.tj�dkr.|rB|j}|j}npytj|j�d}Wnt	k
ry|j}YnXytj
|j�d}Wnt	k
r�|j}YnXyH|j�r�ttd�r�tj
|||�ntj|||�Wn1tk
r-}ztd��WYdd}~XnXdS)z�Set owner of targetpath according to tarinfo. If numeric_owner
           is True, use .gid/.uid instead of .gname/.uname.
        �geteuidrr�lchownzcould not change ownerN)r�r{rvr�rrr�Zgetgrnamr!r��getpwnamr rQr�r�r�ri)r}r�r�r��g�urqr*r*r+r��s$'	

z
TarFile.chowncCs^ttd�rZytj||j�Wn1tk
rY}ztd��WYdd}~XnXdS)zASet file permissions of targetpath according to tarinfo.
        r�zcould not change modeN)r{rvr�r\r�ri)r}r�r�rqr*r*r+r��s
z
TarFile.chmodcCskttd�sdSy tj||j|jf�Wn1tk
rf}ztd��WYdd}~XnXdS)zBSet modification time of targetpath according to tarinfo.
        r�Nz"could not change modification time)r{rvr�rr�ri)r}r�r�rqr*r*r+r��s z
TarFile.utimecCsV|jd�|jdk	r2|j}d|_|S|j|jj�kr|jj|jd�|jjd�std��d}x�y|jj	|�}Wn�t
k
r�}z=|jr�|jdd|j|f�|jt
7_w�WYdd}~Xn(tk
r}}za|jrJ|jdd|j|f�|jt
7_w�n!|jdkrktt|���WYdd}~Xn�tk
r�|jdkr�td��Yn|tk
r�}z&|jdkr�tt|���WYdd}~Xn7tk
r$}ztt|���WYdd}~XnXPq�W|dk	rI|jj|�n	d	|_|S)
z�Return the next member of the archive as a TarInfo object, when
           TarFile is opened for reading. Return None if there is no more
           available.
        ZraNrzunexpected end of datarz0x%X: %srz
empty fileT)r�rpr�r�r�r�rPrjr�r*rprjr�r�r=rrnrorqrmr�rn)r}�mr�rqr*r*r+r4�sL
				$
$%	zTarFile.nextcCs�|j�}|dk	r1|d|j|��}|rItjj|�}xKt|�D]=}|rztjj|j�}n	|j}||krV|SqVWdS)z}Find an archive member by name from bottom to top.
           If tarinfo is given, it is used as the starting point.
        N)r��indexrvr�normpath�reversedr~)r}r~r��	normalizermr�Zmember_namer*r*r+r�
	s	zTarFile._getmembercCs-x|j�}|dkrPqWd|_dS)zWRead through the entire archive file and look for readable
           members.
        NT)r4rn)r}r�r*r*r+r�!	s
z
TarFile._loadcCsQ|jrtd|jj��|dk	rM|j|krMtd|j��dS)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)r�r�r�rer\)r}r\r*r*r+r�+	s	zTarFile._checkcCs�|j�rEdjtdtjj|j�|jf��}d}n|j}|}|j|d|dd�}|dkr�t	d|��|S)zZFind the target member of a symlink or hardlink member in the
           archive.
        r�Nr�r�Tzlinkname %r not found)
rQr�r�rvrr�r~r�r�r�)r}r�r��limitr�r*r*r+r�4	s0		zTarFile._find_link_targetcCs$|jrt|j�St|�SdS)z$Provide an iterator object.
        N)rn�iterrm�TarIter)r}r*r*r+�__iter__G	s	
zTarFile.__iter__cCs&||jkr"t|dtj�dS)z.Write debugging output to sys.stderr.
        �fileN)rkrcra�stderr)r}�level�msgr*r*r+r�O	szTarFile._dbgcCs|j�|S)N)r�)r}r*r*r+�	__enter__U	s
zTarFile.__enter__cCs<|dkr|j�n|js/|jj�d|_dS)NT)r�r�r�r�)r}r�r�	tracebackr*r*r+�__exit__Y	s

	
zTarFile.__exit__)8rerfrgrhrkrirjrlr^rFr_r(r)rr�r�r�rr`r�rrsr�r�r�rtr�r�r�r�r�rDr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r4r�r�r�r�r�r�r�r�r*r*r*r+r_stkZ!
d =(&0
	6
	c@s:eZdZdZdd�Zdd�Zdd�ZdS)	r�zMIterator Class.

       for tarinfo in TarFile(...):
           suite...
    cCs||_d|_dS)z$Construct a TarIter object.
        rN)r�r�)r}r�r*r*r+rk	s	zTarIter.__init__cCs|S)z Return iterator object.
        r*)r}r*r*r+r�p	szTarIter.__iter__cCs�|jdkr3|jjdk	r3|jj�}nm|jt|jj�krd|jj|j}n<|jjs�|jj�}|s�d|j_t�nt�|jd7_|S)z�Return the next item using TarFile's next() method.
           When all members have been read, set TarFile as _loaded.
        rNTr)r�r�rpr4r$rmrn�
StopIteration)r}r�r*r*r+�__next__t	s!	zTarIter.__next__N)rerfrgrhrr�r�r*r*r*r+r�d	sr�cCs;yt|�}|j�dSWntk
r6dSYnXdS)zfReturn True if name points to a tar archive that we
       are able to handle, else return False.
    TFN)rr�r)r~r�r*r*r+r�	s

cCs�ddl}d}|jd|�}|jdddddd	d
d�|j�}|jdd
ddd
d�|jdddddd9d
d�|jdddddd:d
d�|jddddd
d�|j�}|jro|j}t|�rSt|d��*}|j�t	|j�d t
j�WdQRX|jrlt	d!j
|��q�|jd"d#j
|��n{|jr�|j}t|�r�tj|d$��}|jd%|j�WdQRXq�|jd"d#j
|��n|jr�t|j�d"kr|jd}tj}n=t|j�d&kr9|j\}}n|jd"|j��t|�r�tj|d$��}|jd'|�WdQRX|jr�|d(kr�d)j
|�}	nd*j
||�}	t	|	�q�|jd"d#j
|��n|jr�|jjd�}
tjj|
�\}}d+d,d-d,d.d/d0d/d1d2d3d2d4d2d5d2i}
||
krld6|
|nd7}|j}tj|
|��%}x|D]}|j|�q�WWdQRX|jr�t	d8j
|
��n|jd"|j��dS);Nrz3A simple command line interface for tarfile module.�descriptionz-vz	--verbose�action�
store_true�defaultF�helpzVerbose outputz-lz--list�metavar�	<tarfile>zShow listing of a tarfilez-ez	--extract�nargs�+�<output_dir>zExtract tarfile into target dirz-cz--create�<name>�<file>zCreate tarfile from sourcesz-tz--testzTest if a tarfile is validrsr�z{!r} is a tar archive.rz{!r} is not a tar archive.
zr:*r�rrr�z{!r} file is extracted.z+{!r} file is extracted into {!r} directory.z.gzr�z.tgzz.xzr�z.txzz.bz2r�z.tbzz.tbz2z.tb2zw:rtz{!r} file created.)r�r�)r�r�)�argparse�ArgumentParser�add_argumentZadd_mutually_exclusive_group�
parse_argsZtestrrr�rcrar�r�rF�exitrDrr�r$rv�curdirZformat_helpr�Zcreate�popr�splitextr�)r�r��parserr:�argsrRr�Ztfrr�Ztar_name�_ZextZcompressionsZtar_modeZ	tar_files�	file_namer*r*r+�main�	s�		
 				
	
	 	
	r
�__main__)brh�version�
__author__Z__date__Z	__cvsid__�__credits__�builtinsrrgrarvr�rMrZr�rIr�r8r�r�r�r~�NotImplementedErrorr�r��	NameError�__all__r%r�r�r�r�r�r�r
r�rrRrPrTrVr�rXZCONTTYPEr�r�rrrr-r�rBr�r^r1rNr"rKr?rr:rLr~r_�getfilesystemencodingr,r1r@rGrKrNr[rd�	Exceptionrrirjrkrlrmrnrorpr=rqrrr��objectr�r��BufferedReaderr�rrr�rr
rer*r*r*r+�<module>s�


						
�h�������	)	Z

Zerion Mini Shell 1.0