U
    ‰±Ëh   ã                   @   s>   d 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S )
zi
This code file mainly comes from https://github.com/dmlc/gluon-cv/blob/master/gluoncv/utils/download.py
é    N)Útqdmc              	   C   sp   t  ¡ }t| dƒ"}| d¡}|s$q0| |¡ qW 5 Q R X | ¡ }tt|ƒt|ƒƒ}| ¡ d|… |d|… kS )a=  Check whether the sha1 hash of the file content matches the expected hash.
    Parameters
    ----------
    filename : str
        Path to the file.
    sha1_hash : str
        Expected sha1 hash in hexadecimal digits.
    Returns
    -------
    bool
        Whether the file content matches the expected hash.
    Úrbi   r   )ÚhashlibÚsha1ÚopenÚreadÚupdateÚ	hexdigestÚminÚlen)ÚfilenameÚ	sha1_hashr   ÚfÚdataZ	sha1_fileÚl© r   ú>/tmp/pip-unpacked-wheel-5oclok7i/insightface/utils/download.pyÚ
check_sha1
   s    
r   Fc           
   	   C   sŒ  |dkr|   d¡d }n6tj |¡}tj |¡rJtj ||   d¡d ¡}n|}|sptj |¡rp|rˆt||ƒsˆtj tj 	tj |¡¡¡}tj |¡s¢t 
|¡ td|| f ƒ tj| dd}|jdkrÖtd|  ƒ‚|j d	¡}t|d
ƒv}|dkr|jddD ]}	|	r| |	¡ qn@t|ƒ}t|jddt|d d ƒddddD ]}	| |	¡ qLW 5 Q R X |rˆt||ƒsˆtd |¡ƒ‚|S )aA  Download an given URL
    Parameters
    ----------
    url : str
        URL to download
    path : str, optional
        Destination path to store downloaded file. By default stores to the
        current directory with same name as in url.
    overwrite : bool, optional
        Whether to overwrite destination file if already exists.
    sha1_hash : str, optional
        Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified
        but doesn't match.
    Returns
    -------
    str
        The file path of the downloaded file.
    Nú/éÿÿÿÿzDownloading %s from %s...T)ÚstreaméÈ   zFailed downloading url %szcontent-lengthÚwbi   )Ú
chunk_sizeg      @g      à?ZKBF)ÚtotalÚunitZ
unit_scaleZdynamic_ncolsz»File {} is downloaded but the content hash does not match. The repo may be outdated or download may be incomplete. If the "repo_url" is overridden, consider switching to the default repo.)ÚsplitÚosÚpathÚ
expanduserÚisdirÚjoinÚexistsr   ÚdirnameÚabspathÚmakedirsÚprintÚrequestsÚgetÚstatus_codeÚRuntimeErrorÚheadersr   Úiter_contentÚwriteÚintr   ÚUserWarningÚformat)
Úurlr   Ú	overwriter   Úfnamer#   ÚrÚtotal_lengthr   Úchunkr   r   r   Údownload_file$   sJ    ÿÿ


ü
ýr7   )NFN)Ú__doc__r   r   r'   r   r   r7   r   r   r   r   Ú<module>   s   