o
    Uh                     @  s  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mZm	Z	m
Z
 d dlmZ ddlmZmZ erFd dlmZ d dlZd dlmZ ed	 ZeeZe \ZZe	eee f Zd-ddZeedddfd.ddZ	d/d0d"d#Z		d1d2d(d)Z							d3d4d+d,Z dS )5    )annotationsN)TYPE_CHECKINGOptionalTuplecast)unquote   )PythonVersionget_arch_platform)Literal)StrPath)cpythonpypyreturndict[str, str] | Nonec                  C  s&   t d} | d u rd S dd|  dS )NGITHUB_TOKENz
2022-11-28zBearer )zX-GitHub-Api-VersionAuthorization)osgetenv)TOKEN r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/pbs_installer/_install.py_get_headers   s   
r   r   FrequeststrarchplatformimplementationPythonImplementation	build_dirboolfree_threaded tuple[PythonVersion, PythonFile]c           
      C  s   ddl m} | D ];\}}|| |sq
| drd}|||| |f}	|	dur0||	f  S |sE|||d|f }	durE||	f  S q
td| d| )	a  Get the download URL matching the given requested version.

    Parameters:
        request: The version of Python to install, e.g. 3.8,3.10.4
        arch: The architecture to install, e.g. x86_64, arm64
        platform: The platform to install, e.g. linux, macos
        implementation: The implementation of Python to install, allowed values are 'cpython' and 'pypy'
        build_dir: Whether to include the `build/` directory from indygreg builds
        free_threaded: Whether to install the freethreaded version of Python

    Returns:
        A tuple of the PythonVersion and the download URL

    Examples:
        >>> get_download_link("3.10", "x86_64", "linux")
        (PythonVersion(kind='cpython', major=3, minor=10, micro=13),
        'https://github.com/indygreg/python-build-standalone/releases/download/20240224/cpython-3.10.13%2B20240224-x86_64-unknown-linux-gnu-pgo%2Blto-full.tar.zst')
    r   )PYTHON_VERSIONStTNFz*Could not find a version matching version=z, implementation=)	_versionsr#   itemsmatchesendswithget
ValueError)
r   r   r   r   r   r!   r#   py_verurlsmatchedr   r   r   get_download_link#   s"   
r.   python_file
PythonFiledestinationr   clienthttpx.Client | Nonec              	   C  s6  | \}}t d|| zddl}W n ty   tddw |du r)|jddd}t|dd }t	 }|s>t 
d	| t|d
:}|jd|t d!}	|	  |	jddD ]}
|ra||
 ||
 qXW d   n1 sqw   Y  W d   n1 sw   Y  |r| |krtd| d|  |S )a  Download the given url to the destination.

    Note: Extras required
        `pbs-installer[download]` must be installed to use this function.

    Parameters:
        python_file: The (url, checksum) tuple to download
        destination: The file path to download to
        client: A http.Client to use for downloading, or None to create a new one

    Returns:
        The original filename of the downloaded file
    zDownloading url %s to %sr   Nz+You must install httpx to use this functionT)	trust_envfollow_redirects/z0No checksum found for %s, this would be insecurewbGET)headersi    )
chunk_sizezChecksum mismatch. Expected z, got )loggerdebughttpxModuleNotFoundErrorRuntimeErrorClientr   rsplithashlibsha256warningopenstreamr   raise_for_status
iter_bytesupdatewrite	hexdigest)r/   r1   r2   urlchecksumr>   filenamehasherfrespchunkr   r   r   downloadS   s6   

rT   rO   original_filename
str | NoneNonec                 C  s|   ddl m}m}m} |du rt| }td| || tt| } |dr+|| | dS |dr7|| | dS || | dS )a  Unpack the downloaded file to the destination.

    Note: Extras required
        `pbs-installer[install]` must be installed to use this function.

    Parameters:
        filename: The file to unpack
        destination: The directory to unpack to
        original_filename: The original filename of the file, if it was renamed
        build_dir: Whether to include the `build/` directory from indygreg builds
    r   )
unpack_tar
unpack_zip
unpack_zstNz2Extracting file %s to %s with original filename %sz.zstz.zip)	_utilsrX   rY   rZ   r   r<   r=   r   r(   )rO   r1   rU   r   rX   rY   rZ   r   r   r   install_file   s   


r\   version_dirc	                 C  s   |du rt }|du rt}t| |||||d\}	}
|r#tj|t|	}td|	| tj	|dd t
 }|  t|
|j|}t|j||| W d   dS 1 sTw   Y  dS )a  Download and install the requested python version.

    Note: Extras required
        `pbs-installer[all]` must be installed to use this function.

    Parameters:
        request: The version of Python to install, e.g. 3.8,3.10.4
        destination: The directory to install to
        version_dir: Whether to install to a subdirectory named with the python version
        client: A httpx.Client to use for downloading
        arch: The architecture to install, e.g. x86_64, arm64
        platform: The platform to install, e.g. linux, macos
        implementation: The implementation of Python to install, allowed values are 'cpython' and 'pypy'
        build_dir: Whether to include the `build/` directory from indygreg builds
        free_threaded: Whether to install the freethreaded version of Python
    Examples:
        >>> install("3.10", "./python")
        Installing cpython@3.10.4 to ./python
        >>> install("3.10", "./python", version_dir=True)
        Installing cpython@3.10.4 to ./python/cpython@3.10.4
    N)r   r   r   r   r!   zInstalling %s to %sT)exist_ok)THIS_PLATFORM	THIS_ARCHr.   r   pathjoinr   r<   r=   makedirstempfileNamedTemporaryFilecloserT   namer\   )r   r1   r]   r2   r   r   r   r   r!   verr/   tfrU   r   r   r   install   s*    

"rj   )r   r   )r   r   r   r   r   r   r   r   r   r    r!   r    r   r"   )N)r/   r0   r1   r   r2   r3   r   r   )NF)
rO   r   r1   r   rU   rV   r   r    r   rW   )FNNNr   FF)r   r   r1   r   r]   r    r2   r3   r   rV   r   rV   r   r   r   r    r!   r    r   rW   )!
__future__r   rC   loggingr   rd   typingr   r   r   r   urllib.parser   r[   r	   r
   r   r>   	_typeshedr   r   	getLogger__name__r<   r`   r_   r   r0   r   r.   rT   r\   rj   r   r   r   r   <module>   sH    


1/(