o
    Q `:                     @   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
mZmZ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 eeeeegef Z			dddZ			dddZdd Zdd Zdd Zdd Z 	dddZ!			dddZ"			dddZ#dS ) z6Functions for copying resources *between* filesystem.
    )print_functionunicode_literalsN   )FSError	manage_fs)abspathcombinefrombasenormpath)is_thread_safe)Walker)CallableOptionalTextUnion)FSc              	   C      t | d|d|||dS )a  Copy the contents of one filesystem to another.

    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): A walker object that will be
            used to scan for files in ``src_fs``. Set this if you only want
            to consider a sub-set of the resources in ``src_fs``.
        on_copy (callable): A function callback called after a single file copy
            is executed. Expected signature is ``(src_fs, src_path, dst_fs,
            dst_path)``.
        workers (int): Use `worker` threads to copy data, or ``0`` (default) for
            a single-threaded copy.

    /walkeron_copyworkers)copy_dirsrc_fsdst_fsr   r   r    r   )/usr/lib/python3/dist-packages/fs/copy.pycopy_fs   s   r   c              	   C   r   )a  Copy the contents of one filesystem to another, checking times.

    If both source and destination files exist, the copy is executed
    only if the source file is newer than the destination file. In case
    modification times of source or destination files are not available,
    copy file is always executed.

    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): A walker object that will be
            used to scan for files in ``src_fs``. Set this if you only want
            to consider a sub-set of the resources in ``src_fs``.
        on_copy (callable):A function callback called after a single file copy
            is executed. Expected signature is ``(src_fs, src_path, dst_fs,
            dst_path)``.
        workers (int): Use ``worker`` threads to copy data, or ``0`` (default) for
            a single-threaded copy.

    r   r   )copy_dir_if_newerr   r   r   r   copy_fs_if_newer1   s   r!   c                 C   s`   z%| |r#d}| ||j}|dur#|||j}|du p!||kW S W dS  ty/   Y dS w )a  Determine if source file is newer than destination file.

    Arguments:
        src_fs (FS): Source filesystem (instance or URL).
        src_path (str): Path to a file on the source filesystem.
        dst_fs (FS): Destination filesystem (instance or URL).
        dst_path (str): Path to a file on the destination filesystem.

    Returns:
        bool: `True` if the source file is newer than the destination
        file or file modification time cannot be determined, `False`
        otherwise.

    ZdetailsmodifiedNT)existsZgetinfor#   r   )r   src_pathr   dst_path	namespacesrc_modifieddst_modifiedr   r   r   _source_is_newerR   s   
r*   c                 C   sR  t | dd}t |ddz}||u r|j||dd ne| Y | E ||rG||d}||| W d   n1 sAw   Y  n||}||| W d   n1 s]w   Y  W d   n1 slw   Y  W d   n1 s{w   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )a  Copy a file from one filesystem to another.

    If the destination exists, and is a file, it will be first truncated.

    Arguments:
        src_fs (FS or str): Source filesystem (instance or URL).
        src_path (str): Path to a file on the source filesystem.
        dst_fs (FS or str): Destination filesystem (instance or URL).
        dst_path (str): Path to a file on the destination filesystem.

    FZ	writeableTZcreateZ	overwritewN)r   copylock
hassyspathopenbindownloadupload)r   r%   r   r&   _src_fs_dst_fs
write_file	read_filer   r   r   	copy_fileo   s,   
 "r9   c                 C   s   | |u r| j ||dd dS ||r2||d}| || W d   dS 1 s+w   Y  dS | |}||| W d   dS 1 sIw   Y  dS )a  Low level copy, that doesn't call manage_fs or lock.

    If the destination exists, and is a file, it will be first truncated.

    This method exists to optimize copying in loops. In general you
    should prefer `copy_file`.

    Arguments:
        src_fs (FS): Source filesystem.
        src_path (str): Path to a file on the source filesystem.
        dst_fs (FS: Destination filesystem.
        dst_path (str): Path to a file on the destination filesystem.

    Tr-   r.   N)r/   r1   r2   r3   r4   )r   r%   r   r&   r7   r8   r   r   r   copy_file_internal   s   
""r:   c                 C   s  t | dd}t |dd}||u r?t||||r0|j||dd 	 W d   W d   dS 	 W d   W d   dS | Y | E t||||rrt|||| 	 W d   W d   W d   W d   dS 	 W d   W d   W d   W d   dS 1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )a  Copy a file from one filesystem to another, checking times.

    If the destination exists, and is a file, it will be first truncated.
    If both source and destination files exist, the copy is executed only
    if the source file is newer than the destination file. In case
    modification times of source or destination files are not available,
    copy is always executed.

    Arguments:
        src_fs (FS or str): Source filesystem (instance or URL).
        src_path (str): Path to a file on the source filesystem.
        dst_fs (FS or str): Destination filesystem (instance or URL).
        dst_path (str): Path to a file on the destination filesystem.

    Returns:
        bool: `True` if the file copy was executed, `False` otherwise.

    Fr+   Tr,   r-   N)r   r*   r/   r0   r:   )r   r%   r   r&   r5   r6   r   r   r   copy_file_if_newer   s8   	."r;   c                 C   s   |pt  }t| ^}t|dd?}| + |  ||D ]	}|j|dd q W d   n1 s4w   Y  W d   n1 sCw   Y  W d   n1 sRw   Y  W d   dS W d   dS 1 sjw   Y  dS )a  Copy directories (but not files) from ``src_fs`` to ``dst_fs``.

    Arguments:
        src_fs (FS or str): Source filesystem (instance or URL).
        dst_fs (FS or str): Destination filesystem (instance or URL).
        walker (~fs.walk.Walker, optional): A walker object that will be
            used to scan for files in ``src_fs``. Set this if you only
            want to consider a sub-set of the resources in ``src_fs``.

    Tr,   ZrecreateN)r   r   r0   dirsmakedir)r   r   r   r5   r6   dir_pathr   r   r   copy_structure   s   

 "r@   c                    s  |pdd }|p
t  }tt|}tt|}fdd}	 fdd}
ddlm} |	 }|
 }|  | n t||}||rE|nd	d
P}|j|dd |||D ]:\}}}t	|t
||}|D ]}|j||dd qf|D ]}||}||}||||| ||||| quqWW d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )al  Copy a directory from one filesystem to another.

    Arguments:
        src_fs (FS or str): Source filesystem (instance or URL).
        src_path (str): Path to a directory on the source filesystem.
        dst_fs (FS or str): Destination filesystem (instance or URL).
        dst_path (str): Path to a directory on the destination filesystem.
        walker (~fs.walk.Walker, optional): A walker object that will be
            used to scan for files in ``src_fs``. Set this if you only
            want to consider a sub-set of the resources in ``src_fs``.
        on_copy (callable, optional):  A function callback called after
            a single file copy is executed. Expected signature is
            ``(src_fs, src_path, dst_fs, dst_path)``.
        workers (int): Use ``worker`` threads to copy data, or ``0`` (default) for
            a single-threaded copy.

    c                  W      d S Nr   argsr   r   r   <lambda>      zcopy_dir.<locals>.<lambda>c                         t  ddS NFr+   r   r   r   r   r   src     zcopy_dir.<locals>.srcc                      rG   NTr,   r   r   r   r   r   dst  rK   zcopy_dir.<locals>.dstr   Copierr   Znum_workersTr<   N)r   r   r   _bulkrP   r0   r   r>   walkr	   r
   Z	make_pathr/   )r   r%   r   r&   r   r   r   	_src_path	_dst_pathrJ   rN   rP   r5   r6   _thread_safecopierr?   r=   files	copy_pathinfor   r   r   r   r      s<   



 Pr   c                    s$  |pdd }|p
t  }tt|}tt|}fdd}	 fdd}
ddlm} |	 }|
 }|  |  t||}||rE|nd	d
y}|j|dd d}dd ||||D }dd ||||D }|D ]M\}}t	|t
||}|jr|j|dd qm|jrz
|j}|| j}W n ty   d}Y nw |du p|du p||k}|r||||| ||||| qmW d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )ap  Copy a directory from one filesystem to another, checking times.

    If both source and destination files exist, the copy is executed only
    if the source file is newer than the destination file. In case
    modification times of source or destination files are not available,
    copy is always executed.

    Arguments:
        src_fs (FS or str): Source filesystem (instance or URL).
        src_path (str): Path to a directory on the source filesystem.
        dst_fs (FS or str): Destination filesystem (instance or URL).
        dst_path (str): Path to a directory on the destination filesystem.
        walker (~fs.walk.Walker, optional): A walker object that will be
            used to scan for files in ``src_fs``. Set this if you only
            want to consider a sub-set of the resources in ``src_fs``.
        on_copy (callable, optional):  A function callback called after
            a single file copy is executed. Expected signature is
            ``(src_fs, src_path, dst_fs, dst_path)``.
        workers (int): Use ``worker`` threads to copy data, or ``0`` (default) for
            a single-threaded copy.

    c                  W   rA   rB   r   rC   r   r   r   rE   P  rF   z#copy_dir_if_newer.<locals>.<lambda>c                      rG   rH   r   r   rI   r   r   rJ   U  rK   zcopy_dir_if_newer.<locals>.srcc                      rG   rL   r   r   rM   r   r   rN   X  rK   zcopy_dir_if_newer.<locals>.dstr   rO   r   rQ   Tr<   r"   c                 S   s   i | ]
\}}|j r||qS r   )is_file.0pathrZ   r   r   r   
<dictcomp>c  s    z%copy_dir_if_newer.<locals>.<dictcomp>c                 S   s   g | ]\}}||fqS r   r   r]   r   r   r   
<listcomp>h  s    z%copy_dir_if_newer.<locals>.<listcomp>N)r   r   r   rR   rP   r0   r   r>   rZ   r	   r
   is_dirr\   r#   KeyErrorr/   )r   r%   r   r&   r   r   r   rT   rU   rJ   rN   rP   r5   r6   rV   rW   r'   Z	dst_stateZ	src_stater?   Z	copy_inforY   r(   r)   Zdo_copyr   r[   r   r    0  s^    

 Rr    )NNr   rB   )$__doc__Z
__future__r   r   typingerrorsr   Zopenerr   r_   r   r	   r
   r   Ztoolsr   rS   r   ZTYPE_CHECKINGr   r   r   r   baser   objectZ_OnCopyr   r!   r*   r9   r:   r;   r@   r   r    r   r   r   r   <module>   sD    

!#!0

=