U
    d	                  	   @   s   d 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eee dd	d
Ze eee dddZeee dddZedkrddlZe Zedst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 )zPEP 656 support.

This module implements logic to detect if the currently running Python is
linked against musl, and what musl version is used.
    N)Iterator
NamedTupleOptional   )ELFFilec                   @   s   e Zd ZU eed< eed< dS )_MuslVersionmajorminorN)__name__
__module____qualname__int__annotations__ r   r   8/tmp/pip-unpacked-wheel-d55rnsd_/packaging/_musllinux.pyr      s   
r   )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   st   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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   T)stderruniversal_newlines)openr   interpreterOSError	TypeError
ValueError
subprocessrunPIPEr#   r&   )r$   fZldprocr   r   r   _get_musl_version   s    r2   )archr   c                 c   sH   t tj}|dk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.
    NZ
musllinux__)r2   sysr$   ranger	   r   )r3   Zsys_muslr	   r   r   r   platform_tags6   s
    	
r8   __main__zlinux-z	not linuxzplat:zmusl:ztags: )endz[.-]r5   -r4   z
      )__doc__	functoolsr   r-   r6   typingr   r   r   Z_elffiler   r   strr#   	lru_cacher2   r8   r
   	sysconfigget_platformplat
startswithAssertionErrorprintr$   subsplittr   r   r   r   <module>   s(   


"