Adding safe methods for __repr__ to call so that it doesn't break (#1190)

This commit is contained in:
Anton Hvornum 2022-05-16 09:09:51 +02:00 committed by GitHub
parent a748f3fba0
commit 5eac5df9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 2 deletions

View File

@ -32,7 +32,7 @@ class BlockDevice:
# I'm placing the encryption password on a BlockDevice level.
def __repr__(self, *args :str, **kwargs :str) -> str:
return f"BlockDevice({self.device_or_backfile}, size={self.size}GB, free_space={'+'.join(part[2] for part in self.free_space)}, bus_type={self.bus_type})"
return f"BlockDevice({self.device_or_backfile}, size={self._safe_size}GB, free_space={self._safe_free_space}, bus_type={self.bus_type})"
def __iter__(self) -> Iterator[Partition]:
for partition in self.partitions:
@ -167,6 +167,18 @@ class BlockDevice:
"""
return SysCommand(f'blkid -s PTUUID -o value {self.path}').decode('UTF-8')
@property
def _safe_size(self) -> float:
from .helpers import convert_size_to_gb
try:
output = json.loads(SysCommand(f"lsblk --json -b -o+SIZE {self.path}").decode('UTF-8'))
except SysCallError:
return -1.0
for device in output['blockdevices']:
return convert_size_to_gb(device['size'])
@property
def size(self) -> float:
from .helpers import convert_size_to_gb
@ -191,7 +203,14 @@ class BlockDevice:
return device['rota'] is True
@property
def free_space(self) -> Tuple[str, str, str]:
def _safe_free_space(self) -> Tuple[str, ...]:
try:
return '+'.join(part[2] for part in self.free_space)
except SysCallError:
return '?'
@property
def free_space(self) -> Tuple[str, ...]:
# NOTE: parted -s will default to `cancel` on prompt, skipping any partition
# that is "outside" the disk. in /dev/sr0 this is usually the case with Archiso,
# so the free will ignore the ESP partition and just give the "free" space.