platforms.darwin¶
macOS platform implementation: system information, command support, defaults management, and launchd service management.
DarwinPlatformInfo¶
- class DarwinPlatformInfo[source]¶
Bases:
objectmacOS implementation of PlatformInfo.
Retrieves system information using macOS-specific binaries: - system_profiler for serial numbers - stat / id for console user details - id -F for user full names
- get_serial_number() str | None[source]¶
Get serial number via system_profiler.
- Returns:
Hardware serial number, or None on failure
- Return type:
str | None
- get_console_user() tuple[str, int, Path] | None[source]¶
Get the currently logged-in console user on macOS.
Uses
stat -f%Su /dev/consoleto determine the console owner, thenid -ufor the UID, and constructs the home path under /Users/.
- get_user_full_name(username: str) str | None[source]¶
Get full name for a user via
id -F(macOS-specific).
- get_os_version_label() str[source]¶
Return macOS version label for logging.
Reads the marketing macOS productVersion (e.g. “15.4.1”, “26.4.1”) from
platform.mac_ver().platform.release()was used previously but returns the Darwin kernel version (e.g. “25.4.0” on macOS 26.4.1), which is not what users expect to see in logs.- Returns:
e.g. “macOS Version: 26.4.1”
- Return type:
DarwinCommandSupport¶
- class DarwinCommandSupport[source]¶
Bases:
objectmacOS implementation of PlatformCommandSupport.
Uses
launchctl asuserto run commands in the context of a logged-in user.- property min_user_uid: int[source]¶
Minimum UID for non-system accounts on macOS (500).
- Returns:
500
- Return type:
- run_as_user_command(command: list[str], username: str, uid: int) list[str][source]¶
Wrap command with
launchctl asuserfor macOS.
DarwinDefaults¶
- class DarwinDefaults(runner: CommandRunner | None = None)[source]¶
Bases:
objectRead, write, and delete macOS user defaults (plist) values.
Wraps
/usr/bin/defaultsfor non-raising plist operations: all methods returnNoneorFalseon failure instead of raising.Operations run in the calling process’s context by default (i.e. as root when the script is invoked under MDM). Pass a
CommandRunnerwith ausername/uidto allow per-callas_user=Truereads/writes against the logged-in user’s domain.- Example:
>>> # Root context (or whoever the script runs as) >>> defaults = DarwinDefaults() >>> defaults.read("com.apple.SoftwareUpdate", "AutomaticCheckEnabled") '1'
>>> # User context — pipe through a configured CommandRunner >>> runner = CommandRunner(logger=logger, username="jappleseed", uid=501) >>> defaults = DarwinDefaults(runner=runner) >>> defaults.read("com.apple.dock", "orientation", as_user=True) 'bottom' >>> defaults.write("com.apple.dock", "tilesize", "48", "-int", as_user=True) True
- Parameters:
runner (CommandRunner | None)
- read(domain: str, key: str, *, as_user: bool = False) str | None[source]¶
Read a defaults value by domain and key.
- Parameters:
- Returns:
Value as string, or None if the key doesn’t exist
- Return type:
str | None
- write(domain: str, key: str, value: str, value_type: str = '-string', *, as_user: bool = False) bool[source]¶
Write a defaults value.
DarwinServiceManager¶
- class DarwinServiceManager[source]¶
Bases:
objectManage launchd services on macOS.
Wraps
/bin/launchctlfor checking, loading, and unloading services. Targets use the launchctl domain-target format:system/com.example.daemonorgui/<uid>/com.example.agent.- Example:
>>> if DarwinServiceManager.is_loaded("system/com.example.daemon"): ... DarwinServiceManager.bootout("system/com.example.daemon")