# This file is part of craft-platforms.## Copyright 2024 Canonical Ltd.## This program is free software: you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License version 3, as# published by the Free Software Foundation.## This program is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.# See the GNU Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public License along# with this program. If not, see <http://www.gnu.org/licenses/>."""Architecture related utilities."""from__future__importannotationsimportenumimportplatformfromtypingimportLiteral,Optional,Tuple,Unionfromtyping_extensionsimportSelffromcraft_platformsimport_distro
[docs]@classmethoddeffrom_machine(cls,arch:str)->Self:"""Get a DebianArchitecture value from the given platform arch. :param arch: a string containing an architecture as returned by platform.machine() :returns: The DebianArchitecture enum value :raises: ValueError if the architecture is not a valid Debian architecture. """returncls(_ARCH_TRANSLATIONS_PLATFORM_TO_DEB.get(arch.lower(),arch.lower()))
[docs]@classmethoddeffrom_host(cls)->Self:"""Get the DebianArchitecture of the running host."""returncls.from_machine(platform.machine())
[docs]defto_platform_arch(self)->str:"""Convert this DebianArchitecture to a platform string. :returns: A string matching what platform.machine() or uname -m would return. """return_ARCH_TRANSLATIONS_DEB_TO_PLATFORM.get(self.value,self.value)
# architecture translations from the platform syntax to the deb/snap syntax_ARCH_TRANSLATIONS_PLATFORM_TO_DEB={"aarch64":"arm64","armv7l":"armhf","i686":"i386","ppc":"powerpc","ppc64le":"ppc64el","x86_64":"amd64",}# architecture translations from the deb/snap syntax to the platform syntax_ARCH_TRANSLATIONS_DEB_TO_PLATFORM={deb:platformforplatform,debin_ARCH_TRANSLATIONS_PLATFORM_TO_DEB.items()}defparse_base_and_architecture(arch:str,)->Tuple[Optional[_distro.DistroBase],Union[DebianArchitecture,Literal["all"]]]:"""Get the debian arch and optional base from an architecture entry. The architecture may have an optional base prefixed as '[<base>:]<arch>'. :param arch: The architecture entry. :returns: A tuple of the DistroBase and the architecture. The architecture is either a DebianArchitecture or 'all'. :raises ValueError: If the architecture or base is invalid. """if":"inarch:base_str,_,arch_str=arch.partition(":")base=_distro.DistroBase.from_str(base_str)else:base=Nonearch_str=archtry:returnbase,DebianArchitecture(arch_str)ifarch_str!="all"else"all"exceptValueError:raiseValueError(f"{arch_str!r} is not a valid Debian architecture.")fromNone