3

U'ícã@sxdZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZm
Z
mZe
eeeedfdœdd„Ze
ee
edœd	d
„ZGdd„deƒZee
ed
œdd„Zejƒee
edœdd„ƒZeeedœdd„ZedkrtddlZejƒZejdƒstdƒ‚edeƒedeej ƒƒedddx4eej!ddej"d d!ƒd#ƒƒD]Z#ee#d"dq^WdS)$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ÚreturncCstj||jtj|ƒƒƒS)N)ÚstructÚunpackÚreadÚcalcsize)rr©rú9/tmp/pip-build-gk9425m9/packaging/packaging/_musllinux.pyÚ_read_unpackedsr)rr	c$Csd|jdƒyt|dƒ}Wntjk
r.dSX|dd…tdƒkrHdS|jtjdƒdƒydddœ|d\}}}Wntk
rŠdSXtj|Ž}yt||ƒ\}}}}}}}	Wntjk
rÈdSXx”t	|	dƒD]„}
|j|||
ƒy|t||ƒƒ\}}}
Wntjk
rdSX|dkr,qØ|j|ƒt
j|j|
ƒƒj
dƒ}d|krZdS|SWdS)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
    rZ16BNésELFZHHIéÚIIIIHHHÚIIIIIIIIÚQQQIHHHÚIIQQQQQQéé)rréúÚmusl©rrr)rrr©rrr)rrr)Úseekrr
ÚerrorÚtupler
ÚKeyErrorÚoperatorÚ
itemgetterÚrangeÚosÚfsdecoderÚstrip)rÚidentZe_fmtZp_fmtZp_idxZp_getÚ_Ze_phoffZe_phentsizeZe_phnumÚiZp_typeZp_offsetZp_fileszZinterpreterrrrÚ_parse_ld_musl_from_elfsB




r+c@seZdZUeedS)Ú_MuslVersionN)Ú__name__Ú
__module__Ú__qualname__ÚintÚmajorÚminorrrrrr,Gs
r,)Úoutputr	cCsxdd„dd„|jƒDƒDƒ}t|ƒdks<|ddd…dkr@dStjd	|d
ƒ}|sXdStt|jd
ƒƒt|jdƒƒdS)NcSsg|]}|r|‘qSrr)Ú.0Únrrrú
<listcomp>Msz'_parse_musl_version.<locals>.<listcomp>css|]}|jƒVqdS)N)r')r4r5rrrú	<genexpr>Msz&_parse_musl_version.<locals>.<genexpr>rrrrzVersion (\d+)\.(\d+)r)r1r2)Ú
splitlinesÚlenÚreÚmatchr,r0Úgroup)r3ÚlinesÚmrrrÚ_parse_musl_versionLs r?)Ú
executabler	cCsltjƒ8}y|jt|dƒƒ}Wntk
r2dSXt|ƒ}WdQRX|sNdStj|gtjdd}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ÚOSErrorr+Ú
subprocessÚrunÚPIPEr?rB)r@ÚstackrÚldÚprocrrrÚ_get_musl_versionVs
rO)Úarchr	ccsLttjƒ}|dkrdSx0t|jddƒD]}d|j›d|›d|›Vq&WdS)aTGenerate 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.
    NrZ
musllinux_r)éÿÿÿÿrQ)rOÚsysr@r$r2r1)rPZsys_muslr2rrrÚ
platform_tagsns
	
rSÚ__main__zlinux-z	not linuxzplat:zmusl:ztags:ú )Úendz[.-]r)ú-rz
      rQ)$Ú__doc__rDÚ	functoolsr"r%r:r
rIrRÚtypingrrrrrÚbytesÚstrr0rr+r,r?Ú	lru_cacherOrSr-Ú	sysconfigÚget_platformÚplatÚ
startswithÚAssertionErrorÚprintr@ÚsubÚsplitÚtrrrrÚ<module>s21


$