Skip to content

Commit

Permalink
Fix UnicodeDecodeError during backup/restore
Browse files Browse the repository at this point in the history
  • Loading branch information
shasheene committed Oct 29, 2023
1 parent e5cdc17 commit d423de0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Expand Up @@ -266,6 +266,8 @@ def do_backup(self):
self.ui_manager.display_status(msg1=_("Saving: {file}").format(file=info_lshw_filepath), msg2="")
process, flat_command_string, failed_message = Utility.run("Saving Info-lshw.txt", ["lshw"],
use_c_locale=True,
# lshw can output data in binary
encoding=None,
output_filepath=info_lshw_filepath,
logger=self.logger)
if process.returncode != 0:
Expand Down Expand Up @@ -438,6 +440,8 @@ def do_backup(self):
process, flat_command_string, failed_message = Utility.run("Saving EFI NVRAM info",
["efibootmgr", "--verbose"],
use_c_locale=True,
# efibootmgr can output data in binary
encoding=None,
output_filepath=efi_nvram_filepath,
logger=self.logger)
if process.returncode != 0:
Expand Down
Expand Up @@ -494,26 +494,26 @@ def get_human_readable_minutes_seconds(seconds):
return "{:.1f}".format(duration_minutes + frac)

@staticmethod
def run(short_description, cmd_list, use_c_locale, output_filepath=None, logger=None):
def run(short_description, cmd_list, use_c_locale, encoding='utf-8', output_filepath=None, logger=None):
if use_c_locale:
env = Utility.get_env_C_locale()
else:
env = os.environ.copy()
flat_command_string = Utility.print_cli_friendly(short_description, [cmd_list])
process = subprocess.run(cmd_list, encoding='utf-8', capture_output=True, env=env)
logging_output = short_description + ": " + flat_command_string + " returned " + str(process.returncode) + ": " + process.stdout + " " + process.stderr + "\n"
# Not specifying an encoding, as certain commands (efibootmgr, lshw) can output binary data (#412)
process = subprocess.run(cmd_list, encoding=encoding, capture_output=True, env=env)
logging_output = short_description + ": " + flat_command_string + " returned " + str(process.returncode) + ": " + str(process.stdout) + " " + str(process.stderr) + "\n"
if logger is None:
print(logging_output)
else:
logger.write(logging_output)

if output_filepath is not None:
with open(output_filepath, 'a+') as filehandle:
# TODO confirm encoding
filehandle.write('%s' % process.stdout)
filehandle.write(process.stdout)
filehandle.flush()

fail_description = _("Failed to run command: ") + flat_command_string + "\n\n" + process.stdout + "\n" + process.stderr + "\n\n"
fail_description = _("Failed to run command: ") + flat_command_string + "\n\n" + str(process.stdout) + "\n" + str(process.stderr) + "\n\n"
return process, flat_command_string, fail_description

# Similar to run above, but checks whether the is_shutdown() function has triggered.
Expand Down

0 comments on commit d423de0

Please sign in to comment.