o
    i'                      @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
Z
d dlmZ ddlmZmZmZmZ ddlmZmZmZ ddlmZmZ d	d
lmZmZmZmZ dZejdddddee ddddedddddedddddfde
jdede de fddZ!e dddded ee"ef de de d!df
d"d#Z#d.d$ed%ee" d!dfd&d'Z$	d.d(ed)e"d$ed%ee" d!df
d*d+Z%d)e"d!e"fd,d-Z&dS )/    N)Path)AnyDictOptional)msg   )SimpleFrozenDictdownload_fileensure_pathget_checksum)get_git_versiongit_checkoutload_project_config)parse_config_overridesworking_dir   )PROJECT_FILEArgOptappFassetsT)allow_extra_argsignore_unknown_options)context_settingsz>Path to cloned project. Defaults to current working directory.)helpexists	file_okayz--sparsez-SzsUse sparse checkout for assets provided via Git, to only check out and clone the files needed. Requires Git v22.2+.)r   z--extraz-ez7Download all assets, including those marked as 'extra'.ctxproject_dirsparse_checkoutextrac                 C   s   t | j}t||||d dS )ac  Fetch project assets like datasets and pretrained weights. Assets are
    defined in the "assets" section of the project.yml. If a checksum is
    provided in the project.yml, the file is only downloaded if no local file
    with the same checksum exists.

    DOCS: https://github.com/explosion/weasel/tree/main/docs/tutorial/directory-and-assets.md
    	overridesr   r    N)r   argsproject_assets)r   r   r   r    r"    r%   E/home/ubuntu/.local/lib/python3.10/site-packages/weasel/cli/assets.pyproject_assets_cli   s   

r'   r!   r"   returnc                   s  t | }t||d} fdd|dg D }|s$tjdt ddd td	t| d
 |D ]}| |d   }|d}	d|v rd}
t	|
d |
 rp|	rb|	t|krbtd|d   q1| rlt| n|  d|d vs~|d d du rtjddd d|d vs|d d du rtjddd t|d d |d d ||d d|d td|  q1|d}|st||	 q1t||||	 q1dS )aV  Fetch assets for a project using DVC if possible.

    project_dir (Path): Path to project directory.
    sparse_checkout (bool): Use sparse checkout for assets provided via Git, to only check out and clone the files
                            needed.
    extra (bool): Whether to download all assets, including those marked as 'extra'.
    )r"   c                    s    g | ]} s| d ts|qS r    )getEXTRA_DEFAULT).0assetr)   r%   r&   
<listcomp>@   s    
z"project_assets.<locals>.<listcomp>r   zNo assets specified in z< (if assets are marked as extra, download them with --extra)r   )exitsz	Fetching z	 asset(s)destchecksumgitzCloning Weasel project templates requires Git and the 'git' command. Make sure it's installed and that the executable is available.)error*Skipping download with matching checksum: repoNz8A git asset must include 'repo', the repository address.r   pathzFA git asset must include 'path' - use "" to get the entire repository.branch)r7   sparseDownloaded asset url)r
   r   r*   r   warnr   infolenresolver   r   r   goodis_dirshutilrmtreeunlinkfailr   check_private_assetfetch_asset)r   r"   r   r    project_pathconfigr   r-   r0   r1   git_errr:   r%   r)   r&   r$   0   sd   








r$   r0   r1   c                 C   sp   t |  sd|  }t| dS |std|   dS |t| kr.td|   dS td|   dS )a  Check and validate assets without a URL (private assets that the user
    has to provide themselves) and give feedback about the checksum.

    dest (Path): Destination path of the asset.
    checksum (Optional[str]): Optional checksum of the expected file.
    z?No URL provided for asset. You need to add this file yourself: zAsset already exists: z%Asset exists with matching checksum: z-Asset available but with incorrect checksum: N)r   r   r   r;   r?   r   rD   )r0   r1   errr%   r%   r&   rE   {   s   
rE   rG   r:   c                 C   sr  | |   }| r2|r|t|krtd|  dS ntj|dkr2td|  t	| |j
 s>|j
jdd t| V t|}zt|| td|  W n: tjjy } z,t| r}t| r}t|t| td|  n	td	| | W Y d}~nd}~ww W d   n1 sw   Y  |r|t|krtd
t d|  dS dS dS )a  Fetch an asset from a given URL or path. If a checksum is provided and a
    local file exists, it's only re-downloaded if the checksum doesn't match.

    project_path (Path): Path to project directory.
    url (str): URL or path to asset.
    checksum (Optional[str]): Optional expected checksum of local file.
    RETURNS (Optional[Path]): The path to the fetched asset or None if fetching
        the asset failed.
    r4   Nr   z1Asset exists but with size of 0 bytes, deleting: T)parentsr9   zCopied local asset zDownload failed: z(Checksum doesn't match value defined in z: )r>   r   r   r   r?   osr6   getsizer;   removeparentmkdirr   convert_asset_urlr	   requests
exceptionsRequestExceptionr   is_filerA   copystrrD   r   )rG   r:   r0   r1   	dest_pather%   r%   r&   rF      s:   



rF   c                 C   sJ   t d| r#d| vr#d| vr#| dd}t dd|}td| |S | S )	ztCheck and convert the asset URL if needed.

    url (str): The asset URL.
    RETURNS (str): The converted URL.
    z(http(s?)):\/\/github.comzreleases/downloadz/raw/z
github.comzraw.githubusercontent.comz/(tree|blob)//zDownloading from a regular GitHub URL. This will only download the source of the page, not the actual file. Converting the URL to a raw URL.)rematchreplacesubr   r;   )r:   	convertedr%   r%   r&   rQ      s   
rQ   )N)'rL   r[   rA   pathlibr   typingr   r   r   rR   typerwasabir   utilr   r	   r
   r   r   r   r   r   r   mainr   r   r   r   r+   commandcwdContextboolr'   rW   r$   rE   rF   rQ   r%   r%   r%   r&   <module>   sr    

K
,