Added error checks if lsblk returns nothing, also handles empty Partition().info instance.

This commit is contained in:
Anton Hvornum 2023-02-23 07:31:36 +01:00
parent 6926424f8d
commit f0f4e4f1c7
No known key found for this signature in database
GPG Key ID: F1234C5BA67C59DF
1 changed files with 45 additions and 28 deletions

View File

@ -98,13 +98,17 @@ class Partition:
if mountpoint: if mountpoint:
self.mount(mountpoint) self.mount(mountpoint)
self._partition_info = self._fetch_information() try:
self._partition_info = self._fetch_information()
if not autodetect_filesystem and filesystem:
self._partition_info.filesystem_type = filesystem
if not autodetect_filesystem and filesystem: if self._partition_info.filesystem_type == 'crypto_LUKS':
self._partition_info.filesystem_type = filesystem self._encrypted = True
except DiskError:
self._partition_info = None
if self._partition_info.filesystem_type == 'crypto_LUKS':
self._encrypted = True
# I hate doint this but I'm currently unsure where this # I hate doint this but I'm currently unsure where this
# is acutally used to be able to fix the typing issues properly # is acutally used to be able to fix the typing issues properly
@ -120,14 +124,17 @@ class Partition:
def __repr__(self, *args :str, **kwargs :str) -> str: def __repr__(self, *args :str, **kwargs :str) -> str:
mount_repr = '' mount_repr = ''
if mountpoint := self._partition_info.get_first_mountpoint(): if self._partition_info:
mount_repr = f", mounted={mountpoint}" if mountpoint := self._partition_info.get_first_mountpoint():
elif self._target_mountpoint: mount_repr = f", mounted={mountpoint}"
mount_repr = f", rel_mountpoint={self._target_mountpoint}" elif self._target_mountpoint:
mount_repr = f", rel_mountpoint={self._target_mountpoint}"
classname = self.__class__.__name__ classname = self.__class__.__name__
if self._encrypted: if not self._partition_info:
return f'{classname}(path={self._path})'
elif self._encrypted:
return f'{classname}(path={self._path}, size={self.size}, PARTUUID={self.part_uuid}, parent={self.real_device}, fs={self._partition_info.filesystem_type}{mount_repr})' return f'{classname}(path={self._path}, size={self.size}, PARTUUID={self.part_uuid}, parent={self.real_device}, fs={self._partition_info.filesystem_type}{mount_repr})'
else: else:
return f'{classname}(path={self._path}, size={self.size}, PARTUUID={self.part_uuid}, fs={self._partition_info.filesystem_type}{mount_repr})' return f'{classname}(path={self._path}, size={self.size}, PARTUUID={self.part_uuid}, fs={self._partition_info.filesystem_type}{mount_repr})'
@ -146,7 +153,7 @@ class Partition:
'encrypted': self._encrypted, 'encrypted': self._encrypted,
'start': self.start, 'start': self.start,
'size': self.end, 'size': self.end,
'filesystem': self._partition_info.filesystem_type 'filesystem': self._partition_info.filesystem_type if self._partition_info else 'Unknown'
} }
return partition_info return partition_info
@ -164,7 +171,7 @@ class Partition:
'start': self.start, 'start': self.start,
'size': self.end, 'size': self.end,
'filesystem': { 'filesystem': {
'format': self._partition_info.filesystem_type 'format': self._partition_info.filesystem_type if self._partition_info else 'None'
} }
} }
@ -193,7 +200,7 @@ class Partition:
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
log(f"Could not decode JSON: {output}", fg="red", level=logging.ERROR) log(f"Could not decode JSON: {output}", fg="red", level=logging.ERROR)
raise DiskError(f'Failed to read disk "{self.device_path}" with lsblk') raise DiskError(f'Failed to partition "{self.device_path}" with lsblk')
def _call_sfdisk(self) -> Dict[str, Any]: def _call_sfdisk(self) -> Dict[str, Any]:
output = SysCommand(f"sfdisk --json {self.block_device.path}").decode('UTF-8') output = SysCommand(f"sfdisk --json {self.block_device.path}").decode('UTF-8')
@ -245,7 +252,8 @@ class Partition:
@property @property
def filesystem(self) -> str: def filesystem(self) -> str:
return self._partition_info.filesystem_type if self._partition_info:
return self._partition_info.filesystem_type
@property @property
def mountpoint(self) -> Optional[Path]: def mountpoint(self) -> Optional[Path]:
@ -255,43 +263,51 @@ class Partition:
@property @property
def mountpoints(self) -> List[Path]: def mountpoints(self) -> List[Path]:
return self._partition_info.mountpoints if self._partition_info:
return self._partition_info.mountpoints
@property @property
def sector_size(self) -> int: def sector_size(self) -> int:
return self._partition_info.sector_size if self._partition_info:
return self._partition_info.sector_size
@property @property
def start(self) -> Optional[int]: def start(self) -> Optional[int]:
return self._partition_info.start if self._partition_info:
return self._partition_info.start
@property @property
def end(self) -> Optional[int]: def end(self) -> Optional[int]:
return self._partition_info.end if self._partition_info:
return self._partition_info.end
@property @property
def end_sectors(self) -> Optional[int]: def end_sectors(self) -> Optional[int]:
start = self._partition_info.start if self._partition_info:
end = self._partition_info.end start = self._partition_info.start
if start and end: end = self._partition_info.end
return start + end if start and end:
return None return start + end
@property @property
def size(self) -> Optional[float]: def size(self) -> Optional[float]:
return self._partition_info.size if self._partition_info:
return self._partition_info.size
@property @property
def boot(self) -> bool: def boot(self) -> bool:
return self._partition_info.bootable if self._partition_info:
return self._partition_info.bootable
@property @property
def partition_type(self) -> Optional[str]: def partition_type(self) -> Optional[str]:
return self._partition_info.pttype if self._partition_info:
return self._partition_info.pttype
@property @property
def part_uuid(self) -> str: def part_uuid(self) -> str:
return self._partition_info.partuuid if self._partition_info:
return self._partition_info.partuuid
@property @property
def uuid(self) -> Optional[str]: def uuid(self) -> Optional[str]:
@ -357,7 +373,8 @@ class Partition:
log(f"Could not get PARTUUID of partition using 'blkid -s PARTUUID -o value {self.device_path}': {error}") log(f"Could not get PARTUUID of partition using 'blkid -s PARTUUID -o value {self.device_path}': {error}")
return self._partition_info.uuid if self._partition_info:
return self._partition_info.uuid
@property @property
def encrypted(self) -> Union[bool, None]: def encrypted(self) -> Union[bool, None]: