Skip to content

Commit

Permalink
Set mountpoints specific to partitions on Linux OS
Browse files Browse the repository at this point in the history
Add some todos for simplifications
  • Loading branch information
dormant-user committed Jan 3, 2025
1 parent 6d64615 commit e717b66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
47 changes: 25 additions & 22 deletions pyudisk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ def parse_block_devices(
block_devices = {}
block = None
category = None
block_partitions = {
f"{linux.BlockDevices.head}{block_device.device.split('/')[-1]}:": block_device
block_partitions = [
f"{linux.BlockDevices.head}{block_device.device.split('/')[-1]}:"
for block_device in get_partitions(env)
}
]
for line in input_data.splitlines():
if matching_block := block_partitions.get(line):
# Assing a temp value to avoid skipping loop when 'block' has a value
block = matching_block
if line in block_partitions:
# Assigning a placeholder value to avoid skipping loop when 'block' has a value
# This should be a unique value for each partition
# block = str(time.time_ns()) - another alternative
block = line
block_devices[block] = {}
elif block and line.strip() in (
linux.BlockDevices.category1,
Expand Down Expand Up @@ -180,7 +182,13 @@ def parse_block_devices(
)
):
block_devices[block][key] = val
return block_devices
block_devices_updated = {}
for _, value in block_devices.items():
if block_devices_updated.get(value['Drive']):
block_devices_updated[value['Drive']].append(value)
else:
block_devices_updated[value['Drive']] = [value]
return block_devices_updated


def get_disk_data_macos(env: EnvConfig) -> Generator[Dict[str, str | List[str]]]:
Expand Down Expand Up @@ -326,16 +334,12 @@ def smart_metrics(env: EnvConfig) -> Generator[linux.Disk | darwin.Disk]:
LOGGER.error(error.errors())
return
smart_dump = get_smart_metrics(env)
block_devices = dict(
sorted(
parse_block_devices(env, smart_dump).items(),
key=lambda device: device[1]["Drive"],
)
)
block_devices = parse_block_devices(env, smart_dump)
drives = {k: v for k, v in sorted(parse_drives(smart_dump).items())}
if len(block_devices) != len(drives):
# A drive can have multiple partitions, but any mounted drive should have at least one partition
if len(block_devices) < len(drives):
LOGGER.warning(
f"Number of block devices [{len(block_devices)}] does not match the number of drives [{len(drives)}]"
f"Number of block devices [{len(block_devices)}] is less than the number of drives [{len(drives)}]"
)
device_names = set(v["Drive"] for v in block_devices.values())
drive_names = set(drives.keys())
Expand All @@ -355,17 +359,14 @@ def smart_metrics(env: EnvConfig) -> Generator[linux.Disk | darwin.Disk]:
for key in optional_fields:
if key not in drive.keys():
drive[key] = None
for (drive, data), (partition, block_data) in zip(
drives.items(), block_devices.items()
):
if drive == block_data["Drive"]:
for drive, data in drives.items():
if block_data := block_devices.get(drive):
data["Partition"] = block_data
yield linux.Disk(id=drive, model=data.get("Info", {}).get("Model", ""), **data)
else:
raise ValueError(
f"\n\n{drive} not found in {[bd['Drive'] for bd in block_devices.values()]}"
f"{drive} not found in {drives.keys()}"
)
data["Usage"] = humanize_usage_metrics(psutil.disk_usage(partition.mountpoint))
yield linux.Disk(id=drive, model=data.get("Info", {}).get("Model", ""), **data)


def generate_html(
Expand Down Expand Up @@ -433,6 +434,8 @@ def generate_report(**kwargs) -> str:
return report_file


# todo: Remove monitoring features all together

def monitor_disk(env: EnvConfig) -> Generator[linux.Disk]:
"""Monitors disk attributes based on the configuration.
Expand Down
1 change: 1 addition & 0 deletions pyudisk/models/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ class Disk(BaseModel):
ata_smart_error_log: Optional[ATASummary] = None
ata_smart_self_test_log: Optional[ATAStandard] = None
ata_smart_selective_self_test_log: Optional[ATASmartSelectiveSelfTestLog] = None
# todo: Remove usage for macOS - usage should be mountpoint specific (if mounted)
usage: Optional[Usage] = None
mountpoints: List[str] = None

Expand Down
5 changes: 2 additions & 3 deletions pyudisk/models/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Partition(BaseModel):
IdUsage: Optional[str] = None
ReadOnly: Optional[bool] = None
Size: Optional[int] = None
MountPoints: Optional[List[str]] = None
MountPoints: Optional[str] = None
Symlinks: Optional[List[str]] = None


Expand Down Expand Up @@ -153,5 +153,4 @@ class Disk(BaseModel):
model: str
Info: Optional[Info]
Attributes: Optional[Attributes]
Partition: Optional[Partition]
Usage: Optional[Usage]
Partition: Optional[List[Partition]]

0 comments on commit e717b66

Please sign in to comment.