o
    Q `t                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	m	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mZ ddlmZ ejroddlmZmZmZmZmZ ddlmZ eeeeeeef ZejddfddZ 			dddZ!dS )zFunctions to compress the contents of a filesystem.

Currently zip and tar are supported, using the `zipfile` and
`tarfile` modules from the standard library.
    )absolute_import)print_function)unicode_literalsN)datetime   )ResourceType)relpath)datetime_to_epoch)	NoSysPathMissingInfoNamespace)Walker)BinaryIOOptionalTextTupleUnion)FSutf-8c                 C   s  t j|d|dd}|pt }| |j| g dd}|D ]\}}t|jr(|d n|}	tjs4|	|d}	|	drL|
dd	d
}
t|
}|dd }n|jpRt }|j|j|j|j|j|jf}t |	|}z|jd
urt|jjd> |_W n	 ty~   Y nw |jr| jdO  _||d qz| |}W n ty   ||| | Y qw |||	 qW d
   d
S 1 sw   Y  d
S )a  Write the contents of a filesystem to a zip file.

    Arguments:
        src_fs (~fs.base.FS): The source filesystem to compress.
        file (str or io.IOBase): Destination file, may be a file name
            or an open file object.
        compression (int): Compression to use (one of the constants
            defined in the `zipfile` module in the stdlib). Defaults
            to `zipfile.ZIP_DEFLATED`.
        encoding (str):
             The encoding to use for filenames. The default is ``"utf-8"``,
             use ``"CP437"`` if compatibility with WinZip is desired.
        walker (~fs.walk.Walker, optional): A `Walker` instance, or `None`
            to use default walker. You can use this to specify which files
            you want to compress.

    wT)modecompressionZ
allowZip64ZdetailsstataccessZ
namespaces/replacer   st_mtimeNr             ) zipfileZipFiler   infor   is_dirsixPY3encodehas_namespacegettime	localtimemodifiedr   ZutcnowZyearZmonthZdayZhourZminutesecondZZipInfopermissionsr   Zexternal_attrr   ZwritestrZ
getsyspathr
   Z	readbyteswrite)src_fsfiler   encodingwalker_zipgen_walkpathr#   Zzip_namer   Z_mtimeZzip_timemtZzip_infoZsys_path r8   -/usr/lib/python3/dist-packages/fs/compress.py	write_zip    sB   



"r:   c                 C   s  t jtjt jtjt jtjt jtj	t j
tjt jtjt jtjt jtji}g d}d|p+d}t|tjtjfr>tj||d}ntj||d}t }	|pMt }| |j| g dd}
|
D ]\}}t|}tjsm||d}t|}|d	r|d	d
|	}n|j p|	}t|t!rt"|}t|t#rt$|}||_%|D ]\}}t&||ddurt'||t&||d q|drt&|j(dd|_)|j*rtj|_+|,| q\||j+tj|_+|j-|_-| .|}|,|| W d   n1 sw   Y  q\W d   dS 1 sw   Y  dS )aG  Write the contents of a filesystem to a tar file.

    Arguments:
        file (str or io.IOBase): Destination file, may be a file
            name or an open file object.
        compression (str, optional): Compression to use, or `None`
            for a plain Tar archive without compression.
        encoding(str): The encoding to use for filenames. The
            default is ``"utf-8"``.
        walker (~fs.walk.Walker, optional): A `Walker` instance, or
            `None` to use default walker. You can use this to specify
            which files you want to compress.

    ))uidr;   )gidr<   )unameuser)gnamegroupzw:{} )r   )Zfileobjr   r   r   r   r   r   Nr   r   i  )/r   Zblock_special_filetarfileZBLKTYPE	characterZCHRTYPEZ	directoryZDIRTYPEZfifoZFIFOTYPEr1   ZREGTYPEZsocketZAREGTYPEsymlinkZSYMTYPEunknownformat
isinstancer%   Z	text_typeZbinary_typeopenr*   r   r#   r   r&   r'   ZTarInfor(   r)   r,   r   r	   floatintmtimegetattrsetattrr.   r   r$   typeZaddfilesizeZopenbin)r0   r1   r   r2   r3   Ztype_mapZtar_attrr   Z_tarZcurrent_timer5   r6   r#   Ztar_nameZtar_inforK   ZtarattrZinfoattrZbin_filer8   r8   r9   	write_tarl   s`   






$rP   )Nr   N)"__doc__Z
__future__r   r   r   r*   rB   typingr!   r   r%   Zenumsr   r6   r   r	   errorsr
   r   walkr   ZTYPE_CHECKINGr   r   r   r   r   baser   rJ   ZZipTimeZIP_DEFLATEDr:   rP   r8   r8   r8   r9   <module>   s6    
O