
    i                         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       e ej<                         ddd       edddd       edddd      fde
j>                  dede de fd       Z! e       ddddedee"ef   de de d df
d!Z#d)d"ed#ee"   d dfd$Z$	 d)d%ed&e"d"ed#ee"   d df
d'Z%d&e"d e"fd(Z&y)*    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                 L    t        | j                        }t        ||||       y)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$   s        b/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/weasel/cli/assets.pyproject_assets_clir(      s&    & 'sxx0I'	    r#   r$   returnc          	      "   t        |       }t        ||      }|j                  dg       D cg c]  }|s|j                  dt              s| }}|st	        j
                  dt         dd       t	        j                  dt        |       d	       |D ]r  }| |d
   z  j                         }|j                  d      }	d|v rd}
t        |
       |j                         rb|	r*|	t        |      k(  rt	        j                  d|d
           z|j                         rt        j                   |       n|j#                          d|d   vs|d   d   t	        j$                  dd       d|d   vs|d   d   t	        j$                  dd       t'        |d   d   |d   d   ||d   j                  d      |       t	        j                  d|        D|j                  d      }|st)        ||	       et+        ||||	       u yc c}w )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$   r   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)r4   sparseDownloaded asset url)r   r   getEXTRA_DEFAULTr   warnr   infolenresolver   r   r   goodis_dirshutilrmtreeunlinkfailr   check_private_assetfetch_asset)r   r$   r    r!   project_pathconfigassetr   r-   r.   git_errr7   s               r'   r&   r&   0   s    {+L CF ZZ"-		'=9 	F 
 %l^3op	
 HHyVY/0 ,;eFm+44699Z(E>Q  '*{{}L,> >HHDU6]OT {{}d+U5\)U5\&-A-INVW U5\)U5\&-A-I^ eV$eV$U|''1& HH(/0))E"C#D(3c4:Y,;s   !Hr-   r.   c                     t        |       j                         sd|  }t        j                  |       y|st        j                  d|         y|t        |       k(  rt        j                  d|         yt        j                  d|         y)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   rC   )r-   r.   errs      r'   rD   rD   {   sx     :OPTvVHH-dV45d++HH<TFCDHHDTFKLr)   rF   r7   c                    | |z  j                         }|j                         rx|r'|t        |      k(  rht        j                  d|        yt
        j                  j                  |      dk(  r-t        j                  d|        t        j                  |       |j                  j                         s|j                  j                  d       t        |       5  t        |      }	 t        ||       t        j                  d|        ddd       |r/|t        |      k7  r t        j,                  d
t.         d|        yyy# t        j                   $ r}t#        |      j                         rQt#        |      j%                         r8t'        j(                  |t+        |             t        j                  d|        nt        j,                  d	| |       Y d}~d}~ww xY w# 1 sw Y   xY w)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.
    r1   Nr   z1Asset exists but with size of 0 bytes, deleting: T)parentsr6   zCopied local asset zDownload failed: z(Checksum doesn't match value defined in z: )r=   r   r   r   r>   osr3   getsizer:   removeparentmkdirr   convert_asset_urlr
   httpx	HTTPErrorr   is_filer@   copystrrC   r   )rF   r7   r-   r.   	dest_pathes         r'   rE   rE      s    $--/I<	22EdVLM wwy)Q.LTFST		)$ ""$t,	\	" 8$		8#y)HH(/0	8 HY 77;L>D6RS 8x  	8Cy!d3i&7&7&9CY0.tf56,TF3Q7	88 8s1   G+*$EG(BG#G+#G((G++G4c                     t        j                  d|       rId| vrEd| vrA| j                  dd      }t        j                  dd|      }t	        j
                  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:   )r7   	converteds     r'   rS   rS      sj     	-s3s*3KK.IJ	FF+S)<	 		
 Jr)   )N)'rN   r]   r@   pathlibr   typingr   r   r   rT   typerwasabir   utilr	   r
   r   r   r   r   r   r   r   mainr   r   r   r   r9   commandcwdContextboolr(   rX   r&   rD   rE   rS    r)   r'   <module>rm      s   	 	   & &    M M E E 6 - -  *.$O HDHHJ-mvz  GL  Mz4  ?t  ueY3lm	  	
 	6 !1 2!H;H; CH~H; 	H;
 H; 
H;VMd Mhsm Mt M( IM)T)T )T(,)T8@)T	)TX3 3 r)   