U
    +5øi“
  ã                	   @  s  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m	Z	m
Z
 ddlmZ G dd„ de	ƒZd	d
dœdd„Zejd	d
dœdd„ƒZdddœdd„Zedk�rddlZe ¡ Ze d¡sÂt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 )#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.
é    )ÚannotationsN)ÚIteratorÚ
NamedTupleÚSequenceé   )ÚELFFilec                   @  s   e Zd ZU ded< ded< dS )Ú_MuslVersionÚintÚmajorÚminorN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__© r   r   ú8/tmp/pip-unpacked-wheel-xj899fla/packaging/_musllinux.pyr      s   
r   Ústrz_MuslVersion | None)ÚoutputÚreturnc                 C  sx   dd„ dd„ |   ¡ D ƒD ƒ}t|ƒdk s<|d d d… dkr@d S t d	|d
 ¡}|sXd S tt| d
¡ƒt| d¡ƒd�S )Nc                 S  s   g | ]}|r|‘qS r   r   ©Ú.0Únr   r   r   Ú
<listcomp>   s      z'_parse_musl_version.<locals>.<listcomp>c                 s  s   | ]}|  ¡ V  qd S )N)Ústripr   r   r   r   Ú	<genexpr>   s     z&_parse_musl_version.<locals>.<genexpr>é   r   é   ÚmuslzVersion (\d+)\.(\d+)r   )r
   r   )Ú
splitlinesÚlenÚreÚmatchr   r	   Úgroup)r   ÚlinesÚmr   r   r   Ú_parse_musl_version   s     r%   )Ú
executabler   c              
   C  sv   z$t | dƒ�}t|ƒj}W 5 Q R X W n tttfk
r@   Y dS X |dksRd|krVdS tj|gd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
    ÚrbNr   FT)ÚcheckÚstderrÚtext)Úopenr   ÚinterpreterÚOSErrorÚ	TypeErrorÚ
ValueErrorÚ
subprocessÚrunÚPIPEr%   r)   )r&   ÚfZldÚprocr   r   r   Ú_get_musl_version!   s    r5   zSequence[str]zIterator[str])Úarchsr   c                 c  sR   t tjƒ}|dkrdS | D ]2}t|jddƒD ]}d|j› d|› d|› �V  q,qdS )aÆ  Generate musllinux tags compatible to the current platform.

    :param archs: Sequence of compatible architectures.
        The first one shall be the closest to the actual architecture and 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éÿÿÿÿZ
musllinux_Ú_)r5   Úsysr&   Úranger   r
   )r6   Zsys_muslÚarchr   r   r   r   Úplatform_tags8   s    
r<   Ú__main__zlinux-z	not linuxzplat:zmusl:ztags:ú )Úendz[.-]r8   ú-r7   z
      )Ú__doc__Ú
__future__r   Ú	functoolsr    r0   r9   Útypingr   r   r   Z_elffiler   r   r%   Ú	lru_cacher5   r<   r   Ú	sysconfigÚget_platformÚplatÚ
startswithÚAssertionErrorÚprintr&   ÚsubÚsplitÚtr   r   r   r   Ú<module>   s*   


"