Handle lsblk output bytes in json.loads() (#2741)

This commit is contained in:
codefiles 2024-11-04 06:39:31 -05:00 committed by GitHub
parent 481b570dae
commit 7437668f1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View File

@ -1435,7 +1435,7 @@ def _fetch_lsblk_info(
cmd.append(str(dev_path))
try:
result = SysCommand(cmd).decode()
worker = SysCommand(cmd)
except SysCallError as err:
# Get the output minus the message/info from lsblk if it returns a non-zero exit code.
if err.worker:
@ -1448,12 +1448,12 @@ def _fetch_lsblk_info(
raise err
try:
block_devices = json.loads(result)
data = json.loads(worker.output(remove_cr=False))
except json.decoder.JSONDecodeError as err:
error(f"Could not decode lsblk JSON: {result}")
error(f"Could not decode lsblk JSON:\n{worker.output().decode().rstrip()}")
raise err
blockdevices = block_devices['blockdevices']
blockdevices = data['blockdevices']
return [LsblkInfo.from_json(device) for device in blockdevices]

View File

@ -442,11 +442,14 @@ class SysCommand:
return val.strip()
return val
def output(self) -> bytes:
def output(self, remove_cr: bool = True) -> bytes:
if not self.session:
raise ValueError('No session available')
return self.session._trace_log.replace(b'\r\n', b'\n')
if remove_cr:
return self.session._trace_log.replace(b'\r\n', b'\n')
return self.session._trace_log
@property
def exit_code(self) -> Optional[int]: