Your IP : 18.222.98.91


Current Path : /opt/alt/python34/lib64/python3.4/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/__pycache__/tarfile.cpython-34.pyo

�
e f�e�@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:ie;d,6e;d-6e;d'6e<d(6e<d)6e<d&6Z=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`�ndS)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/python34/lib64/python3.4/tarfile.py�stn�sr,cCs>|jd�}|dkr.|d|�}n|j||�S)z8Convert a null-terminated bytes object to a string.
    srN���)�find�decode)r&r(r)�pr*r*r+�nts�sr1cCs�|ddkr�d}x:tt|�d�D]"}|dK}|||d7}q-W|ddkr�dt|�d|}q�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||�\}}xQt|�D]C}|j|�}t|�|kr�|d��n|j|�qXW|dkr�|j|�}t|�|kr�|d��n|j|�ndS)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[cCsSttjdd�}|dk	r?|j|d�j|�}nt|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.
    cCsgitjd6tjtjBtjBd6|}ttd�rK|tjO}ntj||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|_|dkr0t||�}d|_n|dkrWt|�}|j�}n|p`d|_||_||_||_||_d|_	d|_
d|_y�|dkr2yddl}Wnt
k
r�td	��YnX||_|jd�|_|d
kr%|j�|j|_qR|j�n |dkr�yddl}Wnt
k
rntd��YnX|d
kr�d|_|j�|_t|_qR|j�|_n�|d
kr3yddl}Wnt
k
r�td��YnX|d
kr!d|_|j�|_|j|_qR|j�|_n|dkrRtd|��nWn-|jsr|jj�nd|_�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�ndS)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�|_n|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_gzcCsw|jdkr-|jj||j�|_n|jt|�7_|jdkrf|jj|�}n|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�|jdkrR|jdkrR|j|jj�7_n|jdkr�|jr�|jj|j�d|_|jdkr�|jjtj	d|j
d@��|jjtj	d|jd@��q�nWd|js�|jj
�nXdS)	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.closecCsg|jj|jj�|_d|_|jd�dkrItd��n|jd�dkrmtd��nt|jd��}|jd�|d	@r�t|jd��d
t|jd��}|j	|�n|d@rx-|jd�}|s|t
kr�Pq�q�Wn|d@rIx-|jd�}|s>|t
krPqqWn|d@rc|jd�nd
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.seekNcCs||dkrTg}x-|j|j�}|s1Pn|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�}|sVPny|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�}xN||kr_|jj|j�}|s=Pn|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__read2sz_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+rGs	z_StreamProxy.__init__cCs|jj|_|jS)N)r�rPrJ)r}rr*r*r+rPKsz_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�Os2z_StreamProxy.getcomptypecCs|jj�dS)N)r�r�)r}r*r*r+r�Ysz_StreamProxy.closeN)rerfrgrhrrPr�r�r*r*r*r+r�Bs

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}nd|_g|_d}|j}xs|D]k\}}||kr�|jj	d||df�n|jj	d||||f�||7}||}q�W||jkr(|jj	d||jdf�ndS)Nrr~FT)
r��offsetr�positionr`r~r��	map_index�mapr�)r}r�r�rZ	blockinfoZlastposZrealposr*r*r+rfs(								 
z_FileInFile.__init__cCsdS)Nr*)r}r*r*r+r�sz_FileInFile.flushcCsdS)NTr*)r}r*r*r+�readable�sz_FileInFile.readablecCsdS)NFr*)r}r*r*r+�writable�sz_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	Csl|dkr|j|j}nt||j|j�}d}x'|dkrgxv|j|j\}}}}||jko�|knr�PqP|jd7_|jt|j�krPd|_qPqPWt|||j�}|r=|jj||j|�|jj|�}t|�|kr0t	d��n||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�`s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+�_getlinkpathszTarInfo._getlinkpathcCs
||_dS)N)r�)r}r�r*r*r+�_setlinkpathszTarInfo._setlinkpathcCs d|jj|jt|�fS)Nz<%s %r at %#x>)r�rer~�id)r}r*r*r+�__repr__szTarInfo.__repr__cCs�i
|jd6|jd@d6|jd6|jd6|jd6|jd6|jd6|jd	6|jd
6|j	d6|j
d6|jd
6|jd6}|d	t
kr�|djd�r�|dd7<n|S)z9Return the TarInfo's attributes as a dictionary.
        r~i�r\rrrrr�r�r�r r!r�r��/)r~r\rrrrr�r�r�r r!r�r��DIRTYPEr�)r}�infor*r*r+�get_info
s"











$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+�tobuf"sz
TarInfo.tobufcCst|d<t|d�tkr/td��nt|d�tkri|j|d�\|d<|d<n|j|t||�S)z3Return the object as a ustar header block.
        �magicr�zlinkname is too longr~�prefix)�POSIX_MAGICr$�LENGTH_LINKr<�LENGTH_NAME�_posix_split_name�_create_headerr�)r}r�r(r)r*r*r+r�0s
$zTarInfo.create_ustar_headercCs�t|d<d}t|d�tkrI||j|dt||�7}nt|d�tkr�||j|dt||�7}n||j|t||�S)z:Return the object as a GNU header block sequence.
        r�r�r�r~)	�	GNU_MAGICr$r��_create_gnu_long_header�GNUTYPE_LONGLINKr��GNUTYPE_LONGNAMEr�rB)r}r�r(r)rJr*r*r+r�=s
##zTarInfo.create_gnu_headerc
Cs�t|d<|jj�}x�ddtfddtfddfD]�\}}}||kr_q>ny||jd	d
�Wn#tk
r�||||<w>YnXt||�|kr>||||<q>q>Wx�dD]�\}}||kr�d||<q�n||}d|ko d|dkns5t|t	�r�t
|�||<d||<q�q�W|rt|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�)�unamer�r�)�gnamer�r��zuidr4�zgidr4�zsizer��zmtimer�)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�Ks4



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_header|sz TarInfo.create_pax_global_headercCs�|dtd�}x*|r@|ddkr@|dd�}qW|t|�d�}|dd�}|s�t|�tkr�td��n||fS)zUSplit a name longer than 100 chars into a prefix
           and a name part.
        Nrr�zname is too longr-r-r-)�
LENGTH_PREFIXr$r�r<)r}r~r�r*r*r+r��szTarInfo._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�\}}|dkr<|t|t7}n|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)r	r~r�r(r)r�r*r*r+r��s


zTarInfo._create_gnu_long_headercCs�d}xJ|j�D]<\}}y|jdd�Wqtk
rNd}PYqXqWd}|rl|d7}nx�|j�D]�\}}|jd�}|r�|j|d�}n|jd�}t|�t|�d}d	}	}
x0|tt|
��}	|	|
krPn|	}
q�W|tt|
�d
�d|d|d
7}qyWi}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#rr$rrAr�r�r�r)r	r�r�r(Zbinary�keyword�valueZrecords�lr>r0r�r*r*r+r�s<




1


z"TarInfo._create_pax_generic_headercCsft|�dkr!td��nt|�tkrBtd��n|jt�tkrftd��nt|dd��}|t|�kr�t	d��n|�}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�||�}|jtkr6|jjd�r6t|_n|jtkrd}g}x�td�D]u}	y<t|||d��}
t||d|d��}Wntk
r�PYnX|j|
|f�|d7}q^Wt|d�}t|dd��}
|||
f|_ n|j!�r6|jj"d�|_n|rb|jt#krb|d|j|_n|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)r	rJr(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)r	r�rJr$r*r*r+�fromtarfile:szTarInfo.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*Os


zTarInfo._proc_membercCsx|jj�|_|j}|j�s6|jtkrO||j|j�7}n||_|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+r0\s		zTarInfo._proc_builtincCs�|jj|j|j��}y|j|�}Wntk
rQtd��YnX|j|_|jt	kr�t
||j|j�|_
n-|jtkr�t
||j|j�|_n|S)zSProcess the blocks that hold a GNU longname
           or longlink member.
        z missing or bad subsequent header)r�rPr3rr+rmrqr�r�r�r1r(r)r~r�r�)r}r�rJ�nextr*r*r+r,ms
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�n|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�r r�r�r�r3rr�)
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<n|jd�}|dkr�|j
}nd}tjd�}d}x�|j||�}|s�Pn|j�\}	}
t|	�}	||jd	�d|jd�|	d�}|j|
dd|j�}
|
tkrz|j|||j
|j�}n|j|dd|j�}|||
<||	7}q�Wy|j|�}Wntk
r�td
��YnXd|kr|j||�nbd|kr"|j|||�n@|jd
�dkrb|jd�dkrb|j|||�n|jttfkr�|j||j
|j�|j |_ d|kr�|j!}
|j"�s�|jt#kr�|
|j|j�7}
n|
|_ q�n|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�rPr3rr�rr�r�re�search�groupr/rr(�compile�match�groupsr:r^r��_decode_pax_fieldr)�PAX_NAME_FIELDSr+rmrq�_proc_gnusparse_01�_proc_gnusparse_00�_proc_gnusparse_10rr.r4r�r�r1r2)r}r�rJr�r=r7r(Zregexr�r'rrr5r�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)r9�finditerr�r:r;�list�zipr�)r}r5r�rJZoffsetsr=r&r*r*r+rBs  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)�splitrErFr�)r}r5r�r�r*r*r+rAs zTarInfo._proc_gnusparse_01cCs�d}g}|jjt�}|jdd�\}}t|�}xjt|�|dkr�d|kr�||jjt�7}n|jdd�\}}|jt|��qEW|jj�|_t	t
|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 1.0.
        Ns
rr)r�rPr�rKr:r$r�r�r�rErFr�)r}r5r�r�Zfieldsr�rJZnumberr*r*r+rCszTarInfo._proc_gnusparse_10c
Csx�|j�D]�\}}|dkr8t|d|�q
|dkr]t|dt|��q
|dkr�t|dt|��q
|tkr
|tkr�yt||�}Wq�tk
r�d}Yq�Xn|dkr�|jd�}nt|||�q
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"rr�)r}r�r(r)rrr*r*r+r4&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?=s
zTarInfo._decode_pax_fieldcCs0t|t�\}}|r(|d7}n|tS)z_Round up a byte count by BLOCKSIZE and return it,
           e.g. _block(834) => 1024.
        r)rOr�)r}rrTrUr*r*r+r3Es
zTarInfo._blockcCs
|jtkS)N)r��
REGULAR_TYPES)r}r*r*r+r1Nsz
TarInfo.isregcCs
|j�S)N)r1)r}r*r*r+�isfilePszTarInfo.isfilecCs
|jtkS)N)r�r�)r}r*r*r+r!Rsz
TarInfo.isdircCs
|jtkS)N)r��SYMTYPE)r}r*r*r+�issymTsz
TarInfo.issymcCs
|jtkS)N)r��LNKTYPE)r}r*r*r+�islnkVsz
TarInfo.islnkcCs
|jtkS)N)r��CHRTYPE)r}r*r*r+�ischrXsz
TarInfo.ischrcCs
|jtkS)N)r��BLKTYPE)r}r*r*r+�isblkZsz
TarInfo.isblkcCs
|jtkS)N)r��FIFOTYPE)r}r*r*r+�isfifo\szTarInfo.isfifocCs
|jdk	S)N)r�)r}r*r*r+�issparse^szTarInfo.issparsecCs|jtttfkS)N)r�rVrXrZ)r}r*r*r+�isdev`sz
TarInfo.isdevN)znamezmodezuidzgidzsizezmtimezchksumztypezlinknamezunamezgnamezdevmajorzdevminorzoffsetzoffset_datazpax_headerszsparseztarfilez_sparse_structsz_link_target)4rerfrgrh�	__slots__rr�r��propertyrr�r�rr�r��DEFAULT_FORMAT�ENCODINGr�r�r�r��classmethodr
r��staticmethodr�rr�rr)r+r*r0r,r-r/rBrArCr4r?r3r1rQr!rSrUrWrYr[r\r]r*r*r*r+r�s`
1
3?
f	c@s�eZdZdZdZdZdZdZeZ	e
ZdZe
ZeZdddddddddddddd	�Zeddded
d��Zedddd
��Zeddddd��Zeddddd��Zeddddd��Zidd6dd6dd6dd6Zdd�Zdd �Zd!d"�Zd#d$�Zdddd%d&�Zd'd(d)�Zdd'dd*dd+d,�Z dd-d.�Z!d/dd0d1�Z"d2d'd3d4�Z#d5d6�Z$d'd7d8�Z%d9d:�Z&d;d<�Z'd=d>�Z(d?d@�Z)dAdB�Z*dCdD�Z+dEdF�Z,dGdH�Z-dIdJ�Z.dKdL�Z/dddMdN�Z0dOdP�Z1ddQdR�Z2dSdT�Z3dUdV�Z4dWdX�Z5dYdZ�Z6d[d\�Z7dS)]rz=The TarFile Class provides an interface to tar archives.
    rFrNrsr�c
Cs�idd6dd6dd6}
||
kr6td��n||_|
||_|s�|jdkr�tjj|�r�d|_d|_nt||j�}d|_nf|d	kr�t|d
�r�t	|j
ttf�r�|j
}nt|d�r|j|_nd|_|r%tjj
|�nd	|_
||_|d	k	rO||_n|d	k	rg||_n|d	k	r||_n|d	k	r�||_n|d	k	r�||_n|	|_|
d	k	r�|jtkr�|
|_n	i|_|d	k	r||_n|d	k	r||_nd|_g|_d|_|jj�|_i|_yF|jdkr{d	|_|j �|_n|jdkr,x�|jj!|j�y&|jj"|�}|jj#|�Wq�t$k
r�|jj!|j�PYq�t%k
r$}zt&t|���WYd	d	}~Xq�Xq�Wn|jd
kr�d|_|jr�|jj'|jj(��}|jj)|�|jt*|�7_q�nWn-|js�|jj+�nd|_�YnXd	S)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.
        �rbrszr+b�a�wbrtzmode must be 'r', 'a' or 'w'FNr~r\T�aw),r<r\�_modervr�exists�	bltn_openr�r{rr~rrA�abspathr�rFr��dereference�ignore_zerosr(r)r�r��debug�
errorlevelr��members�_loadedr�r��inodes�firstmemberr5r�r+r�rprmrjr
rrQr$r�)r}r~r\r�rFr�rlrmr(r)r�rnroZmodes�erJr*r*r+r~s�	
"		!								
,				zTarFile.__init__c
KsG|r|rtd��n|dkr�x�|jD]�}t||j|�}|dk	rj|j�}ny||d||�SWq3ttfk
r�}	z$|dk	r�|j|�nw3WYdd}	~	Xq3Xq3Wtd��nVd|krh|jdd�\}
}|
pd}
|pd}||jkrEt||j|�}ntd	|��|||
||�Sd
|kr|jd
d�\}
}|
p�d}
|p�d}|
dkr�td��nt||
|||�}y|||
||�}Wn|j	��YnXd
|_
|S|dkr7|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

           '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:*Nz%file could not be opened successfully�:rr�zunknown compression type %r�|rtzmode must be 'r' or 'w'Frezundiscernible mode)rsru)rsrt)rert)r<�	OPEN_METHr`r�rjrkr�rKr�r�r��taropen)
r	r~r\r�r��kwargsr��funcZ	saved_posrtr[�streamr�r*r*r+r�sN
	zTarFile.opencKs.|dkrtd��n|||||�S)zCOpen uncompressed tar archive name for reading or writing.
        rsrertzmode must be 'r', 'a' or 'w')rsrert)r<)r	r~r\r�rzr*r*r+ry;szTarFile.taropenr�cKs2|d
krtd��nyddl}|jWn$ttfk
rXtd��YnXy |j||d||�}Wn<tk
r�|dk	r�|dkr�td��n�YnXy|j||||�}WnNtk
r|j	�|dkr	td��n�Yn|j	��YnXd	|_
|S)zkOpen gzip compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtzmode must be 'r' or 'w'rNzgzip module is not availablerVznot a gzip fileF)rsrt)r<�gzipZGzipFiler��AttributeErrorrkr�rjryr�r�)r	r~r\r��
compresslevelrzr}r�r*r*r+�gzopenCs2 



	zTarFile.gzopencKs�|d
krtd��nyddl}Wntk
rKtd��YnX|j|p[||d|�}y|j||||�}WnTttfk
r�|j�|dkr�t	d��n�Yn|j��YnXd	|_
|S)zlOpen bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtzmode must be 'r' or 'w'.rNzbz2 module is not availablerznot a bzip2 fileF)rsrt)r<r�r�rkZBZ2Fileryr��EOFErrorr�rjr�)r	r~r\r�rrzr�r�r*r*r+�bz2openes(
	

	zTarFile.bz2opencKs�|d
krtd��nyddl}Wntk
rKtd��YnX|j|p[||d|�}y|j||||�}WnW|jtfk
r�|j�|dkr�t	d��n�Yn|j��YnXd	|_
|S)zkOpen lzma compressed tar archive name for reading or writing.
           Appending is not allowed.
        rsrtzmode must be 'r' or 'w'rNzlzma module is not available�presetznot an lzma fileF)rsrt)r<r�r�rkZLZMAFileryr�r�r�rjr�)r	r~r\r�r�rzr�r�r*r*r+�xzopen�s&


	zTarFile.xzopenryr�r�r�r�r�r�r�c
Cs�|jr
dSd|_z�|jdkr�|jjttd�|jtd7_t|jt�\}}|dkr�|jjtt|�q�nWd|j	s�|jj
�nXdS)zlClose the TarFile. In write-mode, two finishing zero blocks are
           appended to the archive.
        NTrgrr)r�r\r�rQr%r�r�rO�
RECORDSIZEr�r�)r}rTrUr*r*r+r��s		"	z
TarFile.closecCs2|j|�}|dkr.td|��n|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�n|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.
        )�_checkrq�_loadrp)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~)rGr�r*r*r+rI�s	z$TarFile.getnames.<locals>.<listcomp>)r�)r}r*r*r+�getnames�szTarFile.getnamescCs^|jd�|dk	r%|j}n|dkr:|}ntjj|�\}}|jtjd�}|jd�}|j�}||_	|dkr�t
td�r�|jr�tj|�}q�tj
|�}ntj|j��}d}|j}t
j|�r�|j|jf}	|jrj|jdkrj|	|jkrj||j|	krjt}
|j|	}qt}
|	dr||j|	<qn�t
j|�r�t}
nst
j|�r�t}
n[t
j|�r�t}
tj|�}n4t
j|�r�t }
nt
j!|�rt"}
ndS||_||_#|j$|_%|j&|_'|
tkr]|j(|_)n	d|_)|j*|_+|
|_,||_-t.r�yt.j/|j%�d|_0Wq�t1k
r�Yq�Xnt2r�yt2j3|j'�d|_4Wq�t1k
r�Yq�Xn|
t t"fkrZt
td�rZt
td	�rZtj5|j6�|_7tj8|j6�|_9qZn|S)
aOCreate a TarInfo object for either the file `name' or the file
           object `fileobj' (using os.fstat on its file descriptor). You can
           modify some of the TarInfo's attributes before you add it using
           addfile(). If given, `arcname' specifies an alternative name for the
           file in the archive.
        rgNr��lstatr�rr�major�minor):r�r~rvr�
splitdriver��sep�lstripr�r�r{rlr�rZ�fstat�fileno�st_mode�S_ISREG�st_ino�st_dev�st_nlinkrrrTr��S_ISDIRr��S_ISFIFOrZ�S_ISLNKrR�readlink�S_ISCHRrV�S_ISBLKrXr\�st_uidr�st_gidr�st_sizer�st_mtimerr�r��pwd�getpwuidr r��grpZgetgrgidr!r��st_rdevr�r�r�)r}r~�arcnamer�Zdrvr�Zstatresr�ZstmdZinoder�r*r*r+�
gettarinfo�s~
			"
									

zTarFile.gettarinfoTcCsK|j�x:|D]2}|r�ttj|j��td|jpH|j|jpW|jf�|j	�sw|j
�r�tdd|j|jf�ntd|j
�tdtj|j�dd��nt|j|j�r�dnd	�|r<|j�rtd
|j�n|j�r<td|j�q<nt�qWdS)z�Print 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.
        z%s/%sz%10sz%d,%dz%10dz%d-%02d-%02d %02d:%02d:%02dNr�r�r�z-> zlink to )r�rdrZr[r\r rr!rrWrYr�r�rr�Z	localtimerr~r!rSr�rUrc)r}�verboser�r*r*r+rE;s&

!#zTarFile.list�filterc	
Cs�|jd�|dkr"|}n|dk	rtddl}|jdtd�||�rt|jdd|�dSn|jdk	r�tjj|�|jkr�|jdd|�dS|jd|�|j	||�}|dkr�|jdd	|�dS|dk	r;||�}|dkr;|jdd|�dSn|j
�rrt|d
��}|j||�WdQXn�|j
�r�|j|�|r�xTtj|�D]@}|jtjj||�tjj||�||d|�q�Wq�n
|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.
        rgNrzuse the filter argument insteadrztarfile: Excluded %rztarfile: Skipped %rrztarfile: Unsupported type %rrdr�)r�rWrXrY�_dbgr~rvrrkr�r1rj�addfiler!�listdir�addr�)	r}r~r�Z	recursiveZexcluder�rWr��fr*r*r+r�XsB
		
*
*zTarFile.addcCs�|jd�tj|�}|j|j|j|j�}|jj|�|jt	|�7_|dk	r�t
||j|j�t|jt
�\}}|dkr�|jjtt
|�|d7}n|j|t
7_n|jj|�dS)a]Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
           given, tarinfo.size bytes are read from it and added to the archive.
           You can create TarInfo objects using gettarinfo().
           On Windows platforms, `fileobj' should always be opened with mode
           'rb' to avoid irritation about the file size.
        rgNrr)r�rr�rFr(r)r�rQr�r$rNrrOr�r%rpr�)r}r�r�rJrTrUr*r*r+r��s

zTarFile.addfile�.cCsOg}|dkr|}nx_|D]W}|j�r\|j|�tj|�}d|_n|j||d|j��q"W|jddd��|j�x�|D]�}tjj	||j
�}y4|j||�|j||�|j
||�Wq�tk
rF}z.|jdkr �n|jdd|�WYdd}~Xq�Xq�WdS)	aMExtract 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().
        Ni��	set_attrs�keycSs|jS)N)r~)rer*r*r+�<lambda>�sz$TarFile.extractall.<locals>.<lambda>rztarfile: %s)r!r�rr\�extract�sort�reverservrr�r~�chown�utime�chmodriror�)r}rrpZdirectoriesr��dirpathrtr*r*r+�
extractall�s*	

!

zTarFile.extractallr�cCsa|jd�t|t�r.|j|�}n|}|j�r^tjj||j�|_	ny,|j
|tjj||j�d|�Wn�tk
r}zc|j
dkr��nI|jdkr�|jdd|j�n |jdd|j|jf�WYdd}~XnNtk
r\}z.|j
dkr6�n|jdd|�WYdd}~XnXdS)axExtract 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.
        rsr�rNrztarfile: %sztarfile: %s %r)r�rrr�rUrvrr�r�r��_extract_memberr~r�ro�filenamer��strerrorri)r}�memberrr�r�rtr*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�r1r�r2�
fileobjectrUrSr�r�rl�extractfile�_find_link_target)r}r�r�r*r*r+r��s
zTarFile.extractfilecCs�|jd�}|jdtj�}tjj|�}|r_tjj|�r_tj|�n|j�sw|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�r5|j||�n]|j�sM|j	�r`|j||�n2|jtkr�|j||�n|j||�|r�|j||�|j	�s�|j||�|j||�q�ndS)z\Extract the TarInfo object tarinfo to a physical
           file called targetpath.
        r�rz%s -> %sN)r"r�rvr�r�dirnameri�makedirsrUrSr�r~r�r1�makefiler!�makedirr[�makefiforWrY�makedev�makelinkr�r2�makeunknownr�r�r�)r}r��
targetpathr�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�Gs
zTarFile.makedirc
Cs�|j}|j|j�t|d���}|jdk	rwxP|jD],\}}|j|�t|||t�qDWnt|||jt�|j|j�|j�WdQXdS)z'Make a file called targetpath.
        rfN)	r�r�r�rjr�rNrjr�truncate)r}r�r��source�targetr�rr*r*r+r�Qs	
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�`s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�hszTarFile.makefifocCs�ttd�s ttd�r/td��n|j}|j�rT|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\rYrZ�S_IFBLK�S_IFCHRr�r�r�r�)r}r�r�r\r*r*r+r�ps 	
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)
rSrv�symlinkr�rrir��linkr�r��symlink_exceptionr�ri)r}r�r�r*r*r+r�s

zTarFile.makelinkc Cstrttd�rtj�dkrytj|j�d}Wntk
r^|j}YnXytj	|j
�d}Wntk
r�|j}YnXyH|j�r�ttd�r�tj
|||�ntj|||�Wqtk
r}ztd��WYdd}~XqXndS)z6Set owner of targetpath according to tarinfo.
        �geteuidrr�lchownzcould not change ownerN)r�r{rvr�r�Zgetgrnamr!r�r�getpwnamr rrSr�r�r�ri)r}r�r��g�urtr*r*r+r��s'

z
TarFile.chowncCsattd�r]ytj||j�Wq]tk
rY}ztd��WYdd}~Xq]XndS)zASet file permissions of targetpath according to tarinfo.
        r�zcould not change modeN)r{rvr�r\r�ri)r}r�r�rtr*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�rtr*r*r+r��s z
TarFile.utimecCsh|jd�|jdk	r2|j}d|_|S|j|jj�kr�|jj|jd�|jjd�s�td��q�nd}x�y|jj	|�}Wn�t
k
r}z@|jr�|jdd|j|f�|jt
7_w�nWYdd}~Xn1tk
r�}zd|jrS|jdd|j|f�|jt
7_w�n$|jdkrwtt|���nWYdd}~Xn�tk
r�|jdkr�td��nYntk
r}z)|jdkr�tt|���nWYdd}~Xn7tk
r6}ztt|���WYdd}~XnXPq�W|dk	r[|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�rsr�r�r�r�rPrjr�r+rprmr�r�r=rrnrorqrpr�rq)r}�mr�rtr*r*r+r5�sL
				'
'%	zTarFile.nextcCs�|j�}|dk	r4|d|j|��}n|rOtjj|�}nxKt|�D]=}|r�tjj|j�}n	|j}||kr\|Sq\WdS)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��	normalizerpr�Zmember_namer*r*r+r��s	zTarFile._getmembercCs0x |j�}|dkrPqqWd|_dS)zWRead through the entire archive file and look for readable
           members.
        NT)r5rq)r}r�r*r*r+r�	s
z
TarFile._loadcCsW|jr"td|jj��n|dk	rS|j|krStd|j��ndS)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|��n|S)zZFind the target member of a symlink or hardlink member in the
           archive.
        r�Nr�r�Tzlinkname %r not found)
rSr�r�rvrr�r~r�r�r�)r}r�r��limitr�r*r*r+r�	s0		zTarFile._find_link_targetcCs$|jrt|j�St|�SdS)z$Provide an iterator object.
        N)rq�iterrp�TarIter)r}r*r*r+�__iter__2	s	
zTarFile.__iter__cCs)||jkr%t|dtj�ndS)z.Write debugging output to sys.stderr.
        �fileN)rnrcra�stderr)r}�level�msgr*r*r+r�:	szTarFile._dbgcCs|j�|S)N)r�)r}r*r*r+�	__enter__@	s
zTarFile.__enter__cCs?|dkr|j�n"|js2|jj�nd|_dS)NT)r�r�r�r�)r}r�r�	tracebackr*r*r+�__exit__D	s

	zTarFile.__exit__)8rerfrgrhrnrlrmror`rFrar(r)rr�r�r�rrbr�rryr�r�r�rxr�r�r�r�r�rEr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r5r�r�r�r�r�r�r�r�r*r*r*r+rdstkO!

b=&#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+rV	s	zTarIter.__init__cCs|S)z Return iterator object.
        r*)r}r*r*r+r�[	szTarIter.__iter__cCs�|jdkr3|jjdk	r3|jj�}np|jt|jj�krd|jj|j}n?|jjs�|jj�}|s�d|j_t�q�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�rsr5r$rprq�
StopIteration)r}r�r*r*r+�__next___	s!zTarIter.__next__N)rerfrgrhrr�r�r*r*r*r+r�O	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+rx	s

cCs�ddl}d}|jd|�}|jdddddd	d
d�|j�}|jdd
ddd
d�|jdddddd9d
d�|jdddddd:d
d�|jddddd
d�|j�}|jrq|j}t|�rUt|d��*}|j�t	|j�d t
j�WdQX|jrnt	d!j
|��qnq�|jd"d#j
|��n�|jr�|j}t|�r�tj|d$��}|jd%|j�WdQXq�|jd"d#j
|��n|jr�t|j�d"kr|jd}tj}n=t|j�d&kr:|j\}}n|jd"|j��t|�r�tj|d$��}|jd'|�WdQX|jr�|d(kr�d)j
|�}	nd*j
||�}	t	|	�q�q�|jd"d#j
|��n|jr�|jjd�}
tjj|
�\}}id+d,6d+d-6d.d/6d.d06d1d26d1d36d1d46d1d56}
||
krwd6|
|nd7}|j}tj|
|��%}x|D]}|j|�q�WWdQX|jr�t	d8j
|
��q�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.r�z.gzz.tgzr�z.xzz.txzr�z.bz2z.tbzz.tbz2z.tb2zw:rtz{!r} file created.)r�r�)r�r�)�argparse�ArgumentParser�add_argumentZadd_mutually_exclusive_group�
parse_argsZtestrrr�rcrar�r�rF�exitrErr�r$rv�curdirZformat_helpr��create�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__�builtinsrrjrarvr�rMrZr�rIrr9r�r�r�r~�NotImplementedErrorr�r��	NameError�__all__r%r�r�r�r�r�r�rr�rrTrRrVrXr�rZZCONTTYPEr�r�rrrr.r�rBr�r`r2rPr#rMr@rr:rNr~ra�getfilesystemencodingr,r1r@rGrKrNr[rd�	Exceptionrrirjrkrlrmrnrorpr=rqrrr��objectr�r��BufferedReaderr�rrr�rr	rer*r*r*r+�<module>s�


					
	
�h�������)	Z

?>