o
    xi&                     @  sj  U d 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m	Z	 ddl
mZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1 erddl2m3Z3 e4e5Z6dZ7de8d< d%ddZ9d&ddZ:d'd!d"Z;G d#d$ d$e1Z<dS )(zArtifact manifest entry.    )annotationsN)suppress)getsize)TYPE_CHECKING	AnnotatedAnyDictFinalOptionalUnion)urlparse)FieldNonNegativeInt)Self)field_validatormodel_validator)nameof)
Deprecated)warn_and_record_deprecation)copy_or_overwrite_changed)B64MD5ETagb64_to_hex_idhex_to_b64_idmd5_file_b64)FilePathStrLogicalPathURIStr   )ArtifactsBase)Artifactzwandb-artifactz
Final[str]_WB_ARTIFACT_SCHEME	file_pathstrreturnc                 C  sT   ddl m} tj| }t|  }| d }|j	ddd t
|| d S )z1Get path for checksum in central cache directory.r   )artifacts_cache_dir	checksumsT)parentsexist_okz	.checksum)'wandb.sdk.artifacts.artifact_file_cacher%   ospathabspathhashlibsha256encode	hexdigestmkdirr#   )r"   r%   abs_path	path_hash	cache_dir r5   _/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/artifacts/artifact_manifest_entry.py_checksum_cache_path.   s   
r7   
str | Nonec              	   C  s   t | }zYt| J}t|4}tj|jtj|jk r-	 W d   W d   W dS |  W  d   W  d   W S 1 sEw   Y  W d   W dS 1 sVw   Y  W dS  tyg   Y dS w )z3Read checksum from cache if it exists and is valid.N)	r7   openr*   r+   getmtimenamereadstripOSError)r"   checksum_pathf
f_checksumr5   r5   r6   _read_cached_checksum=   s   
XrB   checksumNonec                 C  sl   t | }zt|d}|| W d   W dS 1 sw   Y  W dS  ty5   td|  Y dS w )z"Write checksum to cache directory.wNz#Failed to write checksum cache for )r7   r9   writer>   loggerdebug)r"   rC   r?   r@   r5   r5   r6   _write_cached_checksumM   s   &rI   c                   @  s.  e Zd ZU dZded< ded< dZded< dZd	ed
< dZded< ee	dZ
ded< dZded< dZded< dZded< dZded< eddddAddZeddddBd d!Zed"ddCd$d%ZdDd'd(ZedEd)d*ZdFd,d-Z			dGdHd3d4ZdId6d7ZdDd8d9ZdJd;d<ZdKd=d>ZdLd?d@ZdS )MArtifactManifestEntryzoA single entry in an artifact manifest.

    External code should avoid instantiating this class directly.
    r   r+   z(Union[B64MD5, ETag, URIStr, FilePathStr]digestNz Union[URIStr, FilePathStr, None]refz8Annotated[Optional[str], Field(alias='birthArtifactID')]birth_artifact_idzOptional[NonNegativeInt]size)default_factoryzDict[str, Any]extrazOptional[str]
local_pathFbool
skip_cachezOptional[Artifact]_parent_artifact_download_urlbefore)modevr   r$   c                 C  s   t |S )a  Coerce `path` to a LogicalPath.

        LogicalPath does not implement its own pydantic validator, and adding
        one for both pydantic V1 and V2 would add excessive boilerplate. Until
        we drop V1 support, coerce to LogicalPath in this field validator.
        )r   clsrX   r5   r5   r6   _validate_pathn   s   z$ArtifactManifestEntry._validate_pathr8   c                 C  s   |rt |S dS )zFCoerce `local_path` to a str. Necessary if the input is a `PosixPath`.N)r#   rY   r5   r5   r6   _validate_local_pathx   s   z*ArtifactManifestEntry._validate_local_pathafterr   c                 C  s    | j du r| jrt| j| _ | S )z7If `size` isn't set, try to infer it from `local_path`.N)rN   rQ   r   selfr5   r5   r6   _infer_size_from_local_path}   s   z1ArtifactManifestEntry._infer_size_from_local_pathr#   c                 C  sL   | j rd ndh}| jdd|d}tt|  dddd | D  d	S )
NrP   FT)by_aliasexclude_noneexclude(z, c                 s  s"    | ]\}}| d |V  qdS )=Nr5   ).0krX   r5   r5   r6   	<genexpr>   s     z1ArtifactManifestEntry.__repr__.<locals>.<genexpr>))rP   
model_dumpr   typejoinitems)r_   rc   	repr_dictr5   r5   r6   __repr__   s   ,zArtifactManifestEntry.__repr__c                 C  s   t tdddd | jS )zDeprecated; use `path` instead.T)artifactmanifestentry__namez<ArtifactManifestEntry.name is deprecated, use .path instead.)featuremessage)r   r   r+   r^   r5   r5   r6   r;      s
   zArtifactManifestEntry.namer    c                 C  s   | j du rt| j S )zGet the artifact to which this artifact entry belongs.

        Returns:
            (PublicArtifact): The parent artifact
        N)rT   NotImplementedErrorr^   r5   r5   r6   parent_artifact   s   
z%ArtifactManifestEntry.parent_artifactrootbool | Noneexecutor"concurrent.futures.Executor | Noner   c              	   C  s(  |   }||}tj|| j}tt | jt|kr(t	|W  d   S W d   n1 s2w   Y  zt
|}W n ttfyQ   td|d Y nw t|| | j|kr`t	|S |rft	|nd}|jj}	| jdurz|	j| d|d}
n	|	j|| ||d}
t	|pt|
|}t|| j |S )a+  Download this artifact entry to the specified root path.

        Args:
            root: (str, optional) The root path in which to download this
                artifact entry. Defaults to the artifact's root.

        Returns:
            (str): The path of the downloaded artifact entry.
        Nzunable to find z, skip searching for fileT)local	dest_path)rz   rw   )rt   _add_download_rootr*   r+   rl   r   r>   rK   rB   r   r   FileNotFoundErrorIsADirectoryErrorrG   rH   rI   manifeststorage_policyrL   load_reference	load_filer   )r_   ru   rS   rw   artifactrootdirrz   md5_hashoverride_cache_pathr   
cache_path
final_pathr5   r5   r6   download   s>   




zArtifactManifestEntry.downloadFilePathStr | URIStrc                 C  s8   | j du r	td| j }du r| j S |jjj| ddS )zGet the reference URL that is targeted by this artifact entry.

        Returns:
            (str): The reference URL of this artifact entry.

        Raises:
            ValueError: If this artifact entry was not a reference.
        Nz,Only reference entries support ref_target().F)ry   )rL   
ValueErrorrT   r~   r   r   )r_   rt   r5   r5   r6   
ref_target   s
   
	z ArtifactManifestEntry.ref_targetc                 C  sH   |    }du rtd|j }du rtdt dt| d| j S )aw  Get a URL to this artifact entry.

        These URLs can be referenced by another artifact.

        Returns:
            (str): A URL representing this artifact entry.

        Examples:
            Basic usage
            ```
            ref_url = source_artifact.get_entry("file.txt").ref_url()
            derived_artifact.add_reference(ref_url)
            ```
        NzParent artifact is not setzParent artifact ID is not setz:///)rt   r   idr!   r   r+   )r_   rt   	parent_idr5   r5   r6   ref_url   s
   zArtifactManifestEntry.ref_urldict[str, Any]c                 C  s   | j ddS )NT)rb   )rj   r^   r5   r5   r6   to_json   s   zArtifactManifestEntry.to_jsonc                 C  s   | j d uot| j jtkS N)rL   r   schemer!   r^   r5   r5   r6   _is_artifact_reference  s   z,ArtifactManifestEntry._is_artifact_referencec                 C  s   |   sd S tt| jjS r   )r   r   r   rL   netlocr^   r5   r5   r6   _referenced_artifact_id  s   z-ArtifactManifestEntry._referenced_artifact_id)rX   r   r$   r   )rX   r   r$   r8   )r$   r   )r$   r#   )r$   r   )r$   r    )NNN)ru   r8   rS   rv   rw   rx   r$   r   )r$   r   )r$   r   )r$   rR   )r$   r8   )__name__
__module____qualname____doc____annotations__rL   rM   rN   r   dictrP   rQ   rS   rT   rU   r   r[   r\   r   r`   ro   propertyr;   rt   r   r   r   r   r   r   r5   r5   r5   r6   rJ   X   s>   
 

	


<


rJ   )r"   r#   r$   r#   )r"   r#   r$   r8   )r"   r#   rC   r#   r$   rD   )=r   
__future__r   concurrent.futures
concurrentr-   loggingr*   
contextlibr   os.pathr   typingr   r   r   r   r	   r
   r   urllib.parser   pydanticr   r   typing_extensionsr   wandb._pydanticr   r   wandb._strutilsr   wandb.proto.wandb_telemetry_pb2r   wandb.sdk.lib.deprecationr   wandb.sdk.lib.filesystemr   wandb.sdk.lib.hashutilr   r   r   r   r   wandb.sdk.lib.pathsr   r   r   _models.base_modelr   r   r    	getLoggerr   rG   r!   r   r7   rB   rI   rJ   r5   r5   r5   r6   <module>   s8    $



