Your IP : 3.129.26.151


Current Path : /lib64/python3.6/__pycache__/
Upload File :
Current File : //lib64/python3.6/__pycache__/tarfile.cpython-36.pyc

3

�mewl�'@szdZdZdZdZdZdZddlmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZyddlZWnek
r�dZYnXyddlZWnek
r�dZYnXeefZyeef7ZWnek
r�YnXd	d
ddd
ddddddddddgZdZdZedZdZdZdZ dZ!dZ"dZ#dZ$d Z%d!Z&d"Z'd#Z(d$Z)d%Z*d&Z+d'Z,d(Z-d)Z.d*Z/d+Z0d,Z1dZ2d-Z3d.Z4e3Z5e#e$e%e&e)e*e+e'e(e,e-e.fZ6e#e$e+e.fZ7e,e-e.fZ8dkZ9d/d0d5d6hZ:e;e;e;e<e<e<d7�Z=e
j>d8k�r�d9Z?ne	j@�Z?d:d;�ZAd<d=�ZBd>d?�ZCd@e5fdAdB�ZDdCdD�ZEdedfdEdF�ZFdGdH�ZGdIdJ�ZHGdKd�deI�ZJGdLd�deJ�ZKGdMd
�d
eJ�ZLGdNd�deJ�ZMGdOd�deJ�ZNGdPd�deJ�ZOGdQdR�dReO�ZPGdSdT�dTeO�ZQGdUdV�dVeO�ZRGdWdX�dXeO�ZSGdYdZ�dZeO�ZTGd[d\�d\�ZUGd]d^�d^�ZVGd_d`�d`eW�ZXGdadb�dbeW�ZYGdcdd�ddejZ�Z[Gded
�d
eW�Z\Gdfd	�d	eW�Z]dgd�Z^e]jZdhdi�Z_e`djk�rve_�dS)lz,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�TarError�	ReadError�CompressionError�StreamError�ExtractError�HeaderError�ENCODING�USTAR_FORMAT�
GNU_FORMAT�
PAX_FORMAT�DEFAULT_FORMATr�i�sustar  sustar00�d��0�1�2�3�4�5�6�7�L�K�S�x�g�X���path�linkpath�size�mtime�uid�gid�uname�gname)ZatimeZctimer(r)r*r'�ntzutf-8cCs(|j||�}|d|�|t|�tS)z8Convert a string to a null-terminated bytes object.
    N)�encode�len�NUL)�s�length�encoding�errors�r5�/usr/lib64/python3.6/tarfile.py�stn�sr7cCs*|jd�}|dkr|d|�}|j||�S)z8Convert a null-terminated bytes object to a string.
    rr#N���)�find�decode)r1r3r4�pr5r5r6�nts�s
r<cCs�|ddkrfd}x0tt|�d�D]}|dK}|||d7}q"W|ddkr�dt|�d|}n@y"t|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)r=r>)�ranger/r<�int�strip�
ValueError�InvalidHeaderError)r1�n�ir5r5r6�nti�srKr?cCs�t|�}d|ko"d|dknrDtd|d|fd�t}n�|tkr�d|d|kopd|dknr�|dkr�tdg�}ntdg�}d||}x6t|d�D]}|jd|d@�|dL}q�Wntd	��|S)
z/Convert a python number to a number field.
    rr?r#z%0*orAr@r=r>zoverflow in number field)rE�bytesr0r�	bytearrayrD�insertrG)rI�digits�formatr1rJr5r5r6�itn�s 2
rQcCs0dttjd|��}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.
    r@Z
148B8x356BZ
148b8x356b)�sum�struct�unpack_from)�bufZunsigned_chksumZ
signed_chksumr5r5r6�calc_chksums�s	rVc	Cs�|pd}|dkrdS|dkr.tj|||�dSt||�\}}x8t|�D],}|j|�}t|�|krh|d��|j|�qFW|dkr�|j|�}t|�|kr�|d��|j|�dS)zjCopy length bytes from fileobj src to fileobj dst.
       If length is None, copy the entire content.
    �irNzunexpected end of datai@)�shutil�copyfileobj�divmodrD�readr/�write)	�src�dstr2�	exception�bufsize�blocks�	remainder�brUr5r5r6rY�s$


rYcCs ddl}|jdtd�tj|�S)z/Deprecated in this location; use stat.filemode.rNz$deprecated in favor of stat.filemoder$)�warnings�warn�DeprecationWarning�stat�filemode)�moderdr5r5r6rhsrhcCs8ttjdd�}|dk	r(|j|d�j|�}t|dd�dS)Nr3�backslashreplace� )�end)�getattr�sys�stdoutr.r:�print)r1r3r5r5r6�_safe_printsrqc@seZdZdZdS)rzBase exception.N)�__name__�
__module__�__qualname__�__doc__r5r5r5r6rsc@seZdZdZdS)r
z%General exception for extract errors.N)rrrsrtrur5r5r5r6r
sc@seZdZdZdS)rz&Exception for unreadable tar archives.N)rrrsrtrur5r5r5r6rsc@seZdZdZdS)rz.Exception for unavailable compression methods.N)rrrsrtrur5r5r5r6rsc@seZdZdZdS)r	z=Exception for unsupported operations on stream-like TarFiles.N)rrrsrtrur5r5r5r6r	sc@seZdZdZdS)rz!Base exception for header errors.N)rrrsrtrur5r5r5r6r"sc@seZdZdZdS)�EmptyHeaderErrorzException for empty headers.N)rrrsrtrur5r5r5r6rv%srvc@seZdZdZdS)�TruncatedHeaderErrorz Exception for truncated headers.N)rrrsrtrur5r5r5r6rw(srwc@seZdZdZdS)�EOFHeaderErrorz"Exception for end of file headers.N)rrrsrtrur5r5r5r6rx+srxc@seZdZdZdS)rHzException for invalid headers.N)rrrsrtrur5r5r5r6rH.srHc@seZdZdZdS)�SubsequentHeaderErrorz3Exception for missing and invalid extended headers.N)rrrsrtrur5r5r5r6ry1sryc@s0eZdZdZdd�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.
    cCsFtjtjtjBtjBd�|}ttd�r2|tjO}tj||d�|_dS)N)�r�w�O_BINARYi�)	�os�O_RDONLY�O_WRONLY�O_CREAT�O_TRUNC�hasattrr}r�fd)�self�namerir5r5r6�__init__>s

z_LowLevelFile.__init__cCstj|j�dS)N)r~�closer�)r�r5r5r6r�Gsz_LowLevelFile.closecCstj|j|�S)N)r~r[r�)r�r'r5r5r6r[Jsz_LowLevelFile.readcCstj|j|�dS)N)r~r\r�)r�r1r5r5r6r\Msz_LowLevelFile.writeN)rrrsrtrur�r�r[r\r5r5r5r6rz8s
	rzc@steZdZdZdd�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|_|dkrt||�}d|_|dkr6t|�}|j�}|p<d|_||_||_||_||_d|_	d|_
d|_�yL|dkr�yddl}Wnt
k
r�td	��YnX||_|jd�|_|d
kr�|j�|j|_n|j�n�|dk�r:yddl}Wnt
k
�r
td��YnX|d
k�r.d|_|j�|_t|_n
|j�|_n||d
k�r�yddl}Wnt
k
�rntd��YnX|d
k�r�d|_|j�|_|j|_n
|j�|_n|dk�r�td|��Wn&|j�s�|jj�d|_�YnXdS)z$Construct a _Stream object.
        TNF�*��r�gzzzlib module is not availabler{�bz2zbz2 module is not available�xzzlzma module is not available�tarzunknown compression type %r) �_extfileobjrz�_StreamProxy�getcomptyper�ri�comptype�fileobjr`rU�pos�closed�zlib�ImportErrorr�crc32�crc�
_init_read_gz�errorr_�_init_write_gzr��dbufZBZ2Decompressor�cmp�OSErrorZ
BZ2Compressor�lzmaZLZMADecompressor�	LZMAErrorZLZMACompressorr�)	r�r�rir�r�r`r�r�r�r5r5r6r�[sl












z_Stream.__init__cCst|d�r|jr|j�dS)Nr�)r�r�r�)r�r5r5r6�__del__�sz_Stream.__del__cCs�|jjd|jj|jj|jjd�|_tjdtt	j	���}|j
d|d�|jjd�rf|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�rS�packrE�time�_Stream__writer��endswithr.r0)r�Z	timestampr5r5r6r��sz_Stream._init_write_gzcCsR|jdkr|jj||j�|_|jt|�7_|jdkrD|jj|�}|j|�dS)z&Write string s to the stream.
        r�r�N)	r�r�r�r�r�r/r��compressr�)r�r1r5r5r6r\�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)rUr/r`r�r\)r�r1r5r5r6Z__write�sz_Stream.__writecCs�|jr
dSd|_z�|jdkr:|jdkr:|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.
        NTr|r�r�r�z<Ll��)r�rir�rUr��flushr�r\rSr�r�r�r�r�)r�r5r5r6r��s
z
_Stream.closecCs�|jj|jj�|_d|_|jd�dkr0td��|jd�dkrFtd��t|jd��}|jd�|d	@r�t|jd��d
t|jd��}|j	|�|d@r�x|jd�}|s�|t
kr�Pq�W|d@r�x|jd�}|s�|t
kr�Pq�W|d@r�|jd�d
S)z:Initialize for reading a gzip compressed fileobj.
        r�r$s�znot a gzip filer#�zunsupported compression method��r@r?rWN)r�Z
decompressobjr�r�r��
_Stream__readrr�ordr[r0)r��flagZxlenr1r5r5r6r��s.
 


z_Stream._init_read_gzcCs|jS)z3Return the stream's file pointer position.
        )r�)r�r5r5r6�tell�sz_Stream.tellrcCs\||jdkrNt||j|j�\}}xt|�D]}|j|j�q.W|j|�ntd��|jS)zXSet the stream's file pointer to pos. Negative seeking
           is forbidden.
        rz seeking backwards is not allowed)r�rZr`rDr[r	)r�r�rarbrJr5r5r6�seeksz_Stream.seekNcCsZ|dkr:g}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'�trUr5r5r6r[
s
z_Stream.readcCs�|jdkr|j|�St|j�}|jg}xd||kr�|j|j�}|sBPy|jj|�}Wn|jk
rptd��YnX|j	|�|t|�7}q(Wdj
|�}||d�|_|d|�S)z+Return size bytes from the stream.
        r�zinvalid compressed datar�N)r�r�r/r�r`r�Z
decompressr_rr�r�)r�r'�cr�rUr5r5r6r�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.
        r�N)r/rUr�r[r`r�r�)r�r'r�r�rUr5r5r6Z__read5s



z_Stream.__read)r)N)rrrsrtrur�r�r�r\r�r�r�r�r�r[r�r�r5r5r5r6r�Ps	F
	

r�c@s0eZdZdZdd�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�r[�	BLOCKSIZErU)r�r�r5r5r6r�Ksz_StreamProxy.__init__cCs|jj|_|jS)N)r�r[rU)r�r'r5r5r6r[Os
z_StreamProxy.readcCsP|jjd�rdS|jdd�dkr8|jdd�dkr8d	S|jjd�rHdSd
SdS)Ns�r�rr�sBZhr��
s1AY&SYr��]���7zXZr�r�)r�r�)rU�
startswith)r�r5r5r6r�Ss$z_StreamProxy.getcomptypecCs|jj�dS)N)r�r�)r�r5r5r6r�]sz_StreamProxy.closeN)rrrsrtrur�r[r�r�r5r5r5r6r�Fs

r�c@sjeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
jfdd�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}xT|D]L\}}||kr~|jj	d||df�|jj	d||||f�||7}||}qZW||jkr�|jj	d||jdf�dS)Nrr�FT)
r��offsetr'�positionrmr�r��	map_index�mapr�)r�r�r�r'Z	blockinfoZlastposZrealposr5r5r6r�js(

z_FileInFile.__init__cCsdS)Nr5)r�r5r5r6r��sz_FileInFile.flushcCsdS)NTr5)r�r5r5r6�readable�sz_FileInFile.readablecCsdS)NFr5)r�r5r5r6�writable�sz_FileInFile.writablecCs
|jj�S)N)r��seekable)r�r5r5r6r��sz_FileInFile.seekablecCs|jS)z*Return the current file position.
        )r�)r�r5r5r6r��sz_FileInFile.tellcCs�|tjkr tt|d�|j�|_nj|tjkr\|dkrFt|j|d�|_q�t|j||j�|_n.|tjkr�tt|j||j�d�|_ntd��|jS)z(Seek to a position in the file.
        rzInvalid argument)	�io�SEEK_SET�min�maxr'r��SEEK_CUR�SEEK_ENDrG)r�r��whencer5r5r6r��s


z_FileInFile.seekc	Cs|dkr|j|j}nt||j|j�}d}x�|dk�rxZ|j|j\}}}}||jkob|knrlPq:|jd7_|jt|j�kr:d|_q:Wt|||j�}|r�|jj||j|�|jj|�}t|�|kr�t	d��||7}n|t
|7}||8}|j|7_q.W|S)z!Read data from the file.
        Nr�rr#zunexpected end of data)r'r�r�r�r�r/r�r�r[rr0)	r�r'rU�data�start�stopr�r2rcr5r5r6r[�s.

z_FileInFile.readcCs&|jt|��}||dt|��<t|�S)N)r[r/)r�rcrUr5r5r6�readinto�sz_FileInFile.readintocCs
d|_dS)NT)r�)r�r5r5r6r��sz_FileInFile.close)N)N)rrrsrtrur�r�r�r�r�r�r�r�r�r[r�r�r5r5r5r6r�ds

r�cseZdZ�fdd�Z�ZS)�ExFileObjectcs&t|j|j|j|j�}t�j|�dS)N)r�r��offset_datar'�sparse�superr�)r��tarfile�tarinfor�)�	__class__r5r6r��s
zExFileObject.__init__)rrrsrtr��
__classcell__r5r5)r�r6r��sr�c@s�eZdZdZdiZdjdd�Zdd�Zdd�Zeee�Z	dd�Z
dd �Zee
e�Zd!d"�Z
d#d$�Zeed%fd&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)kraInformational class which holds the details about an
       archive member given by a tar header block.
       TarInfo objects are returned by TarFile.getmember(),
       TarFile.getmembers() and TarFile.gettarinfo() and are
       usually created internally.
    r�rir)r*r'r(�chksum�type�linknamer+r,�devmajor�devminorr�r��pax_headersr�r��_sparse_structs�_link_targetr�cCsj||_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�rir)r*r'r(r��REGTYPEr�r�r+r,r�r�r�r�r�r�)r�r�r5r5r6r��s"zTarInfo.__init__cCs|jS)N)r�)r�r5r5r6�_getpath�szTarInfo._getpathcCs
||_dS)N)r�)r�r�r5r5r6�_setpathszTarInfo._setpathcCs|jS)N)r�)r�r5r5r6�_getlinkpathszTarInfo._getlinkpathcCs
||_dS)N)r�)r�r�r5r5r6�_setlinkpathszTarInfo._setlinkpathcCsd|jj|jt|�fS)Nz<%s %r at %#x>)r�rrr��id)r�r5r5r6�__repr__szTarInfo.__repr__cCsn|j|jd@|j|j|j|j|j|j|j|j	|j
|j|jd�
}|dt
krj|djd�rj|dd7<|S)z9Return the TarInfo's attributes as a dictionary.
        i�)
r�rir)r*r'r(r�r�r�r+r,r�r�r�r��/)r�rir)r*r'r(r�r�r�r+r,r�r��DIRTYPEr�)r��infor5r5r6�get_infos 
zTarInfo.get_info�surrogateescapecCsT|j�}|tkr|j|||�S|tkr4|j|||�S|tkrH|j||�Std��dS)z<Return a tar header as a string of 512 byte blocks.
        zinvalid formatN)r�r
�create_ustar_headerr�create_gnu_headerr�create_pax_headerrG)r�rPr3r4r�r5r5r6�tobuf&sz
TarInfo.tobufcCsnt|d<t|dj||��tkr(td��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_LINKrG�LENGTH_NAME�_posix_split_name�_create_headerr
)r�r�r3r4r5r5r6r�4szTarInfo.create_ustar_headercCs�t|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.
        r�r�r�r�)
�	GNU_MAGICr/r.r��_create_gnu_long_header�GNUTYPE_LONGLINKr��GNUTYPE_LONGNAMEr�r)r�r�r3r4rUr5r5r6r�AszTarInfo.create_gnu_headerc
Cs4t|d<|jj�}x�ddtfddtfddfD]h\}}}||kr@q,y||jd	d
�Wn"tk
rv||||<w,YnXt||�|kr,||||<q,WxldD]d\}}||kr�d||<q�||}d|ko�d|dkn�s�t|t	�r�t
|�||<d||<q�W|�r|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�r%r�r&r+� r,rArBr)r?r*r'�r(rr#r�r�)r+r+r)r,r,r�r)r?�r*r?�r'r�r(r)rrrr)r�r��copyr�r�r.�UnicodeEncodeErrorr/�
isinstance�float�str�_create_pax_generic_header�XHDTYPEr�r
)
r�r�r3r�r�Zhnamer2rO�valrUr5r5r6r�Os4
.zTarInfo.create_pax_headercCs|j|td�S)zAReturn the object as a pax global header block sequence.
        zutf-8)r�XGLTYPE)�clsr�r5r5r6�create_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.
        r�r#Nzname is too long)�splitrDr/r�r.�
LENGTH_PREFIXr�rG)r�r�r3r4Z
componentsrJr�r5r5r6r��s
zTarInfo._posix_split_namecCsVt|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�rriri�r?r)r*r'rr(s        r�r�r�r+rr,r�r�r�rz%dsr�Nilz%06orAiei����i����)r7�getrQr�r�rSr�r�r�rVrL)r�rPr3r4�partsrUr�r5r5r6r��s&

&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)rZr/r�r0)Zpayloadrarbr5r5r6�_create_payload�szTarInfo._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
././@LongLinkr�r�r'r�)r.r0r/r�r�r
r)rr�r�r3r4r�r5r5r6r��szTarInfo._create_gnu_long_headercCs:d}x@|j�D]4\}}y|jdd�Wqtk
r@d}PYqXqWd}|rV|d7}x�|j�D]�\}}|jd�}|r�|j|d�}n
|jd�}t|�t|�d}d	}	}
x"|tt|
��}	|	|
kr�P|	}
q�W|tt|
�d
�d|d|d
7}q`Wi}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-8rBTr�s21 hdrcharset=BINARY
r�r�rrA� �=�
z././@PaxHeaderr�r�r'r�r�)
�itemsr.rr/r
rLr�r�r
r)rr�r�r3Zbinary�keyword�valueZrecords�lrIr;r�r5r5r6r�s<

*z"TarInfo._create_pax_generic_headercCstt|�dkrtd��t|�tkr(td��|jt�tkr>td��t|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�||�}|jtk�r�|jjd��r�t|_|jtk�r6d}g}xrtd�D]f}	y0t|||d��}
t||d|d��}Wntk
�r�PYnX|j|
|f�|d7}�q�Wt|d�}t|dd��}
|||
f|_ |j!��rN|jj"d�|_|�rp|jt#k�rp|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/rvr�rw�countr0rxrKrVrHr<r�rir)r*r'r(r�r�r�r+r,r�r��AREGTYPEr�r��GNUTYPE_SPARSErDrGr��boolr��isdir�rstrip�	GNU_TYPES)rrUr3r4r��objr�r��structsrJr��numbytes�
isextended�origsizer5r5r6�frombufsZ
zTarInfo.frombufcCs8|jjt�}|j||j|j�}|jj�t|_|j|�S)zOReturn the next TarInfo object from TarFile object
           tarfile.
        )	r�r[r�r1r3r4r�r��_proc_member)rr�rUr,r5r5r6�fromtarfile?s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)r�r�r��
_proc_gnulongr'�_proc_sparserr�SOLARIS_XHDTYPE�	_proc_pax�
_proc_builtin)r�r�r5r5r6r2Ts



zTarInfo._proc_membercCsR|jj�|_|j}|j�s$|jtkr4||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�_blockr'r��_apply_pax_infor�r3r4)r�r�r�r5r5r6r8aszTarInfo._proc_builtincCs�|jj|j|j��}y|j|�}Wntk
r>td��YnX|j|_|jt	krft
||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�r[r;r'r3rryr�r�r�r<r3r4r�r�r�)r�r�rU�nextr5r5r6r4rs

zTarInfo._proc_gnulongc
Cs�|j\}}}|`x�|r�|jjt�}d}xvtd�D]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.
        r�rr$i�)r�r�r[r�rDrKrGr�r(r�r�r�r;r'r�)
r�r�r-r/r0rUr�rJr�r.r5r5r6r5�s(zTarInfo._proc_sparsecCs0|jj|j|j��}|jtkr&|j}n
|jj�}tj	d|�}|dk	rX|j
d�jd�|d<|jd�}|dkrr|j
}nd}tjd�}d}x�|j||�}|s�P|j�\}	}
t|	�}	|	dkr�td	��||jd
�d|jd�|	d�}|j|
dd|j�}
|
tk�r|j|||j
|j�}n|j|dd|j�}|||
<||	7}q�Wy|j|�}Wntk
�rdtd��YnXd|k�r~|j||�nHd
|k�r�|j|||�n.|jd�dk�r�|jd�dk�r�|j|||�|jttfk�r,|j ||j
|j�|j!|_!d|k�r,|j"}
|j#��s|jt$k�r&|
|j|j�7}
|
|_!|S)zVProcess an extended or global header as described in
           POSIX.1-2008.
        s\d+ hdrcharset=([^\n]+)\nNr#zutf-8�
hdrcharsetZBINARYs(\d+) ([^=]+)=rzinvalid headerr$z missing or bad subsequent headerzGNU.sparse.mapzGNU.sparse.sizezGNU.sparse.major�1zGNU.sparse.minorrCr')%r�r[r;r'r�rr�r�re�search�groupr:rr3�compile�match�groupsrErHrlr��_decode_pax_fieldr4�PAX_NAME_FIELDSr3rry�_proc_gnusparse_01�_proc_gnusparse_00�_proc_gnusparse_10rr6r<r�r�r9r:)r�r�rUr�rEr?r3Zregexr�r2rrr=r�r5r5r6r7�sd



$	





 
zTarInfo._proc_paxcCspg}x(tjd|�D]}|jt|jd���qWg}x(tjd|�D]}|jt|jd���q@Wtt||��|_dS)z?Process a GNU tar extended sparse header, version 0.0.
        s\d+ GNU.sparse.offset=(\d+)\nr#s\d+ GNU.sparse.numbytes=(\d+)\nN)rA�finditerr�rErC�list�zipr�)r�r=r�rUZoffsetsrEr.r5r5r6rJszTarInfo._proc_gnusparse_00cCs@dd�|djd�D�}tt|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 0.1.
        cSsg|]}t|��qSr5)rE)�.0�xr5r5r6�
<listcomp>sz.TarInfo._proc_gnusparse_01.<locals>.<listcomp>zGNU.sparse.map�,Nr$r#)rrMrNr�)r�r=r�r�r5r5r6rIszTarInfo._proc_gnusparse_01cCs�d}g}|jjt�}|jdd�\}}t|�}xJt|�|dkrvd|krV||jjt�7}|jdd�\}}|jt|��q.W|jj�|_t	t
|ddd�|ddd���|_dS)z?Process a GNU tar extended sparse header, version 1.0.
        Nrr#r$)r�r[r�rrEr/r�r�r�rMrNr�)r�r=r�r�Zfieldsr�rUZnumberr5r5r6rKszTarInfo._proc_gnusparse_10c
Cs�x�|j�D]�\}}|dkr(t|d|�q
|dkrBt|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.namer%zGNU.sparse.sizer'zGNU.sparse.realsizerr�N)	r�setattrrE�
PAX_FIELDS�PAX_NUMBER_FIELDSrGr*rr�)r�r�r3r4rrr5r5r6r<-s"

zTarInfo._apply_pax_infocCs.y|j|d�Stk
r(|j||�SXdS)z1Decode a single field from a pax record.
        rBN)r:�UnicodeDecodeError)r�rr3Zfallback_encodingZfallback_errorsr5r5r6rGDszTarInfo._decode_pax_fieldcCs"t|t�\}}|r|d7}|tS)z_Round up a byte count by BLOCKSIZE and return it,
           e.g. _block(834) => 1024.
        r#)rZr�)r�r%rarbr5r5r6r;LszTarInfo._blockcCs
|jtkS)N)r��
REGULAR_TYPES)r�r5r5r6r9Usz
TarInfo.isregcCs|j�S)N)r9)r�r5r5r6�isfileWszTarInfo.isfilecCs
|jtkS)N)r�r�)r�r5r5r6r)Ysz
TarInfo.isdircCs
|jtkS)N)r��SYMTYPE)r�r5r5r6�issym[sz
TarInfo.issymcCs
|jtkS)N)r��LNKTYPE)r�r5r5r6�islnk]sz
TarInfo.islnkcCs
|jtkS)N)r��CHRTYPE)r�r5r5r6�ischr_sz
TarInfo.ischrcCs
|jtkS)N)r��BLKTYPE)r�r5r5r6�isblkasz
TarInfo.isblkcCs
|jtkS)N)r��FIFOTYPE)r�r5r5r6�isfifocszTarInfo.isfifocCs
|jdk	S)N)r�)r�r5r5r6�issparseeszTarInfo.issparsecCs|jtttfkS)N)r�r]r_ra)r�r5r5r6�isdevgsz
TarInfo.isdevN)r�rir)r*r'r(r�r�r�r+r,r�r�r�r�r�r�r�r�r�)r�)4rrrsrtru�	__slots__r�r�r��propertyr%r�r�r&r�r�rrr�r�r�r��classmethodrr��staticmethodr�rr�rr1r3r2r8r4r5r7rJrIrKr<rGr;r9rXr)rZr\r^r`rbrcrdr5r5r5r6r�s`



1
3?
h	c
@s�eZdZdZdZdZdZdZeZ	e
ZdZe
ZeZd\dd	�Zedddefd
d��Zed]dd
��Zed^dd��Zed_dd��Zed`dd��Zddddd�Zdd�Zdd�Zdd�Zd d!�Zdad"d#�Zdbdd%�d&d'�Zdcdd(�d)d*�Z ddd+d,�Z!dedd.�d/d0�Z"dfdd.�d2d3�Z#d4d5�Z$dgd6d7�Z%d8d9�Z&d:d;�Z'd<d=�Z(d>d?�Z)d@dA�Z*dBdC�Z+dDdE�Z,dFdG�Z-dHdI�Z.dJdK�Z/dhdLdM�Z0dNdO�Z1didPdQ�Z2dRdS�Z3dTdU�Z4dVdW�Z5dXdY�Z6dZd[�Z7dS)jrz=The TarFile Class provides an interface to tar archives.
    rFr#Nr{r�cCs�ddddd�}||krtd��||_|||_|sj|jdkrVtjj|�rVd|_d|_t||j�}d	|_n@|d
kr�t|d�r�t	|j
ttf�r�|j
}t|d�r�|j|_d
|_|r�tjj
|�nd
|_
||_|d
k	r�||_|d
k	r�||_|d
k	r�||_|d
k	�r||_|d
k	�r||_|	|_|
d
k	�r4|jtk�r4|
|_ni|_|d
k	�rJ||_|d
k	�rZ||_|
|_d	|_g|_d	|_|jj�|_i|_�y�|jdk�r�d
|_ |j!�|_ |jdk�r8x�|jj"|j�y|jj#|�}|jj$|�WnTt%k
�r|jj"|j�PYn0t&k
�r0}zt't|���WYd
d
}~XnX�q�W|jdk�r�d
|_|j�r�|jj(|jj)��}|jj*|�|jt+|�7_Wn&|j�s�|jj,�d
|_�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.
        �rbzr+b�wbZxb)r{�ar|rPz!mode must be 'r', 'a', 'w' or 'x'rkr|FNr�riTr{rP)rkr|rP)-rGri�_moder~r%�exists�	bltn_openr�r�rr�r
rL�abspathr�rPr��dereference�ignore_zerosr3r4rr��debug�
errorlevel�copybufsizer��members�_loadedr�r��inodes�firstmemberr=r�r3r�rxrrrrr\r/r�)r�r�rir�rPr�rprqr3r4r�rrrsrtZmodes�erUr5r5r6r��s�






$
zTarFile.__init__c

s�|r|rtd��|dkr��fdd�}xtt�j|d�D]b}t��j|�}|dk	r\|j�}	y||d|f|�Sttfk
r�|dk	r�|j|	�w8Yq8Xq8Wtd���nd	|k�r|jd	d
�\}
}|
p�d}
|p�d}|�jkr�t��j|�}ntd|��|||
|f|�Sd
|k�r�|jd
d
�\}
}|
�p.d}
|�p8d}|
dk�rLtd��t	||
|||�}y�||
|f|�}Wn|j
��YnXd|_|S|dk�r��j|||f|�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 openr{�r:*cs�j|dkS)N�taropen)�	OPEN_METH)r�)rr5r6�not_compressedsz$TarFile.open.<locals>.not_compressed)�keyNz%file could not be opened successfully�:r#r�zunknown compression type %r�|r|zmode must be 'r' or 'w'FrkrPzundiscernible mode)r{rz)r{r|)rkr|rP)
rG�sortedr|rmr�rrr�rr�r�r�r{)
rr�rir�r`�kwargsr}r��funcZ	saved_posrh�streamr�r5)rr6r�sP%







zTarFile.opencKs |dkrtd��||||f|�S)zCOpen uncompressed tar archive name for reading or writing.
        r{rkr|rPz!mode must be 'r', 'a', 'w' or 'x')r{rkr|rP)rG)rr�rir�r�r5r5r6r{OszTarFile.taropenr�cKs�|dkrtd��yddl}|jWn ttfk
rBtd��YnXy|j||d||�}Wn.tk
r�|dk	r�|dkr�td	���YnXy|j|||f|�}WnBtk
r�|j	�|dkr�td	���Yn|j	��YnXd
|_
|S)zkOpen gzip compressed tar archive name for reading or writing.
           Appending is not allowed.
        r{r|rPzmode must be 'r', 'w' or 'x'rNzgzip module is not availablercznot a gzip fileF)r{r|rP)rG�gzipZGzipFiler��AttributeErrorrr�rr{r�r�)rr�rir��
compresslevelr�r�r�r5r5r6�gzopenWs2
zTarFile.gzopencKs�|dkrtd��yddl}Wntk
r8td��YnX|j|pD|||d�}y|j|||f|�}WnFttfk
r�|j�|dkr�t	d	���Yn|j��YnXd
|_
|S)zlOpen bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        r{r|rPzmode must be 'r', 'w' or 'x'rNzbz2 module is not available)r�znot a bzip2 fileF)r{r|rP)rGr�r�rZBZ2Filer{r��EOFErrorr�rr�)rr�rir�r�r�r�r�r5r5r6�bz2openys(zTarFile.bz2opencKs�|dkrtd��yddl}Wntk
r8td��YnX|j|pD|||d�}y|j|||f|�}WnH|jtfk
r�|j�|dkr�t	d	���Yn|j��YnXd
|_
|S)zkOpen lzma compressed tar archive name for reading or writing.
           Appending is not allowed.
        r{r|rPzmode must be 'r', 'w' or 'x'rNzlzma module is not available)�presetznot an lzma fileF)r{r|rP)rGr�r�rZLZMAFiler{r�r�r�rr�)rr�rir�r�r�r�r�r5r5r6�xzopen�s&zTarFile.xzopenr{r�r�r�)r�r�r�r�c
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.
        NTrkr|rPr$r)rkr|rP)r�rir�r\r0r�r�rZ�
RECORDSIZEr�r�)r�rarbr5r5r6r��s
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)�
_getmember�KeyError)r�r�r�r5r5r6�	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.
        )�_checkrv�_loadru)r�r5r5r6�
getmembers�szTarFile.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�qSr5)r�)rOr�r5r5r6rQ�sz$TarFile.getnames.<locals>.<listcomp>)r�)r�r5r5r6�getnames�szTarFile.getnamescCsp|jd�|dk	r|j}|dkr$|}tjj|�\}}|jtjd�}|jd�}|j�}||_	|dkr�t
td�r�|jr�tj|�}q�tj
|�}ntj|j��}d}|j}t
j|��r|j|jf}	|jr�|jdkr�|	|jkr�||j|	kr�t}
|j|	}nt}
|	d�rx||j|	<nht
j|��r"t}
nVt
j|��r4t}
nDt
j|��rPt}
tj|�}n(t
j|��rbt }
nt
j!|��rtt"}
ndS||_||_#|j$|_%|j&|_'|
tk�r�|j(|_)nd|_)|j*|_+|
|_,||_-t.�r�yt.j/|j%�d|_0Wnt1k
�r�YnXt2�r*yt2j3|j'�d|_4Wnt1k
�r(YnX|
t t"fk�rlt
td��rlt
td	��rltj5|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�r#r�major�minor):r�r�r~r%�
splitdriver��sep�lstripr�r�r�rpr�rg�fstat�fileno�st_mode�S_ISREG�st_ino�st_dev�st_nlinkrwr[r��S_ISDIRr��S_ISFIFOra�S_ISLNKrY�readlink�S_ISCHRr]�S_ISBLKr_ri�st_uidr)�st_gidr*�st_sizer'�st_mtimer(r�r��pwd�getpwuidr+r��grpZgetgrgidr,r��st_rdevr�r�r�)r�r��arcnamer�Zdrvr�Zstatresr�Zstmd�inoder�r5r5r6�
gettarinfo�s~	




zTarFile.gettarinfoT)rucCs�|j�|dkr|}x�|D]�}|r�ttj|j��td|jp@|j|jpJ|jf�|j	�sb|j
�r|tdd|j|jf�ntd|j
�tdtj|j�dd��t|j|j�r�dnd	�|r�|j�r�td
|j�|j�r�td|j�t�qWdS)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�rqrgrhrir+r)r,r*r^r`r�r�r'r�Z	localtimer(r�r)rZr�r\rp)r��verboserur�r5r5r6rMQs*
zTarFile.list)�filterc	
Csv|jd�|dkr|}|dk	rPddl}|jdtd�||�rP|jdd|�dS|jdk	r�tjj|�|jkr�|jdd|�dS|jd|�|j	||�}|dkr�|jdd	|�dS|dk	r�||�}|dkr�|jdd|�dS|j
��rt|d
��}|j||�WdQRXnd|j
��rh|j|�|�rrxHtj|�D].}|jtjj||�tjj||�|||d��q4Wn
|j|�dS)a~Add the file `name' to the archive. `name' may be any type of file
           (directory, fifo, symbolic link, etc.). If given, `arcname'
           specifies an alternative name for the file in the archive.
           Directories are added recursively by default. This can be avoided by
           setting `recursive' to False. `exclude' is a function that should
           return True for each filename to be excluded. `filter' is a function
           that expects a TarInfo object argument and returns the changed
           TarInfo object, if it returns None the TarInfo object will be
           excluded from the archive.
        r�Nrzuse the filter argument insteadr$ztarfile: Excluded %rztarfile: Skipped %rr#ztarfile: Unsupported type %rri)r�)r�rdrerf�_dbgr�r~r%ror�r9rn�addfiler)�listdir�addr�)	r�r�r��	recursiveZexcluder�rdr��fr5r5r6r�qsB



zTarFile.addcCs�|jd�tj|�}|j|j|j|j�}|jj|�|jt	|�7_|j
}|dk	r�t||j|j|d�t
|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().
        r�N)r`rr#)r�rr�rPr3r4r�r\r�r/rtrYr'rZr�r0rur�)r�r�r�rUr`rarbr5r5r6r��s

zTarFile.addfile�.)�
numeric_ownercCsg}|dkr|}xF|D]>}|j�r<|j|�tj|�}d|_|j|||j�|d�qW|jdd�d�|j�x�|D]�}tjj	||j
�}y,|j|||d�|j||�|j
||�Wqvtk
r�}z$|jdkrւn|jdd	|�WYdd}~XqvXqvWdS)
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�cSs|jS)N)r�)rkr5r5r6�<lambda>�sz$TarFile.extractall.<locals>.<lambda>)r~)r�r#ztarfile: %s)r)r�rri�extract�sort�reverser~r%r�r��chown�utime�chmodr
rsr�)r�r%rur�Zdirectoriesr��dirpathryr5r5r6�
extractall�s,




zTarFile.extractallr�cCs|jd�t|t�r |j|�}n|}|j�r>tjj||j�|_	y"|j
|tjj||j�||d�Wn�tk
r�}zJ|j
dkr��n6|jdkr�|jdd|j�n|jdd|j|jf�WYdd}~XnBtk
�r}z$|j
dkr�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.
        r{)r�r�rNr#ztarfile: %sztarfile: %s %r)r�rr
r�r\r~r%r�r�r��_extract_memberr�r�rs�filenamer��strerrorr
)r��memberr%r�r�r�ryr5r5r6r��s(	



(
zTarFile.extractcCs�|jd�t|t�r |j|�}n|}|j�s6|jtkrB|j||�S|j�sR|j	�rzt|j
t�rht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.
        r{z'cannot extract (sym)link as file objectN)r�rr
r�r9r�r:�
fileobjectr\rZr�r�r	�extractfile�_find_link_target)r�r�r�r5r5r6r�s


zTarFile.extractfilecCsV|jd�}|jdtj�}tjj|�}|r@tjj|�r@tj|�|j�sP|j	�rj|j
dd|j|jf�n|j
d|j�|j
�r�|j||�n�|j�r�|j||�nx|j�r�|j||�nb|j�s�|j�r�|j||�nD|j�s�|j	�r�|j||�n&|jtk�r|j||�n|j||�|�rR|j|||�|j	��sR|j||�|j||�dS)z\Extract the TarInfo object tarinfo to a physical
           file called targetpath.
        r�r#z%s -> %sN)r*r�r~r�r%�dirnamerm�makedirsr\rZr�r�r�r9�makefiler)�makedirrb�makefifor^r`�makedev�makelinkr�r:�makeunknownr�r�r�)r�r��
targetpathr�r�Z	upperdirsr5r5r6r�4s4


zTarFile._extract_membercCs*ytj|d�Wntk
r$YnXdS)z,Make a directory called targetpath.
        i�N)r~�mkdir�FileExistsError)r�r�r�r5r5r6r�eszTarFile.makedircCs�|j}|j|j�|j}t|d��f}|jdk	rrx,|jD]"\}}|j|�t|||t|�q6W|j|j�|j	�nt|||jt|�WdQRXdS)z'Make a file called targetpath.
        rjN)
r�r�r�rtrnr�rYrr'�truncate)r�r�r��sourcer`�targetr�r'r5r5r6r�os


zTarFile.makefilecCs"|j||�|jdd|j�dS)zYMake a file from a TarInfo object with an unknown type
           at targetpath.
        r#z9tarfile: Unknown file type %r, extracted as regular file.N)r�r�r�)r�r�r�r5r5r6r�szTarFile.makeunknowncCs"ttd�rtj|�ntd��dS)z'Make a fifo called targetpath.
        �mkfifozfifo not supported by systemN)r�r~r�r
)r�r�r�r5r5r6r��s
zTarFile.makefifocCsbttd�sttd�r td��|j}|j�r:|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�r~r
rir`rg�S_IFBLK�S_IFCHRr�r�r�r�)r�r�r�rir5r5r6r��s
zTarFile.makedevcCs�yL|j�rtj|j|�n0tjj|j�r8tj|j|�n|j|j	|�|�WnHt
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)
rZr~�symlinkr�r%rmr��linkr�r��symlink_exceptionr�r
)r�r�r�r5r5r6r��s

zTarFile.makelinkcCs�ttd�r�tj�dkr�|j}|j}|s�ytr<tj|j�d}Wntk
rRYnXyt	rjt	j
|j�d}Wntk
r�YnXy4|j�r�ttd�r�tj
|||�ntj|||�Wntk
r�td��YnXdS)z�Set 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$�lchownzcould not change ownerN)r�r~r�r*r)r�Zgetgrnamr,r�r��getpwnamr+rZr�r�r�r
)r�r�r�r��g�ur5r5r6r��s(z
TarFile.chowncCs>ttd�r:ytj||j�Wntk
r8td��YnXdS)zASet file permissions of targetpath according to tarinfo.
        r�zcould not change modeN)r�r~r�rir�r
)r�r�r�r5r5r6r��s

z
TarFile.chmodcCsHttd�sdSytj||j|jf�Wntk
rBtd��YnXdS)zBSet modification time of targetpath according to tarinfo.
        r�Nz"could not change modification time)r�r~r�r(r�r
)r�r�r�r5r5r6r��s
z
TarFile.utimecCs�|jd�|jdk	r$|j}d|_|S|j|jj�krZ|jj|jd�|jjd�sZtd��d}�xby|jj	|�}W�nHt
k
r�}z2|jr�|jdd|j|f�|jt
7_wbWYdd}~Xn�tk
�r,}zN|j�r|jdd|j|f�|jt
7_wbn|jdk�rtt|���WYdd}~Xn�tk
�rT|jdk�rPtd��Ynjtk
�r�}z|jdk�r~tt|���WYdd}~Xn0tk
�r�}ztt|���WYdd}~XnXPqbW|dk	�r�|jj|�nd	|_|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.
        ZraNr#zunexpected end of datar$z0x%X: %srz
empty fileT)r�rxr�r�r�r�r[rr�r3rxrqr�r�rHr
rvrwryrur�rv)r��mr�ryr5r5r6r=�sL


zTarFile.nextcCsn|j�}|dk	r"|d|j|��}|r2tjj|�}x6t|�D]*}|rTtjj|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��indexr~r%�normpath�reversedr�)r�r�r��	normalizerur��member_namer5r5r6r�	szTarFile._getmembercCs"x|j�}|dkrPqWd|_dS)zWRead through the entire archive file and look for readable
           members.
        NT)r=rv)r�r�r5r5r6r�3	s
z
TarFile._loadcCs:|jrtd|jj��|dk	r6|j|kr6td|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�rrri)r�rir5r5r6r�=	szTarFile._checkcCs`|j�r.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.
        r�NT)r�r�zlinkname %r not found)
rZr�r�r~r%r�r�r�r�r�)r�r�r��limitr�r5r5r6r�F	s zTarFile._find_link_targetccs�|jr|jEdHdSd}|jdk	r:|j�}|d7}|VxN|t|j�krV|j|}n"|jst|j�}|sxd|_dSndS|d7}|Vq<WdS)z$Provide an iterator object.
        Nrr#T)rvrurxr=r/)r�r�r�r5r5r6�__iter__Y	s&
zTarFile.__iter__cCs||jkrt|tjd�dS)z.Write debugging output to sys.stderr.
        )�fileN)rrrprn�stderr)r��level�msgr5r5r6r�x	s
zTarFile._dbgcCs|j�|S)N)r�)r�r5r5r6�	__enter__~	szTarFile.__enter__cCs,|dkr|j�n|js"|jj�d|_dS)NT)r�r�r�r�)r�r�r�	tracebackr5r5r6�__exit__�	s


zTarFile.__exit__)
Nr{NNNNNNr�NNNN)r{N)r{Nr�)r{Nr�)r{NN)NNN)T)NTN)N)r�N)r�T)TF)NF)N)8rrrsrtrurrrprqrsrrPrr3r4rr�r�r�r�rgr�rr{r�r�r�r|r�r�r�r�r�rMr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r=r�r�r�r�r�r�r�r�r5r5r5r6rksv
lZ!

d =
(&
0
	6


	cCs.yt|�}|j�dStk
r(dSXdS)zfReturn True if name points to a tar archive that we
       are able to handle, else return False.
    TFN)rr�r)r�r�r5r5r6r�	scCs�ddl}d}|j|d�}|jdddddd	�|j�}|jd
ddd
d�|jdddd0dd�|jdddd1dd�|jddddd�|j�}|j�r�|j}t|�r�t|d�� }|j�t	|j�t
jd�WdQRX|jr�t	dj
|��n|jd d!j
|���n�|j�rP|j}t|��r:tj|d"��}|j|jd#�WdQRXn|jd d!j
|���n�|j�rt|j�d k�rz|jd}tj}n,t|j�d$k�r�|j\}}n|jd |j��t|��rtj|d"��}|j|d%�WdQRX|j�r|d&k�r�d'j
|�}	nd(j
||�}	t	|	�n|jd d!j
|��n�|j�r�|jjd�}
tjj|
�\}}d)d)d*d*d+d+d+d+d,�}
||
k�rld-|
|nd.}|j}tj|
|�� }x|D]}|j|��q�WWdQRX|j�r�t	d/j
|
��n|jd |j��dS)2Nrz3A simple command line interface for tarfile module.)�descriptionz-vz	--verbose�
store_trueFzVerbose output)�action�default�helpz-lz--list�	<tarfile>zShow listing of a tarfile)�metavarr�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{)r�z{!r} is a tar archive.r#z{!r} is not a tar archive.
zr:*)r�r$)r%r�z{!r} file is extracted.z+{!r} file is extracted into {!r} directory.r�r�r�)z.gzz.tgzz.xzz.txzz.bz2z.tbzz.tbz2z.tb2zw:r|z{!r} file created.)r�r)rr)�argparse�ArgumentParser�add_argumentZadd_mutually_exclusive_group�
parse_argsZtestrrr�rprnr�r�rP�exitrMrr�r/r~�curdirZformat_helpr�Zcreate�popr%�splitextr�)rr��parserrC�argsr]r�Ztfr	r�Ztar_name�_ZextZcompressionsZtar_modeZ	tar_files�	file_namer5r5r6�main�	s�









r�__main__)r%r&r'r(r)r*r+r,)aru�version�
__author__Z__date__Z	__cvsid__�__credits__�builtinsrrnrnr~r�rXrgr�rSrrAr�r�r�r��NotImplementedErrorr�r��	NameError�__all__r0r�r�r�r�r�r�rr�r&r[rYr]r_r�raZCONTTYPEr�r�r'rrr6r
rrrr:rWr+rTrHr	rErUr�r�getfilesystemencodingr7r<rKrQrVrYrhrq�	Exceptionrr
rrr	rrvrwrxrHryrzr��objectr�r��BufferedReaderr�rrrrrrr5r5r5r6�<module>s�



wh,Z