2023-01-14 02:55:00 +00:00
|
|
|
"""PEP 656 support.
|
|
|
|
|
|
|
|
This module implements logic to detect if the currently running Python is
|
|
|
|
linked against musl, and what musl version is used.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import functools
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2024-06-07 16:18:00 +00:00
|
|
|
from typing import Iterator, NamedTuple, Optional, Sequence
|
2023-01-14 02:55:00 +00:00
|
|
|
|
2023-02-17 01:17:58 +00:00
|
|
|
from ._elffile import ELFFile
|
2023-01-14 02:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _MuslVersion(NamedTuple):
|
|
|
|
major: int
|
|
|
|
minor: int
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_musl_version(output: str) -> Optional[_MuslVersion]:
|
|
|
|
lines = [n for n in (n.strip() for n in output.splitlines()) if n]
|
|
|
|
if len(lines) < 2 or lines[0][:4] != "musl":
|
|
|
|
return None
|
|
|
|
m = re.match(r"Version (\d+)\.(\d+)", lines[1])
|
|
|
|
if not m:
|
|
|
|
return None
|
|
|
|
return _MuslVersion(major=int(m.group(1)), minor=int(m.group(2)))
|
|
|
|
|
|
|
|
|
|
|
|
@functools.lru_cache()
|
|
|
|
def _get_musl_version(executable: str) -> Optional[_MuslVersion]:
|
|
|
|
"""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
|
|
|
|
"""
|
2023-02-17 01:17:58 +00:00
|
|
|
try:
|
|
|
|
with open(executable, "rb") as f:
|
|
|
|
ld = ELFFile(f).interpreter
|
|
|
|
except (OSError, TypeError, ValueError):
|
|
|
|
return None
|
|
|
|
if ld is None or "musl" not in ld:
|
2023-01-14 02:55:00 +00:00
|
|
|
return None
|
2024-06-07 16:18:00 +00:00
|
|
|
proc = subprocess.run([ld], stderr=subprocess.PIPE, text=True)
|
2023-01-14 02:55:00 +00:00
|
|
|
return _parse_musl_version(proc.stderr)
|
|
|
|
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
def platform_tags(archs: Sequence[str]) -> Iterator[str]:
|
2023-01-14 02:55:00 +00:00
|
|
|
"""Generate musllinux tags compatible to the current platform.
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
: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.
|
2023-01-14 02:55:00 +00:00
|
|
|
|
|
|
|
:returns: An iterator of compatible musllinux tags.
|
|
|
|
"""
|
|
|
|
sys_musl = _get_musl_version(sys.executable)
|
|
|
|
if sys_musl is None: # Python not dynamically linked against musl.
|
|
|
|
return
|
2024-06-07 16:18:00 +00:00
|
|
|
for arch in archs:
|
|
|
|
for minor in range(sys_musl.minor, -1, -1):
|
|
|
|
yield f"musllinux_{sys_musl.major}_{minor}_{arch}"
|
2023-01-14 02:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
|
|
import sysconfig
|
|
|
|
|
|
|
|
plat = sysconfig.get_platform()
|
|
|
|
assert plat.startswith("linux-"), "not linux"
|
|
|
|
|
|
|
|
print("plat:", plat)
|
|
|
|
print("musl:", _get_musl_version(sys.executable))
|
|
|
|
print("tags:", end=" ")
|
|
|
|
for t in platform_tags(re.sub(r"[.-]", "_", plat.split("-", 1)[-1])):
|
|
|
|
print(t, end="\n ")
|