o
    #j,                     @   s  d dl Z d dlZd dlm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
zd dlmZ W n   G dd dZY d dlZeeZdgZedZdZdd	 Zd$d
dZdd Zdd Z	d%ddZdd ZdeiZd&ddZd$ddZdd Zdd Zd'ddZd d! Z d"d# Z!dS )(    N)tqdmc                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r   Nc                 C   s   || _ d| _d S )Nr   )totaln)selfr    r   V/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/utils/download.py__init__   s   
ztqdm.__init__c                 C   sf   |  j |7  _ | jd u rtjd| j dd ntjdd| j  t| j dd tj  d S )Nz.1fz bytesd   %)r   r   sysstderrwritefloatflush)r   r   r   r   r   update#   s
   
&ztqdm.updatec                 C   s   | S Nr   )r   r   r   r   	__enter__+   s   ztqdm.__enter__c                 C   s   t jd d S )N
)r   r   r   )r   exc_typeexc_valexc_tbr   r   r   __exit__.   s   ztqdm.__exit__r   )__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s
    
r   get_weights_path_from_urlz~/.cache/paddle/hapi/weights   c                 C   s   |  dp	|  dS )zR
    Whether path is URL.
    Args:
        path (string): URL string or not.
    zhttp://zhttps://)
startswith)pathr   r   r   is_url=   s   r    c                 C   s   t | t|}|S )a:  Get weights path from WEIGHT_HOME, if not exists,
    download it from url.

    Args:
        url (str): download url
        md5sum (str): md5 sum of download package

    Returns:
        str: a local path to save downloaded weights.

    Examples:
        .. code-block:: python

            >>> from paddle.utils.download import get_weights_path_from_url

            >>> resnet18_pretrained_weight_url = 'https://paddle-hapi.bj.bcebos.com/models/resnet18.pdparams'
            >>> local_weight_path = get_weights_path_from_url(resnet18_pretrained_weight_url)

    )get_path_from_urlWEIGHTS_HOME)urlmd5sumr   r   r   r   r   F   s   c                 C   s   t | d }|}t ||S )N)ospsplitjoin)r#   root_dirfnameZfpathr   r   r   	_map_path^   s   r+   c                 C   s^   |    t }t }| D ]}|dd }||v rq|| || qtd|  |S )N:r   zunique_endpoints )sortsetr'   addloggerinfo)trainer_endpointsipsunique_endpointsZendpointipr   r   r   _get_unique_endpointse   s   
r6   Tgetc           	      C   s   ddl m} t| sJ d|  dt| |}t| jdd }t|r6|r6t||r6t	
d|  n | j|v rEt| |||d}ntj|sVtd tj|rK| j|v rl|rlt|sht|rlt|}|S )	a  Download from given url to root_dir.
    if file or directory specified by url is exists under
    root_dir, return the path directly, otherwise download
    from url and decompress it, return the path.

    Args:
        url (str): download url
        root_dir (str): root dir for downloading, it should be
                        WEIGHTS_HOME or DATASET_HOME
        md5sum (str): md5 sum of download package
        decompress (bool): decompress zip or tar file. Default is `True`
        method (str): which download method to use. Support `wget` and `get`. Default is `get`.

    Returns:
        str: a local path to save downloaded models & weights & datasets.
    r   )ParallelEnvzdownloading from z
 not a urlNzFound )method   )Zpaddle.distributedr8   r    r+   r6   r2   r&   exists	_md5checkr0   r1   Zcurrent_endpoint	_downloadosr   timesleeptarfile
is_tarfilezipfile
is_zipfile_decompress)	r#   r)   r$   Zcheck_exist
decompressr9   r8   fullpathr4   r   r   r   r!   t   s&   

r!   c           
      C   s~  t |}ztjd| d dd}|jdkr!td|  d|j d|d }|jd	}t|d
I}|rbt	t
|d d d}|jddD ]}|| |d qEW d    n1 s\w   Y  n|jddD ]	}|rq|| qhW d    n1 s|w   Y  t|| |W  d    W S 1 sw   Y  W d S  ty }	 ztd| d|  dt|	  W Y d }	~	dS d }	~	ww )NGETT)timeoutZfollow_redirects   zDownloading from z failed with code !Z_tmpzcontent-lengthwbi  i   )r   )
chunk_sizer:   Downloading  from z failed with exception F)r&   basenamehttpxstreamstatus_codeRuntimeErrorheadersr7   openr   intZ
iter_bytesr   r   shutilmove	Exceptionr0   r1   str)
r#   fullnamer*   reqZtmp_fullnameZ
total_sizefZpbarchunker   r   r   _get_download   sL   




(ra   c                 C   s   |t v sJ d| dt|st| t| d }t||}d}td| d|   t|r:t	||smtd| d|  |t
k rN|d	7 }ntd
|  dt | | |sctd	 q0t|r:t	||r:|S )z
    Download from url, save to path.

    url (str): download url
    path (str): download to given path
    md5sum (str): md5 sum of download package
    method (str): which download method to use. Support `wget` and `get`. Default is `get`.

    zmake sure `z` implementedr%   r   rN   rO   z	md5check z and r:   zDownload from z failed. Retry limit reached)_download_methodsr&   r;   r>   makedirsr'   r(   r0   r1   r<   DOWNLOAD_RETRY_LIMITrT   r?   r@   )r#   r   r$   r9   r*   r\   Z	retry_cntr   r   r   r=      s&   





r=   c              	      s   |d u rdS t d|  d t }t| d t fdddD ]}|| q"W d    n1 s4w   Y  | }||krRt d|  d| d	| d
 dS dS )NTzFile z md5 checking...rbc                      s
     dS )Ni   )readr   r^   r   r   <lambda>   s   
 z_md5check.<locals>.<lambda>    z md5 check failed, z
(calc) != z(base)F)r0   r1   hashlibmd5rV   iterr   	hexdigest)r\   r$   rk   r_   Zcalc_md5sumr   rg   r   r<      s$   r<   c                 C   sL   t d|  d t| rt| }|S t| rt| }|S td|  )z)
    Decompress for zip and tar file
    zDecompressing z...zUnsupport compress file type )	r0   r1   rA   rB   _uncompress_file_tarrC   rD   _uncompress_file_zip	TypeError)r*   uncompressed_pathr   r   r   rE     s   

rE   c                 C   s0  t | d}| }g }|D ]}||dd qtj| }t|r6|d }tj	||}|
| nOt|r[tj|d tjd tjd }tj	||}|
| n*tj| d tjd }tj	||}tj|s{t| |
tj	|| |W  d    S 1 sw   Y  d S )Nr../ r   r%   )rC   ZipFilenamelistappendreplacer>   r   dirname_is_a_single_filer(   
extractall_is_a_single_dirsplitextstripsepr'   r;   rc   )filepathfilesfile_list_tmp	file_listfilefile_dirrootpathrq   r   r   r   ro     s2   
$ro   r:*c           	      C   sL  t | |}| }g }|D ]}|d dksJ d| d||dd qtj| }t|rD|d }tj	||}|
| nOt|ritj|d tjd tjd }tj	||}|
| n*tj| d tjd }tj	||}tj|st| |
tj	|| |W  d    S 1 sw   Y  d S )Nr   /zuncompress file path z should not start with /rs   rt   r%   )rA   rV   getnamesrw   rx   r>   r   ry   rz   r(   r{   r|   r}   r~   r   r'   r;   rc   )	r   moder   r   r   r   r   r   rq   r   r   r   rn   6  s8   

$rn   c                 C   s(   t | dkr| d tjdk rdS dS )Nr:   r   TF)lenfindr>   r   )r   r   r   r   rz   W  s    rz   c                 C   s   g }| D ]}d|v r| dtj}nd|v r| dtj}|| q|d tjd }tdt|D ]}||| tjd krE dS q4dS )Nr   \r   r:   FT)rx   r>   r   rw   r'   ranger   )r   Znew_file_list	file_path	file_nameir   r   r   r|   ]  s   r|   r   )NTTr7   )Nr7   )r   )"rj   r>   Zos.pathr   r&   rX   r   rA   r?   rC   rQ   r   logging	getLoggerr   r0   __all__
expanduserr"   rd   r    r   r+   r6   r!   ra   rb   r=   r<   rE   ro   rn   rz   r|   r   r   r   r   <module>   sB   


	
/#

$
 !