pmb.parse.arch: Break out marchine type to arch into separate function (MR 2282)

Sometimes it's useful to map an arbitrary machine type to an Alpine
architecture. alpine_native only lets you get the Alpine architecture
mapped to the machine type of the system pmbootstrap is running on. As
such, break out this functionality into a new function that takes the
machine type as a parameter.
This commit is contained in:
Newbyte 2024-03-23 14:49:20 +01:00
parent ced93fee7b
commit 1fc83f8bce
No known key found for this signature in database
GPG key ID: 8A700086A9FE41FD

View file

@ -8,19 +8,7 @@ import pmb.parse.arch
def alpine_native(): def alpine_native():
machine = platform.machine() machine = platform.machine()
mapping = { return machine_type_to_alpine(machine)
"i686": "x86",
"x86_64": "x86_64",
"aarch64": "aarch64",
"arm64": "aarch64",
"armv6l": "armhf",
"armv7l": "armv7",
"armv8l": "armv7",
}
if machine in mapping:
return mapping[machine]
raise ValueError("Can not map platform.machine '" + machine + "'"
" to the right Alpine Linux architecture")
def from_chroot_suffix(args, suffix): def from_chroot_suffix(args, suffix):
@ -123,3 +111,21 @@ def cpu_emulation_required(arch):
# No match: then it's required # No match: then it's required
return True return True
def machine_type_to_alpine(machine_type: str) -> str:
"""Translate a machine type to an Alpine architecture. A machine type can come from
for example `$ uname -m` or platform.machine() from Python's standard library."""
mapping = {
"i686": "x86",
"x86_64": "x86_64",
"aarch64": "aarch64",
"arm64": "aarch64",
"armv6l": "armhf",
"armv7l": "armv7",
"armv8l": "armv7",
}
if machine_type in mapping:
return mapping[machine_type]
raise ValueError(f"Can not map machine type '{machine_type}'"
" to the right Alpine Linux architecture")