Fix logic errors in `_fetch_lsblk_info()` (#1754)

This commit is contained in:
Daemon Coder 2023-05-04 03:45:43 -04:00 committed by GitHub
parent 9e5d45c5d8
commit adceed22ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 14 deletions

View File

@ -974,21 +974,23 @@ def _fetch_lsblk_info(dev_path: Optional[Union[Path, str]] = None, retry: int =
if retry == 0: if retry == 0:
retry = 1 retry = 1
result = None for retry_attempt in range(retry):
for i in range(retry):
try: try:
result = SysCommand(f'lsblk --json -b -o+{lsblk_fields} {dev_path}') result = SysCommand(f'lsblk --json -b -o+{lsblk_fields} {dev_path}')
break
except SysCallError as error: except SysCallError as error:
# Get the output minus the message/info from lsblk if it returns a non-zero exit code. # Get the output minus the message/info from lsblk if it returns a non-zero exit code.
if error.worker: if error.worker:
err = error.worker.decode('UTF-8') err = error.worker.decode('UTF-8')
log(f'Error calling lsblk: {err}', level=logging.DEBUG) log(f'Error calling lsblk: {err}', level=logging.DEBUG)
time.sleep(1)
else: else:
raise error raise error
if result and result.exit_code == 0: if retry_attempt == retry - 1:
raise error
time.sleep(1)
try: try:
if decoded := result.decode('utf-8'): if decoded := result.decode('utf-8'):
block_devices = json.loads(decoded) block_devices = json.loads(decoded)
@ -1000,7 +1002,6 @@ def _fetch_lsblk_info(dev_path: Optional[Union[Path, str]] = None, retry: int =
raise DiskError(f'Failed to read disk "{dev_path}" with lsblk') raise DiskError(f'Failed to read disk "{dev_path}" with lsblk')
def get_lsblk_info(dev_path: Union[Path, str]) -> LsblkInfo: def get_lsblk_info(dev_path: Union[Path, str]) -> LsblkInfo:
if infos := _fetch_lsblk_info(dev_path): if infos := _fetch_lsblk_info(dev_path):
return infos[0] return infos[0]