| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from typing import TYPE_CHECKING, NamedTuple |
| 5 |
|
| 6 |
if sys.version_info >= (3, 14): |
| 7 |
import tarfile |
| 8 |
|
| 9 |
ZSTD_SUPPORT = True |
| 10 |
else: |
| 11 |
try: |
| 12 |
from backports.zstd import tarfile |
| 13 |
|
| 14 |
ZSTD_SUPPORT = True |
| 15 |
except ModuleNotFoundError: |
| 16 |
import tarfile |
| 17 |
|
| 18 |
ZSTD_SUPPORT = False |
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from _typeshed import StrPath |
| 22 |
|
| 23 |
ARCH_MAPPING = { |
| 24 |
"arm64": "aarch64", |
| 25 |
"amd64": "x86_64", |
| 26 |
"i686": "x86", |
| 27 |
} |
| 28 |
PLATFORM_MAPPING = {"darwin": "macos"} |
| 29 |
|
| 30 |
|
| 31 |
class PythonVersion(NamedTuple): |
| 32 |
implementation: str |
| 33 |
major: int |
| 34 |
minor: int |
| 35 |
micro: int |
| 36 |
freethreaded: bool = False |
| 37 |
|
| 38 |
def __str__(self) -> str: |
| 39 |
return f"{self.implementation}@{self.major}.{self.minor}.{self.micro}{'t' if self.freethreaded else ''}" |
| 40 |
|
| 41 |
def matches(self, request: str, implementation: str) -> bool: |
| 42 |
if implementation != self.implementation: |
| 43 |
return False |
| 44 |
if self.freethreaded != request.endswith("t"): |
| 45 |
return False |
| 46 |
try: |
| 47 |
parts = tuple(int(v) for v in request.rstrip("t").split(".")) |
| 48 |
except ValueError: |
| 49 |
raise ValueError( |
| 50 |
f"Invalid version: {request!r}, each part must be an integer" |
| 51 |
) from None |
| 52 |
|
| 53 |
if len(parts) < 1: |
| 54 |
raise ValueError("Version must have at least one part") |
| 55 |
|
| 56 |
if parts[0] != self.major: |
| 57 |
return False |
| 58 |
if len(parts) > 1 and parts[1] != self.minor: |
| 59 |
return False |
| 60 |
if len(parts) > 2 and parts[2] != self.micro: |
| 61 |
return False |
| 62 |
return True |
| 63 |
|
| 64 |
|
| 65 |
def get_arch_platform() -> tuple[str, str]: |
| 66 |
import platform |
| 67 |
|
| 68 |
plat = platform.system().lower() |
| 69 |
arch = platform.machine().lower() |
| 70 |
return ARCH_MAPPING.get(arch, arch), PLATFORM_MAPPING.get(plat, plat) |
| 71 |
|
| 72 |
|
| 73 |
def _unpack_tar(tf: tarfile.TarFile, destination: StrPath) -> None: |
| 74 |
"""Unpack the tarfile to the destination, with the first skip_parts parts of the path removed""" |
| 75 |
members: list[tarfile.TarInfo] = [] |
| 76 |
for member in tf.getmembers(): |
| 77 |
parts = member.name.lstrip("/").split("/") |
| 78 |
member.name = "/".join(parts[1:]) |
| 79 |
if member.name: |
| 80 |
members.append(member) |
| 81 |
tf.extractall(destination, members=members) |
| 82 |
|
| 83 |
|
| 84 |
def unpack_tar(filename: str, destination: StrPath, original_filename: str) -> None: |
| 85 |
"""Unpack the tarfile to the destination""" |
| 86 |
if not ZSTD_SUPPORT and original_filename.endswith(".zstd"): |
| 87 |
raise ModuleNotFoundError("backports.zstd is required to unpack .zst files") |
| 88 |
with tarfile.open(filename) as z: |
| 89 |
_unpack_tar(z, destination) |
| 90 |
|
| 91 |
|
| 92 |
def unpack_zip(filename: str, destination: StrPath) -> None: |
| 93 |
"""Unpack the zip file to the destination""" |
| 94 |
import zipfile |
| 95 |
|
| 96 |
with zipfile.ZipFile(filename) as z: |
| 97 |
members: list[zipfile.ZipInfo] = [] |
| 98 |
for member in z.infolist(): |
| 99 |
parts = member.filename.lstrip("/").split("/") |
| 100 |
member.filename = "/".join(parts[1:]) |
| 101 |
if member.filename: |
| 102 |
members.append(member) |
| 103 |
|
| 104 |
z.extractall(destination, members=members) |
| 105 |
|
| 106 |
|
| 107 |
def get_available_arch_platforms() -> tuple[list[str], list[str]]: |
| 108 |
from ._versions import PYTHON_VERSIONS |
| 109 |
|
| 110 |
archs: set[str] = set() |
| 111 |
platforms: set[str] = set() |
| 112 |
for items in PYTHON_VERSIONS.values(): |
| 113 |
for item in items: |
| 114 |
platforms.add(item[0]) |
| 115 |
archs.add(item[1]) |
| 116 |
return sorted(archs), sorted(platforms) |