1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-13 11:29:46 +03:00

core: add an Arch type (MR 2252)

Move pmb/parse/arch.py over to core and refactor it as an Arch type,
similar to how Chroot was done. Fix all the uses (that I can find) of
arch in the codebase that need adjusting.

The new Arch type is an Enum, making it clear what architectures can be
represented and making it much easier to reason about. Since we support
~5 (kinda) different representations of an Architecture (Alpine, Kernel,
target triple, platform, and QEMU), we now formalise that the Alpine
format is what we represent internally, with methods to convert to any
of the others as-needed.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-06-08 21:27:27 +02:00 committed by Oliver Smith
parent 505165dc13
commit 866e5bcfab
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
42 changed files with 389 additions and 303 deletions

View file

@ -3,9 +3,10 @@
from __future__ import annotations
import enum
from typing import Generator, Optional
from typing import Generator, Optional, Union
from pathlib import Path, PosixPath, PurePosixPath
import pmb.config
from pmb.core.arch import Arch
from .context import get_context
class ChrootType(enum.Enum):
@ -21,10 +22,10 @@ class Chroot:
__type: ChrootType
__name: str
def __init__(self, suffix_type: ChrootType, name: Optional[str] = ""):
def __init__(self, suffix_type: ChrootType, name: Optional[Union[str, Arch]] = ""):
self.__type = suffix_type
self.__name = name or ""
self.__name = str(name or "")
self.__validate()
def __validate(self) -> None:
@ -74,16 +75,15 @@ class Chroot:
@property
# FIXME: make an Arch type
def arch(self) -> str:
def arch(self) -> Arch:
if self.type == ChrootType.NATIVE:
return pmb.config.arch_native
return Arch.native()
if self.type == ChrootType.BUILDROOT:
return self.name()
return Arch.from_str(self.name())
# FIXME: this is quite delicate as it will only be valid
# for certain pmbootstrap commands... It was like this
# before but it should be fixed.
arch = pmb.core.get_context().device_arch
arch = pmb.parse.deviceinfo().arch
if arch is not None:
return arch
@ -118,8 +118,12 @@ class Chroot:
def __rtruediv__(self, other: object) -> Path:
if isinstance(other, PosixPath) or isinstance(other, PurePosixPath):
# Important to produce a new Path object here, otherwise we
# end up with one object getting shared around and modified
# and lots of weird stuff happens.
return Path(other) / self.path
if isinstance(other, str):
# This implicitly creates a new Path object
return other / self.path
return NotImplemented
@ -139,7 +143,7 @@ class Chroot:
@staticmethod
def buildroot(arch: str) -> Chroot:
def buildroot(arch: Arch) -> Chroot:
return Chroot(ChrootType.BUILDROOT, arch)