diff --git a/pyarchitecture/__init__.py b/pyarchitecture/__init__.py index fbd1fd3..8e99d1c 100644 --- a/pyarchitecture/__init__.py +++ b/pyarchitecture/__init__.py @@ -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]: diff --git a/pyarchitecture/disks/macOS.py b/pyarchitecture/disks/macOS.py index 36fcb7c..139523d 100644 --- a/pyarchitecture/disks/macOS.py +++ b/pyarchitecture/disks/macOS.py @@ -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 @@ -80,6 +81,26 @@ 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. @@ -87,12 +108,15 @@ def drive_info(disk_lib: str | os.PathLike) -> List[Dict[str, str | List[str]]]: 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"), @@ -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