§
    ÎÑßjž(  ã                  óö   — 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ZddlZddl	m
Z
 ddlmZmZmZmZmZmZ e
rddlmZ eej        e         z  Zdej        v Z ej        e¦  «        Zdd„Z G d„ d¦  «        ZdS )z9
Download utility class for fetching build dependencies.
é    )ÚannotationsN)ÚTYPE_CHECKING)ÚChecksumValidationErrorÚConfigurationErrorÚRelenvExceptionÚdownload_urlÚget_download_locationÚruncmd)ÚCallableÚCIÚfileÚPathLikeÚchecksumú
str | NoneÚreturnÚboolc                ó(  — |€t                                d¦  «         dS t          |¦  «        dk    rt          j        ¦   «         }d}nIt          |¦  «        dk    rt          j        ¦   «         }d}n t          dt          |¦  «        › d	�¦  «        ‚t          | d
¦  «        5 }|                     | 	                    ¦   «         ¦  «         | 
                    ¦   «         }||k    rt          |› d|› d|› �¦  «        ‚	 ddd¦  «         n# 1 swxY w Y   dS )aõ  
    Verify the checksum of a file.

    Supports both SHA-1 (40 hex chars) and SHA-256 (64 hex chars) checksums.
    The hash algorithm is auto-detected based on checksum length.

    :param file: The path to the file to check.
    :type file: str
    :param checksum: The checksum to verify against (SHA-1 or SHA-256)
    :type checksum: str

    :raises RelenvException: If the checksum verification failed

    :return: True if it succeeded, or False if the checksum was None
    :rtype: bool
    Nz,Can't verify checksum because none was givenFé@   Úsha256é(   Úsha1zInvalid checksum length z%. Expected 40 (SHA-1) or 64 (SHA-256)Úrbz( checksum verification failed. expected=z found=T)ÚlogÚerrorÚlenÚhashlibr   r   r   ÚopenÚupdateÚreadÚ	hexdigest)r   r   Ú	hash_algoÚ	hash_nameÚfpÚfile_checksums         úP/opt/saltstack/salt/lib/python3.11/site-packages/relenv/build/common/download.pyÚverify_checksumr&   &   s^  € ð" ÐÝ�	Š	Ð@ÑAÔAÐAØˆuõ ˆ8�}„}˜ÒÐÝ”NÑ$Ô$ˆ	Øˆ	ˆ	Ý	ˆX‰Œ˜"Ò	Ð	Ý”L‘N”Nˆ	Øˆ	ˆ	å%Ð&uÅÀXÁÄÐ&uÐ&uÐ&uÑvÔvÐvå	ˆd�DÑ	Ô	ð ˜RØ×Ò˜Ÿš™œÑ#Ô#Ð#Ø!×+Ò+Ñ-Ô-ˆØ�}Ò$Ð$Ý)ØÐfÐfÀhÐfÐfÐWdÐfÐfñô ð ð %ðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð ˆ4s   Â!ADÄDÄDc                  óF  — e Zd ZdZ	 	 	 	 	 d,d-d„Zd.d„Zed/d„¦   «         Zej        d0d„¦   «         Zed1d„¦   «         Z	ed2d„¦   «         Z
ed1d„¦   «         Zed/d„¦   «         Zed1d„¦   «         Zd3d4d„Zd3d5d „Zd6d"„Zd7d#„Zed8d%„¦   «         Zed9d&„¦   «         Z	 	 	 	 d:d;d+„ZdS )<ÚDownloada  
    A utility that holds information about content to be downloaded.

    :param name: The name of the download
    :type name: str
    :param url: The url of the download
    :type url: str
    :param signature: The signature of the download, defaults to None
    :type signature: str
    :param destination: The path to download the file to
    :type destination: str
    :param version: The version of the content to download
    :type version: str
    :param sha1: The sha1 sum of the download
    :type sha1: str

    NÚ ÚnameÚstrÚurlÚfallback_urlr   Ú	signatureÚdestinationr   Úversionr   r   ÚNonec                óÀ   — || _         || _        || _        || _        t	          j        ¦   «         | _        |rt	          j        |¦  «        | _        || _        || _        d S ©N)	r*   Úurl_tplÚfallback_url_tplÚsignature_tplÚpathlibÚPathÚ_destinationr0   r   )Úselfr*   r,   r-   r.   r/   r0   r   s           r%   Ú__init__zDownload.__init__c   s\   € ð ˆŒ	ØˆŒØ ,ˆÔØ&ˆÔÝ*1¬,©.¬.ˆÔØð 	:Ý '¤¨[Ñ 9Ô 9ˆDÔØˆŒØ ˆŒˆˆó    c           	     ór   — t          | j        | j        | j        | j        | j        | j        | j        ¦  «        S )z(Create a copy of this Download instance.)r(   r*   r4   r5   r6   r/   r0   r   ©r:   s    r%   ÚcopyzDownload.copyw   s:   € åØŒIØŒLØÔ!ØÔØÔØŒLØŒMñ
ô 
ð 	
r<   úpathlib.Pathc                ó   — | j         S )z#Get the destination directory path.)r9   r>   s    r%   r/   zDownload.destinationƒ   s   € ð Ô Ð r<   ÚvalueúPathLike | Nonec                óp   — |rt          j        |¦  «        | _        dS t          j        ¦   «         | _        dS )z#Set the destination directory path.N)r7   r8   r9   )r:   rB   s     r%   r/   zDownload.destinationˆ   s8   € ð ð 	/Ý '¤¨UÑ 3Ô 3ˆDÔÐÐå '¤¡¤ˆDÔÐÐr<   c                óB   — | j                              | j        ¬¦  «        S )zGet the formatted download URL.©r0   ©r4   Úformatr0   r>   s    r%   r,   zDownload.url�   ó   € ð Œ|×"Ò"¨4¬<Ð"Ñ8Ô8Ð8r<   c                óT   — | j         r | j                              | j        ¬¦  «        S dS )z-Get the formatted fallback URL if configured.rF   N)r5   rH   r0   r>   s    r%   r-   zDownload.fallback_url•   s1   € ð Ô ð 	FØÔ(×/Ò/¸¼Ð/ÑEÔEÐEØˆtr<   c                ón   — | j         €t          d¦  «        ‚| j                              | j        ¬¦  «        S )z Get the formatted signature URL.Nz!Signature template not configuredrF   )r6   r   rH   r0   r>   s    r%   Úsignature_urlzDownload.signature_urlœ   s8   € ð ÔÐ%Ý$Ð%HÑIÔIÐIØÔ!×(Ò(°´Ð(Ñ>Ô>Ð>r<   c                óR   — | j                              dd¦  «        \  }}| j        |z  S )z8Get the full file path where the download will be saved.ú/é   )r,   Úrsplitr/   )r:   Ú_r*   s      r%   ÚfilepathzDownload.filepath£   s+   € ð ”(—/’/ # qÑ)Ô)‰ˆˆ4ØÔ $Ñ&Ð&r<   c                óB   — | j                              | j        ¬¦  «        S )z/Get the formatted URL (alias for url property).rF   rG   r>   s    r%   Úformatted_urlzDownload.formatted_url©   rI   r<   Úprogress_callbackú!Callable[[int, int], None] | Noneútuple[str, bool]c                ó  — 	 t          | j        | j        t          |¬¦  «        dfS # t          $ rN}| j        }|r?t          d| j        › d|› d�¦  «         t          || j        t          |¬¦  «        dfcY d}~S ‚ d}~ww xY w)a3  
        Download the file.

        :param progress_callback: Optional callback(downloaded_bytes, total_bytes)
        :type progress_callback: Optional[Callable[[int, int], None]]
        :return: The path to the downloaded content, and whether it was downloaded.
        :rtype: tuple(str, bool)
        )rU   TzDownload failed z (z); trying fallback urlN)r   r,   r/   ÚCICDÚ	Exceptionr-   Úprint)r:   rU   ÚexcÚfallbacks       r%   Ú
fetch_filezDownload.fetch_file®   sÜ   € ð	åØ”HØÔ$ÝØ&7ð	ñ ô ð ðð øõ ð 	ð 	ð 	ØÔ(ˆHØð 
ÝÐP¨¬ÐPÐP°SÐPÐPÐPÑQÔQÐQå Ø ØÔ(ÝØ*;ð	ñ ô ð ðð ð ð ð ð ð ð øøøøð	øøøs!   ‚#& ¦
A>°AA9Á2A>Á8A9Á9A>c                óF   — t          | j        | j        t          ¦  «        dfS )zz
        Download the file signature.

        :return: The path to the downloaded signature.
        :rtype: str
        T)r   rL   r/   rY   )r:   r0   s     r%   Úfetch_signaturezDownload.fetch_signatureÐ   s!   € õ ˜DÔ.°Ô0@Å$ÑGÔGÈÐMÐMr<   r   c                ó4   — | j                              ¦   «         S )z•
        True when the artifact already exists on disk.

        :return: True when the artifact already exists on disk
        :rtype: bool
        )rR   Úexistsr>   s    r%   rb   zDownload.existsÙ   s   € ð Œ}×#Ò#Ñ%Ô%Ð%r<   c                ó   — dS )z>Validate the hash of the downloaded file (placeholder method).N© r>   s    r%   Ú
valid_hashzDownload.valid_hashâ   s   € àˆr<   Úarchivec                ó   — |€t                                d¦  «         dS 	 t          dd|| gt          j        t          j        ¬¦  «         dS # t
          $ r'}t                                d| |¦  «         Y d}~dS d}~ww xY w)	aK  
        True when the archive's signature is valid.

        :param archive: The path to the archive to validate
        :type archive: str
        :param signature: The path to the signature to validate against
        :type signature: str

        :return: True if it validated properly, else False
        :rtype: bool
        Nz,Can't check signature because none was givenFÚgpgz--verify)ÚstderrÚstdoutTz%Signature validation failed on %s: %s)r   r   r
   Ú
subprocessÚPIPEr   )rf   r.   r\   s      r%   Úvalidate_signaturezDownload.validate_signatureæ   sž   € ð ÐÝ�IŠIÐDÑEÔEÐEØ�5ð		ÝØ˜
 I¨wÐ7Ý!”Ý!”ðñ ô ð ð
 �4øÝð 	ð 	ð 	Ý�IŠIÐ=¸wÈÑLÔLÐLØ�5�5�5�5�5øøøøð	øøøs    *A Á
A=ÁA8Á8A=c                ó�   — 	 t          | |¦  «         dS # t          $ r'}t                               d| |¦  «         Y d}~dS d}~ww xY w)a<  
        True when when the archive matches the sha1 hash.

        :param archive: The path to the archive to validate
        :type archive: str
        :param checksum: The sha1 sum to validate against
        :type checksum: str
        :return: True if the sums matched, else False
        :rtype: bool
        Tz sha1 validation failed on %s: %sNF)r&   r   r   r   )rf   r   r\   s      r%   Úvalidate_checksumzDownload.validate_checksum  s_   € ð	Ý˜G XÑ.Ô.Ð.Ø�4øÝð 	ð 	ð 	Ý�IŠIÐ8¸'À3ÑGÔGÐGØ�5�5�5�5�5øøøøð	øøøs   ‚ ”
AžA Á AFÚforce_downloadÚshow_uiÚexit_on_failurec                ó¨  — t          j        | j        j        d¬¦  «         d}|r|                      |¦  «        \  }}n˜d}t          | j        | j        ¦  «        }| j        r:t           j	         
                    |¦  «        r|                      || j        ¦  «        }|r!t                               d| j        ¦  «         n|                      |¦  «        \  }}d}	|rà| j        �6|                      ¦   «         \  }
}|                      | j        |
¦  «        }|	o|}	| j        �$|                      | j        | j        ¦  «        }|	o|}	|	svt                               d| j        | j        ¦  «         |rNt&          j                             d| j        › d| j        › d	�¦  «         t&          j                             ¦   «          |r|	st'          j        d
¦  «         |	S )a1  
        Downloads the url and validates the signature and sha1 sum.

        :param progress_callback: Optional callback(downloaded_bytes, total_bytes)
        :type progress_callback: Optional[Callable[[int, int], None]]
        :return: Whether or not validation succeeded
        :rtype: bool
        T)Úexist_okFz %s already downloaded, skipping.NzChecksum did not match %s: %sz
Checksum did not match z: ú
rO   )ÚosÚmakedirsrR   Úparentr^   r	   r,   r/   r   Úpathrb   ro   r   Údebugr6   r`   rm   Úwarningr*   Úsysri   ÚwriteÚflushÚexit)r:   rp   rq   rr   rU   Ú
downloadedrQ   Úfile_is_validÚdestÚvalidÚsigÚ	valid_sigÚvalid_checksums                r%   Ú__call__zDownload.__call__  sÓ  € õ 	Œ�D”MÔ(°4Ð8Ñ8Ô8Ð8àˆ
Øð 
	CØ ŸOšOÐ,=Ñ>Ô>‰MˆAˆzˆzà!ˆMÝ(¨¬°4Ô3CÑDÔDˆDØŒ}ð L¥¤§¢°Ñ!5Ô!5ð LØ $× 6Ò 6°t¸T¼]Ñ KÔ K�Øð CÝ—	’	Ð<¸d¼hÑGÔGÐGÐGà $§¢Ð0AÑ BÔ B‘��:ØˆØð 	'ØÔ!Ð-Ø×-Ò-Ñ/Ô/‘��QØ ×3Ò3°D´MÀ3ÑGÔG�	ØÐ+ )�ØŒ}Ð(Ø!%×!7Ò!7¸¼ÀtÄ}Ñ!UÔ!U�ØÐ0 .�àð 'Ý—’Ð;¸T¼YÈÌÑVÔVÐVØð 'Ý”J×$Ò$Ð%_ÀÄÐ%_Ð%_ÈdÌmÐ%_Ð%_Ð%_Ñ`Ô`Ð`Ý”J×$Ò$Ñ&Ô&Ð&Øð 	 5ð 	ÝŒH�Q‰KŒKˆKØˆr<   )NNr)   r)   N)r*   r+   r,   r+   r-   r   r.   r   r/   r   r0   r+   r   r   r   r1   )r   r(   )r   r@   )rB   rC   r   r1   )r   r+   )r   r   r3   )rU   rV   r   rW   )r0   r   r   rW   )r   r   )r   r1   )rf   r   r.   rC   r   r   )rf   r   r   r   r   r   )FFFN)
rp   r   rq   r   rr   r   rU   rV   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r?   Úpropertyr/   Úsetterr,   r-   rL   rR   rT   r^   r`   rb   re   Ústaticmethodrm   ro   r‡   rd   r<   r%   r(   r(   P   s  € € € € € ðð ð, $(Ø $Ø "ØØ#ð!ð !ð !ð !ð !ð(

ð 

ð 

ð 

ð ð!ð !ð !ñ „Xð!ð Ôð/ð /ð /ñ Ôð/ð ð9ð 9ð 9ñ „Xð9ð ðð ð ñ „Xðð ð?ð ?ð ?ñ „Xð?ð ð'ð 'ð 'ñ „Xð'ð
 ð9ð 9ð 9ñ „Xð9ð ð  ð  ð  ð  ðDNð Nð Nð Nð Nð&ð &ð &ð &ðð ð ð ð ðð ð ñ „\ðð4 ðð ð ñ „\ðð(  %ØØ %Ø?Cð.ð .ð .ð .ð .ð .ð .r<   r(   )r   r   r   r   r   r   )r‹   Ú
__future__r   r   Úloggingrv   r7   rk   r|   Útypingr   Úrelenv.commonr   r   r   r   r	   r
   Úcollections.abcr   r+   r   ÚenvironrY   Ú	getLoggerrˆ   r   r&   r(   rd   r<   r%   ú<module>r–      s^  ððð ð #Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø 	€	€	€	Ø €€€Ø Ð Ð Ð Ø 
€
€
€
Ø  Ð  Ð  Ð  Ð  Ð  ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð )Ø(Ð(Ð(Ð(Ð(Ð(ð �”˜SÔ!Ñ!€ð ˆrŒzÐ€à€gÔ˜Ñ!Ô!€ð'ð 'ð 'ð 'ðTrð rð rð rð rñ rô rð rð rð rr<   