
    ob<                         d Z ddlmZmZ ddlZddlmZmZ ddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ejR                  r<ddlm*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl	m3Z3 ddlm4Z4 ddl5m6Z6  ejn                  dd      Z8g dZ9ejt                  rd Z;nd Z; G d de(      Z<ejz                   G d d e(             Z>ejz                   G d! de             Z?e@d"k(  rdd#lAmBZB  e<d$      5 ZC eDeCj                  d%              eDeCj                  d&              eDeCj                  d'              eBeC        eDeC        eD eGeC             ddd        e<d(d)*      5 ZCeCj                  d+       eCj                  d,d-        eDeC        eD eGeC             ddd       yy# 1 sw Y   WxY w# 1 sw Y   yxY w).z(Manage the filesystem in a Tar archive.
    )print_functionunicode_literalsN)IOcast)OrderedDict   )errors)	url_quote)FS)	write_tar)ResourceType)IllegalBackReferenceNoURL)Info)
RawWrapper)open_fs)basenamefrombaseisbasenormpathpartsrelpath)Permissions)WrapFS)	AnyBinaryIO
CollectionDictListOptionalTextTupleUnion)TarInfo)RawInfo)SubFST	ReadTarFS)bound)TarFS
WriteTarFSr(   c                 &    | j                  |d       S Nget_infomemberencodings     */usr/lib/python3/dist-packages/fs/tarfs.py_get_member_infor4   5   s    x..    c                 "    | j                         S r-   r.   r0   s     r3   r4   r4   ;   s       r5   c                   V    e Zd ZdZddddZ	 	 	 	 d	dZej                  r		 	 	 	 d	dZyy)
r*   a  Read and write tar files.

    There are two ways to open a `TarFS` for the use cases of reading
    a tar file, and creating a new one.

    If you open the `TarFS` with  ``write`` set to `False` (the
    default), then the filesystem will be a read only filesystem which
    maps to the files and directories within the tar file. Files are
    decompressed on the fly when you open them.

    Here's how you might extract and print a readme from a tar file::

        with TarFS('foo.tar.gz') as tar_fs:
            readme = tar_fs.readtext('readme.txt')

    If you open the TarFS with ``write`` set to `True`, then the `TarFS`
    will be a empty temporary filesystem. Any files / directories you
    create in the `TarFS` will be written in to a tar file when the `TarFS`
    is closed. The compression is set from the new file name but may be
    set manually with the ``compression`` argument.

    Here's how you might write a new tar file containing a readme.txt
    file::

        with TarFS('foo.tar.xz', write=True) as new_tar:
            new_tar.writetext(
                'readme.txt',
                'This tar file was written by PyFilesystem'
            )

    Arguments:
        file (str or io.IOBase): An OS filename, or an open file handle.
        write (bool): Set to `True` to write a new tar file, or
            use default (`False`) to read an existing tar file.
        compression (str, optional): Compression to use (one of the formats
            supported by `tarfile`: ``xz``, ``gz``, ``bz2``, or `None`).
        temp_fs (str): An FS URL or an FS instance to use to store
            data prior to tarring. Defaults to creating a new
            `~fs.tempfs.TempFS`.

    )z.tar.xzz.txz)z.tar.bz2z.tbz)z.tar.gzz.tgz)xzbz2gzNc                 t   t        |t        j                  t        j                  f      r"t        j
                  j                  |      }|}nt        |dd      }|r@|>d }t        j                  | j                        D ]  \  }}|j                  |      s|} n |rt        ||||      S t        ||      S )Nname )compressionr2   temp_fs)r2   )
isinstancesix	text_typebinary_typeospath
expandusergetattr	iteritems_compression_formatsendswithr+   r(   )	clsfilewriter>   r2   r?   filenamecomp
extensionss	            r3   __new__zTarFS.__new__t   s     dS]]COO<=77%%d+DHtVR0H[(K$'MM#2J2J$K  j$$Z0"&K
 +'  TH55r5   c                      y r-    )selfrL   rM   r>   r2   r?   s         r3   __init__zTarFS.__init__   s     r5   )FNutf-8temp://__tartemp__)	__name__
__module____qualname____doc__rI   rQ   typingTYPE_CHECKINGrU   rS   r5   r3   r*   r*   B   sQ    (X "#!	 $6: 
 (		 r5   r*   c                   Z     e Zd ZdZ	 	 	 d	 fd	Zd Zd Zd Zd Z fdZ		 	 	 d
dZ
 xZS )r+   zA writable tar file.c                     || _         || _        || _        || _        t	        |      | _        t        | j
                  j                               | _        t        t        | /  | j
                         y r-   )_filer>   r2   _temp_fs_urlr   _temp_fsdictgetmeta_metasuperr+   rU   )rT   rL   r>   r2   r?   	__class__s        r3   rU   zWriteTarFS.__init__   s[     
& #($--//12
j$(7r5   c                 ~    d}|j                  | j                  | j                  | j                  | j                        S )Nz?WriteTarFS({!r}, compression={!r}, encoding={!r}, temp_fs={!r}))formatr`   r>   r2   ra   )rT   ts     r3   __repr__zWriteTarFS.__repr__   s0    Mxx

D$4$4dmmTEVEVWWr5   c                 8    dj                  | j                        S )Nz<TarFS-write '{}'>ri   r`   rT   s    r3   __str__zWriteTarFS.__str__   s    #**4::66r5   c                     | j                   |fS r-   rb   rT   rE   s     r3   delegate_pathzWriteTarFS.delegate_path   s    }}d""r5   c                     | j                   S r-   rq   rn   s    r3   delegate_fszWriteTarFS.delegate_fs   s    }}r5   c                     | j                         s+	 | j                          | j                  j                          t        t
        |           y # | j                  j                          w xY wr-   )isclosedr   rb   closerf   r+   rT   rg   s    r3   rx   zWriteTarFS.close   sL    }}& ##%j$%' ##%s   A A,c                     | j                         sDt        | j                  |xs | j                  |xs | j                  |xs | j
                         yy)a*  Write tar to a file.

        Arguments:
            file (str or io.IOBase, optional): Destination file, may be
                a file name or an open file object.
            compression (str, optional): Compression to use (one of
                the constants defined in `tarfile` in the stdlib).
            encoding (str, optional): The character encoding to use
                (default uses the encoding defined in
                `~WriteTarFS.__init__`).

        Note:
            This is called automatically when the TarFS is closed.

        )r>   r2   N)rw   r   rb   r`   r>   r2   )rT   rL   r>   r2   s       r3   r   zWriteTarFS.write_tar   sG    , }}"

';4+;+;!2T]]	 r5   )NrV   rW   )NNN)rX   rY   rZ   r[   rU   rk   ro   rs   ru   rx   r   __classcell__rg   s   @r3   r+   r+      s@    
 $8 X
7#( 	r5   r+   c                       e Zd ZdZddddddddZej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                   ej"                  ej                   ej$                  ej&                  ej(                  ej                   ej*                  ej&                  i	xZZej2                  j4                  d fd	       Zed        Zd Zd Ze jB                  rd	 Z"d
 Z#nd Z"d Z#ddZ$d Z%d Z&d Z'd Z(	 	 ddZ)ddZ*d Z+d Z, fdZ-d Z.ddZ/ xZ0S )r(   zA readable tar file.TF)case_insensitivenetwork	read_onlysupports_renamethread_safeunicode_pathsvirtualc                 (   t         t        |           || _        || _        t        |t        j                  t        j                  f      r$t        j                  |d      | _        d | _        y t        j                  |d      | _        d | _        y )Nrmode)fileobjr   )rf   r(   rU   r`   r2   r@   rA   rB   rC   tarfileopen_tar_directory_cache)rT   rL   r2   rg   s      r3   rU   zReadTarFS.__init__  so     	i')
 dS]]COO<=T4DI !%  T<DI $r5   c                     | j                   ;| j                  fd| j                  D        fd}t         |             | _         | j                   S )zLazy directory cache.c              3   d   K   | ]'  } |j                         j                  d       |f ) yw)/N)r<   strip).0info_decodes     r3   	<genexpr>z/ReadTarFS._directory_entries.<locals>.<genexpr>  s/      ":>#))#.5"s   -0c               3   j   K   D ]  \  } }	 t        |       }|s||f  y # t        $ r Y )w xY wwr-   )r   r   )r<   r   _name_directory_entriess      r3   	_list_tarz/ReadTarFS._directory_entries.<locals>._list_tar  sJ     "4 .JD$. (
 !"'+-. 0 s    	3$3	3	0303)r   r   r   r   )rT   r   r   r   s     @@r3   r   zReadTarFS._directory_entries  sP       (llG"BF))"	. %0	$<D!$$$r5   c                 8    dj                  | j                        S )NzReadTarFS({!r})rm   rn   s    r3   rk   zReadTarFS.__repr__*  s     ''

33r5   c                 8    dj                  | j                        S )Nz<TarFS '{}'>rm   rn   s    r3   ro   zReadTarFS.__str__.  s    $$TZZ00r5   c                 8    |j                  | j                        S r-   )encoder2   rT   ss     r3   _encodezReadTarFS._encode4      88DMM**r5   c                 8    |j                  | j                        S r-   )decoder2   r   s     r3   r   zReadTarFS._decode8  r   r5   c                     |S r-   rS   r   s     r3   r   zReadTarFS._encode>      Hr5   c                     |S r-   rS   r   s     r3   r   zReadTarFS._decodeB  r   r5   c                 n   t        | j                  |            }|xs d}i }|s6ddd|d<   d|v rdt        t        j                        i|d<   tE        |      S 	 d}| j
                  |   }t        | j                  |j                               |j                         d|d<   d|v rF|j"                  t        | j$                  |j                           d	|d<   |s|j&                  |d   d
<   d|v rW|sU|j(                  |j*                  t-        |j.                        j1                         |j2                  |j4                  d|d<   d|v rw|sut7        || j8                        |d<   |d   j;                  t=        |      D ci c]5  }|j?                  d      r"|jA                  dd       tC        ||             7 c}       tE        |      S # t        $ rV | j                  |      st        j                  |      d}t        j                  |      }t        j                  |_        Y w xY wc c}w )NrS   r=   T)r<   is_dirbasicdetailstypeF)sizer   modifiedaccessr   )gidgrouppermissionsuidusertarisis_)#r   validatepathintr   	directoryr   KeyErrorisdirr	   ResourceNotFoundr   r$   DIRTYPEr   r   r   r<   r   type_mapmtimer   gnamer   r   dumpr   unamer4   r2   updatedir
startswithreplacerG   r   )rT   rE   
namespaces_pathraw_infoimplicitr1   ks           r3   getinfozReadTarFS.getinfoF  s   ))$/0%2
)+t <HWJ&'-s<3I3I/J&K#V H~Q. 007 !fkk!:; ,,.!HW
 J&"KKfkk :;'#  6<llHY'
3:%h!::#\\#.FKK#@#E#E#G!::"LL&" 
"8"264=="I&& "%V<<- 		$.0B0B0DD H~K  .zz%( 11$77 /%oo.<s   G :H2AH/.H/c                     t        | j                  |            	 | j                     j                         S # t        $ r! t        fd| j                  D              cY S w xY w)Nc              3   6   K   | ]  }t        |        y wr-   )r   )r   r<   r   s     r3   r   z"ReadTarFS.isdir.<locals>.<genexpr>  s     OtveT*Os   )r   r   r   r   r   anyrT   rE   r   s     @r3   r   zReadTarFS.isdir|  s`    ))$/0	P**517799 	POt7N7NOOO	Ps   : 'A$#A$c                     t        | j                  |            }	 | j                  |   j                         S # t        $ r Y yw xY wNF)r   r   r   isfiler   r   s      r3   r   zReadTarFS.isfile  sH    ))$/0	**5188:: 		s   9 	AAc                 L    | j                          t        j                  |      r-   checkr	   ResourceReadOnly)rT   rE   r   s      r3   setinfozReadTarFS.setinfo      

%%d++r5   c                    t        | j                  |            | j                  |      t        j                  urt        j                  |      fd| j                  D        }d |D        }t        t        j                  |            S )Nc              3   P   K   | ]  }t        |      st        |        y wr-   )r   r   )r   nr   s     r3   r   z$ReadTarFS.listdir.<locals>.<genexpr>  s'      
#$6%QRCSHUA
s   &&c              3   P   K   | ]  }t        |      st        |      d       yw)r   N)r   r   )r   childs     r3   r   z$ReadTarFS.listdir.<locals>.<genexpr>  s     KuGEN5<?Ks   &&)r   r   gettyper   r   r	   DirectoryExpectedr   listr   fromkeys)rT   rE   childrencontentr   s       @r3   listdirzReadTarFS.listdir  sx    ))$/0||D!\%;%;;**400
(,(?(?
 LKK((122r5   c                 L    | j                          t        j                  |      r-   r   )rT   rE   r   recreates       r3   makedirzReadTarFS.makedir  s     	

%%d++r5   c                    t        | j                  |            }d|v sd|v sd|v rt        j                  |      	 | j                  |   }j                         st        j                  |      t        t        t        | j                  j                  |                  }t        j                   r
d }||_        |S # t
        $ r, t        j                  t        j                  |      d        Y w xY w)Nw+ac                       y r-   rS   rS   r5   r3   _flushz!ReadTarFS.openbin.<locals>._flush  s    r5   )r   r   r	   r   r   r   rA   
raise_fromr   r   FileExpectedr   r   r   r   extractfilePY2flush)	rT   rE   r   	bufferingoptionsr   r1   rwr   s	            r3   openbinzReadTarFS.openbin  s    ))$/0$;#+))$//	@,,U3F }}%%d++R!6!6v!>?@77 BH	  	@NN62248$?	@s   B? ?2C43C4c                 L    | j                          t        j                  |      r-   r   rr   s     r3   removezReadTarFS.remove  r   r5   c                 L    | j                          t        j                  |      r-   r   rr   s     r3   	removedirzReadTarFS.removedir  r   r5   c                 x    t         t        |           t        | d      r| j                  j                          y y )Nr   )rf   r(   rx   hasattrr   ry   s    r3   rx   zReadTarFS.close  s-    i$&4 IIOO !r5   c                 .    | j                   j                  S r-   )r   closedrn   s    r3   rw   zReadTarFS.isclosed  s    yyr5   c                     |dk(  rVt        | j                  t        j                        r2t	        | j                        }t	        |      }dj                  ||      S t        ||      )Nfsztar://{}!/{})r@   r`   rA   string_typesr
   ri   r   )rT   rE   purposequoted_filequoted_paths        r3   geturlzReadTarFS.geturl  sS    d?z$**c6F6FG#DJJ/K#D/K!((kBBg&&r5   )rV   r-   r   )r   )download)1rX   rY   rZ   r[   re   r   BLKTYPEr   block_special_fileCHRTYPE	characterr   r   FIFOTYPEfifoREGTYPErL   AREGTYPESYMTYPEsymlinkCONTTYPELNKTYPE_typemapr   r	   CreateFailed	catch_allrU   propertyr   rk   ro   rA   r   r   r   r   r   r   r   r   r   r   r   r   rx   rw   r  r{   r|   s   @r3   r(   r(      sb    ! E 	88////,++**,++--,++--
 
Hx ""
% #
% % %,41 ww	+	+		4lP,
3  	,4,
,
 'r5   __main__)renderz	tests.tarr   z/tests/ztests/ttt/settings.iniz	TarFS.tarT)rM   zfoo/barzfoo/bar/baz.txtzHello, World)Jr[   
__future__r   r   r\   r   r   rD   rA   r   collectionsr   r=   r	   
_url_toolsr
   baser   compressr   enumsr   r   r   r   r   iotoolsr   openerr   rE   r   r   r   r   r   r   r   r   wrapfsr   r]   r   r   r   r   r   r    r!   r"   r#   r$   r%   subfsr&   TypeVarr'   __all__r   r4   r*   python_2_unicode_compatibler+   r(   rX   fs.treer  tar_fsprintr   readtextreprmakedirs	writetextrS   r5   r3   <module>r/     s   8   	 
  #  !    /    F F $ 	
 
 
  s+.A / 77/!ZF Zz   I I !IX   n' n' !n'b z	{	 vfnnS!"fnnY'(foo678vfd6l 
{$	' 6	"*N;fd6l	    s   .A$G($:G4(G14G=