]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | """PEP 656 support. |
2 | ||
3 | This module implements logic to detect if the currently running Python is | |
4 | linked against musl, and what musl version is used. | |
5 | """ | |
6 | ||
7 | import functools | |
8 | import re | |
9 | import subprocess | |
10 | import sys | |
11 | from typing import Iterator, NamedTuple, Optional, Sequence | |
12 | ||
13 | from ._elffile import ELFFile | |
14 | ||
15 | ||
16 | class _MuslVersion(NamedTuple): | |
17 | major: int | |
18 | minor: int | |
19 | ||
20 | ||
21 | def _parse_musl_version(output: str) -> Optional[_MuslVersion]: | |
22 | lines = [n for n in (n.strip() for n in output.splitlines()) if n] | |
23 | if len(lines) < 2 or lines[0][:4] != "musl": | |
24 | return None | |
25 | m = re.match(r"Version (\d+)\.(\d+)", lines[1]) | |
26 | if not m: | |
27 | return None | |
28 | return _MuslVersion(major=int(m.group(1)), minor=int(m.group(2))) | |
29 | ||
30 | ||
31 | @functools.lru_cache() | |
32 | def _get_musl_version(executable: str) -> Optional[_MuslVersion]: | |
33 | """Detect currently-running musl runtime version. | |
34 | ||
35 | This is done by checking the specified executable's dynamic linking | |
36 | information, and invoking the loader to parse its output for a version | |
37 | string. If the loader is musl, the output would be something like:: | |
38 | ||
39 | musl libc (x86_64) | |
40 | Version 1.2.2 | |
41 | Dynamic Program Loader | |
42 | """ | |
43 | try: | |
44 | with open(executable, "rb") as f: | |
45 | ld = ELFFile(f).interpreter | |
46 | except (OSError, TypeError, ValueError): | |
47 | return None | |
48 | if ld is None or "musl" not in ld: | |
49 | return None | |
50 | proc = subprocess.run([ld], stderr=subprocess.PIPE, text=True) | |
51 | return _parse_musl_version(proc.stderr) | |
52 | ||
53 | ||
54 | def platform_tags(archs: Sequence[str]) -> Iterator[str]: | |
55 | """Generate musllinux tags compatible to the current platform. | |
56 | ||
57 | :param archs: Sequence of compatible architectures. | |
58 | The first one shall be the closest to the actual architecture and be the part of | |
59 | platform tag after the ``linux_`` prefix, e.g. ``x86_64``. | |
60 | The ``linux_`` prefix is assumed as a prerequisite for the current platform to | |
61 | be musllinux-compatible. | |
62 | ||
63 | :returns: An iterator of compatible musllinux tags. | |
64 | """ | |
65 | sys_musl = _get_musl_version(sys.executable) | |
66 | if sys_musl is None: # Python not dynamically linked against musl. | |
67 | return | |
68 | for arch in archs: | |
69 | for minor in range(sys_musl.minor, -1, -1): | |
70 | yield f"musllinux_{sys_musl.major}_{minor}_{arch}" | |
71 | ||
72 | ||
73 | if __name__ == "__main__": # pragma: no cover | |
74 | import sysconfig | |
75 | ||
76 | plat = sysconfig.get_platform() | |
77 | assert plat.startswith("linux-"), "not linux" | |
78 | ||
79 | print("plat:", plat) | |
80 | print("musl:", _get_musl_version(sys.executable)) | |
81 | print("tags:", end=" ") | |
82 | for t in platform_tags(re.sub(r"[.-]", "_", plat.split("-", 1)[-1])): | |
83 | print(t, end="\n ") |