77
88import platform
99import sys
10- from collections .abc import Callable
11- from dataclasses import dataclass , field
10+ from dataclasses import dataclass
1211from pathlib import Path
1312from typing import ClassVar
1413
2019 MACOSX_AMD64 ,
2120 WINDOWS_AMD64 ,
2221)
23-
24- # ========================================
25- # PLATFORM CAPABILITY MODELS
26- # ========================================
27-
28-
29- @dataclass (frozen = True )
30- class PlatformIdentifier :
31- """Unique identifier for a platform (OS + architecture).
32-
33- Examples: 'linux-amd64', 'darwin-arm64', 'windows-amd64'
34- """
35-
36- os_type : str # 'linux', 'darwin', 'windows'
37- architecture : str # 'amd64', 'arm64', '386'
38-
39-
40- @dataclass (frozen = True )
41- class EmulationCapability :
42- """Describes emulation support for running foreign platform binaries.
43-
44- Example: Linux ARM64 can run linux-amd64 binaries via QEMU.
45- """
46-
47- target_platform : PlatformIdentifier # Platform that can be emulated
48- emulation_type : str # 'rosetta', 'qemu'
49- detector : Callable [[], bool ] # Function to check if emulation available
50- command_prefix : list [str ] # Command prefix for emulation (e.g., ["qemu-x86_64"])
51- performance_note : str | None = None # Warning message for users
52-
53-
54- @dataclass
55- class PlatformCapability :
56- """Declares which platforms a device can execute binaries for.
57-
58- Supports both native execution and emulated platforms.
59-
60- Example for Linux ARM64 with QEMU:
61- - native_support: linux-arm64
62- - emulation_capabilities: [linux-amd64 via QEMU]
63- """
64-
65- host_platform : PlatformIdentifier # The actual hardware platform
66- native_support : PlatformIdentifier # Always can run native binaries
67- emulation_capabilities : list [EmulationCapability ] = field (default_factory = list )
68-
69- def get_runnable_platforms (self ) -> list [PlatformIdentifier ]:
70- """Get all platforms this device can execute, prioritized.
71-
72- Returns native first, then emulated platforms (only if emulator available).
73-
74- Returns:
75- List of PlatformIdentifier, native first
76- """
77- platforms = [self .native_support ]
78-
79- # Add emulated platforms with available emulators
80- for ec in self .emulation_capabilities :
81- if ec .detector ():
82- platforms .append (ec .target_platform )
83-
84- return platforms
85-
86- def get_emulation_for_platform (self , target : PlatformIdentifier ) -> EmulationCapability | None :
87- """Get emulation info for a target platform.
88-
89- Args:
90- target: Platform to check
91-
92- Returns:
93- EmulationCapability if target requires emulation, None if native
94- """
95- if target == self .native_support :
96- return None
97- return next (
98- (ec for ec in self .emulation_capabilities if ec .target_platform == target ),
99- None ,
100- )
22+ from .platform_capabilities import (
23+ DARWIN_ARM64_CAPABILITY ,
24+ LINUX_ARM64_CAPABILITY ,
25+ EmulationCapability ,
26+ PlatformCapability ,
27+ PlatformIdentifier ,
28+ )
10129
10230
10331@dataclass (frozen = True )
@@ -242,8 +170,11 @@ class Platform:
242170 os_type : str # 'linux', 'darwin', 'windows'
243171 architecture : str # 'amd64', 'arm64'
244172
245- # Class-level capability registry
246- _capability_registry : ClassVar [dict [str , PlatformCapability ]] = {}
173+ # Class-level capability registry (hardcoded for darwin-arm64 and linux-arm64)
174+ _capability_registry : ClassVar [dict [str , PlatformCapability ]] = {
175+ "darwin-arm64" : DARWIN_ARM64_CAPABILITY ,
176+ "linux-arm64" : LINUX_ARM64_CAPABILITY ,
177+ }
247178
248179 def __post_init__ (self ) -> None :
249180 """Validate platform components."""
@@ -255,16 +186,6 @@ def __post_init__(self) -> None:
255186 if self .architecture not in valid_arch :
256187 raise ValueError (f"Invalid architecture: { self .architecture } " )
257188
258- @classmethod
259- def register_capability (cls , capability : PlatformCapability ) -> None :
260- """Register a platform capability configuration.
261-
262- Args:
263- capability: PlatformCapability to register
264- """
265- key = f"{ capability .host_platform .os_type } -{ capability .host_platform .architecture } "
266- cls ._capability_registry [key ] = capability
267-
268189 def get_capability (self ) -> PlatformCapability :
269190 """Get the capability declaration for this platform.
270191
0 commit comments