_musllinux.py 2.6 KB

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