]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pkg_resources/_vendor/packaging/_musllinux.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / pkg_resources / _vendor / packaging / _musllinux.py
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
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, universal_newlines=True)
51 return _parse_musl_version(proc.stderr)
52
53
54 def platform_tags(arch: str) -> Iterator[str]:
55 """Generate musllinux tags compatible to the current platform.
56
57 :param arch: Should be the part of platform tag after the ``linux_``
58 prefix, e.g. ``x86_64``. The ``linux_`` prefix is assumed as a
59 prerequisite for the current platform to be musllinux-compatible.
60
61 :returns: An iterator of compatible musllinux tags.
62 """
63 sys_musl = _get_musl_version(sys.executable)
64 if sys_musl is None: # Python not dynamically linked against musl.
65 return
66 for minor in range(sys_musl.minor, -1, -1):
67 yield f"musllinux_{sys_musl.major}_{minor}_{arch}"
68
69
70 if __name__ == "__main__": # pragma: no cover
71 import sysconfig
72
73 plat = sysconfig.get_platform()
74 assert plat.startswith("linux-"), "not linux"
75
76 print("plat:", plat)
77 print("musl:", _get_musl_version(sys.executable))
78 print("tags:", end=" ")
79 for t in platform_tags(re.sub(r"[.-]", "_", plat.split("-", 1)[-1])):
80 print(t, end="\n ")