Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pip/_vendor/packaging/_musllinux.py: 41%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

39 statements  

1"""PEP 656 support. 

2 

3This module implements logic to detect if the currently running Python is 

4linked against musl, and what musl version is used. 

5""" 

6 

7from __future__ import annotations 

8 

9import functools 

10import re 

11import subprocess 

12import sys 

13from typing import TYPE_CHECKING, NamedTuple 

14 

15from ._elffile import ELFFile 

16 

17if TYPE_CHECKING: 

18 from collections.abc import Iterator, Sequence 

19 

20 

21class _MuslVersion(NamedTuple): 

22 major: int 

23 minor: int 

24 

25 

26def _parse_musl_version(output: str) -> _MuslVersion | None: 

27 lines = [n for n in (n.strip() for n in output.splitlines()) if n] 

28 if len(lines) < 2 or lines[0][:4] != "musl": 

29 return None 

30 m = re.match(r"Version (\d+)\.(\d+)", lines[1]) 

31 if not m: 

32 return None 

33 return _MuslVersion(major=int(m.group(1)), minor=int(m.group(2))) 

34 

35 

36@functools.lru_cache 

37def _get_musl_version(executable: str) -> _MuslVersion | None: 

38 """Detect currently-running musl runtime version. 

39 

40 This is done by checking the specified executable's dynamic linking 

41 information, and invoking the loader to parse its output for a version 

42 string. If the loader is musl, the output would be something like:: 

43 

44 musl libc (x86_64) 

45 Version 1.2.2 

46 Dynamic Program Loader 

47 """ 

48 try: 

49 with open(executable, "rb") as f: 

50 ld = ELFFile(f).interpreter 

51 except (OSError, TypeError, ValueError): 

52 return None 

53 if ld is None or "musl" not in ld: 

54 return None 

55 proc = subprocess.run([ld], check=False, stderr=subprocess.PIPE, text=True) 

56 return _parse_musl_version(proc.stderr) 

57 

58 

59def platform_tags(archs: Sequence[str]) -> Iterator[str]: 

60 """Generate musllinux tags compatible to the current platform. 

61 

62 :param archs: Sequence of compatible architectures. 

63 The first one shall be the closest to the actual architecture and be the part of 

64 platform tag after the ``linux_`` prefix, e.g. ``x86_64``. 

65 The ``linux_`` prefix is assumed as a prerequisite for the current platform to 

66 be musllinux-compatible. 

67 

68 :returns: An iterator of compatible musllinux tags. 

69 """ 

70 sys_musl = _get_musl_version(sys.executable) 

71 if sys_musl is None: # Python not dynamically linked against musl. 

72 return 

73 for arch in archs: 

74 for minor in range(sys_musl.minor, -1, -1): 

75 yield f"musllinux_{sys_musl.major}_{minor}_{arch}" 

76 

77 

78if __name__ == "__main__": # pragma: no cover 

79 import sysconfig 

80 

81 plat = sysconfig.get_platform() 

82 assert plat.startswith("linux-"), "not linux" 

83 

84 print("plat:", plat) 

85 print("musl:", _get_musl_version(sys.executable)) 

86 print("tags:", end=" ") 

87 for t in platform_tags([re.sub(r"[.-]", "_", plat.split("-", 1)[-1])]): 

88 print(t, end="\n ")