3
Á3W^X!  ã               @   sü   d dl Z d dlZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 dd„ Z
d-dd„Zdd	„ Zd.d
d„Zdd„ Zd/dd„Zd0dd„Zd1dd„Zd2dd„Zdd„ Zd3dd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd4d$d%„Zd5d&d'„Zd(d)„ Zd6d*d+„ZdS )7é    N)Útqdmc                 s   t d d�‰ ‡ fdd„} | S )N)Útotalc                s0   ˆ j d kr|r|ˆ _ | | }ˆ j|ˆ j ƒ d S )N)r   ÚupdateÚn)ÚcountÚ
block_sizeZ
total_sizeZprogress_bytes)Úpbar© úA/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/utils.pyÚ
bar_update   s    z#gen_bar_updater.<locals>.bar_update)r   )r   r	   )r   r
   Úgen_bar_updater   s    
r   i   c                sN   t jƒ }t| dƒ�.‰x&t‡ ‡fdd„dƒD ]}|j|ƒ q*W W d Q R X |jƒ S )NÚrbc                  s
   ˆj ˆ ƒS )N)Úreadr	   )Ú
chunk_sizeÚfr	   r
   Ú<lambda>   s    zcalculate_md5.<locals>.<lambda>ó    )ÚhashlibÚmd5ÚopenÚiterr   Ú	hexdigest)Úfpathr   r   Úchunkr	   )r   r   r
   Úcalculate_md5   s
    r   c             K   s   |t | f|ŽkS )N)r   )r   r   Úkwargsr	   r	   r
   Ú	check_md5!   s    r   c             C   s&   t jj| ƒsdS |d krdS t| |ƒS )NFT)ÚosÚpathÚisfiler   )r   r   r	   r	   r
   Úcheck_integrity%   s
    r    c             C   sF   yt j| ƒ W n2 tk
r@ } z|jtjkr.n‚ W Y dd}~X nX dS )z<
    Python2 support for os.makedirs(.., exist_ok=True)
    N)r   ÚmakedirsÚOSErrorÚerrnoÚEEXIST)ÚdirpathÚer	   r	   r
   Úmakedir_exist_ok-   s    r'   c             C   sü   ddl m} tjj|ƒ}|s(tjj| ƒ}tjj||ƒ}t|ƒ t||ƒrVt	d| ƒ n¢y,t	d|  d | ƒ |j
j| |tƒ d� W nt |jjtfk
rö } zP| dd… d	krâ| jd
dƒ} t	d|  d | ƒ |j
j| |tƒ d� n|‚W Y dd}~X nX dS )a\  Download a file from a url and place it in root.

    Args:
        url (str): URL to download file from
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the basename of the URL
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   )Úurllibz$Using downloaded and verified file: zDownloading z to )Ú
reporthookNé   Úhttpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading )Z	six.movesr(   r   r   Ú
expanduserÚbasenameÚjoinr'   r    ÚprintÚrequestÚurlretriever   ÚerrorÚURLErrorÚIOErrorÚreplace)ÚurlÚrootÚfilenamer   r(   r   r&   r	   r	   r
   Údownload_url:   s,    	
r9   Fc                sF   t jjˆ ƒ‰ tt‡ fdd„t jˆ ƒƒƒ}|dkrB‡ fdd„|D ƒ}|S )a  List all directories at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the directories found
    c                s   t jjt jjˆ | ƒƒS )N)r   r   Úisdirr.   )Úp)r7   r	   r
   r   n   s    zlist_dir.<locals>.<lambda>Tc                s   g | ]}t jjˆ |ƒ‘qS r	   )r   r   r.   )Ú.0Úd)r7   r	   r
   ú
<listcomp>t   s    zlist_dir.<locals>.<listcomp>)r   r   r,   ÚlistÚfilterÚlistdir)r7   ÚprefixÚdirectoriesr	   )r7   r
   Úlist_dirc   s    
rD   c                sH   t jjˆ ƒ‰ tt‡ ‡fdd„t jˆ ƒƒƒ}|dkrD‡ fdd„|D ƒ}|S )a¾  List all files ending with a suffix at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
            It uses the Python "str.endswith" method and is passed directly
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the files found
    c                s    t jjt jjˆ | ƒƒo| jˆƒS )N)r   r   r   r.   Úendswith)r;   )r7   Úsuffixr	   r
   r   †   s    zlist_files.<locals>.<lambda>Tc                s   g | ]}t jjˆ |ƒ‘qS r	   )r   r   r.   )r<   r=   )r7   r	   r
   r>   Œ   s    zlist_files.<locals>.<listcomp>)r   r   r,   r?   r@   rA   )r7   rF   rB   Úfilesr	   )r7   rF   r
   Ú
list_filesy   s    
rH   c             C   sª   ddl }d}tjj|ƒ}|s | }tjj||ƒ}t|ƒ tjj|ƒrZt||ƒrZtd| ƒ nL|j	ƒ }|j
|d| idd�}t|ƒ}	|	rœ| |	dœ}
|j
||
dd�}t||ƒ dS )	af  Download a Google Drive file from  and place it in root.

    Args:
        file_id (str): id of file to be downloaded
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the id of the file.
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   Nz*https://docs.google.com/uc?export=downloadz$Using downloaded and verified file: ÚidT)ÚparamsÚstream)rI   Zconfirm)Úrequestsr   r   r,   r.   r'   r   r    r/   ÚSessionÚgetÚ_get_confirm_tokenÚ_save_response_content)Zfile_idr7   r8   r   rL   r6   r   ÚsessionÚresponseÚtokenrJ   r	   r	   r
   Údownload_file_from_google_drive‘   s     

rT   c             C   s*   x$| j jƒ D ]\}}|jdƒr|S qW d S )NZdownload_warning)ÚcookiesÚitemsÚ
startswith)rR   ÚkeyÚvaluer	   r	   r
   rO   ´   s    
rO   é €  c             C   sn   t |dƒ�Z}td d�}d}x<| j|ƒD ].}|r&|j|ƒ |t|ƒ7 }|j||j ƒ q&W |jƒ  W d Q R X d S )NÚwb)r   r   )r   r   Úiter_contentÚwriteÚlenr   r   Úclose)rR   Zdestinationr   r   r   Úprogressr   r	   r	   r
   rP   ¼   s    

rP   c             C   s
   | j dƒS )Nz.tar)rE   )r8   r	   r	   r
   Ú_is_tarÈ   s    ra   c             C   s
   | j dƒS )Nz.tar.gz)rE   )r8   r	   r	   r
   Ú	_is_targzÌ   s    rb   c             C   s   | j dƒo| j dƒ S )Nz.gzz.tar.gz)rE   )r8   r	   r	   r
   Ú_is_gzipÐ   s    rc   c             C   s
   | j dƒS )Nz.zip)rE   )r8   r	   r	   r
   Ú_is_zipÔ   s    rd   c             C   s&  |d krt jj| ƒ}t| ƒrBtj| dƒ�}|j|d� W d Q R X nÐt| ƒrptj| dƒ�}|j|d� W d Q R X n¢t| ƒrÖt jj	|t jj
t jj| ƒƒd ƒ}t|dƒ�*}tj| ƒ�}|j|jƒ ƒ W d Q R X W d Q R X n<t| ƒ�rtj| dƒ�}|j|ƒ W d Q R X ntdj| ƒƒ‚|�r"t j| ƒ d S )NÚr)r   zr:gzr   r[   zExtraction of {} not supported)r   r   Údirnamera   Útarfiler   Ú
extractallrb   rc   r.   Úsplitextr-   ÚgzipÚGzipFiler]   r   rd   ÚzipfileÚZipFileÚ
ValueErrorÚformatÚremove)Ú	from_pathZto_pathÚremove_finishedÚtarZout_fZzip_fÚzr	   r	   r
   Úextract_archiveØ   s$    "$
ru   c             C   sd   t jj|ƒ}|d kr|}|s(t jj| ƒ}t| |||ƒ t jj||ƒ}tdj||ƒƒ t|||ƒ d S )NzExtracting {} to {})	r   r   r,   r-   r9   r.   r/   ro   ru   )r6   Zdownload_rootZextract_rootr8   r   rr   Úarchiver	   r	   r
   Údownload_and_extract_archiveð   s    rw   c             C   s   ddj dd„ | D ƒƒ d S )Nú'z', 'c             S   s   g | ]}t |ƒ‘qS r	   )Ústr)r<   Úitemr	   r	   r
   r>      s    z#iterable_to_str.<locals>.<listcomp>)r.   )Úiterabler	   r	   r
   Úiterable_to_strÿ   s    r|   c             C   s€   t | tjjƒs:|d krd}nd}|jt| ƒ|d�}t|ƒ‚|d krF| S | |kr||d k	r\|}nd}|j| |t|ƒd�}t|ƒ‚| S )Nz'Expected type str, but got type {type}.z:Expected type str for argument {arg}, but got type {type}.)ÚtypeÚargzPUnknown value '{value}' for argument {arg}. Valid values are {{{valid_values}}}.)rY   r~   Úvalid_values)Ú
isinstanceÚtorchZ_sixZstring_classesro   r}   rn   r|   )rY   r~   r   Z
custom_msgÚmsgr	   r	   r
   Úverify_str_arg  s     rƒ   é   )r„   )N)NN)F)F)NN)rZ   )NF)NNNF)NNN)r   Úos.pathr   rj   r#   rg   rl   r�   Ztorch.utils.model_zoor   r   r   r   r    r'   r9   rD   rH   rT   rO   rP   ra   rb   rc   rd   ru   rw   r|   rƒ   r	   r	   r	   r
   Ú<module>   s8   


)


#

 
