o
    Ëý°j  ã                	   @   s~  d 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ZddlZddl	m
Z
mZmZmZmZ de
e dedeedf fdd	„Zde
e dee fd
d„ZG dd„ deƒZdedee fdd„Ze ¡ dedee fdd„ƒZdedee fdd„Zedkr»ddlZe ¡ Ze d¡s�J dƒ‚edeƒ edeejƒƒ eddd� ee  dd e !d!d"¡d# ¡ƒD ]
Z"ee"d$d� q²dS dS )%z�PEP 656 support.

This module implements logic to detect if the currently running Python is
linked against musl, and what musl version is used.
é    N)ÚIOÚIteratorÚ
NamedTupleÚOptionalÚTupleÚfÚfmtÚreturn.c                 C   s   t  ||  t  |¡¡¡S ©N)ÚstructÚunpackÚreadÚcalcsize)r   r   © r   úš/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/setuptools/_vendor/packaging/_musllinux.pyÚ_read_unpacked   s   r   c              	   C   sb  |   d¡ zt| dƒ}W n tjy   Y dS w |dd… tdƒkr$dS |   t d¡d¡ zdd	d
œ|d  \}}}W n
 tyE   Y dS w tj|Ž }zt| |ƒ\}}}}}}}	W n tjyd   Y dS w t	|	d ƒD ]C}
|   |||
  ¡ z|t| |ƒƒ\}}}W n tjyŽ   Y  dS w |dkr”qk|   |¡ t
 |  |¡¡ d¡}d|vr« dS |  S dS )záDetect musl libc location by parsing the Python executable.

    Based on: https://gist.github.com/lyssdod/f51579ae8d93c8657a5564aefc2ffbca
    ELF header: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
    r   Ú16BNé   s   ELFÚHHIé   )ÚIIIIHHHÚIIIIIIII)r   r   r   )ÚQQQIHHHÚIIQQQQQQ)r   é   é   )r   r   é   ú Úmusl)Úseekr   r   ÚerrorÚtupler   ÚKeyErrorÚoperatorÚ
itemgetterÚrangeÚosÚfsdecoder   Ústrip)r   ÚidentÚe_fmtÚp_fmtÚp_idxÚp_getÚ_Úe_phoffÚe_phentsizeÚe_phnumÚiÚp_typeÚp_offsetÚp_fileszÚinterpreterr   r   r   Ú_parse_ld_musl_from_elf   sN   
ÿþýÿ
ÿÿ
r7   c                   @   s   e Zd ZU eed< eed< dS )Ú_MuslVersionÚmajorÚminorN)Ú__name__Ú
__module__Ú__qualname__ÚintÚ__annotations__r   r   r   r   r8   G   s   
 r8   Úoutputc                 C   sx   dd„ dd„ |   ¡ D ƒD ƒ}t|ƒdk s|d d d… dkr d S t d	|d
 ¡}|s,d S tt| d
¡ƒt| d¡ƒd�S )Nc                 S   s   g | ]}|r|‘qS r   r   ©Ú.0Únr   r   r   Ú
<listcomp>M   s    z'_parse_musl_version.<locals>.<listcomp>c                 s   s   � | ]}|  ¡ V  qd S r
   )r(   rA   r   r   r   Ú	<genexpr>M   s   € z&_parse_musl_version.<locals>.<genexpr>r   r   r   r   zVersion (\d+)\.(\d+)r   )r9   r:   )Ú
splitlinesÚlenÚreÚmatchr8   r>   Úgroup)r@   ÚlinesÚmr   r   r   Ú_parse_musl_versionL   s     rM   Ú
executablec              	   C   sŒ   t  ¡ �'}z
| t| dƒ¡}W n ty   Y W d  ƒ dS w t|ƒ}W d  ƒ n1 s.w   Y  |s7dS tj|gtjdd�}t	|j
ƒS )a`  Detect currently-running musl runtime version.

    This is done by checking the specified executable's dynamic linking
    information, and invoking the loader to parse its output for a version
    string. If the loader is musl, the output would be something like::

        musl libc (x86_64)
        Version 1.2.2
        Dynamic Program Loader
    ÚrbNT)ÚstderrÚuniversal_newlines)Ú
contextlibÚ	ExitStackÚenter_contextÚopenÚIOErrorr7   Ú
subprocessÚrunÚPIPErM   rP   )rN   Ústackr   ÚldÚprocr   r   r   Ú_get_musl_versionV   s   
ü
û
r]   Úarchc                 c   sJ   � t tjƒ}|du rdS t|jddƒD ]}d|j› d|› d| › �V  qdS )aT  Generate musllinux tags compatible to the current platform.

    :param arch: Should be the part of platform tag after the ``linux_``
        prefix, e.g. ``x86_64``. The ``linux_`` prefix is assumed as a
        prerequisite for the current platform to be musllinux-compatible.

    :returns: An iterator of compatible musllinux tags.
    NéÿÿÿÿÚ
musllinux_r.   )r]   ÚsysrN   r%   r:   r9   )r^   Úsys_muslr:   r   r   r   Úplatform_tagsn   s   €
	ÿrc   Ú__main__zlinux-z	not linuxzplat:zmusl:ztags:ú )Úendz[.-]r.   ú-r   r_   z
      )#Ú__doc__rR   Ú	functoolsr#   r&   rH   r   rW   ra   Útypingr   r   r   r   r   ÚbytesÚstrr>   r   r7   r8   rM   Ú	lru_cacher]   rc   r;   Ú	sysconfigÚget_platformÚplatÚ
startswithÚprintrN   ÚsubÚsplitÚtr   r   r   r   Ú<module>   s8    "1

"ö	