]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/packaging/_musllinux.py
3 This module implements logic to detect if the currently running Python is
4 linked against musl, and what musl version is used.
15 from typing
import IO
, Iterator
, NamedTuple
, Optional
, Tuple
18 def _read_unpacked(f
: IO
[bytes], fmt
: str) -> Tuple
[int, ...]:
19 return struct
.unpack(fmt
, f
.read(struct
.calcsize(fmt
)))
22 def _parse_ld_musl_from_elf(f
: IO
[bytes]) -> Optional
[str]:
23 """Detect musl libc location by parsing the Python executable.
25 Based on: https://gist.github.com/lyssdod/f51579ae8d93c8657a5564aefc2ffbca
26 ELF header: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
30 ident
= _read_unpacked(f
, "16B")
33 if ident
[:4] != tuple(b
"\x7fELF"): # Invalid magic, not ELF.
35 f
.seek(struct
.calcsize("HHI"), 1) # Skip file type, machine, and version.
38 # e_fmt: Format for program header.
39 # p_fmt: Format for section header.
40 # p_idx: Indexes to find p_type, p_offset, and p_filesz.
41 e_fmt
, p_fmt
, p_idx
= {
42 1: ("IIIIHHH", "IIIIIIII", (0, 1, 4)), # 32-bit.
43 2: ("QQQIHHH", "IIQQQQQQ", (0, 2, 5)), # 64-bit.
48 p_get
= operator
.itemgetter(*p_idx
)
50 # Find the interpreter section and return its content.
52 _
, e_phoff
, _
, _
, _
, e_phentsize
, e_phnum
= _read_unpacked(f
, e_fmt
)
55 for i
in range(e_phnum
+ 1):
56 f
.seek(e_phoff
+ e_phentsize
* i
)
58 p_type
, p_offset
, p_filesz
= p_get(_read_unpacked(f
, p_fmt
))
61 if p_type
!= 3: # Not PT_INTERP.
64 interpreter
= os
.fsdecode(f
.read(p_filesz
)).strip("\0")
65 if "musl" not in interpreter
:
71 class _MuslVersion(NamedTuple
):
76 def _parse_musl_version(output
: str) -> Optional
[_MuslVersion
]:
77 lines
= [n
for n
in (n
.strip() for n
in output
.splitlines()) if n
]
78 if len(lines
) < 2 or lines
[0][:4] != "musl":
80 m
= re
.match(r
"Version (\d+)\.(\d+)", lines
[1])
83 return _MuslVersion(major
=int(m
.group(1)), minor
=int(m
.group(2)))
86 @functools.lru_cache()
87 def _get_musl_version(executable
: str) -> Optional
[_MuslVersion
]:
88 """Detect currently-running musl runtime version.
90 This is done by checking the specified executable's dynamic linking
91 information, and invoking the loader to parse its output for a version
92 string. If the loader is musl, the output would be something like::
96 Dynamic Program Loader
98 with contextlib
.ExitStack() as stack
:
100 f
= stack
.enter_context(open(executable
, "rb"))
103 ld
= _parse_ld_musl_from_elf(f
)
106 proc
= subprocess
.run([ld
], stderr
=subprocess
.PIPE
, universal_newlines
=True)
107 return _parse_musl_version(proc
.stderr
)
110 def platform_tags(arch
: str) -> Iterator
[str]:
111 """Generate musllinux tags compatible to the current platform.
113 :param arch: Should be the part of platform tag after the ``linux_``
114 prefix, e.g. ``x86_64``. The ``linux_`` prefix is assumed as a
115 prerequisite for the current platform to be musllinux-compatible.
117 :returns: An iterator of compatible musllinux tags.
119 sys_musl
= _get_musl_version(sys
.executable
)
120 if sys_musl
is None: # Python not dynamically linked against musl.
122 for minor
in range(sys_musl
.minor
, -1, -1):
123 yield f
"musllinux_{sys_musl.major}_{minor}_{arch}"
126 if __name__
== "__main__": # pragma: no cover
129 plat
= sysconfig
.get_platform()
130 assert plat
.startswith("linux-"), "not linux"
133 print("musl:", _get_musl_version(sys
.executable
))
134 print("tags:", end
=" ")
135 for t
in platform_tags(re
.sub(r
"[.-]", "_", plat
.split("-", 1)[-1])):