Refactor get_disks_macos() to improve performance

Found a way to easily exclude disk images from the list under El Cap
This commit is contained in:
2Shirt 2022-05-20 19:44:12 -07:00
parent 544ffb1aff
commit 29d4e80f7e
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -3,6 +3,7 @@
import logging
import pathlib
import platform
import plistlib
import re
@ -331,12 +332,12 @@ def get_disks_macos() -> list[Disk]:
cmd = ['diskutil', 'list', '-plist', 'physical']
disks = []
# El Capitan workaround
if platform.mac_ver()[0].startswith('10.11'):
cmd.pop()
# Get info from diskutil
proc = run_program(cmd, encoding=None, errors=None, check=False)
if proc.returncode != 0:
# Assuming we're running on an older macOS version
cmd.pop(-1)
proc = run_program(cmd, encoding=None, errors=None, check=False)
# Parse plist data
try:
@ -347,13 +348,20 @@ def get_disks_macos() -> list[Disk]:
return disks
# Add valid disks
for disk in plist_data['WholeDisks']:
disks.append(Disk(f'/dev/{disk}'))
for disk in plist_data['AllDisksAndPartitions']:
name = disk['DeviceIdentifier']
if name not in plist_data['WholeDisks']:
# Only check "WholeDisks"
continue
if not disk['Content']:
# This lists GPT or MBR for device, blank should mean it's an image
continue
disks.append(Disk(f'/dev/{name}'))
# Remove virtual disks
# TODO: Test more to figure out why some drives are being marked 'Unknown'
disks = [
d for d in disks if d.details.get('VirtualOrPhysical') != 'Virtual'
d for d in disks if d.raw_details.get('VirtualOrPhysical') != 'Virtual'
]
# Done