Skip to content

Commit 1662ad3

Browse files
committed
Simplify capability registration
1 parent bedfecd commit 1662ad3

File tree

6 files changed

+98
-129
lines changed

6 files changed

+98
-129
lines changed

solc_select/models.py

Lines changed: 13 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
import platform
99
import sys
10-
from collections.abc import Callable
11-
from dataclasses import dataclass, field
10+
from dataclasses import dataclass
1211
from pathlib import Path
1312
from typing import ClassVar
1413

@@ -20,84 +19,13 @@
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

solc_select/platform_capabilities.py

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,86 @@
66
"""
77

88
import subprocess
9+
from collections.abc import Callable
10+
from dataclasses import dataclass, field
911

10-
from .models import EmulationCapability, Platform, PlatformCapability, PlatformIdentifier
12+
# ========================================
13+
# CAPABILITY DATACLASSES
14+
# ========================================
15+
16+
17+
@dataclass(frozen=True)
18+
class PlatformIdentifier:
19+
"""Unique identifier for a platform (OS + architecture).
20+
21+
Examples: 'linux-amd64', 'darwin-arm64', 'windows-amd64'
22+
"""
23+
24+
os_type: str # 'linux', 'darwin', 'windows'
25+
architecture: str # 'amd64', 'arm64', '386'
26+
27+
28+
@dataclass(frozen=True)
29+
class EmulationCapability:
30+
"""Describes emulation support for running foreign platform binaries.
31+
32+
Example: Linux ARM64 can run linux-amd64 binaries via QEMU.
33+
"""
34+
35+
target_platform: PlatformIdentifier # Platform that can be emulated
36+
emulation_type: str # 'rosetta', 'qemu'
37+
detector: Callable[[], bool] # Function to check if emulation available
38+
command_prefix: list[str] # Command prefix for emulation (e.g., ["qemu-x86_64"])
39+
performance_note: str | None = None # Warning message for users
40+
41+
42+
@dataclass
43+
class PlatformCapability:
44+
"""Declares which platforms a device can execute binaries for.
45+
46+
Supports both native execution and emulated platforms.
47+
48+
Example for Linux ARM64 with QEMU:
49+
- native_support: linux-arm64
50+
- emulation_capabilities: [linux-amd64 via QEMU]
51+
"""
52+
53+
host_platform: PlatformIdentifier # The actual hardware platform
54+
native_support: PlatformIdentifier # Always can run native binaries
55+
emulation_capabilities: list[EmulationCapability] = field(default_factory=list)
56+
57+
def get_runnable_platforms(self) -> list[PlatformIdentifier]:
58+
"""Get all platforms this device can execute, prioritized.
59+
60+
Returns native first, then emulated platforms (only if emulator available).
61+
62+
Returns:
63+
List of PlatformIdentifier, native first
64+
"""
65+
platforms = [self.native_support]
66+
67+
# Add emulated platforms with available emulators
68+
for ec in self.emulation_capabilities:
69+
if ec.detector():
70+
platforms.append(ec.target_platform)
71+
72+
return platforms
73+
74+
def get_emulation_for_platform(self, target: PlatformIdentifier) -> EmulationCapability | None:
75+
"""Get emulation info for a target platform.
76+
77+
Args:
78+
target: Platform to check
79+
80+
Returns:
81+
EmulationCapability if target requires emulation, None if native
82+
"""
83+
if target == self.native_support:
84+
return None
85+
return next(
86+
(ec for ec in self.emulation_capabilities if ec.target_platform == target),
87+
None,
88+
)
1189

1290
# ========================================
1391
# EMULATION DETECTORS
@@ -79,22 +157,3 @@ def detect_qemu() -> bool:
79157
),
80158
],
81159
)
82-
83-
84-
# ========================================
85-
# CAPABILITY REGISTRATION
86-
# ========================================
87-
88-
89-
def register_capabilities() -> None:
90-
"""Register all platform capabilities with the Platform class.
91-
92-
This function should be called at module import time to ensure
93-
capabilities are available when Platform.get_capability() is called.
94-
"""
95-
Platform.register_capability(DARWIN_ARM64_CAPABILITY)
96-
Platform.register_capability(LINUX_ARM64_CAPABILITY)
97-
98-
99-
# Auto-register capabilities when module is imported
100-
register_capabilities()

solc_select/repository_registry.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
specifying what platforms and version ranges each repository supports.
66
"""
77

8-
from .models import (
9-
PlatformIdentifier,
10-
PlatformSupport,
11-
RepositoryManifest,
12-
VersionRange,
13-
)
8+
from .models import PlatformSupport, RepositoryManifest, VersionRange
9+
from .platform_capabilities import PlatformIdentifier
1410

1511
# ========================================
1612
# SOLIDITYLANG REPOSITORY (Primary Source)

solc_select/services/artifact_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
from ..exceptions import ChecksumMismatchError, SolcSelectError
1919
from ..infrastructure.filesystem import FilesystemManager
20-
from ..models import Platform, PlatformCapability, SolcArtifact, SolcArtifactOnDisk, SolcVersion
20+
from ..models import Platform, SolcArtifact, SolcArtifactOnDisk, SolcVersion
21+
from ..platform_capabilities import PlatformCapability
2122
from .repository_matcher import RepositoryMatcher
2223

2324

solc_select/services/repository_matcher.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
import requests
99

1010
from ..exceptions import VersionNotFoundError
11-
from ..models import (
12-
Platform,
13-
PlatformCapability,
14-
PlatformIdentifier,
15-
RepositoryManifest,
16-
SolcVersion,
17-
)
11+
from ..models import Platform, RepositoryManifest, SolcVersion
12+
from ..platform_capabilities import PlatformCapability, PlatformIdentifier
1813
from ..repositories import (
1914
AlloyRepository,
2015
CryticRepository,

solc_select/services/solc_service.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import subprocess
99
import sys
1010

11-
# Import platform_capabilities to ensure capabilities are registered
12-
import solc_select.platform_capabilities # noqa: F401
13-
1411
from ..exceptions import (
1512
ArchitectureUpgradeError,
1613
InstallationError,

0 commit comments

Comments
 (0)