3
g                 @   sx  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 e
e eeedf dddZe
e ee dd	d
ZG dd deZeee dddZej eee dddZeee dddZedkrtddlZej Zejdstdede edeej  eddd x4eej!ddej"d d!d# D ]Z#ee#d"d q^W 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)IOIterator
NamedTupleOptionalTuple.)ffmtreturnc             C   s   t j|| jt j|S )N)structunpackreadcalcsize)r   r    r   ^/var/www/tester-filtro-web/env/lib/python3.6/site-packages/pip/_vendor/packaging/_musllinux.py_read_unpacked   s    r   )r   r	   c          $   C   sd  | j d yt| d}W n tjk
r.   dS X |dd tdkrHdS | j tjdd yddd|d  \}}}W n tk
r   dS X tj| }yt| |\}}}}}}}	W n tjk
r   dS X xt	|	d D ]}
| j |||
   y|t| |\}}}W n tjk
r   dS X |dkr,q| j | t
j| j|jd}d|krZdS |S W dS )zDetect 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   Z16BN   s   ELFZHHI   IIIIHHHIIIIIIIIQQQIHHHIIQQQQQQ      )r   r       muslr   r   r   )r   r   r   r   r   r   )r   r   r   )seekr   r
   errortupler   KeyErroroperator
itemgetterrangeosfsdecoder   strip)r   identZe_fmtZp_fmtZp_idxZp_get_e_phoffe_phentsizee_phnumiZp_typeZp_offsetZp_fileszinterpreterr   r   r   _parse_ld_musl_from_elf   sB    




r/   c               @   s   e Zd ZU eedS )_MuslVersionN)__name__
__module____qualname__intmajorminorr   r   r   r   r0   G   s   
r0   )outputr	   c             C   sx   dd dd | j  D D }t|dk s<|d d d dkr@d S tjd	|d
 }|sXd S tt|jd
t|j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   | ]}|j  V  qd S )N)r'   )r8   r9   r   r   r   	<genexpr>M   s    z&_parse_musl_version.<locals>.<genexpr>r   r   r   r   zVersion (\d+)\.(\d+)r   )r5   r6   )
splitlineslenrematchr0   r4   group)r7   linesmr   r   r   _parse_musl_versionL   s     rC   )
executabler	   c             C   sl   t j 8}y|jt| d}W n tk
r2   dS X t|}W dQ R X |sNd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IOErrorr/   
subprocessrunPIPErC   rF   )rD   stackr   Zldprocr   r   r   _get_musl_versionV   s    
rR   )archr	   c             c   sL   t tj}|dkrdS x0t|jddD ]}d|j d| d|  V  q&W 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.
    Nr   Z
musllinux_r)   rT   )rR   sysrD   r$   r6   r5   )rS   Zsys_muslr6   r   r   r   platform_tagsn   s
    	
rV   __main__zlinux-z	not linuxzplat:zmusl:ztags: )endz[.-]r)   -r   z
      rT   )$__doc__rH   	functoolsr"   r%   r>   r
   rM   rU   typingr   r   r   r   r   bytesstrr4   r   r/   r0   rC   	lru_cacherR   rV   r1   	sysconfigget_platformZplat
startswithAssertionErrorprintrD   subsplittr   r   r   r   <module>   s2   1


$