o
    pf                     @   s   d Z ddlmZ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rMd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d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)sizemodified)info1info2date1date2 r   B/home/ertert/spirit/venv/lib/python3.10/site-packages/fs/mirror.py_compare%   s
   	r   TFc                    s  fdd} fdd}| v}| Z}	t ||	}
t|
r|nd|d;}| ' |	  t||	|||j|d W d   n1 sCw   Y  W d   n1 sRw   Y  W d   n1 saw   Y  W d   n1 spw   Y  W d   dS W d   dS 1 sw   Y  dS )	aY  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.
        preserve_time (bool): If `True`, try to preserve mtime of the
            resources (defaults to `False`).

    c                         t  ddS )NF)	writeabler   r   )src_fsr   r   srcR      zmirror.<locals>.srcc                      r   )NT)creater   r   )dst_fsr   r   dstU   r   zmirror.<locals>.dstr   )num_workerspreserve_time)walkercopy_if_newer	copy_filer$   N)r
   r   lock_mirrorcopy)r   r!   r%   r&   workersr$   r   r"   _src_fs_dst_fs_thread_safecopierr   )r!   r   r   mirror6   s0   
 Pr0   c              	   C   sN  |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~|| qd|j|dd qd|
r|
 \}}||}|jr|	| n|| |
sqd S )Ndetails)
namespacesc                 S   s   i | ]}|j |qS r   )name).0infor   r   r   
<dictcomp>u   s    z_mirror.<locals>.<dictcomp>T)recreate)r   walkscandirr   makedir	make_pathpopr3   is_dir
removetreer   removepopitem)r   r!   r%   r&   r'   r$   r8   pathdirsfilesr"   _file_pathdst_file_dirdst_dir_r5   r   r   r   r)   h   sJ   
	






r)   )NTr   F)__doc__
__future__r   r   typing_bulkr   r*   r   errorsr   openerr	   toolsr
   r8   r   TYPE_CHECKINGr   r   r   r   baser   r5   r   r   r0   r)   r   r   r   r   <module>   s0    
5