o
    Q `                     @   s   d Z ddlm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 dd
lmZ ddlmZ ejrQddlmZmZmZmZ ddlmZ ddlmZ dd Z			dddZddefddZdS )a  Function for *mirroring* a filesystem.

Mirroring will create a copy of a source filesystem on a destination
filesystem. If there are no files on the destination, then mirroring
is simply a straight copy. If there are any files or directories on the
destination they may be deleted or modified to match the source.

In order to avoid redundant copying of files, `mirror` can compare
timestamps, and only copy files with a newer modified date. This
timestamp comparison is only done if the file sizes are different.

This scheme will work if you have mirrored a directory previously, and
you would like to copy any changes. Otherwise you should set the
``copy_if_newer`` parameter to `False` to guarantee an exact copy, at
the expense of potentially copying extra files.

    )print_function)unicode_literalsN   )Copier)copy_file_internal)ResourceNotFound	manage_fs)is_thread_safe)Walker)CallableOptionalTextUnion)FS)Infoc                 C   s4   | j |j krdS | j}|j}|du p|du p||kS )zCompare two `Info` objects to see if they should be copied.

    Returns:
        bool: `True` if the `Info` are different in size or mtime.

    TN)sizeZmodified)Zinfo1Zinfo2Zdate1Zdate2 r   +/usr/lib/python3/dist-packages/fs/mirror.py_compare%   s
   	r   Tc                    s  fdd} fdd}| t}| X}|  D |  0 t||}	t|	r(|ndd}
t|||||
jd W d   n1 sAw   Y  W d   n1 sPw   Y  W d   n1 s_w   Y  W d   n1 snw   Y  W d   dS W d   dS 1 sw   Y  dS )	a  Mirror files / directories from one filesystem to another.

    Mirroring a filesystem will create an exact copy of ``src_fs`` on
    ``dst_fs``, by removing any files / directories on the destination
    that aren't on the source, and copying files that aren't.

    Arguments:
        src_fs (FS or str): Source filesystem (URL or instance).
        dst_fs (FS or str): Destination filesystem (URL or instance).
        walker (~fs.walk.Walker, optional): An optional walker instance.
        copy_if_newer (bool): Only copy newer files (the default).
        workers (int): Number of worker threads used
            (0 for single threaded). Set to a relatively low number
            for network filesystems, 4 would be a good start.
    c                         t  ddS )NF)Z	writeabler   r   )src_fsr   r   srcN      zmirror.<locals>.srcc                      r   )NT)Zcreater   r   )dst_fsr   r   dstQ   r   zmirror.<locals>.dstr   )Znum_workers)walkercopy_if_newer	copy_fileN)lockr
   r   _mirrorcopy)r   r   r   r   Zworkersr   r   Z_src_fsZ_dst_fsZ_thread_safeZcopierr   )r   r   r   mirror6   s(   
 Pr"   c              	   C   sL  |pt  }|j| dgd}|D ]\}}}zdd |j|dgdD }	W n ty3   || i }	Y nw |D ]*}
|
|}|	|
jd }|d urY|jrQ|	| n|rYt
|
|sYq6|| ||| q6|D ]"}||}|	|jd }|d ur~|js}|| qc|j|dd qc|	r|	 \}}||}|jr|	| n|| |	sqd S )NZdetails)Z
namespacesc                 S   s   i | ]}|j |qS r   )name).0infor   r   r   
<dictcomp>i   s    z_mirror.<locals>.<dictcomp>T)Zrecreate)r   walkscandirr   ZmakedirZ	make_pathpopr#   is_dirZ
removetreer   removepopitem)r   r   r   r   r   r'   pathdirsfilesr   Z_file_pathZdst_fileZ_dirZdst_dir_r%   r   r   r   r    a   sJ   







r    )NTr   )__doc__Z
__future__r   r   typingZ_bulkr   r!   r   errorsr   Zopenerr	   Ztoolsr
   r'   r   ZTYPE_CHECKINGr   r   r   r   baser   r%   r   r   r"   r    r   r   r   r   <module>   s*    
,