Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion craft_platforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Package base for craft_platforms."""

from ._architectures import DebianArchitecture, parse_base_and_architecture
from ._architectures import (
DebianArchitecture,
GoArchitectureValue,
GoArchitecture,
parse_base_and_architecture,
)
from ._build import get_build_plan
from ._buildinfo import BuildInfo
from . import charm, rock, snap
Expand Down
32 changes: 31 additions & 1 deletion craft_platforms/_architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import enum
import platform
from typing import Literal, Optional, Tuple, Union
from typing import Literal, NamedTuple, Optional, Tuple, Union

from typing_extensions import Self

Expand Down Expand Up @@ -70,6 +70,36 @@ def to_platform_arch(self) -> str:
"""
return _ARCH_TRANSLATIONS_DEB_TO_PLATFORM.get(self.value, self.value)

def to_go(self) -> GoArchitecture:
"""Convert this architecture value to a Go architecture."""
return GoArchitecture[self.name]


class GoArchitectureValue(NamedTuple):
"""A value for a single Go architecture."""

arch: str
variant: str | None = None


class GoArchitecture(enum.Enum):
"""An architecture for Go.

The names of these values are Debian architecture names; the values are Go arches.
"""

AMD64 = GoArchitectureValue("amd64")
ARM64 = GoArchitectureValue("arm64", "v8")
ARMHF = GoArchitectureValue("arm", "v7")
I386 = GoArchitectureValue("386")
PPC64EL = GoArchitectureValue("ppc64le")
RISCV64 = GoArchitectureValue("riscv64")
S390X = GoArchitectureValue("s390x")

def to_debian(self) -> DebianArchitecture:
"""Convert this go architecture to a Debian architecture."""
return DebianArchitecture[self.name]


# architecture translations from the platform syntax to the deb/snap syntax
_ARCH_TRANSLATIONS_PLATFORM_TO_DEB = {
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import pytest
from craft_platforms import DebianArchitecture, DistroBase, parse_base_and_architecture
from craft_platforms._architectures import GoArchitecture
from craft_platforms.test import strategies
from hypothesis import given

Expand Down Expand Up @@ -94,6 +95,24 @@ def test_fuzz_parse_base_and_architecture(base, arch):
assert out_arch == arch


@pytest.mark.parametrize(
("go_arch", "debian_arch"),
[
# All pairs
*zip(GoArchitecture, DebianArchitecture),
# Specific examples
(GoArchitecture.PPC64EL, DebianArchitecture.PPC64EL),
(GoArchitecture.RISCV64, DebianArchitecture.RISCV64),
],
)
def test_convert_arch(go_arch: GoArchitecture, debian_arch: DebianArchitecture):
assert go_arch.to_debian() == debian_arch
assert debian_arch.to_go() == go_arch

assert debian_arch.to_go().to_debian() == debian_arch
assert go_arch.to_debian().to_go() == go_arch


@pytest.mark.parametrize("deb_arch", list(DebianArchitecture))
def test_debian_architecture_repr(deb_arch: DebianArchitecture):
"""Test that the repr of DebianArchitectures is the repr of their string value."""
Expand Down