B
    #=6\                 @   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ejd  dkrXd dlmZ d dlm	Z	 nd dl
m	Z	 d dlmZ d dlZd dlm  mZ dZdZdZdZdad	d
 Zdd Zdd Zdd Zdd Zdd ZdddZdS )    N   )urlparse)urlopenZmasterZTORCH_HUB_DIRz~/.torch/hubi    c             C   sv   t jdkr"dd l}|j| d k	S t jdkrBdd l}|| d k	S ydd l}||  W n t	k
rl   dS X dS d S )N)      r   )r   r   FT)
sysversion_infoimportlib.utilutil	find_specZimportlib.find_loaderfind_loaderimpfind_module	Exception)name	importlibr    r   ,/tmp/pip-install-l3r2oljg/torch/torch/hub.py_check_module_exists   s    

r   c             C   s2   t j| r.t j| r$t |  n
t|  d S )N)ospathexistsisfileremoveshutilrmtree)r   r   r   r   _remove_if_exists+   s    r   c             C   s   d|  d | d S )Nzhttps://github.com/z	/archive/z.zipr   )repobranchr   r   r   _git_archive_link3   s    r   c          	   C   s^   t jd| | t| }t|d.}x&|t}t|dkrBP || q*W W d Q R X d S )NzDownloading: "{}" to {}wbr   )	r   stderrwriteformatr   openreadREAD_DATA_CHUNKlen)urlfilenameresponsefdatar   r   r   _download_url_to_file7   s    
r-   c             C   s$   t | }|t|krd S t||S )N)r   import_moduledirgetattr)module_name	func_namemr   r   r   _load_attr_from_moduleB   s    
r4   c             C   s   | a dS )a6  
    Optionally set hub_dir to a local dir to save downloaded models & weights.

    If this argument is not set, env variable `TORCH_HUB_DIR` will be searched first,
    `~/.torch/hub` will be created and used as fallback.

    Args:
        d: path to a local folder to save downloaded models & weights.
    N)hub_dir)dr   r   r   set_dirJ   s    r7   Fc             O   s  t |tstdtdkr&tttadtkr:tj	tatj
tsPtt t}d| krl| d\}}n| }|d\}}t||}	tjt|d }
tjt|d | }tjt|d | }| otj
|}|rtjd	| nFt|
 t| t| t|	|
 t|
t t|
 t|| tjd
| tdd}|dk	rdd |D }t|rtdd|td|}|dkrtd|t|std||||S )a  
    Load a model from a github repo, with pretrained weights.

    Args:
        github: Required, a string with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `master` if not specified.
            Example: 'pytorch/vision[:hub]'
        model: Required, a string of entrypoint name defined in repo's hubconf.py
        force_reload: Optional, whether to discard the existing cache and force a fresh download.
            Default is `False`.
        *args: Optional, the corresponding args for callable `model`.
        **kwargs: Optional, the corresponding kwargs for callable `model`.

    Returns:
        a single model with corresponding pretrained weights.
    z8Invalid input: model should be a string of function nameN~:/z.zip-_zUsing cache found in {}r   Zhubconfdependenciesc             S   s   g | ]}t |s|qS r   )r   ).0pkgr   r   r   
<listcomp>   s    zload.<locals>.<listcomp>zMissing dependencies: {}z, z"Cannot find callable {} in hubconfz{} is not callable) 
isinstancestr
ValueErrorr5   r   getenvENV_TORCH_HUB_DIRDEFAULT_TORCH_HUB_DIRr   
expanduserr   makedirsMASTER_BRANCHsplitr   joinr   r!   r"   r#   r   r-   zipfileZipFile
extractallr   moveinsertr4   r'   RuntimeErrorcallable)ZgithubmodelZforce_reloadargskwargsr   Z	repo_infoZ
repo_owner	repo_namer(   Zcached_fileZextracted_repoZrepo_dirZ	use_cacher=   missing_depsfuncr   r   r   loadX   sN    









rY   )F)r   r   r   r   tempfilerL   r   r   urllib2r   urllib.requesturllib.parseZtorchZtorch.utils.model_zooutilsZ	model_zoorI   rE   rF   r&   r5   r   r   r   r-   r4   r7   rY   r   r   r   r   <module>   s0   