Platform
provide.foundation.platform
¶
TODO: Add module docstring.
Classes¶
PlatformError
¶
PlatformError(
message: str,
*,
platform: str | None = None,
operation: str | None = None,
**kwargs: Any
)
Bases: FoundationError
Raised when platform detection or system operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message describing the platform issue. |
required |
platform
|
str | None
|
Optional platform identifier. |
None
|
operation
|
str | None
|
Optional operation that failed. |
None
|
**kwargs
|
Any
|
Additional context passed to FoundationError. |
{}
|
Examples:
>>> raise PlatformError("Failed to detect OS")
>>> raise PlatformError("Unsupported platform", platform="freebsd")
Source code in provide/foundation/errors/platform.py
SystemInfo
¶
System information container.
Functions¶
get_arch_name
¶
Get normalized architecture name.
Returns:
| Type | Description |
|---|---|
str
|
Normalized architecture (amd64, arm64, x86, i386) |
Source code in provide/foundation/platform/detection.py
get_cpu_brand
¶
Get CPU brand string.
Returns:
| Type | Description |
|---|---|
str
|
CPU brand string (e.g., "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz") |
Example
from provide.foundation.platform import get_cpu_brand get_cpu_brand() 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz'
Source code in provide/foundation/platform/cpu.py
get_cpu_count
¶
Get number of logical CPUs.
Returns:
| Type | Description |
|---|---|
int | None
|
Number of logical CPUs, or None if not available |
Example
from provide.foundation.platform import get_cpu_count get_cpu_count() 8
Source code in provide/foundation/platform/cpu.py
get_cpu_flags
¶
Get list of CPU flags/features.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of CPU flags (e.g., ["sse", "avx", "avx2"]), or empty list if not available |
Example
from provide.foundation.platform import get_cpu_flags flags = get_cpu_flags() 'avx2' in flags True
Source code in provide/foundation/platform/cpu.py
get_cpu_info
¶
Get detailed CPU information.
Returns comprehensive CPU information including brand, vendor, architecture, flags, and feature support.
Returns:
| Type | Description |
|---|---|
dict[str, str | int | list[str] | None]
|
Dictionary containing CPU information. Keys include: |
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
|
dict[str, str | int | list[str] | None]
|
When py-cpuinfo is not available, returns basic information from |
dict[str, str | int | list[str] | None]
|
platform module. |
Example
from provide.foundation.platform import get_cpu_info info = get_cpu_info() info['brand'] 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz' 'avx2' in info.get('flags', []) True
Source code in provide/foundation/platform/cpu.py
get_cpu_type
¶
Get CPU type/family information.
Returns:
| Type | Description |
|---|---|
str | None
|
CPU type string or None if unavailable |
Source code in provide/foundation/platform/detection.py
get_os_name
¶
Get normalized OS name.
Returns:
| Type | Description |
|---|---|
str
|
Normalized OS name (darwin, linux, windows) |
Source code in provide/foundation/platform/detection.py
get_os_version
¶
Get OS version information.
Returns:
| Type | Description |
|---|---|
str | None
|
OS version string or None if unavailable |
Source code in provide/foundation/platform/detection.py
get_platform_string
¶
Get normalized platform string in format 'os_arch'.
Returns:
| Type | Description |
|---|---|
str
|
Platform string like 'darwin_arm64' or 'linux_amd64' |
Source code in provide/foundation/platform/detection.py
get_system_info
¶
Gather comprehensive system information.
Returns:
| Type | Description |
|---|---|
SystemInfo
|
SystemInfo object with all available system details |
Source code in provide/foundation/platform/info.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
has_cpu_flag
¶
Check if CPU has a specific flag/feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
str
|
Flag name to check (e.g., "avx2", "sse4_2") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if CPU has the flag, False otherwise |
Example
from provide.foundation.platform import has_cpu_flag has_cpu_flag('avx2') True has_cpu_flag('avx512f') False
Source code in provide/foundation/platform/cpu.py
has_cpuinfo
¶
Check if py-cpuinfo is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if py-cpuinfo is available, False otherwise |
Example
from provide.foundation.platform import has_cpuinfo if has_cpuinfo(): ... # Use detailed CPU features ... pass
Source code in provide/foundation/platform/cpu.py
has_systemd
¶
Check if systemd integration is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if running on Linux and sdnotify is installed, False otherwise |
Example
from provide.foundation.platform import has_systemd if has_systemd(): ... # Use systemd features ... pass
Source code in provide/foundation/platform/systemd.py
is_64bit
¶
is_arm
¶
is_linux
¶
is_macos
¶
is_windows
¶
normalize_platform_components
¶
Normalize OS and architecture names to standard format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
os_name
|
str
|
Operating system name |
required |
arch_name
|
str
|
Architecture name |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
Tuple of (normalized_os, normalized_arch) |
Source code in provide/foundation/platform/detection.py
notify_error
¶
Notify systemd of an error condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errno
|
int
|
Error number (errno value) |
required |
message
|
str | None
|
Optional error message |
None
|
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
Example
from provide.foundation.platform import notify_error import errno notify_error(errno.ECONNREFUSED, "Failed to connect to database") True
Source code in provide/foundation/platform/systemd.py
notify_ready
¶
Notify systemd that the service is ready.
This should be called once the service has completed initialization and is ready to handle requests. For Type=notify services, systemd waits for this notification before considering the service started.
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
bool
|
or running in test mode |
Example
from provide.foundation.platform import notify_ready
After service initialization¶
notify_ready() True
Source code in provide/foundation/platform/systemd.py
notify_reloading
¶
Notify systemd that the service is reloading configuration.
Call this at the beginning of configuration reload, and call notify_ready() when reload is complete.
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
Example
from provide.foundation.platform import notify_reloading, notify_ready notify_reloading() True
Reload configuration¶
notify_ready() True
Source code in provide/foundation/platform/systemd.py
notify_status
¶
Send status text to systemd.
The status will be visible in systemctl status output.
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
Status message to send to systemd |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
Example
from provide.foundation.platform import notify_status notify_status("Processing requests: 42 active connections") True
Source code in provide/foundation/platform/systemd.py
notify_stopping
¶
Notify systemd that the service is stopping.
Call this at the beginning of graceful shutdown.
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
Example
from provide.foundation.platform import notify_stopping
At shutdown¶
notify_stopping() True
Perform cleanup¶
Exit¶
Source code in provide/foundation/platform/systemd.py
notify_watchdog
¶
Send watchdog keepalive to systemd.
If WatchdogSec is configured in the systemd service unit, the service must call this periodically to prevent systemd from considering it hung and restarting it.
Automatically disabled in test mode (via @skip_in_test_mode decorator).
Returns:
| Type | Description |
|---|---|
bool
|
True if notification sent successfully, False if sdnotify not available |
Example
from provide.foundation.platform import notify_watchdog import asyncio async def watchdog_loop(): ... while True: ... await asyncio.sleep(10) # Must be < WatchdogSec ... notify_watchdog()