Skip to content

Commit

Permalink
Include classic diskutil list to get physical disks when -all doe…
Browse files Browse the repository at this point in the history
…sn't
  • Loading branch information
dormant-user committed Jan 2, 2025
1 parent 2d6c58f commit e48ec55
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyarchitecture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pyarchitecture import cpu, disks, gpu, memory

version = "0.2.0"
version = "0.2.1"


def all_components() -> Dict[str, Any]:
Expand Down
34 changes: 29 additions & 5 deletions pyarchitecture/disks/macOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import subprocess
from collections import defaultdict
from collections.abc import Generator
from typing import Dict, List

from pyarchitecture import squire
Expand Down Expand Up @@ -80,19 +81,42 @@ def parse_diskutil_output(stdout: str) -> List[Dict[str, str]]:
return disks


def base_physical_device_id(disk_lib: str | os.PathLike) -> Generator[str]:
"""Get base physical device IDs for macOS devices.
Args:
disk_lib: Disk library path.
Yields:
str:
Yields base physical device IDs.
"""
result = subprocess.run([disk_lib, "list"], capture_output=True, text=True)
for line in result.stdout.splitlines():
if (
(line := line.strip())
and "physical" in line
and line.startswith("/dev/disk")
):
yield line.split()[0]


def drive_info(disk_lib: str | os.PathLike) -> List[Dict[str, str | List[str]]]:
"""Get disks attached to macOS devices.
Returns:
List[Dict[str, str | List[str]]]:
Returns disks information for macOS devices.
"""
result = subprocess.run([disk_lib, "info", "-all"], capture_output=True, text=True)
disks = parse_diskutil_output(result.stdout)
all_disk_info = subprocess.run(
[disk_lib, "info", "-all"], capture_output=True, text=True
)
all_disks = parse_diskutil_output(all_disk_info.stdout)
device_ids = defaultdict(list)
physical_disks = []
for disk in disks:
if disk.get("Virtual") == "No":
physical_disk_ids = list(base_physical_device_id(disk_lib))
for disk in all_disks:
if disk.get("Virtual") == "No" or disk.get("Device Node") in physical_disk_ids:
physical_disks.append(
{
"name": disk.get("Device / Media Name"),
Expand All @@ -105,7 +129,7 @@ def drive_info(disk_lib: str | os.PathLike) -> List[Dict[str, str | List[str]]]:
)
# Instantiate default dict with keys as DeviceIDs and values as empty list
_ = device_ids[disk["Device Identifier"]]
mountpoints = update_mountpoints(disks, device_ids)
mountpoints = update_mountpoints(all_disks, device_ids)
for disk in physical_disks:
disk["mountpoints"] = mountpoints[disk["device_id"]]
return physical_disks

0 comments on commit e48ec55

Please sign in to comment.